@Test(expected = ClientException.class) public void testParseUnsupportedByteRange5() throws Exception { DownloadServlet.parseRange("bytes=0-123,-45", 12345); }
public static ByteRange parseRange(String range, long fileSize) throws ClientException { if (!range.startsWith("bytes=") || range.indexOf(',') >= 0) { throw new ClientException("Cannot parse range : " + range); } int sepIndex = range.indexOf('-', 6); if (sepIndex < 0) { throw new ClientException("Cannot parse range : " + range); } String start = range.substring(6, sepIndex).trim(); String end = range.substring(sepIndex + 1).trim(); long rangeStart = 0; long rangeEnd = fileSize - 1; if (start.isEmpty()) { if (end.isEmpty()) { throw new ClientException("Cannot parse range : " + range); } rangeStart = fileSize - Integer.parseInt(end); if (rangeStart < 0) { rangeStart = 0; } } else { rangeStart = Integer.parseInt(start); if (!end.isEmpty()) { rangeEnd = Integer.parseInt(end); } } if (rangeStart > rangeEnd) { throw new ClientException("Cannot parse range : " + range); } return new ByteRange(rangeStart, rangeEnd); }
DownloadServlet extends HttpServlet { public static ByteRange parseRange(String range, long fileSize) throws ClientException { if (!range.startsWith("bytes=") || range.indexOf(',') >= 0) { throw new ClientException("Cannot parse range : " + range); } int sepIndex = range.indexOf('-', 6); if (sepIndex < 0) { throw new ClientException("Cannot parse range : " + range); } String start = range.substring(6, sepIndex).trim(); String end = range.substring(sepIndex + 1).trim(); long rangeStart = 0; long rangeEnd = fileSize - 1; if (start.isEmpty()) { if (end.isEmpty()) { throw new ClientException("Cannot parse range : " + range); } rangeStart = fileSize - Integer.parseInt(end); if (rangeStart < 0) { rangeStart = 0; } } else { rangeStart = Integer.parseInt(start); if (!end.isEmpty()) { rangeEnd = Integer.parseInt(end); } } if (rangeStart > rangeEnd) { throw new ClientException("Cannot parse range : " + range); } return new ByteRange(rangeStart, rangeEnd); } }
DownloadServlet extends HttpServlet { public static ByteRange parseRange(String range, long fileSize) throws ClientException { if (!range.startsWith("bytes=") || range.indexOf(',') >= 0) { throw new ClientException("Cannot parse range : " + range); } int sepIndex = range.indexOf('-', 6); if (sepIndex < 0) { throw new ClientException("Cannot parse range : " + range); } String start = range.substring(6, sepIndex).trim(); String end = range.substring(sepIndex + 1).trim(); long rangeStart = 0; long rangeEnd = fileSize - 1; if (start.isEmpty()) { if (end.isEmpty()) { throw new ClientException("Cannot parse range : " + range); } rangeStart = fileSize - Integer.parseInt(end); if (rangeStart < 0) { rangeStart = 0; } } else { rangeStart = Integer.parseInt(start); if (!end.isEmpty()) { rangeEnd = Integer.parseInt(end); } } if (rangeStart > rangeEnd) { throw new ClientException("Cannot parse range : " + range); } return new ByteRange(rangeStart, rangeEnd); }  }
DownloadServlet extends HttpServlet { public static ByteRange parseRange(String range, long fileSize) throws ClientException { if (!range.startsWith("bytes=") || range.indexOf(',') >= 0) { throw new ClientException("Cannot parse range : " + range); } int sepIndex = range.indexOf('-', 6); if (sepIndex < 0) { throw new ClientException("Cannot parse range : " + range); } String start = range.substring(6, sepIndex).trim(); String end = range.substring(sepIndex + 1).trim(); long rangeStart = 0; long rangeEnd = fileSize - 1; if (start.isEmpty()) { if (end.isEmpty()) { throw new ClientException("Cannot parse range : " + range); } rangeStart = fileSize - Integer.parseInt(end); if (rangeStart < 0) { rangeStart = 0; } } else { rangeStart = Integer.parseInt(start); if (!end.isEmpty()) { rangeEnd = Integer.parseInt(end); } } if (rangeStart > rangeEnd) { throw new ClientException("Cannot parse range : " + range); } return new ByteRange(rangeStart, rangeEnd); }  void handleClientDisconnect(IOException ioe); static void writeStream(InputStream in, OutputStream out,
            ByteRange range); static ByteRange parseRange(String range, long fileSize); }
DownloadServlet extends HttpServlet { public static ByteRange parseRange(String range, long fileSize) throws ClientException { if (!range.startsWith("bytes=") || range.indexOf(',') >= 0) { throw new ClientException("Cannot parse range : " + range); } int sepIndex = range.indexOf('-', 6); if (sepIndex < 0) { throw new ClientException("Cannot parse range : " + range); } String start = range.substring(6, sepIndex).trim(); String end = range.substring(sepIndex + 1).trim(); long rangeStart = 0; long rangeEnd = fileSize - 1; if (start.isEmpty()) { if (end.isEmpty()) { throw new ClientException("Cannot parse range : " + range); } rangeStart = fileSize - Integer.parseInt(end); if (rangeStart < 0) { rangeStart = 0; } } else { rangeStart = Integer.parseInt(start); if (!end.isEmpty()) { rangeEnd = Integer.parseInt(end); } } if (rangeStart > rangeEnd) { throw new ClientException("Cannot parse range : " + range); } return new ByteRange(rangeStart, rangeEnd); }  void handleClientDisconnect(IOException ioe); static void writeStream(InputStream in, OutputStream out,
            ByteRange range); static ByteRange parseRange(String range, long fileSize);  }