StreamFile extends DfsServlet { static void sendPartialData(FSDataInputStream in, OutputStream out, HttpServletResponse response, long contentLength, List<InclusiveByteRange> ranges) throws IOException { if (ranges == null || ranges.size() != 1) { response.setContentLength(0); response.setStatus(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE); response.setHeader("Content-Range", InclusiveByteRange.to416HeaderRangeString(contentLength)); } else { InclusiveByteRange singleSatisfiableRange = ranges.get(0); long singleLength = singleSatisfiableRange.getSize(contentLength); response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT); response.setHeader("Content-Range", singleSatisfiableRange.toHeaderRangeString(contentLength)); copyFromOffset(in, out, singleSatisfiableRange.getFirst(contentLength), singleLength); } }  @Override @SuppressWarnings("unchecked") void doGet(HttpServletRequest request, HttpServletResponse response); static final String CONTENT_LENGTH; }
@Test public void testSendPartialData() throws IOException { FSDataInputStream in = new FSDataInputStream(new MockFSInputStream()); ByteArrayOutputStream os = new ByteArrayOutputStream(); { List<InclusiveByteRange> ranges = strToRanges("0-,10-300", 500); HttpServletResponse response = Mockito.mock(HttpServletResponse.class); StreamFile.sendPartialData(in, os, response, 500, ranges); Mockito.verify(response).setStatus(416); } { os.reset(); HttpServletResponse response = Mockito.mock(HttpServletResponse.class); StreamFile.sendPartialData(in, os, response, 500, null); Mockito.verify(response).setStatus(416); } { List<InclusiveByteRange> ranges = strToRanges("600-800", 500); HttpServletResponse response = Mockito.mock(HttpServletResponse.class); StreamFile.sendPartialData(in, os, response, 500, ranges); Mockito.verify(response).setStatus(416); } { List<InclusiveByteRange> ranges = strToRanges("100-300", 500); HttpServletResponse response = Mockito.mock(HttpServletResponse.class); StreamFile.sendPartialData(in, os, response, 500, ranges); Mockito.verify(response).setStatus(206); assertArrayEquals("Byte range from 100-300", getOutputArray(100, 201), os.toByteArray()); } }