ContentByteRange { protected void parseRange(String range) { String prefix = "bytes="; if (!range.startsWith(prefix) || StringUtils.containsNone(range, "-")) { throw new IllegalArgumentException(getUsage(range)); } else { String byteRange = range.substring(prefix.length()).split(",")[0]; try { setRangeStart(byteRange.substring(0, byteRange.indexOf("-"))); setRangeEnd(byteRange.substring(byteRange.indexOf("-") + 1, byteRange.length())); } catch (NumberFormatException e) { throw new IllegalArgumentException(getUsage(range)); } if (null == getRangeStart() && null == getRangeEnd()) { throw new IllegalArgumentException(getUsage(range)); } } } ContentByteRange(String range); Long getRangeStart(); Long getRangeEnd(); String getUsage(String range);  }
@Test public void testParseRange() { ContentByteRange range; range = new ContentByteRange("bytes=0-5"); assertEquals(Long.valueOf(0), range.getRangeStart()); assertEquals(Long.valueOf(5), range.getRangeEnd()); range = new ContentByteRange("bytes=5-10"); assertEquals(Long.valueOf(5), range.getRangeStart()); assertEquals(Long.valueOf(10), range.getRangeEnd()); range = new ContentByteRange("bytes=5-"); assertEquals(Long.valueOf(5), range.getRangeStart()); assertNull(range.getRangeEnd()); range = new ContentByteRange("bytes=-5"); assertNull(range.getRangeStart()); assertEquals(Long.valueOf(5), range.getRangeEnd()); range = new ContentByteRange("bytes=5-10,15-20"); assertEquals(Long.valueOf(5), range.getRangeStart()); assertEquals(Long.valueOf(10), range.getRangeEnd()); try { new ContentByteRange("0-5"); } catch (IllegalArgumentException e) { assertNotNull(e); } try { new ContentByteRange("bytes=one-five"); } catch (IllegalArgumentException e) { assertNotNull(e); } try { new ContentByteRange("bytes=-"); } catch (IllegalArgumentException e) { assertNotNull(e); } try { new ContentByteRange("bytes=5"); } catch (IllegalArgumentException e) { assertNotNull(e); } try { new ContentByteRange(""); } catch (IllegalArgumentException e) { assertNotNull(e); } }