Return-Path: X-Original-To: apmail-sling-commits-archive@www.apache.org Delivered-To: apmail-sling-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 8049810AA5 for ; Fri, 22 Nov 2013 14:09:41 +0000 (UTC) Received: (qmail 45101 invoked by uid 500); 22 Nov 2013 14:09:40 -0000 Delivered-To: apmail-sling-commits-archive@sling.apache.org Received: (qmail 45015 invoked by uid 500); 22 Nov 2013 14:09:36 -0000 Mailing-List: contact commits-help@sling.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@sling.apache.org Delivered-To: mailing list commits@sling.apache.org Received: (qmail 44849 invoked by uid 99); 22 Nov 2013 14:09:35 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 22 Nov 2013 14:09:35 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 22 Nov 2013 14:09:33 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 4C84123888E2; Fri, 22 Nov 2013 14:09:13 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: commits@sling.apache.org From: bdelacretaz@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20131122140913.4C84123888E2@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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);