Harmonize filesystem and transient getBlob

This commit also simplifies HTTP range logic calculations, sets the
correct Content-Length, and avoids an unneeded copy.
This commit is contained in:
Andrew Gaul 2012-05-14 10:13:15 -07:00 committed by Andrew Gaul
parent cc016d5dc8
commit a30aad05ed
2 changed files with 36 additions and 22 deletions

View File

@ -91,7 +91,9 @@ import org.jclouds.http.HttpCommand;
import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse; import org.jclouds.http.HttpResponse;
import org.jclouds.http.HttpResponseException; import org.jclouds.http.HttpResponseException;
import org.jclouds.http.HttpUtils;
import org.jclouds.http.options.HttpRequestOptions; import org.jclouds.http.options.HttpRequestOptions;
import org.jclouds.io.ContentMetadata;
import org.jclouds.io.Payloads; import org.jclouds.io.Payloads;
import org.jclouds.io.payloads.BaseMutableContentMetadata; import org.jclouds.io.payloads.BaseMutableContentMetadata;
import org.jclouds.javax.annotation.Nullable; import org.jclouds.javax.annotation.Nullable;
@ -557,25 +559,32 @@ public class FilesystemAsyncBlobStore extends BaseAsyncBlobStore {
} }
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
for (String s : options.getRanges()) { for (String s : options.getRanges()) {
// HTTP uses a closed interval while Java array indexing uses a
// half-open interval.
int offset = 0;
int last = data.length - 1;
if (s.startsWith("-")) { if (s.startsWith("-")) {
int length = Integer.parseInt(s.substring(1)); offset = last - Integer.parseInt(s.substring(1)) + 1;
out.write(data, data.length - length, length);
} else if (s.endsWith("-")) { } else if (s.endsWith("-")) {
int offset = Integer.parseInt(s.substring(0, s.length() - 1)); offset = Integer.parseInt(s.substring(0, s.length() - 1));
out.write(data, offset, data.length - offset);
} else if (s.contains("-")) { } else if (s.contains("-")) {
String[] firstLast = s.split("\\-"); String[] firstLast = s.split("\\-");
int offset = Integer.parseInt(firstLast[0]); offset = Integer.parseInt(firstLast[0]);
int last = Integer.parseInt(firstLast[1]); last = Integer.parseInt(firstLast[1]);
int length = last - offset + 1; // the range end is included
out.write(data, offset, length);
} else { } else {
return immediateFailedFuture(new IllegalArgumentException("first and last were null!")); return immediateFailedFuture(new IllegalArgumentException("illegal range: " + s));
} }
if (offset > last || last + 1 > data.length) {
return immediateFailedFuture(new IllegalArgumentException("illegal range: " + s));
}
out.write(data, offset, last - offset + 1);
} }
blob.setPayload(out.toByteArray()); ContentMetadata cmd = blob.getPayload().getContentMetadata();
blob.getMetadata().getContentMetadata().setContentLength(new Long(data.length)); byte[] byteArray = out.toByteArray();
blob.setPayload(byteArray);
HttpUtils.copy(cmd, blob.getPayload().getContentMetadata());
blob.getPayload().getContentMetadata().setContentLength(new Long(byteArray.length));
} }
} }
checkNotNull(blob.getPayload(), "payload " + blob); checkNotNull(blob.getPayload(), "payload " + blob);

View File

@ -590,27 +590,32 @@ public class TransientAsyncBlobStore extends BaseAsyncBlobStore {
} }
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
for (String s : options.getRanges()) { for (String s : options.getRanges()) {
// HTTP uses a closed interval while Java array indexing uses a
// half-open interval.
int offset = 0;
int last = data.length - 1;
if (s.startsWith("-")) { if (s.startsWith("-")) {
int length = Integer.parseInt(s.substring(1)); offset = last - Integer.parseInt(s.substring(1)) + 1;
out.write(data, data.length - length, length);
} else if (s.endsWith("-")) { } else if (s.endsWith("-")) {
int offset = Integer.parseInt(s.substring(0, s.length() - 1)); offset = Integer.parseInt(s.substring(0, s.length() - 1));
out.write(data, offset, data.length - offset);
} else if (s.contains("-")) { } else if (s.contains("-")) {
String[] firstLast = s.split("\\-"); String[] firstLast = s.split("\\-");
int offset = Integer.parseInt(firstLast[0]); offset = Integer.parseInt(firstLast[0]);
int last = Integer.parseInt(firstLast[1]); last = Integer.parseInt(firstLast[1]);
int length = (last < data.length) ? last + 1 : data.length - offset;
out.write(data, offset, length);
} else { } else {
return immediateFailedFuture(new IllegalArgumentException("first and last were null!")); return immediateFailedFuture(new IllegalArgumentException("illegal range: " + s));
} }
if (offset > last || last + 1 > data.length) {
return immediateFailedFuture(new IllegalArgumentException("illegal range: " + s));
}
out.write(data, offset, last - offset + 1);
} }
ContentMetadata cmd = blob.getPayload().getContentMetadata(); ContentMetadata cmd = blob.getPayload().getContentMetadata();
blob.setPayload(out.toByteArray()); byte[] byteArray = out.toByteArray();
blob.setPayload(byteArray);
HttpUtils.copy(cmd, blob.getPayload().getContentMetadata()); HttpUtils.copy(cmd, blob.getPayload().getContentMetadata());
blob.getPayload().getContentMetadata().setContentLength(new Long(out.toByteArray().length)); blob.getPayload().getContentMetadata().setContentLength(new Long(byteArray.length));
} }
} }
checkNotNull(blob.getPayload(), "payload " + blob); checkNotNull(blob.getPayload(), "payload " + blob);