Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: When passing a sub-array (offset, length) to the Storage#create method the array is needlessly cloned #506

Merged
merged 1 commit into from Sep 14, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -154,7 +154,7 @@ public Blob create(BlobInfo blobInfo, BlobTargetOption... options) {
.setMd5(EMPTY_BYTE_ARRAY_MD5)
.setCrc32c(EMPTY_BYTE_ARRAY_CRC32C)
.build();
return internalCreate(updatedInfo, EMPTY_BYTE_ARRAY, options);
return internalCreate(updatedInfo, EMPTY_BYTE_ARRAY, 0, 0, options);
}

@Override
Expand All @@ -168,23 +168,26 @@ public Blob create(BlobInfo blobInfo, byte[] content, BlobTargetOption... option
BaseEncoding.base64()
.encode(Ints.toByteArray(Hashing.crc32c().hashBytes(content).asInt())))
.build();
return internalCreate(updatedInfo, content, options);
return internalCreate(updatedInfo, content, 0, content.length, options);
}

@Override
public Blob create(
BlobInfo blobInfo, byte[] content, int offset, int length, BlobTargetOption... options) {
content = firstNonNull(content, EMPTY_BYTE_ARRAY);
byte[] subContent = Arrays.copyOfRange(content, offset, offset + length);
BlobInfo updatedInfo =
blobInfo
.toBuilder()
.setMd5(BaseEncoding.base64().encode(Hashing.md5().hashBytes(subContent).asBytes()))
.setMd5(
BaseEncoding.base64()
.encode(Hashing.md5().hashBytes(content, offset, length).asBytes()))
.setCrc32c(
BaseEncoding.base64()
.encode(Ints.toByteArray(Hashing.crc32c().hashBytes(subContent).asInt())))
.encode(
Ints.toByteArray(
Hashing.crc32c().hashBytes(content, offset, length).asInt())))
.build();
return internalCreate(updatedInfo, subContent, options);
return internalCreate(updatedInfo, content, offset, length, options);
}

@Override
Expand All @@ -199,7 +202,12 @@ public Blob create(BlobInfo blobInfo, InputStream content, BlobWriteOption... op
return Blob.fromPb(this, storageRpc.create(blobPb, inputStreamParam, optionsMap));
}

private Blob internalCreate(BlobInfo info, final byte[] content, BlobTargetOption... options) {
private Blob internalCreate(
BlobInfo info,
final byte[] content,
final int offset,
final int length,
BlobTargetOption... options) {
Preconditions.checkNotNull(content);
final StorageObject blobPb = info.toPb();
final Map<StorageRpc.Option, ?> optionsMap = optionMap(info, options);
Expand All @@ -210,7 +218,8 @@ private Blob internalCreate(BlobInfo info, final byte[] content, BlobTargetOptio
new Callable<StorageObject>() {
@Override
public StorageObject call() {
return storageRpc.create(blobPb, new ByteArrayInputStream(content), optionsMap);
return storageRpc.create(
blobPb, new ByteArrayInputStream(content, offset, length), optionsMap);
}
},
getOptions().getRetrySettings(),
Expand Down