Skip to content

Commit c7f641d

Browse files
committed
Added fix for reuquest body
1 parent 86ea017 commit c7f641d

File tree

4 files changed

+44
-24
lines changed

4 files changed

+44
-24
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import com.google.api.client.http.ByteArrayContent;
2121
import com.google.api.client.http.GenericUrl;
22+
import com.google.api.client.http.HttpContent;
2223
import com.google.api.client.http.HttpRequest;
2324
import com.google.api.client.http.HttpRequestFactory;
2425
import com.google.api.client.http.HttpResponse;
@@ -55,7 +56,7 @@ public HttpResponse sendCreateMultipartUploadRequest(
5556

5657
public HttpResponse sendUploadPartRequest(
5758
String uri,
58-
byte[] partData,
59+
HttpContent content,
5960
String authHeader,
6061
String contentType,
6162
String contentMd5,
@@ -64,7 +65,7 @@ public HttpResponse sendUploadPartRequest(
6465
throws IOException {
6566
HttpRequest httpRequest =
6667
requestFactory.buildPutRequest(
67-
new GenericUrl(uri), new ByteArrayContent(contentType, partData));
68+
new GenericUrl(uri), content);
6869
httpRequest.getHeaders().setAuthorization(authHeader);
6970
httpRequest.getHeaders().setContentType(contentType);
7071
httpRequest.getHeaders().setContentMD5(contentMd5);

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,8 @@ public UploadPartResponse uploadPart(UploadPartRequest request, RequestBody requ
169169
"?partNumber=" + request.partNumber() + "&uploadId=" + encode(request.uploadId());
170170
String uri = GCS_ENDPOINT + resourcePath + queryString;
171171
String contentType = "application/octet-stream";
172-
MessageDigest md = MessageDigest.getInstance("MD5");
173-
byte[] partData = requestBody.getPartData();
174-
String contentMd5 = Base64.getEncoder().encodeToString(md.digest(partData));
175-
String crc32cString =
176-
Base64.getEncoder()
177-
.encodeToString(
178-
ByteBuffer.allocate(4)
179-
.putInt(Hashing.crc32c().hashBytes(partData).asInt())
180-
.array());
172+
String contentMd5 = requestBody.getContent().getMd5();
173+
String crc32cString = requestBody.getContent().getCrc32c();
181174
Map<String, String> extensionHeaders = getGenericExtensionHeader();
182175
extensionHeaders.put("x-goog-hash", "crc32c=" + crc32cString + ",md5=" + contentMd5);
183176

@@ -187,7 +180,7 @@ public UploadPartResponse uploadPart(UploadPartRequest request, RequestBody requ
187180
credentials.refreshIfExpired();
188181
AccessToken accessToken = credentials.getAccessToken();
189182
String authHeader = "Bearer " + accessToken.getTokenValue();
190-
return httpRequestManager.sendUploadPartRequest(uri, partData, authHeader, contentType,
183+
return httpRequestManager.sendUploadPartRequest(uri, requestBody.getContent(), authHeader, contentType,
191184
contentMd5, crc32cString, extensionHeaders);
192185
},
193186
Decoder.identity());

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

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
public final class RequestBody {
2828

2929
private final RewindableContent content;
30-
private byte[] byteArray;
3130

3231
private RequestBody(RewindableContent content) {
3332
this.content = content;
@@ -38,9 +37,7 @@ RewindableContent getContent() {
3837
}
3938

4039
public static RequestBody empty() {
41-
RequestBody requestBody = new RequestBody(RewindableContent.empty());
42-
requestBody.byteArray = new byte[0];
43-
return requestBody;
40+
return new RequestBody(RewindableContent.empty());
4441
}
4542

4643
public static RequestBody of(ByteBuffer... buffers) {
@@ -51,9 +48,7 @@ public static RequestBody fromByteBuffer(ByteBuffer buffer) {
5148
ByteBuffer duplicate = buffer.duplicate();
5249
byte[] arr = new byte[duplicate.remaining()];
5350
duplicate.get(arr);
54-
RequestBody requestBody = new RequestBody(RewindableContent.of(buffer));
55-
requestBody.byteArray = arr;
56-
return requestBody;
51+
return new RequestBody(RewindableContent.of(buffer));
5752
}
5853

5954
public static RequestBody of(ByteBuffer[] srcs, int srcsOffset, int srcsLength) {
@@ -63,8 +58,4 @@ public static RequestBody of(ByteBuffer[] srcs, int srcsOffset, int srcsLength)
6358
public static RequestBody of(Path path) throws IOException {
6459
return new RequestBody(RewindableContent.of(path));
6560
}
66-
67-
public byte[] getPartData() {
68-
return byteArray;
69-
}
7061
}

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818

1919
import com.google.api.client.http.AbstractHttpContent;
2020
import com.google.api.client.http.HttpMediaType;
21+
import com.google.api.client.util.Base64;
2122
import com.google.common.base.Preconditions;
23+
import com.google.common.hash.Hashing;
24+
import com.google.common.hash.HashingOutputStream;
2225
import com.google.common.io.ByteStreams;
2326
import java.io.ByteArrayOutputStream;
2427
import java.io.IOException;
@@ -31,11 +34,17 @@
3134
import java.nio.file.Files;
3235
import java.nio.file.Path;
3336
import java.nio.file.StandardOpenOption;
37+
import java.security.DigestOutputStream;
38+
import java.security.MessageDigest;
39+
import java.security.NoSuchAlgorithmException;
3440
import java.util.Arrays;
3541
import java.util.Locale;
3642

3743
abstract class RewindableContent extends AbstractHttpContent {
3844

45+
private String md5;
46+
private String crc32c;
47+
3948
private RewindableContent() {
4049
super((HttpMediaType) null);
4150
}
@@ -58,7 +67,6 @@ private RewindableContent() {
5867
* this may cause an OutOfMemoryError.
5968
*
6069
* @return The byte array representation of the content.
61-
* @throws IOException if an I/O error occurs.
6270
*/
6371
public byte[] asByteArray() {
6472
if (getLength() == 0) {
@@ -80,6 +88,31 @@ public final boolean retrySupported() {
8088
return false;
8189
}
8290

91+
public String getMd5() throws IOException {
92+
if (md5 == null) {
93+
try {
94+
MessageDigest md = MessageDigest.getInstance("MD5");
95+
writeTo(new DigestOutputStream(ByteStreams.nullOutputStream(), md));
96+
md5 = Base64.encodeBase64String(md.digest());
97+
} catch (NoSuchAlgorithmException e) {
98+
throw new IOException(e);
99+
}
100+
}
101+
return md5;
102+
}
103+
104+
public String getCrc32c() throws IOException {
105+
if (crc32c == null) {
106+
HashingOutputStream hashingOutputStream =
107+
new HashingOutputStream(Hashing.crc32c(), ByteStreams.nullOutputStream());
108+
writeTo(hashingOutputStream);
109+
byte[] bytes =
110+
ByteBuffer.allocate(4).putInt(hashingOutputStream.hash().asInt()).array();
111+
crc32c = Base64.encodeBase64String(bytes);
112+
}
113+
return crc32c;
114+
}
115+
83116
static RewindableContent empty() {
84117
return EmptyRewindableContent.INSTANCE;
85118
}
@@ -121,6 +154,8 @@ public void writeTo(OutputStream out) throws IOException {
121154
out.flush();
122155
}
123156

157+
158+
124159
@Override
125160
long writeTo(WritableByteChannel gbc) {
126161
return 0;

0 commit comments

Comments
 (0)