You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2014/03/27 19:50:05 UTC

svn commit: r1582440 - /tomcat/trunk/java/org/apache/catalina/servlets/DefaultServlet.java

Author: markt
Date: Thu Mar 27 18:50:05 2014
New Revision: 1582440

URL: http://svn.apache.org/r1582440
Log:
Restore optimisation removed in r1549711
Refactor so InputStream is never opened if sendfile is used
Add lots of comments

Modified:
    tomcat/trunk/java/org/apache/catalina/servlets/DefaultServlet.java

Modified: tomcat/trunk/java/org/apache/catalina/servlets/DefaultServlet.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/servlets/DefaultServlet.java?rev=1582440&r1=1582439&r2=1582440&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/servlets/DefaultServlet.java (original)
+++ tomcat/trunk/java/org/apache/catalina/servlets/DefaultServlet.java Thu Mar 27 18:50:05 2014
@@ -898,34 +898,48 @@ public class DefaultServlet extends Http
                 }
             }
 
-            InputStream renderResult = null;
             if (serveContent) {
-                if (resource.isDirectory()) {
-                    // Serve the directory browser
-                    renderResult = render(getPathPrefix(request), resource);
-                } else {
-                    renderResult = resource.getInputStream();
-                }
-
-                // Copy the input stream to our output stream
                 try {
                     response.setBufferSize(output);
                 } catch (IllegalStateException e) {
                     // Silent catch
                 }
-                if (ostream != null) {
-                    if (checkSendfile(request, response, resource,
-                            contentLength, null)) {
-                        try {
-                            renderResult.close();
-                        } catch (IOException ioe) {
-                            // Ignore
-                        }
+                InputStream renderResult = null;
+                if (ostream == null) {
+                    // Output via a writer so can't use sendfile or write
+                    // content directly.
+                    if (resource.isDirectory()) {
+                        renderResult = render(getPathPrefix(request), resource);
                     } else {
-                        copy(resource, renderResult, ostream);
+                        renderResult = resource.getInputStream();
                     }
-                } else {
                     copy(resource, renderResult, writer, encoding);
+                } else {
+                    // Output is via an InputStream
+                    if (resource.isDirectory()) {
+                        renderResult = render(getPathPrefix(request), resource);
+                    } else {
+                        // Output is content of resource
+                        if (!checkSendfile(request, response, resource,
+                                contentLength, null)) {
+                            // sendfile not possible so check if resource
+                            // content is available directly
+                            byte[] resourceBody = resource.getContent();
+                            if (resourceBody == null) {
+                                // Resource content not available, use
+                                // inputstream
+                                renderResult = resource.getInputStream();
+                            } else {
+                                // Use the resource content directly
+                                ostream.write(resourceBody);
+                            }
+                        }
+                        // If a stream was configured, it needs to be copied to
+                        // the output (this method closes the stream)
+                        if (renderResult != null) {
+                            copy(resource, renderResult, ostream);
+                        }
+                    }
                 }
             }
 



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org