Skip to content

Commit 8862ddf

Browse files
author
andrey-qlogic
committed
4117: Added Crc32cString() method and updated tests. Added to snippets.
1 parent 26ff7c6 commit 8862ddf

File tree

5 files changed

+36
-0
lines changed

5 files changed

+36
-0
lines changed

google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobInfo.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,26 @@ public String getCrc32c() {
704704
return Data.isNull(crc32c) ? null : crc32c;
705705
}
706706

707+
/**
708+
* Returns the CRC32C checksum of blob's data as described in <a
709+
* href="http://tools.ietf.org/html/rfc4960#appendix-B">RFC 4960, Appendix B;</a> decoded to
710+
* string.
711+
*
712+
* @see <a href="https://cloud.google.com/storage/docs/hashes-etags#_JSONAPI">Hashes and ETags:
713+
* Best Practices</a>
714+
*/
715+
public String getCrc32cString() {
716+
if (crc32c == null) {
717+
return null;
718+
}
719+
byte[] decodeCrc32c = BaseEncoding.base64().decode(crc32c);
720+
StringBuilder stringBuilder = new StringBuilder();
721+
for (byte b : decodeCrc32c) {
722+
stringBuilder.append(String.format("%02x", b & 0xff));
723+
}
724+
return stringBuilder.toString();
725+
}
726+
707727
/** Returns the blob's media download link. */
708728
public String getMediaLink() {
709729
return mediaLink;

google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobInfoTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,13 @@ public class BlobInfoTest {
4848
private static final String CONTENT_ENCODING = "UTF-8";
4949
private static final String CONTENT_LANGUAGE = "En";
5050
private static final String CRC32 = "0xFF00";
51+
private static final String CRC32_STRING = "d31145d3";
5152
private static final Long DELETE_TIME = System.currentTimeMillis();
5253
private static final String ETAG = "0xFF00";
5354
private static final Long GENERATION = 1L;
5455
private static final String GENERATED_ID = "B/N:1";
5556
private static final String MD5 = "0xFF00";
57+
private static final String MD5_STRING = "d31145d3";
5658
private static final String MEDIA_LINK = "http://media/b/n";
5759
private static final Map<String, String> METADATA = ImmutableMap.of("n1", "v1", "n2", "v2");
5860
private static final Long META_GENERATION = 10L;
@@ -142,11 +144,13 @@ public void testBuilder() {
142144
assertEquals(CONTENT_LANGUAGE, BLOB_INFO.getContentLanguage());
143145
assertEquals(CUSTOMER_ENCRYPTION, BLOB_INFO.getCustomerEncryption());
144146
assertEquals(CRC32, BLOB_INFO.getCrc32c());
147+
assertEquals(CRC32_STRING, BLOB_INFO.getCrc32cString());
145148
assertEquals(DELETE_TIME, BLOB_INFO.getDeleteTime());
146149
assertEquals(ETAG, BLOB_INFO.getEtag());
147150
assertEquals(GENERATION, BLOB_INFO.getGeneration());
148151
assertEquals(GENERATED_ID, BLOB_INFO.getGeneratedId());
149152
assertEquals(MD5, BLOB_INFO.getMd5());
153+
assertEquals(MD5_STRING, BLOB_INFO.getMd5String());
150154
assertEquals(MEDIA_LINK, BLOB_INFO.getMediaLink());
151155
assertEquals(METADATA, BLOB_INFO.getMetadata());
152156
assertEquals(META_GENERATION, BLOB_INFO.getMetageneration());
@@ -172,12 +176,14 @@ public void testBuilder() {
172176
assertNull(DIRECTORY_INFO.getContentLanguage());
173177
assertNull(DIRECTORY_INFO.getCustomerEncryption());
174178
assertNull(DIRECTORY_INFO.getCrc32c());
179+
assertNull(DIRECTORY_INFO.getCrc32cString());
175180
assertNull(DIRECTORY_INFO.getCreateTime());
176181
assertNull(DIRECTORY_INFO.getDeleteTime());
177182
assertNull(DIRECTORY_INFO.getEtag());
178183
assertNull(DIRECTORY_INFO.getGeneration());
179184
assertNull(DIRECTORY_INFO.getGeneratedId());
180185
assertNull(DIRECTORY_INFO.getMd5());
186+
assertNull(DIRECTORY_INFO.getMd5String());
181187
assertNull(DIRECTORY_INFO.getMediaLink());
182188
assertNull(DIRECTORY_INFO.getMetadata());
183189
assertNull(DIRECTORY_INFO.getMetageneration());
@@ -201,12 +207,14 @@ private void compareBlobs(BlobInfo expected, BlobInfo value) {
201207
assertEquals(expected.getContentLanguage(), value.getContentLanguage());
202208
assertEquals(expected.getCustomerEncryption(), value.getCustomerEncryption());
203209
assertEquals(expected.getCrc32c(), value.getCrc32c());
210+
assertEquals(expected.getCrc32cString(), value.getCrc32cString());
204211
assertEquals(expected.getCreateTime(), value.getCreateTime());
205212
assertEquals(expected.getDeleteTime(), value.getDeleteTime());
206213
assertEquals(expected.getEtag(), value.getEtag());
207214
assertEquals(expected.getGeneration(), value.getGeneration());
208215
assertEquals(expected.getGeneratedId(), value.getGeneratedId());
209216
assertEquals(expected.getMd5(), value.getMd5());
217+
assertEquals(expected.getMd5String(), value.getMd5String());
210218
assertEquals(expected.getMediaLink(), value.getMediaLink());
211219
assertEquals(expected.getMetadata(), value.getMetadata());
212220
assertEquals(expected.getMetageneration(), value.getMetageneration());
@@ -253,12 +261,14 @@ public void testToPbAndFromPb() {
253261
assertNull(blobInfo.getContentLanguage());
254262
assertNull(blobInfo.getCustomerEncryption());
255263
assertNull(blobInfo.getCrc32c());
264+
assertNull(blobInfo.getCrc32cString());
256265
assertNull(blobInfo.getCreateTime());
257266
assertNull(blobInfo.getDeleteTime());
258267
assertNull(blobInfo.getEtag());
259268
assertNull(blobInfo.getGeneration());
260269
assertNull(blobInfo.getGeneratedId());
261270
assertNull(blobInfo.getMd5());
271+
assertNull(blobInfo.getMd5String());
262272
assertNull(blobInfo.getMediaLink());
263273
assertNull(blobInfo.getMetadata());
264274
assertNull(blobInfo.getMetageneration());

google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public class BlobTest {
7171
private static final String CONTENT_ENCODING = "UTF-8";
7272
private static final String CONTENT_LANGUAGE = "En";
7373
private static final String CRC32 = "0xFF00";
74+
private static final String CRC32_STRING = "d31145d3";
7475
private static final Long DELETE_TIME = System.currentTimeMillis();
7576
private static final String ETAG = "0xFF00";
7677
private static final Long GENERATION = 1L;
@@ -507,6 +508,7 @@ public void testBuilder() {
507508
assertEquals(CONTENT_ENCODING, blob.getContentEncoding());
508509
assertEquals(CONTENT_LANGUAGE, blob.getContentLanguage());
509510
assertEquals(CRC32, blob.getCrc32c());
511+
assertEquals(CRC32_STRING, blob.getCrc32cString());
510512
assertEquals(CREATE_TIME, blob.getCreateTime());
511513
assertEquals(CUSTOMER_ENCRYPTION, blob.getCustomerEncryption());
512514
assertEquals(KMS_KEY_NAME, blob.getKmsKeyName());
@@ -539,6 +541,7 @@ public void testBuilder() {
539541
assertNull(blob.getContentEncoding());
540542
assertNull(blob.getContentLanguage());
541543
assertNull(blob.getCrc32c());
544+
assertNull(blob.getCrc32cString());
542545
assertNull(blob.getCreateTime());
543546
assertNull(blob.getCustomerEncryption());
544547
assertNull(blob.getKmsKeyName());

google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/StorageSnippets.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,6 +1136,7 @@ public void getBlobMetadata(String bucketName, String blobName) throws StorageEx
11361136
System.out.println("ContentLanguage: " + blob.getContentLanguage());
11371137
System.out.println("ContentType: " + blob.getContentType());
11381138
System.out.println("Crc32c: " + blob.getCrc32c());
1139+
System.out.println("Crc32cString: " + blob.getCrc32cString());
11391140
System.out.println("ETag: " + blob.getEtag());
11401141
System.out.println("Generation: " + blob.getGeneration());
11411142
System.out.println("Id: " + blob.getBlobId());

google-cloud-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITStorageSnippets.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,11 +458,13 @@ public void testGetBlobMetadata() {
458458
assertTrue(snippetOutput.contains("ContentLanguage: " + remoteBlob.getContentLanguage()));
459459
assertTrue(snippetOutput.contains("ContentType: " + remoteBlob.getContentType()));
460460
assertTrue(snippetOutput.contains("Crc32c: " + remoteBlob.getCrc32c()));
461+
assertTrue(snippetOutput.contains("Crc32cString: " + remoteBlob.getCrc32cString()));
461462
assertTrue(snippetOutput.contains("ETag: " + remoteBlob.getEtag()));
462463
assertTrue(snippetOutput.contains("Generation: " + remoteBlob.getGeneration()));
463464
assertTrue(snippetOutput.contains("Id: " + remoteBlob.getBlobId()));
464465
assertTrue(snippetOutput.contains("KmsKeyName: " + remoteBlob.getKmsKeyName()));
465466
assertTrue(snippetOutput.contains("Md5Hash: " + remoteBlob.getMd5()));
467+
assertTrue(snippetOutput.contains("Md5HashString: " + remoteBlob.getMd5String()));
466468
assertTrue(snippetOutput.contains("MediaLink: " + remoteBlob.getMediaLink()));
467469
assertTrue(snippetOutput.contains("Metageneration: " + remoteBlob.getMetageneration()));
468470
assertTrue(snippetOutput.contains("Name: " + remoteBlob.getName()));

0 commit comments

Comments
 (0)