Range { public static Range parse(String value) { LOG.debug("Attempting to parse range: {}", value); value = value.replace("bytes", "").trim(); String[] parts = value.split("/"); if (parts.length != 2) { throw new IllegalArgumentException( "Must provide <range>/<length> format for range request: " + value); } String byteRangePart = parts[0]; String contentLengthPart = parts[1]; Long contentLength = contentLengthPart.equals("*") ? null : Long.parseLong(contentLengthPart); String[] byteValues = byteRangePart.split("-", -1); if (byteValues.length != 2) { throw new IllegalArgumentException( "Must provide <start>-<end> format for range request: " + value); } Long startByte = StringUtils.isBlank(byteValues[0]) ? null : Long.parseLong(byteValues[0]); Long endByte = StringUtils.isBlank(byteValues[1]) ? null : Long.parseLong(byteValues[1]); if (startByte == null && endByte == null) { throw new IllegalArgumentException( "Must provide start/end byte for range request: " + value); } Range range = new Range(startByte, endByte); range.contentLength = contentLength; return range; }  @Override String toString(); static Range parse(String value);  }
@Test public void parse_noContentLength() { Range range = Range.parse("bytes 5-10/*"); assertEquals(5L, (long) range.getStartByte()); assertEquals(10L, (long) range.getEndByte()); assertNull(range.getContentLength()); }
@Test public void parse_hasContentLength() { Range range = Range.parse("bytes 5-10/25"); assertEquals(5L, (long) range.getStartByte()); assertEquals(10L, (long) range.getEndByte()); assertEquals(25L, (long) range.getContentLength()); }
@Test public void parse_noStartnoContentLength() { Range range = Range.parse("bytes -10/*"); assertNull(range.getStartByte()); assertEquals(10L, (long) range.getEndByte()); assertNull(range.getContentLength()); }
@Test public void parse_noStartHasContentLength() { Range range = Range.parse("bytes -10/25"); assertNull(range.getStartByte()); assertEquals(10L, (long) range.getEndByte()); assertEquals(25L, (long) range.getContentLength()); }
@Test public void parse_noEndContentLength() { Range range = Range.parse("bytes 5-/*"); assertEquals(5L, (long) range.getStartByte()); assertNull(range.getEndByte()); assertNull(range.getContentLength()); }
@Test public void parse_noEndHasContentLength() { Range range = Range.parse("bytes 5-/25"); assertEquals(5L, (long) range.getStartByte()); assertNull(range.getEndByte()); assertEquals(25L, (long) range.getContentLength()); }
@Test(expected = IllegalArgumentException.class) public void parse_noStartNoEndHasContentLength() { Range.parse("bytes -/25"); }
@Test(expected = IllegalArgumentException.class) public void parse_noSlash() { Range.parse("bytes 1-10"); }