StreamHelper { public void serveMultiRangeStream(final InputStream inputStream, final OutputStream outputStream) throws IOException { int numberOfBufferReadBytes; byte[] buffer = new byte[BUFFER_SIZE]; while ((numberOfBufferReadBytes = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, numberOfBufferReadBytes); outputStream.flush(); Statistics.addBytesSent(numberOfBufferReadBytes); } } StreamHelper(final RangeHelper rangeHelper,
                        final RangePartHeaderSerializer rangePartHeaderSerializer); void serveMultiRangeStream(final InputStream inputStream, final OutputStream outputStream); void serveMultiRangeStream(final InputStream inputStream,
                                      final OutputStream outputStream,
                                      final List<Range> rangeList,
                                      final String boundary,
                                      final String contentType,
                                      final long totalLength); void serveMultiRangeStream(final InputStream inputStream,
                                      final OutputStream outputStream,
                                      final Range range);  }
@Test public void shouldServeTheSameBytes() throws IOException { streamHelper.serveMultiRangeStream(inputStream, outputStream); byte[] out = outputStream.toByteArray(); assertThat(out.length, is(greaterThan(0))); assertThat(out, equalTo(inputBytes)); }
@Test public void shouldServeTheSameBytesForSingeRangeSmallerThanTheBuffer() throws IOException { Range range = new Range(3, 200); byte[] inputBytesSliced = sliceHelper.getSliceForRanges(inputBytes, Arrays.asList(range)); streamHelper.serveMultiRangeStream(inputStream, outputStream, range); byte[] out = outputStream.toByteArray(); assertThat(out.length, is(equalTo((int) rangeHelper.getTotalLength(Arrays.asList(range))))); assertThat(out, equalTo(inputBytesSliced)); }
@Test public void shouldServeTheSameBytesForSingeRangeGreaterThanTheBuffer() throws IOException { Range range = new Range(3, 1024); byte[] inputBytesSliced = sliceHelper.getSliceForRanges(inputBytes, Arrays.asList(range)); streamHelper.serveMultiRangeStream(inputStream, outputStream, range); byte[] out = outputStream.toByteArray(); assertThat(out.length, is(equalTo((int) rangeHelper.getTotalLength(Arrays.asList(range))))); assertThat(out, equalTo(inputBytesSliced)); }
@Test public void shouldServeTheSameBytesForMultipleRangeSmallerThanTheBuffer() throws IOException { List<Range> ranges = new ArrayList<>(); ranges.add(new Range(0, 2)); ranges.add(new Range(2, 2)); byte[] inputBytesSliced = sliceHelper.getSliceForRanges(inputBytes, ranges); streamHelper.serveMultiRangeStream(inputStream, outputStream, ranges, BOUNDARY, CONTENT_TYPE, 0); byte[] out = outputStream.toByteArray(); assertThat(out.length, is(equalTo(inputBytesSliced.length))); assertThat(out, equalTo(inputBytesSliced)); }
@Test public void shouldServeTheSameBytesForMultipleRangeSmallerThanTheBufferOverlapping() throws IOException { List<Range> ranges = new ArrayList<>(); ranges.add(new Range(0, 49)); ranges.add(new Range(40, 59)); byte[] inputBytesSliced = sliceHelper.getSliceForRanges(inputBytes, ranges); streamHelper.serveMultiRangeStream(inputStream, outputStream, ranges, BOUNDARY, CONTENT_TYPE, TOTAL_LENGTH); byte[] out = outputStream.toByteArray(); assertThat(out.length, is(equalTo(inputBytesSliced.length))); assertThat(out, equalTo(inputBytesSliced)); }
@Test public void shouldServeTheSameBytesForMultipleRangeGreaterThanTheBuffer() throws IOException { List<Range> ranges = new ArrayList<>(); ranges.add(new Range(0, 550)); ranges.add(new Range(1024, 1623)); byte[] inputBytesSliced = sliceHelper.getSliceForRanges(inputBytes, ranges); streamHelper.serveMultiRangeStream(inputStream, outputStream, ranges, BOUNDARY, CONTENT_TYPE, TOTAL_LENGTH); byte[] out = outputStream.toByteArray(); assertThat(out.length, is(equalTo(inputBytesSliced.length))); assertThat(out, equalTo(inputBytesSliced)); }