incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1544543 - in /sling/trunk/bundles/servlets/get/src: main/java/org/apache/sling/servlets/get/impl/helpers/StreamRendererServlet.java test/java/org/apache/sling/servlets/get/impl/helpers/StreamRendererServletTest.java
Date Fri, 22 Nov 2013 14:09:13 GMT
Author: bdelacretaz
Date: Fri Nov 22 14:09:12 2013
New Revision: 1544543

URL: http://svn.apache.org/r1544543
Log:
SLING-3255 - cleanup: remove unneeded private methods and unusual exception handling

Modified:
    sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/helpers/StreamRendererServlet.java
    sling/trunk/bundles/servlets/get/src/test/java/org/apache/sling/servlets/get/impl/helpers/StreamRendererServletTest.java

Modified: sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/helpers/StreamRendererServlet.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/helpers/StreamRendererServlet.java?rev=1544543&r1=1544542&r2=1544543&view=diff
==============================================================================
--- sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/helpers/StreamRendererServlet.java
(original)
+++ sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/helpers/StreamRendererServlet.java
Fri Nov 22 14:09:12 2013
@@ -102,7 +102,7 @@ public class StreamRendererServlet exten
                 "StreamRendererServlet does not support for extension " + ext);
             if (included || response.isCommitted()) {
                 log.error(
-                    "StreamRendererServlet does not support for extension {}",
+                    "StreamRendererServlet does not support extension {}",
                     ext);
             } else {
                 response.sendError(HttpServletResponse.SC_NOT_FOUND);
@@ -481,9 +481,8 @@ public class StreamRendererServlet exten
                     + currentRange.end + "/" + currentRange.length);
                 ostream.println();
 
-                // Printing content
-                exception = copyRange(istream, ostream, currentRange.start,
-                    currentRange.end);
+                // Copy content
+                copy(istream, ostream, currentRange);
             } finally {
                 closeSilently(istream);
             }
@@ -492,10 +491,6 @@ public class StreamRendererServlet exten
 
         ostream.println();
         ostream.print("--" + mimeSeparation + "--");
-
-        // Rethrow any exception that has occurred
-        if (exception != null) throw exception;
-
     }
 
     /**
@@ -509,67 +504,41 @@ public class StreamRendererServlet exten
     */
     private void copy(InputStream istream, OutputStream ostream,
             Range range) throws IOException {
-        IOException exception = copyRange(istream, ostream, range.start, range.end);
-
-        // Rethrow any exception that has occurred
-        if (exception != null) {
-            throw exception;
-        }
-    }
-
-    /**
-     * Copy the contents of the specified input stream to the specified output
-     * stream.
-     *
-     * @param istream The input stream to read from
-     * @param ostream The output stream to write to
-     * @param start Start of the range which will be copied
-     * @param end End of the range which will be copied
-     * @return Exception which occurred during processing
-     */
-    private IOException copyRange(InputStream istream,
-            OutputStream ostream, long start, long end) {
-        log.debug("copyRange: Serving bytes: {}-{}", start, end);
-        return staticCopyRange(istream, ostream, start, end);
+        log.debug("copy: Serving bytes: {}-{}", range.start, range.end);
+        staticCopyRange(istream, ostream, range.start, range.end);
     }
 
     // static, package-private method to make unit testing easier
