Renamed InclusiveByteRange to just ByteRange.

Signed-off-by: Simone Bordet <simone.bordet@gmail.com>
This commit is contained in:
Simone Bordet 2022-08-05 13:40:16 +02:00
parent 24b6ae0fb7
commit 2e01ed7e08
3 changed files with 47 additions and 47 deletions

View File

@ -45,14 +45,14 @@ import org.slf4j.LoggerFactory;
* *
* @version $version$ * @version $version$
*/ */
public class InclusiveByteRange public class ByteRange
{ {
private static final Logger LOG = LoggerFactory.getLogger(InclusiveByteRange.class); private static final Logger LOG = LoggerFactory.getLogger(ByteRange.class);
private long first; private long first;
private long last; private long last;
public InclusiveByteRange(long first, long last) public ByteRange(long first, long last)
{ {
this.first = first; this.first = first;
this.last = last; this.last = last;
@ -68,13 +68,13 @@ public class InclusiveByteRange
return last; return last;
} }
private void coalesce(InclusiveByteRange r) private void coalesce(ByteRange r)
{ {
first = Math.min(first, r.first); first = Math.min(first, r.first);
last = Math.max(last, r.last); last = Math.max(last, r.last);
} }
private boolean overlaps(InclusiveByteRange range) private boolean overlaps(ByteRange range)
{ {
return (range.first >= this.first && range.first <= this.last) || return (range.first >= this.first && range.first <= this.last) ||
(range.last >= this.first && range.last <= this.last) || (range.last >= this.first && range.last <= this.last) ||
@ -110,11 +110,11 @@ public class InclusiveByteRange
if (obj == null) if (obj == null)
return false; return false;
if (!(obj instanceof InclusiveByteRange)) if (!(obj instanceof ByteRange))
return false; return false;
return ((InclusiveByteRange)obj).first == this.first && return ((ByteRange)obj).first == this.first &&
((InclusiveByteRange)obj).last == this.last; ((ByteRange)obj).last == this.last;
} }
@Override @Override
@ -132,9 +132,9 @@ public class InclusiveByteRange
* @param size Size of the resource. * @param size Size of the resource.
* @return List of satisfiable ranges * @return List of satisfiable ranges
*/ */
public static List<InclusiveByteRange> satisfiableRanges(Enumeration<String> headers, long size) public static List<ByteRange> satisfiableRanges(Enumeration<String> headers, long size)
{ {
List<InclusiveByteRange> ranges = null; List<ByteRange> ranges = null;
final long end = size - 1; final long end = size - 1;
// walk through all Range headers // walk through all Range headers
@ -204,21 +204,21 @@ public class InclusiveByteRange
break; break;
} }
InclusiveByteRange range = new InclusiveByteRange(first, last); ByteRange range = new ByteRange(first, last);
if (ranges == null) if (ranges == null)
ranges = new ArrayList<>(); ranges = new ArrayList<>();
boolean coalesced = false; boolean coalesced = false;
for (Iterator<InclusiveByteRange> i = ranges.listIterator(); i.hasNext(); ) for (Iterator<ByteRange> i = ranges.listIterator(); i.hasNext(); )
{ {
InclusiveByteRange r = i.next(); ByteRange r = i.next();
if (range.overlaps(r)) if (range.overlaps(r))
{ {
coalesced = true; coalesced = true;
r.coalesce(range); r.coalesce(range);
while (i.hasNext()) while (i.hasNext())
{ {
InclusiveByteRange r2 = i.next(); ByteRange r2 = i.next();
if (r2.overlaps(r)) if (r2.overlaps(r))
{ {

View File

@ -26,18 +26,18 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
public class InclusiveByteRangeTest public class ByteRangeTest
{ {
private void assertInvalidRange(String rangeString) private void assertInvalidRange(String rangeString)
{ {
Vector<String> strings = new Vector<>(); Vector<String> strings = new Vector<>();
strings.add(rangeString); strings.add(rangeString);
List<InclusiveByteRange> ranges = InclusiveByteRange.satisfiableRanges(strings.elements(), 200); List<ByteRange> ranges = ByteRange.satisfiableRanges(strings.elements(), 200);
assertNull(ranges, "Invalid Range [" + rangeString + "] should result in no satisfiable ranges"); assertNull(ranges, "Invalid Range [" + rangeString + "] should result in no satisfiable ranges");
} }
private void assertRange(String msg, int expectedFirst, int expectedLast, int size, InclusiveByteRange actualRange) private void assertRange(String msg, int expectedFirst, int expectedLast, int size, ByteRange actualRange)
{ {
assertEquals(expectedFirst, actualRange.getFirst(), msg + " - first"); assertEquals(expectedFirst, actualRange.getFirst(), msg + " - first");
assertEquals(expectedLast, actualRange.getLast(), msg + " - last"); assertEquals(expectedLast, actualRange.getLast(), msg + " - last");
@ -47,7 +47,7 @@ public class InclusiveByteRangeTest
private void assertSimpleRange(int expectedFirst, int expectedLast, String rangeId, int size) private void assertSimpleRange(int expectedFirst, int expectedLast, String rangeId, int size)
{ {
InclusiveByteRange range = parseRange(rangeId, size); ByteRange range = parseRange(rangeId, size);
assertEquals(expectedFirst, range.getFirst(), "Range [" + rangeId + "] - first"); assertEquals(expectedFirst, range.getFirst(), "Range [" + rangeId + "] - first");
assertEquals(expectedLast, range.getLast(), "Range [" + rangeId + "] - last"); assertEquals(expectedLast, range.getLast(), "Range [" + rangeId + "] - last");
@ -55,18 +55,18 @@ public class InclusiveByteRangeTest
assertEquals(expectedHeader, range.toHeaderRangeString(size), "Range [" + rangeId + "] - header range string"); assertEquals(expectedHeader, range.toHeaderRangeString(size), "Range [" + rangeId + "] - header range string");
} }
private InclusiveByteRange parseRange(String rangeString, int size) private ByteRange parseRange(String rangeString, int size)
{ {
Vector<String> strings = new Vector<>(); Vector<String> strings = new Vector<>();
strings.add(rangeString); strings.add(rangeString);
List<InclusiveByteRange> ranges = InclusiveByteRange.satisfiableRanges(strings.elements(), size); List<ByteRange> ranges = ByteRange.satisfiableRanges(strings.elements(), size);
assertNotNull(ranges, "Satisfiable Ranges should not be null"); assertNotNull(ranges, "Satisfiable Ranges should not be null");
assertEquals(1, ranges.size(), "Satisfiable Ranges of [" + rangeString + "] count"); assertEquals(1, ranges.size(), "Satisfiable Ranges of [" + rangeString + "] count");
return ranges.iterator().next(); return ranges.iterator().next();
} }
private List<InclusiveByteRange> parseRanges(int size, String... rangeString) private List<ByteRange> parseRanges(int size, String... rangeString)
{ {
Vector<String> strings = new Vector<>(); Vector<String> strings = new Vector<>();
for (String range : rangeString) for (String range : rangeString)
@ -74,7 +74,7 @@ public class InclusiveByteRangeTest
strings.add(range); strings.add(range);
} }
List<InclusiveByteRange> ranges = InclusiveByteRange.satisfiableRanges(strings.elements(), size); List<ByteRange> ranges = ByteRange.satisfiableRanges(strings.elements(), size);
assertNotNull(ranges, "Satisfiable Ranges should not be null"); assertNotNull(ranges, "Satisfiable Ranges should not be null");
return ranges; return ranges;
} }
@ -82,8 +82,8 @@ public class InclusiveByteRangeTest
@Test @Test
public void testHeader416RangeString() public void testHeader416RangeString()
{ {
assertEquals("bytes */100", InclusiveByteRange.to416HeaderRangeString(100), "416 Header on size 100"); assertEquals("bytes */100", ByteRange.to416HeaderRangeString(100), "416 Header on size 100");
assertEquals("bytes */123456789", InclusiveByteRange.to416HeaderRangeString(123456789), "416 Header on size 123456789"); assertEquals("bytes */123456789", ByteRange.to416HeaderRangeString(123456789), "416 Header on size 123456789");
} }
@Test @Test
@ -107,9 +107,9 @@ public class InclusiveByteRangeTest
rangeString = "bytes=5-20,35-65"; rangeString = "bytes=5-20,35-65";
List<InclusiveByteRange> ranges = parseRanges(size, rangeString); List<ByteRange> ranges = parseRanges(size, rangeString);
assertEquals(2, ranges.size(), "Satisfiable Ranges of [" + rangeString + "] count"); assertEquals(2, ranges.size(), "Satisfiable Ranges of [" + rangeString + "] count");
Iterator<InclusiveByteRange> inclusiveByteRangeIterator = ranges.iterator(); Iterator<ByteRange> inclusiveByteRangeIterator = ranges.iterator();
assertRange("Range [" + rangeString + "]", 5, 20, size, inclusiveByteRangeIterator.next()); assertRange("Range [" + rangeString + "]", 5, 20, size, inclusiveByteRangeIterator.next());
assertRange("Range [" + rangeString + "]", 35, 49, size, inclusiveByteRangeIterator.next()); assertRange("Range [" + rangeString + "]", 35, 49, size, inclusiveByteRangeIterator.next());
} }
@ -122,9 +122,9 @@ public class InclusiveByteRangeTest
{ {
int size = 50; int size = 50;
List<InclusiveByteRange> ranges = parseRanges(size, "bytes=5-20", "bytes=35-65"); List<ByteRange> ranges = parseRanges(size, "bytes=5-20", "bytes=35-65");
assertEquals(2, ranges.size()); assertEquals(2, ranges.size());
Iterator<InclusiveByteRange> inclusiveByteRangeIterator = ranges.iterator(); Iterator<ByteRange> inclusiveByteRangeIterator = ranges.iterator();
assertRange("testMultipleAbsoluteRangesSplit[0]", 5, 20, size, inclusiveByteRangeIterator.next()); assertRange("testMultipleAbsoluteRangesSplit[0]", 5, 20, size, inclusiveByteRangeIterator.next());
assertRange("testMultipleAbsoluteRangesSplit[1]", 35, 49, size, inclusiveByteRangeIterator.next()); assertRange("testMultipleAbsoluteRangesSplit[1]", 35, 49, size, inclusiveByteRangeIterator.next());
} }
@ -140,9 +140,9 @@ public class InclusiveByteRangeTest
rangeString = "bytes=5-20,35-65,-5"; rangeString = "bytes=5-20,35-65,-5";
List<InclusiveByteRange> ranges = parseRanges(size, rangeString); List<ByteRange> ranges = parseRanges(size, rangeString);
assertEquals(2, ranges.size(), "Satisfiable Ranges of [" + rangeString + "] count"); assertEquals(2, ranges.size(), "Satisfiable Ranges of [" + rangeString + "] count");
Iterator<InclusiveByteRange> inclusiveByteRangeIterator = ranges.iterator(); Iterator<ByteRange> inclusiveByteRangeIterator = ranges.iterator();
assertRange("Range [" + rangeString + "]", 5, 20, size, inclusiveByteRangeIterator.next()); assertRange("Range [" + rangeString + "]", 5, 20, size, inclusiveByteRangeIterator.next());
assertRange("Range [" + rangeString + "]", 35, 49, size, inclusiveByteRangeIterator.next()); assertRange("Range [" + rangeString + "]", 35, 49, size, inclusiveByteRangeIterator.next());
} }
@ -155,9 +155,9 @@ public class InclusiveByteRangeTest
rangeString = "bytes=5-20,15-25"; rangeString = "bytes=5-20,15-25";
List<InclusiveByteRange> ranges = parseRanges(size, rangeString); List<ByteRange> ranges = parseRanges(size, rangeString);
assertEquals(1, ranges.size(), "Satisfiable Ranges of [" + rangeString + "] count"); assertEquals(1, ranges.size(), "Satisfiable Ranges of [" + rangeString + "] count");
Iterator<InclusiveByteRange> inclusiveByteRangeIterator = ranges.iterator(); Iterator<ByteRange> inclusiveByteRangeIterator = ranges.iterator();
assertRange("Range [" + rangeString + "]", 5, 25, size, inclusiveByteRangeIterator.next()); assertRange("Range [" + rangeString + "]", 5, 25, size, inclusiveByteRangeIterator.next());
} }
@ -168,9 +168,9 @@ public class InclusiveByteRangeTest
String rangeString; String rangeString;
rangeString = "bytes=5-10,15-20"; rangeString = "bytes=5-10,15-20";
List<InclusiveByteRange> ranges = parseRanges(size, rangeString); List<ByteRange> ranges = parseRanges(size, rangeString);
assertEquals(2, ranges.size(), "Satisfiable Ranges of [" + rangeString + "] count"); assertEquals(2, ranges.size(), "Satisfiable Ranges of [" + rangeString + "] count");
Iterator<InclusiveByteRange> inclusiveByteRangeIterator = ranges.iterator(); Iterator<ByteRange> inclusiveByteRangeIterator = ranges.iterator();
assertRange("Range [" + rangeString + "]", 5, 10, size, inclusiveByteRangeIterator.next()); assertRange("Range [" + rangeString + "]", 5, 10, size, inclusiveByteRangeIterator.next());
assertRange("Range [" + rangeString + "]", 15, 20, size, inclusiveByteRangeIterator.next()); assertRange("Range [" + rangeString + "]", 15, 20, size, inclusiveByteRangeIterator.next());
} }
@ -182,9 +182,9 @@ public class InclusiveByteRangeTest
String rangeString; String rangeString;
rangeString = "bytes=5-10,15-20,5-10,15-20,5-10,5-10,5-10,5-10,5-10,5-10"; rangeString = "bytes=5-10,15-20,5-10,15-20,5-10,5-10,5-10,5-10,5-10,5-10";
List<InclusiveByteRange> ranges = parseRanges(size, rangeString); List<ByteRange> ranges = parseRanges(size, rangeString);
assertEquals(2, ranges.size(), "Satisfiable Ranges of [" + rangeString + "] count"); assertEquals(2, ranges.size(), "Satisfiable Ranges of [" + rangeString + "] count");
Iterator<InclusiveByteRange> inclusiveByteRangeIterator = ranges.iterator(); Iterator<ByteRange> inclusiveByteRangeIterator = ranges.iterator();
assertRange("Range [" + rangeString + "]", 5, 10, size, inclusiveByteRangeIterator.next()); assertRange("Range [" + rangeString + "]", 5, 10, size, inclusiveByteRangeIterator.next());
assertRange("Range [" + rangeString + "]", 15, 20, size, inclusiveByteRangeIterator.next()); assertRange("Range [" + rangeString + "]", 15, 20, size, inclusiveByteRangeIterator.next());
} }
@ -196,9 +196,9 @@ public class InclusiveByteRangeTest
String rangeString; String rangeString;
rangeString = "bytes=5-15,20-30,10-25"; rangeString = "bytes=5-15,20-30,10-25";
List<InclusiveByteRange> ranges = parseRanges(size, rangeString); List<ByteRange> ranges = parseRanges(size, rangeString);
assertEquals(1, ranges.size(), "Satisfiable Ranges of [" + rangeString + "] count"); assertEquals(1, ranges.size(), "Satisfiable Ranges of [" + rangeString + "] count");
Iterator<InclusiveByteRange> inclusiveByteRangeIterator = ranges.iterator(); Iterator<ByteRange> inclusiveByteRangeIterator = ranges.iterator();
assertRange("Range [" + rangeString + "]", 5, 30, size, inclusiveByteRangeIterator.next()); assertRange("Range [" + rangeString + "]", 5, 30, size, inclusiveByteRangeIterator.next());
} }
@ -209,9 +209,9 @@ public class InclusiveByteRangeTest
String rangeString; String rangeString;
rangeString = "bytes=20-30,5-15,0-5,25-35"; rangeString = "bytes=20-30,5-15,0-5,25-35";
List<InclusiveByteRange> ranges = parseRanges(size, rangeString); List<ByteRange> ranges = parseRanges(size, rangeString);
assertEquals(2, ranges.size(), "Satisfiable Ranges of [" + rangeString + "] count"); assertEquals(2, ranges.size(), "Satisfiable Ranges of [" + rangeString + "] count");
Iterator<InclusiveByteRange> inclusiveByteRangeIterator = ranges.iterator(); Iterator<ByteRange> inclusiveByteRangeIterator = ranges.iterator();
assertRange("Range [" + rangeString + "]", 20, 35, size, inclusiveByteRangeIterator.next()); assertRange("Range [" + rangeString + "]", 20, 35, size, inclusiveByteRangeIterator.next());
assertRange("Range [" + rangeString + "]", 0, 15, size, inclusiveByteRangeIterator.next()); assertRange("Range [" + rangeString + "]", 0, 15, size, inclusiveByteRangeIterator.next());
} }
@ -222,10 +222,10 @@ public class InclusiveByteRangeTest
int size = 200; int size = 200;
String rangeString; String rangeString;
rangeString = "bytes=20-30,5-15,0-5,25-35"; rangeString = "bytes=20-30,5-15,0-5,25-35";
List<InclusiveByteRange> ranges = parseRanges(size, "bytes=20-30", "bytes=5-15", "bytes=0-5,25-35"); List<ByteRange> ranges = parseRanges(size, "bytes=20-30", "bytes=5-15", "bytes=0-5,25-35");
assertEquals(2, ranges.size(), "Satisfiable Ranges of [" + rangeString + "] count"); assertEquals(2, ranges.size(), "Satisfiable Ranges of [" + rangeString + "] count");
Iterator<InclusiveByteRange> inclusiveByteRangeIterator = ranges.iterator(); Iterator<ByteRange> inclusiveByteRangeIterator = ranges.iterator();
assertRange("Range [" + rangeString + "]", 20, 35, size, inclusiveByteRangeIterator.next()); assertRange("Range [" + rangeString + "]", 20, 35, size, inclusiveByteRangeIterator.next());
assertRange("Range [" + rangeString + "]", 0, 15, size, inclusiveByteRangeIterator.next()); assertRange("Range [" + rangeString + "]", 0, 15, size, inclusiveByteRangeIterator.next());
} }
@ -237,10 +237,10 @@ public class InclusiveByteRangeTest
String rangeString; String rangeString;
rangeString = "bytes=90-100, 10-20, 30-40, -161"; rangeString = "bytes=90-100, 10-20, 30-40, -161";
List<InclusiveByteRange> ranges = parseRanges(size, rangeString); List<ByteRange> ranges = parseRanges(size, rangeString);
assertEquals(2, ranges.size(), "Satisfiable Ranges of [" + rangeString + "] count"); assertEquals(2, ranges.size(), "Satisfiable Ranges of [" + rangeString + "] count");
Iterator<InclusiveByteRange> inclusiveByteRangeIterator = ranges.iterator(); Iterator<ByteRange> inclusiveByteRangeIterator = ranges.iterator();
assertRange("Range [" + rangeString + "]", 30, 199, size, inclusiveByteRangeIterator.next()); assertRange("Range [" + rangeString + "]", 30, 199, size, inclusiveByteRangeIterator.next());
assertRange("Range [" + rangeString + "]", 10, 20, size, inclusiveByteRangeIterator.next()); assertRange("Range [" + rangeString + "]", 10, 20, size, inclusiveByteRangeIterator.next());
} }
@ -268,7 +268,7 @@ public class InclusiveByteRangeTest
Vector<String> strings = new Vector<>(); Vector<String> strings = new Vector<>();
strings.add(badRange); strings.add(badRange);
List<InclusiveByteRange> ranges = InclusiveByteRange.satisfiableRanges(strings.elements(), size); List<ByteRange> ranges = ByteRange.satisfiableRanges(strings.elements(), size);
// if one part is bad, the entire set of ranges should be treated as bad, per RFC7233 // if one part is bad, the entire set of ranges should be treated as bad, per RFC7233
assertThat("Should have no ranges", ranges, is(nullValue())); assertThat("Should have no ranges", ranges, is(nullValue()));
} }

