SLING-3255 - cleanup: remove unneeded private methods and unusual exception handling

git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1544543 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/sling/servlets/get/impl/helpers/StreamRendererServlet.java b/src/main/java/org/apache/sling/servlets/get/impl/helpers/StreamRendererServlet.java
index 9fcf879..e8216ad 100644
--- a/src/main/java/org/apache/sling/servlets/get/impl/helpers/StreamRendererServlet.java
+++ b/src/main/java/org/apache/sling/servlets/get/impl/helpers/StreamRendererServlet.java
@@ -102,7 +102,7 @@
                 "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 @@
                     + 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 @@
 
         ostream.println();
         ostream.print("--" + mimeSeparation + "--");
-
-        // Rethrow any exception that has occurred
-        if (exception != null) throw exception;
-
     }
 
     /**
@@ -509,67 +504,41 @@
     */
     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];
+    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) {
-                        return new IOException("Failed to skip " + start
-                                + " bytes; only skipped " + position + " bytes");
-                    }
-                }
-                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 < 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;
+        }
 
-            return null;
-        } catch (IOException e) {
-            return e;
+        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;
+            }
         }
     }
 
diff --git a/src/test/java/org/apache/sling/servlets/get/impl/helpers/StreamRendererServletTest.java b/src/test/java/org/apache/sling/servlets/get/impl/helpers/StreamRendererServletTest.java
index 339c50e..3e2aab6 100644
--- a/src/test/java/org/apache/sling/servlets/get/impl/helpers/StreamRendererServletTest.java
+++ b/src/test/java/org/apache/sling/servlets/get/impl/helpers/StreamRendererServletTest.java
@@ -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 @@
 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 @@
         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 @@
         }
     }
 
-    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 @@
     }
 
     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);