Skip to content
Permalink
Browse files
JCLOUDS-1367: Do not open open InputStream in copyBlob
This avoids an unneeded call to ByteStreams2.toByteArrayAndClose in
getBlob for range requests and elides a large memory allocation.
  • Loading branch information
gaul committed Aug 1, 2020
1 parent 8de7b69 commit 9c21bf2cc2af3d9f45542285f278d1b85f700f2a
Showing 1 changed file with 21 additions and 22 deletions.
@@ -685,6 +685,7 @@ public Blob getBlob(String containerName, String key, GetOptions options) {
try {
byteSource = (ByteSource) blob.getPayload().getRawContent();
} catch (ClassCastException cce) {
// This should not happen; both FilesystemStorageStrategyImpl and TransientStorageStrategy return ByteSource
try {
byteSource = ByteSource.wrap(ByteStreams2.toByteArrayAndClose(blob.getPayload().openStream()));
} catch (IOException e) {
@@ -724,18 +725,31 @@ public Blob getBlob(String containerName, String key, GetOptions options) {
"bytes " + offset + "-" + last + "/" + blob.getPayload().getContentMetadata().getContentLength());
}
ContentMetadata cmd = blob.getPayload().getContentMetadata();
// return InputStream to more closely follow real blobstore
try {
blob.setPayload(ByteSource.concat(streams.build()).openStream());
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
blob.setPayload(ByteSource.concat(streams.build()));
HttpUtils.copy(cmd, blob.getPayload().getContentMetadata());
blob.getPayload().getContentMetadata().setContentLength(size);
blob.getMetadata().setSize(size);
}
}
checkNotNull(blob.getPayload(), "payload " + blob);
// return InputStream to more closely follow real blobstore
Payload payload;
try {
InputStream is = blob.getPayload().openStream();
if (is instanceof FileInputStream) {
// except for FileInputStream since large MPU can open too many fds
is.close();
payload = blob.getPayload();
} else {
blob.resetPayload(/*release=*/ false);
payload = new InputStreamPayload(is);
}
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
payload.setContentMetadata(blob.getMetadata().getContentMetadata());
blob.setPayload(payload);
copyPayloadHeadersToBlob(blob.getPayload(), blob);
return blob;
}

@@ -753,22 +767,7 @@ public BlobMetadata blobMetadata(String containerName, String key) {

private Blob copyBlob(Blob blob) {
Blob returnVal = blobFactory.create(BlobStoreUtils.copy(blob.getMetadata()));
// return InputStream to more closely follow real blobstore
Payload payload;
try {
InputStream is = blob.getPayload().openStream();
if (is instanceof FileInputStream) {
// except for FileInputStream since large MPU can open too many fds
is.close();
payload = blob.getPayload();
} else {
payload = new InputStreamPayload(blob.getPayload().openStream());
}
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
payload.setContentMetadata(blob.getMetadata().getContentMetadata());
returnVal.setPayload(payload);
returnVal.setPayload(blob.getPayload());
copyPayloadHeadersToBlob(blob.getPayload(), returnVal);
return returnVal;
}

0 comments on commit 9c21bf2

Please sign in to comment.