View File

@ -26,6 +26,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import org.eclipse.jetty.http.ByteRange;
import org.eclipse.jetty.http.CompressedContentFormat; import org.eclipse.jetty.http.CompressedContentFormat;
import org.eclipse.jetty.http.DateParser; import org.eclipse.jetty.http.DateParser;
import org.eclipse.jetty.http.HttpContent; import org.eclipse.jetty.http.HttpContent;
@ -34,7 +35,6 @@ import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpHeaderValue; import org.eclipse.jetty.http.HttpHeaderValue;
import org.eclipse.jetty.http.HttpStatus; import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.http.HttpURI; import org.eclipse.jetty.http.HttpURI;
import org.eclipse.jetty.http.InclusiveByteRange;
import org.eclipse.jetty.http.PreEncodedHttpField; import org.eclipse.jetty.http.PreEncodedHttpField;
import org.eclipse.jetty.http.QuotedCSV; import org.eclipse.jetty.http.QuotedCSV;
import org.eclipse.jetty.http.QuotedQualityCSV; import org.eclipse.jetty.http.QuotedQualityCSV;
@ -659,7 +659,7 @@ public class ResourceService
return true; return true;
} }
protected void writeHttpPartialContent(Request request, Response response, Callback callback, HttpContent content, InclusiveByteRange singleSatisfiableRange) protected void writeHttpPartialContent(Request request, Response response, Callback callback, HttpContent content, ByteRange singleSatisfiableRange)
{ {
// TODO: implement this // TODO: implement this
} }