StaticResourceServlet extends HttpServlet { protected IOException copyRange(InputStream istream, ServletOutputStream ostream, long start, long end) { logger.debug("Serving bytes:{}-{}", start, end); try { istream.skip(start); } catch (IOException e) { logger.trace("Cannot skip to input stream position {}. The user probably closed the client side.", start, e); return e; } long bytesToRead = end - start + 1; byte[] buffer = new byte[2048]; int len = buffer.length; try { len = (int) bytesToRead % buffer.length; if (len > 0) { len = istream.read(buffer, 0, len); if (len > 0) { ostream.write(buffer, 0, len); bytesToRead -= len; if (bytesToRead == 0) return null; } else return null; } for (len = istream.read(buffer); len > 0; len = istream.read(buffer)) { ostream.write(buffer, 0, len); bytesToRead -= len; if (bytesToRead < 1) break; } } catch (IOException e) { logger.trace("IOException after starting the byte copy, current length {}, buffer {}." + " The user probably closed the client side after the file started copying.", len, buffer, e); return e; } return null; } StaticResourceServlet(); @Activate void activate(ComponentContext cc);  }
@Test public void testCopyRange() { testCopyRangeMethod(new byte[2048], 0, 2048); testCopyRangeMethod(new byte[2049], 0, 2049); testCopyRangeMethod(new byte[2047], 0, 2047); testCopyRangeMethod(new byte[0], 0, 0); }