-    static IOException staticCopyRange(InputStream istream,
-            OutputStream ostream, long start, long end) {
-        try {
-            long position = 0;
-            byte buffer[] = new byte[IO_BUFFER_SIZE];
-
-            while (position < start) {
-                long skipped = istream.skip(start - position);
-                if (skipped == 0) {
-                    // skip() may return zero if for whatever reason it wasn't
-                    // able to advance the stream. In such cases we need to
-                    // fall back to read() to force the skipping of bytes.
-                    int len = (int) Math.min(start - position, buffer.length);
-                    skipped = istream.read(buffer, 0, len);
-                    if (skipped == -1) {
-                        return new IOException("Failed to skip " + start
-                                + " bytes; only skipped " + position + " bytes");
-                    }
+    static void staticCopyRange(InputStream istream,
+            OutputStream ostream, long start, long end) throws IOException {
+        long position = 0;
+        byte buffer[] = new byte[IO_BUFFER_SIZE];
+
+        while (position < start) {
+            long skipped = istream.skip(start - position);
+            if (skipped == 0) {
+                // skip() may return zero if for whatever reason it wasn't
+                // able to advance the stream. In such cases we need to
+                // fall back to read() to force the skipping of bytes.
+                int len = (int) Math.min(start - position, buffer.length);
+                skipped = istream.read(buffer, 0, len);
+                if (skipped == -1) {
+                    throw new IOException("Failed to skip " + start
+                            + " bytes; only skipped " + position + " bytes");
                 }
-                position += skipped;
             }
+            position += skipped;
+        }
 
-            while (position < end) {
-                int len = (int) Math.min(end - position, buffer.length);
-                int read = istream.read(buffer, 0, len);
-                if (read != -1) {
-                    position += read;
-                    ostream.write(buffer, 0, len);
-                } else {
-                    break;
-                }
+        while (position < end) {
+            int len = (int) Math.min(end - position, buffer.length);
+            int read = istream.read(buffer, 0, len);
+            if (read != -1) {
+                position += read;
+                ostream.write(buffer, 0, len);
+            } else {
+                break;
             }
-
-            return null;
-        } catch (IOException e) {
-            return e;
         }
     }
 

Modified: sling/trunk/bundles/servlets/get/src/test/java/org/apache/sling/servlets/get/impl/helpers/StreamRendererServletTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/get/src/test/java/org/apache/sling/servlets/get/impl/helpers/StreamRendererServletTest.java?rev=1544543&r1=1544542&r2=1544543&view=diff
==============================================================================
--- sling/trunk/bundles/servlets/get/src/test/java/org/apache/sling/servlets/get/impl/helpers/StreamRendererServletTest.java
(original)
+++ sling/trunk/bundles/servlets/get/src/test/java/org/apache/sling/servlets/get/impl/helpers/StreamRendererServletTest.java
Fri Nov 22 14:09:12 2013
@@ -19,9 +19,11 @@
 package org.apache.sling.servlets.get.impl.helpers;
 
 import static org.junit.Assert.assertEquals;
+
 import java.io.BufferedInputStream;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.io.IOException;
 import java.io.InputStream;
 import java.util.Random;
 
@@ -30,12 +32,22 @@ import org.junit.Test;
 public class StreamRendererServletTest {
 
     @Test
-    public void testCopyRange() {
+    public void testCopyRange() throws IOException {
         runTests(1234);
         runTests(4321);
     }
 
-    private void runTests(int randomSeed) {
+    @Test
+    public void testResultingLength() throws IOException {
+        final ByteArrayInputStream in = new ByteArrayInputStream("12345678".getBytes());
+        final ByteArrayOutputStream out = new ByteArrayOutputStream();
+        StreamRendererServlet.staticCopyRange(in, out, 2, 4);
+        final String result = out.toString();
+        assertEquals(2, result.length());
+        assertEquals("34", result);
+    }
+    
+    private void runTests(int randomSeed) throws IOException {
         final Random random = new Random(randomSeed);
         assertCopyRange(random, StreamRendererServlet.IO_BUFFER_SIZE * 2 + 42);
         assertCopyRange(random, StreamRendererServlet.IO_BUFFER_SIZE * 3);
@@ -46,7 +58,7 @@ public class StreamRendererServletTest {
         assertCopyRange(random, 1);
     }
 
-    private void assertCopyRange(Random random, int bufferSize) {
+    private void assertCopyRange(Random random, int bufferSize) throws IOException {
 
         // generate some random test data
         final byte[] expected = new byte[bufferSize];
@@ -66,7 +78,7 @@ public class StreamRendererServletTest {
         }
     }
 
-    private void assertCopyRange(byte[] expected, int a, int b) {
+    private void assertCopyRange(byte[] expected, int a, int b) throws IOException {
         assertCopyRange(expected, new ByteArrayInputStream(expected), a, b);
         // with BufferedInputStream
         assertCopyRange(expected, new BufferedInputStream(new ByteArrayInputStream(expected)),
a, b);
@@ -87,7 +99,7 @@ public class StreamRendererServletTest {
     }
 
     private void assertCopyRange(
-            byte[] expected, InputStream input, int a, int b) {
+            byte[] expected, InputStream input, int a, int b) throws IOException {
         ByteArrayOutputStream output = new ByteArrayOutputStream();
         StreamRendererServlet.staticCopyRange(
                 new ByteArrayInputStream(expected), output, a, b);



Mime
View raw message