mirror of https://github.com/apache/jclouds.git
Improve Range handling.
* allowing to GET last n bytes. * start from GET range now spec conform (to field empty instead of Long.MAX_VALUE)
This commit is contained in:
parent
46a42c22d8
commit
5843d5fee9
|
@ -51,11 +51,11 @@ public class BlobToHttpGetOptions implements Function<org.jclouds.blobstore.opti
|
||||||
httpOptions.ifUnmodifiedSince(from.getIfUnmodifiedSince());
|
httpOptions.ifUnmodifiedSince(from.getIfUnmodifiedSince());
|
||||||
}
|
}
|
||||||
for (String range : from.getRanges()) {
|
for (String range : from.getRanges()) {
|
||||||
String[] firstLast = range.split("\\-");
|
String[] firstLast = range.split("\\-", 2);
|
||||||
if (firstLast.length == 2)
|
if (!firstLast[0].isEmpty() && !firstLast[1].isEmpty())
|
||||||
httpOptions.range(Long.parseLong(firstLast[0]), Long.parseLong(firstLast[1]));
|
httpOptions.range(Long.parseLong(firstLast[0]), Long.parseLong(firstLast[1]));
|
||||||
else if (range.startsWith("-"))
|
else if (firstLast[0].isEmpty() && !firstLast[1].isEmpty())
|
||||||
httpOptions.tail(Long.parseLong(firstLast[0]));
|
httpOptions.tail(Long.parseLong(firstLast[1]));
|
||||||
else
|
else
|
||||||
httpOptions.startAt(Long.parseLong(firstLast[0]));
|
httpOptions.startAt(Long.parseLong(firstLast[0]));
|
||||||
}
|
}
|
||||||
|
|
|
@ -72,6 +72,16 @@ public class GetOptions {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* download the specified range of the object starting from the end of the object.
|
||||||
|
*/
|
||||||
|
public GetOptions tail(long length) {
|
||||||
|
checkArgument(length >= 0, "length must be >= 0");
|
||||||
|
getRanges().add(String.format("-%d", length));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Only return the object if it has changed since this time.
|
* Only return the object if it has changed since this time.
|
||||||
* <p />
|
* <p />
|
||||||
|
|
|
@ -104,6 +104,29 @@ public class BlobToHttpGetOptionsTest {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRangesTail(){
|
||||||
|
org.jclouds.blobstore.options.GetOptions in = new org.jclouds.blobstore.options.GetOptions();
|
||||||
|
in.tail(1024);
|
||||||
|
|
||||||
|
GetOptions expected = new GetOptions();
|
||||||
|
expected.tail(1024);
|
||||||
|
|
||||||
|
assertEquals(fn.apply(in), expected);
|
||||||
|
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void testRangesStart(){
|
||||||
|
org.jclouds.blobstore.options.GetOptions in = new org.jclouds.blobstore.options.GetOptions();
|
||||||
|
in.startAt(1024);
|
||||||
|
|
||||||
|
GetOptions expected = new GetOptions();
|
||||||
|
expected.startAt(1024);
|
||||||
|
|
||||||
|
assertEquals(fn.apply(in), expected);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Test(expectedExceptions = { NullPointerException.class, IllegalStateException.class })
|
@Test(expectedExceptions = { NullPointerException.class, IllegalStateException.class })
|
||||||
public void testNullIsBad() {
|
public void testNullIsBad() {
|
||||||
fn.apply(null);
|
fn.apply(null);
|
||||||
|
|
|
@ -136,6 +136,21 @@ public class GetOptionsTest {
|
||||||
assertEquals(options.getRanges(), ImmutableList.of("0-5", "10-100"));
|
assertEquals(options.getRanges(), ImmutableList.of("0-5", "10-100"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRangeStartAt() {
|
||||||
|
GetOptions options = new GetOptions();
|
||||||
|
options.startAt(5);
|
||||||
|
assertEquals(options.getRanges(), ImmutableList.of("5-"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRangeTail() {
|
||||||
|
GetOptions options = new GetOptions();
|
||||||
|
options.tail(5);
|
||||||
|
assertEquals(options.getRanges(), ImmutableList.of("-5"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNoRange() {
|
public void testNoRange() {
|
||||||
GetOptions options = new GetOptions();
|
GetOptions options = new GetOptions();
|
||||||
|
|
|
@ -80,7 +80,7 @@ public class GetOptions extends BaseHttpRequestOptions {
|
||||||
*/
|
*/
|
||||||
public GetOptions startAt(long start) {
|
public GetOptions startAt(long start) {
|
||||||
checkArgument(start >= 0, "start must be >= 0");
|
checkArgument(start >= 0, "start must be >= 0");
|
||||||
ranges.add(String.format("%d-%d", start, Long.MAX_VALUE));
|
ranges.add(String.format("%d-", start));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -155,13 +155,13 @@ public class GetOptionsTest {
|
||||||
public void testStartAt() {
|
public void testStartAt() {
|
||||||
GetOptions options = new GetOptions();
|
GetOptions options = new GetOptions();
|
||||||
options.startAt(100);
|
options.startAt(100);
|
||||||
assertEquals(options.getRange(), "bytes=100-9223372036854775807");
|
assertEquals(options.getRange(), "bytes=100-");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testStartAtStatic() {
|
public void testStartAtStatic() {
|
||||||
GetOptions options = startAt(100);
|
GetOptions options = startAt(100);
|
||||||
assertEquals(options.getRange(), "bytes=100-9223372036854775807");
|
assertEquals(options.getRange(), "bytes=100-");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expectedExceptions = IllegalArgumentException.class)
|
@Test(expectedExceptions = IllegalArgumentException.class)
|
||||||
|
|
Loading…
Reference in New Issue