Skip to content

Commit 9415bb7

Browse files
authored
fix: When passing a sub-array (offset, length) to the Storage#create method the array is needlessly cloned (#506)
leading to unnecessary memory allocations. No new tests as it was already covered. Signed-off-by: benoit <[email protected]> Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [X] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/java-storage/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [X] Ensure the tests and linter pass - [X] Code coverage does not decrease (if any source code was changed) - [ ] Appropriate docs were updated (if necessary) Fixes #505 ☕️
1 parent 0e58c1c commit 9415bb7

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java

+17-8
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public Blob create(BlobInfo blobInfo, BlobTargetOption... options) {
154154
.setMd5(EMPTY_BYTE_ARRAY_MD5)
155155
.setCrc32c(EMPTY_BYTE_ARRAY_CRC32C)
156156
.build();
157-
return internalCreate(updatedInfo, EMPTY_BYTE_ARRAY, options);
157+
return internalCreate(updatedInfo, EMPTY_BYTE_ARRAY, 0, 0, options);
158158
}
159159

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

174174
@Override
175175
public Blob create(
176176
BlobInfo blobInfo, byte[] content, int offset, int length, BlobTargetOption... options) {
177177
content = firstNonNull(content, EMPTY_BYTE_ARRAY);
178-
byte[] subContent = Arrays.copyOfRange(content, offset, offset + length);
179178
BlobInfo updatedInfo =
180179
blobInfo
181180
.toBuilder()
182-
.setMd5(BaseEncoding.base64().encode(Hashing.md5().hashBytes(subContent).asBytes()))
181+
.setMd5(
182+
BaseEncoding.base64()
183+
.encode(Hashing.md5().hashBytes(content, offset, length).asBytes()))
183184
.setCrc32c(
184185
BaseEncoding.base64()
185-
.encode(Ints.toByteArray(Hashing.crc32c().hashBytes(subContent).asInt())))
186+
.encode(
187+
Ints.toByteArray(
188+
Hashing.crc32c().hashBytes(content, offset, length).asInt())))
186189
.build();
187-
return internalCreate(updatedInfo, subContent, options);
190+
return internalCreate(updatedInfo, content, offset, length, options);
188191
}
189192

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

202-
private Blob internalCreate(BlobInfo info, final byte[] content, BlobTargetOption... options) {
205+
private Blob internalCreate(
206+
BlobInfo info,
207+
final byte[] content,
208+
final int offset,
209+
final int length,
210+
BlobTargetOption... options) {
203211
Preconditions.checkNotNull(content);
204212
final StorageObject blobPb = info.toPb();
205213
final Map<StorageRpc.Option, ?> optionsMap = optionMap(info, options);
@@ -210,7 +218,8 @@ private Blob internalCreate(BlobInfo info, final byte[] content, BlobTargetOptio
210218
new Callable<StorageObject>() {
211219
@Override
212220
public StorageObject call() {
213-
return storageRpc.create(blobPb, new ByteArrayInputStream(content), optionsMap);
221+
return storageRpc.create(
222+
blobPb, new ByteArrayInputStream(content, offset, length), optionsMap);
214223
}
215224
},
216225
getOptions().getRetrySettings(),

0 commit comments

Comments
 (0)