From 26ff7c6c27fcddf38ef6d032e637074d1b927d51 Mon Sep 17 00:00:00 2001 From: andrey-qlogic Date: Mon, 21 Jan 2019 12:38:51 +0000 Subject: [PATCH 01/11] 4117: Added Md5String() method and updated tests. --- .../com/google/cloud/storage/BlobInfo.java | 20 ++++++++++++++++++- .../com/google/cloud/storage/BlobTest.java | 3 +++ .../storage/snippets/StorageSnippets.java | 1 + 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobInfo.java b/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobInfo.java index 2ace22d6f9f1..0aca9cd95e60 100644 --- a/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobInfo.java +++ b/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobInfo.java @@ -25,13 +25,13 @@ import com.google.api.services.storage.model.ObjectAccessControl; import com.google.api.services.storage.model.StorageObject; import com.google.api.services.storage.model.StorageObject.Owner; -import com.google.cloud.storage.Blob.Builder; import com.google.common.base.Function; import com.google.common.base.MoreObjects; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Lists; import com.google.common.collect.Maps; +import com.google.common.io.BaseEncoding; import java.io.Serializable; import java.math.BigInteger; import java.util.AbstractMap; @@ -674,6 +674,24 @@ public String getMd5() { return Data.isNull(md5) ? null : md5; } + /** + * Returns the MD5 hash of blob's data decoded to string. + * + * @see Hashes and ETags: + * Best Practices + */ + public String getMd5String() { + if (md5 == null) { + return null; + } + byte[] decodedMd5 = BaseEncoding.base64().decode(md5); + StringBuilder stringBuilder = new StringBuilder(); + for (byte b : decodedMd5) { + stringBuilder.append(String.format("%02x", b & 0xff)); + } + return stringBuilder.toString(); + } + /** * Returns the CRC32C checksum of blob's data as described in RFC 4960, Appendix B; encoded in diff --git a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobTest.java b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobTest.java index ff517d360dd9..c8ce99a1110f 100644 --- a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobTest.java +++ b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobTest.java @@ -76,6 +76,7 @@ public class BlobTest { private static final Long GENERATION = 1L; private static final String GENERATED_ID = "B/N:1"; private static final String MD5 = "0xFF00"; + private static final String MD5_STRING = "d31145d3"; private static final String MEDIA_LINK = "http://media/b/n"; private static final Map METADATA = ImmutableMap.of("n1", "v1", "n2", "v2"); private static final Long META_GENERATION = 10L; @@ -516,6 +517,7 @@ public void testBuilder() { assertEquals(ETAG, blob.getEtag()); assertEquals(GENERATED_ID, blob.getGeneratedId()); assertEquals(MD5, blob.getMd5()); + assertEquals(MD5_STRING, blob.getMd5String()); assertEquals(MEDIA_LINK, blob.getMediaLink()); assertEquals(METADATA, blob.getMetadata()); assertEquals(META_GENERATION, blob.getMetageneration()); @@ -547,6 +549,7 @@ public void testBuilder() { assertNull(blob.getEtag()); assertNull(blob.getGeneratedId()); assertNull(blob.getMd5()); + assertNull(blob.getMd5String()); assertNull(blob.getMediaLink()); assertNull(blob.getMetadata()); assertNull(blob.getMetageneration()); diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/StorageSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/StorageSnippets.java index ab2ec4df8edf..7c1f58992ed0 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/StorageSnippets.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/StorageSnippets.java @@ -1141,6 +1141,7 @@ public void getBlobMetadata(String bucketName, String blobName) throws StorageEx System.out.println("Id: " + blob.getBlobId()); System.out.println("KmsKeyName: " + blob.getKmsKeyName()); System.out.println("Md5Hash: " + blob.getMd5()); + System.out.println("Md5String: " + blob.getMd5String()); System.out.println("MediaLink: " + blob.getMediaLink()); System.out.println("Metageneration: " + blob.getMetageneration()); System.out.println("Name: " + blob.getName()); From 8862ddfe9ae751c7836aa2937ae688cbf89a0756 Mon Sep 17 00:00:00 2001 From: andrey-qlogic Date: Wed, 23 Jan 2019 10:31:02 +0000 Subject: [PATCH 02/11] 4117: Added Crc32cString() method and updated tests. Added to snippets. --- .../com/google/cloud/storage/BlobInfo.java | 20 +++++++++++++++++++ .../google/cloud/storage/BlobInfoTest.java | 10 ++++++++++ .../com/google/cloud/storage/BlobTest.java | 3 +++ .../storage/snippets/StorageSnippets.java | 1 + .../storage/snippets/ITStorageSnippets.java | 2 ++ 5 files changed, 36 insertions(+) diff --git a/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobInfo.java b/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobInfo.java index 0aca9cd95e60..ac3fb5e7b38f 100644 --- a/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobInfo.java +++ b/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobInfo.java @@ -704,6 +704,26 @@ public String getCrc32c() { return Data.isNull(crc32c) ? null : crc32c; } + /** + * Returns the CRC32C checksum of blob's data as described in RFC 4960, Appendix B; decoded to + * string. + * + * @see Hashes and ETags: + * Best Practices + */ + public String getCrc32cString() { + if (crc32c == null) { + return null; + } + byte[] decodeCrc32c = BaseEncoding.base64().decode(crc32c); + StringBuilder stringBuilder = new StringBuilder(); + for (byte b : decodeCrc32c) { + stringBuilder.append(String.format("%02x", b & 0xff)); + } + return stringBuilder.toString(); + } + /** Returns the blob's media download link. */ public String getMediaLink() { return mediaLink; diff --git a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobInfoTest.java b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobInfoTest.java index aea38d161f9a..5dd6518e3031 100644 --- a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobInfoTest.java +++ b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobInfoTest.java @@ -48,11 +48,13 @@ public class BlobInfoTest { private static final String CONTENT_ENCODING = "UTF-8"; private static final String CONTENT_LANGUAGE = "En"; private static final String CRC32 = "0xFF00"; + private static final String CRC32_STRING = "d31145d3"; private static final Long DELETE_TIME = System.currentTimeMillis(); private static final String ETAG = "0xFF00"; private static final Long GENERATION = 1L; private static final String GENERATED_ID = "B/N:1"; private static final String MD5 = "0xFF00"; + private static final String MD5_STRING = "d31145d3"; private static final String MEDIA_LINK = "http://media/b/n"; private static final Map METADATA = ImmutableMap.of("n1", "v1", "n2", "v2"); private static final Long META_GENERATION = 10L; @@ -142,11 +144,13 @@ public void testBuilder() { assertEquals(CONTENT_LANGUAGE, BLOB_INFO.getContentLanguage()); assertEquals(CUSTOMER_ENCRYPTION, BLOB_INFO.getCustomerEncryption()); assertEquals(CRC32, BLOB_INFO.getCrc32c()); + assertEquals(CRC32_STRING, BLOB_INFO.getCrc32cString()); assertEquals(DELETE_TIME, BLOB_INFO.getDeleteTime()); assertEquals(ETAG, BLOB_INFO.getEtag()); assertEquals(GENERATION, BLOB_INFO.getGeneration()); assertEquals(GENERATED_ID, BLOB_INFO.getGeneratedId()); assertEquals(MD5, BLOB_INFO.getMd5()); + assertEquals(MD5_STRING, BLOB_INFO.getMd5String()); assertEquals(MEDIA_LINK, BLOB_INFO.getMediaLink()); assertEquals(METADATA, BLOB_INFO.getMetadata()); assertEquals(META_GENERATION, BLOB_INFO.getMetageneration()); @@ -172,12 +176,14 @@ public void testBuilder() { assertNull(DIRECTORY_INFO.getContentLanguage()); assertNull(DIRECTORY_INFO.getCustomerEncryption()); assertNull(DIRECTORY_INFO.getCrc32c()); + assertNull(DIRECTORY_INFO.getCrc32cString()); assertNull(DIRECTORY_INFO.getCreateTime()); assertNull(DIRECTORY_INFO.getDeleteTime()); assertNull(DIRECTORY_INFO.getEtag()); assertNull(DIRECTORY_INFO.getGeneration()); assertNull(DIRECTORY_INFO.getGeneratedId()); assertNull(DIRECTORY_INFO.getMd5()); + assertNull(DIRECTORY_INFO.getMd5String()); assertNull(DIRECTORY_INFO.getMediaLink()); assertNull(DIRECTORY_INFO.getMetadata()); assertNull(DIRECTORY_INFO.getMetageneration()); @@ -201,12 +207,14 @@ private void compareBlobs(BlobInfo expected, BlobInfo value) { assertEquals(expected.getContentLanguage(), value.getContentLanguage()); assertEquals(expected.getCustomerEncryption(), value.getCustomerEncryption()); assertEquals(expected.getCrc32c(), value.getCrc32c()); + assertEquals(expected.getCrc32cString(), value.getCrc32cString()); assertEquals(expected.getCreateTime(), value.getCreateTime()); assertEquals(expected.getDeleteTime(), value.getDeleteTime()); assertEquals(expected.getEtag(), value.getEtag()); assertEquals(expected.getGeneration(), value.getGeneration()); assertEquals(expected.getGeneratedId(), value.getGeneratedId()); assertEquals(expected.getMd5(), value.getMd5()); + assertEquals(expected.getMd5String(), value.getMd5String()); assertEquals(expected.getMediaLink(), value.getMediaLink()); assertEquals(expected.getMetadata(), value.getMetadata()); assertEquals(expected.getMetageneration(), value.getMetageneration()); @@ -253,12 +261,14 @@ public void testToPbAndFromPb() { assertNull(blobInfo.getContentLanguage()); assertNull(blobInfo.getCustomerEncryption()); assertNull(blobInfo.getCrc32c()); + assertNull(blobInfo.getCrc32cString()); assertNull(blobInfo.getCreateTime()); assertNull(blobInfo.getDeleteTime()); assertNull(blobInfo.getEtag()); assertNull(blobInfo.getGeneration()); assertNull(blobInfo.getGeneratedId()); assertNull(blobInfo.getMd5()); + assertNull(blobInfo.getMd5String()); assertNull(blobInfo.getMediaLink()); assertNull(blobInfo.getMetadata()); assertNull(blobInfo.getMetageneration()); diff --git a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobTest.java b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobTest.java index c8ce99a1110f..5eef4c66cfe6 100644 --- a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobTest.java +++ b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobTest.java @@ -71,6 +71,7 @@ public class BlobTest { private static final String CONTENT_ENCODING = "UTF-8"; private static final String CONTENT_LANGUAGE = "En"; private static final String CRC32 = "0xFF00"; + private static final String CRC32_STRING = "d31145d3"; private static final Long DELETE_TIME = System.currentTimeMillis(); private static final String ETAG = "0xFF00"; private static final Long GENERATION = 1L; @@ -507,6 +508,7 @@ public void testBuilder() { assertEquals(CONTENT_ENCODING, blob.getContentEncoding()); assertEquals(CONTENT_LANGUAGE, blob.getContentLanguage()); assertEquals(CRC32, blob.getCrc32c()); + assertEquals(CRC32_STRING, blob.getCrc32cString()); assertEquals(CREATE_TIME, blob.getCreateTime()); assertEquals(CUSTOMER_ENCRYPTION, blob.getCustomerEncryption()); assertEquals(KMS_KEY_NAME, blob.getKmsKeyName()); @@ -539,6 +541,7 @@ public void testBuilder() { assertNull(blob.getContentEncoding()); assertNull(blob.getContentLanguage()); assertNull(blob.getCrc32c()); + assertNull(blob.getCrc32cString()); assertNull(blob.getCreateTime()); assertNull(blob.getCustomerEncryption()); assertNull(blob.getKmsKeyName()); diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/StorageSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/StorageSnippets.java index 7c1f58992ed0..61f34a689d82 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/StorageSnippets.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/StorageSnippets.java @@ -1136,6 +1136,7 @@ public void getBlobMetadata(String bucketName, String blobName) throws StorageEx System.out.println("ContentLanguage: " + blob.getContentLanguage()); System.out.println("ContentType: " + blob.getContentType()); System.out.println("Crc32c: " + blob.getCrc32c()); + System.out.println("Crc32cString: " + blob.getCrc32cString()); System.out.println("ETag: " + blob.getEtag()); System.out.println("Generation: " + blob.getGeneration()); System.out.println("Id: " + blob.getBlobId()); diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITStorageSnippets.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITStorageSnippets.java index 19db91dcf44b..239a00d90482 100644 --- a/google-cloud-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITStorageSnippets.java +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITStorageSnippets.java @@ -458,11 +458,13 @@ public void testGetBlobMetadata() { assertTrue(snippetOutput.contains("ContentLanguage: " + remoteBlob.getContentLanguage())); assertTrue(snippetOutput.contains("ContentType: " + remoteBlob.getContentType())); assertTrue(snippetOutput.contains("Crc32c: " + remoteBlob.getCrc32c())); + assertTrue(snippetOutput.contains("Crc32cString: " + remoteBlob.getCrc32cString())); assertTrue(snippetOutput.contains("ETag: " + remoteBlob.getEtag())); assertTrue(snippetOutput.contains("Generation: " + remoteBlob.getGeneration())); assertTrue(snippetOutput.contains("Id: " + remoteBlob.getBlobId())); assertTrue(snippetOutput.contains("KmsKeyName: " + remoteBlob.getKmsKeyName())); assertTrue(snippetOutput.contains("Md5Hash: " + remoteBlob.getMd5())); + assertTrue(snippetOutput.contains("Md5HashString: " + remoteBlob.getMd5String())); assertTrue(snippetOutput.contains("MediaLink: " + remoteBlob.getMediaLink())); assertTrue(snippetOutput.contains("Metageneration: " + remoteBlob.getMetageneration())); assertTrue(snippetOutput.contains("Name: " + remoteBlob.getName())); From 85588a3c990f2a080ed38ee4555711ca33f1522d Mon Sep 17 00:00:00 2001 From: andrey-qlogic Date: Fri, 25 Jan 2019 11:36:19 +0000 Subject: [PATCH 03/11] 4117: Renamed new methods using naming convention getMd5ToHexString(), getCrc32cToHexString() --- .../java/com/google/cloud/storage/BlobInfo.java | 4 ++-- .../com/google/cloud/storage/BlobInfoTest.java | 16 ++++++++-------- .../java/com/google/cloud/storage/BlobTest.java | 8 ++++---- .../storage/snippets/StorageSnippets.java | 4 ++-- .../storage/snippets/ITStorageSnippets.java | 4 ++-- 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobInfo.java b/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobInfo.java index ac3fb5e7b38f..bb8864174dfe 100644 --- a/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobInfo.java +++ b/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobInfo.java @@ -680,7 +680,7 @@ public String getMd5() { * @see Hashes and ETags: * Best Practices */ - public String getMd5String() { + public String getMd5ToHexString() { if (md5 == null) { return null; } @@ -712,7 +712,7 @@ public String getCrc32c() { * @see Hashes and ETags: * Best Practices */ - public String getCrc32cString() { + public String getCrc32cToHexString() { if (crc32c == null) { return null; } diff --git a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobInfoTest.java b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobInfoTest.java index 5dd6518e3031..3fa1bae7e6d5 100644 --- a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobInfoTest.java +++ b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobInfoTest.java @@ -144,13 +144,13 @@ public void testBuilder() { assertEquals(CONTENT_LANGUAGE, BLOB_INFO.getContentLanguage()); assertEquals(CUSTOMER_ENCRYPTION, BLOB_INFO.getCustomerEncryption()); assertEquals(CRC32, BLOB_INFO.getCrc32c()); - assertEquals(CRC32_STRING, BLOB_INFO.getCrc32cString()); + assertEquals(CRC32_STRING, BLOB_INFO.getCrc32cToHexString()); assertEquals(DELETE_TIME, BLOB_INFO.getDeleteTime()); assertEquals(ETAG, BLOB_INFO.getEtag()); assertEquals(GENERATION, BLOB_INFO.getGeneration()); assertEquals(GENERATED_ID, BLOB_INFO.getGeneratedId()); assertEquals(MD5, BLOB_INFO.getMd5()); - assertEquals(MD5_STRING, BLOB_INFO.getMd5String()); + assertEquals(MD5_STRING, BLOB_INFO.getMd5ToHexString()); assertEquals(MEDIA_LINK, BLOB_INFO.getMediaLink()); assertEquals(METADATA, BLOB_INFO.getMetadata()); assertEquals(META_GENERATION, BLOB_INFO.getMetageneration()); @@ -176,14 +176,14 @@ public void testBuilder() { assertNull(DIRECTORY_INFO.getContentLanguage()); assertNull(DIRECTORY_INFO.getCustomerEncryption()); assertNull(DIRECTORY_INFO.getCrc32c()); - assertNull(DIRECTORY_INFO.getCrc32cString()); + assertNull(DIRECTORY_INFO.getCrc32cToHexString()); assertNull(DIRECTORY_INFO.getCreateTime()); assertNull(DIRECTORY_INFO.getDeleteTime()); assertNull(DIRECTORY_INFO.getEtag()); assertNull(DIRECTORY_INFO.getGeneration()); assertNull(DIRECTORY_INFO.getGeneratedId()); assertNull(DIRECTORY_INFO.getMd5()); - assertNull(DIRECTORY_INFO.getMd5String()); + assertNull(DIRECTORY_INFO.getMd5ToHexString()); assertNull(DIRECTORY_INFO.getMediaLink()); assertNull(DIRECTORY_INFO.getMetadata()); assertNull(DIRECTORY_INFO.getMetageneration()); @@ -207,14 +207,14 @@ private void compareBlobs(BlobInfo expected, BlobInfo value) { assertEquals(expected.getContentLanguage(), value.getContentLanguage()); assertEquals(expected.getCustomerEncryption(), value.getCustomerEncryption()); assertEquals(expected.getCrc32c(), value.getCrc32c()); - assertEquals(expected.getCrc32cString(), value.getCrc32cString()); + assertEquals(expected.getCrc32cToHexString(), value.getCrc32cToHexString()); assertEquals(expected.getCreateTime(), value.getCreateTime()); assertEquals(expected.getDeleteTime(), value.getDeleteTime()); assertEquals(expected.getEtag(), value.getEtag()); assertEquals(expected.getGeneration(), value.getGeneration()); assertEquals(expected.getGeneratedId(), value.getGeneratedId()); assertEquals(expected.getMd5(), value.getMd5()); - assertEquals(expected.getMd5String(), value.getMd5String()); + assertEquals(expected.getMd5ToHexString(), value.getMd5ToHexString()); assertEquals(expected.getMediaLink(), value.getMediaLink()); assertEquals(expected.getMetadata(), value.getMetadata()); assertEquals(expected.getMetageneration(), value.getMetageneration()); @@ -261,14 +261,14 @@ public void testToPbAndFromPb() { assertNull(blobInfo.getContentLanguage()); assertNull(blobInfo.getCustomerEncryption()); assertNull(blobInfo.getCrc32c()); - assertNull(blobInfo.getCrc32cString()); + assertNull(blobInfo.getCrc32cToHexString()); assertNull(blobInfo.getCreateTime()); assertNull(blobInfo.getDeleteTime()); assertNull(blobInfo.getEtag()); assertNull(blobInfo.getGeneration()); assertNull(blobInfo.getGeneratedId()); assertNull(blobInfo.getMd5()); - assertNull(blobInfo.getMd5String()); + assertNull(blobInfo.getMd5ToHexString()); assertNull(blobInfo.getMediaLink()); assertNull(blobInfo.getMetadata()); assertNull(blobInfo.getMetageneration()); diff --git a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobTest.java b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobTest.java index 5eef4c66cfe6..5861f9d67c0b 100644 --- a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobTest.java +++ b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobTest.java @@ -508,7 +508,7 @@ public void testBuilder() { assertEquals(CONTENT_ENCODING, blob.getContentEncoding()); assertEquals(CONTENT_LANGUAGE, blob.getContentLanguage()); assertEquals(CRC32, blob.getCrc32c()); - assertEquals(CRC32_STRING, blob.getCrc32cString()); + assertEquals(CRC32_STRING, blob.getCrc32cToHexString()); assertEquals(CREATE_TIME, blob.getCreateTime()); assertEquals(CUSTOMER_ENCRYPTION, blob.getCustomerEncryption()); assertEquals(KMS_KEY_NAME, blob.getKmsKeyName()); @@ -519,7 +519,7 @@ public void testBuilder() { assertEquals(ETAG, blob.getEtag()); assertEquals(GENERATED_ID, blob.getGeneratedId()); assertEquals(MD5, blob.getMd5()); - assertEquals(MD5_STRING, blob.getMd5String()); + assertEquals(MD5_STRING, blob.getMd5ToHexString()); assertEquals(MEDIA_LINK, blob.getMediaLink()); assertEquals(METADATA, blob.getMetadata()); assertEquals(META_GENERATION, blob.getMetageneration()); @@ -541,7 +541,7 @@ public void testBuilder() { assertNull(blob.getContentEncoding()); assertNull(blob.getContentLanguage()); assertNull(blob.getCrc32c()); - assertNull(blob.getCrc32cString()); + assertNull(blob.getCrc32cToHexString()); assertNull(blob.getCreateTime()); assertNull(blob.getCustomerEncryption()); assertNull(blob.getKmsKeyName()); @@ -552,7 +552,7 @@ public void testBuilder() { assertNull(blob.getEtag()); assertNull(blob.getGeneratedId()); assertNull(blob.getMd5()); - assertNull(blob.getMd5String()); + assertNull(blob.getMd5ToHexString()); assertNull(blob.getMediaLink()); assertNull(blob.getMetadata()); assertNull(blob.getMetageneration()); diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/StorageSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/StorageSnippets.java index 61f34a689d82..837756368421 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/StorageSnippets.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/StorageSnippets.java @@ -1136,13 +1136,13 @@ public void getBlobMetadata(String bucketName, String blobName) throws StorageEx System.out.println("ContentLanguage: " + blob.getContentLanguage()); System.out.println("ContentType: " + blob.getContentType()); System.out.println("Crc32c: " + blob.getCrc32c()); - System.out.println("Crc32cString: " + blob.getCrc32cString()); + System.out.println("Crc32cString: " + blob.getCrc32cToHexString()); System.out.println("ETag: " + blob.getEtag()); System.out.println("Generation: " + blob.getGeneration()); System.out.println("Id: " + blob.getBlobId()); System.out.println("KmsKeyName: " + blob.getKmsKeyName()); System.out.println("Md5Hash: " + blob.getMd5()); - System.out.println("Md5String: " + blob.getMd5String()); + System.out.println("Md5String: " + blob.getMd5ToHexString()); System.out.println("MediaLink: " + blob.getMediaLink()); System.out.println("Metageneration: " + blob.getMetageneration()); System.out.println("Name: " + blob.getName()); diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITStorageSnippets.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITStorageSnippets.java index 239a00d90482..2f02cd3896c7 100644 --- a/google-cloud-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITStorageSnippets.java +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITStorageSnippets.java @@ -458,13 +458,13 @@ public void testGetBlobMetadata() { assertTrue(snippetOutput.contains("ContentLanguage: " + remoteBlob.getContentLanguage())); assertTrue(snippetOutput.contains("ContentType: " + remoteBlob.getContentType())); assertTrue(snippetOutput.contains("Crc32c: " + remoteBlob.getCrc32c())); - assertTrue(snippetOutput.contains("Crc32cString: " + remoteBlob.getCrc32cString())); + assertTrue(snippetOutput.contains("Crc32cString: " + remoteBlob.getCrc32cToHexString())); assertTrue(snippetOutput.contains("ETag: " + remoteBlob.getEtag())); assertTrue(snippetOutput.contains("Generation: " + remoteBlob.getGeneration())); assertTrue(snippetOutput.contains("Id: " + remoteBlob.getBlobId())); assertTrue(snippetOutput.contains("KmsKeyName: " + remoteBlob.getKmsKeyName())); assertTrue(snippetOutput.contains("Md5Hash: " + remoteBlob.getMd5())); - assertTrue(snippetOutput.contains("Md5HashString: " + remoteBlob.getMd5String())); + assertTrue(snippetOutput.contains("Md5HashString: " + remoteBlob.getMd5ToHexString())); assertTrue(snippetOutput.contains("MediaLink: " + remoteBlob.getMediaLink())); assertTrue(snippetOutput.contains("Metageneration: " + remoteBlob.getMetageneration())); assertTrue(snippetOutput.contains("Name: " + remoteBlob.getName())); From 420184bcac445b356282d2267ba3debb8f87e3e7 Mon Sep 17 00:00:00 2001 From: andrey-qlogic Date: Mon, 4 Feb 2019 09:15:44 +0000 Subject: [PATCH 04/11] 4117: Added methods to set Md5 and Crc32s from hex string. --- .../java/com/google/cloud/storage/Blob.java | 12 +++++ .../com/google/cloud/storage/BlobInfo.java | 49 ++++++++++++++++++- .../google/cloud/storage/BlobInfoTest.java | 18 ++++++- .../storage/snippets/StorageSnippets.java | 4 +- .../storage/snippets/ITStorageSnippets.java | 4 +- 5 files changed, 79 insertions(+), 8 deletions(-) diff --git a/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/Blob.java b/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/Blob.java index 7cdf742ef31e..da7e27a72851 100644 --- a/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/Blob.java +++ b/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/Blob.java @@ -333,12 +333,24 @@ public Builder setMd5(String md5) { return this; } + @Override + public Builder setMd5FromHexString(String md5HexString) { + infoBuilder.setMd5FromHexString(md5HexString); + return this; + } + @Override public Builder setCrc32c(String crc32c) { infoBuilder.setCrc32c(crc32c); return this; } + @Override + public Builder setCrc32cFromHexString(String crc32cHexString) { + infoBuilder.setCrc32cFromHexString(crc32cHexString); + return this; + } + @Override Builder setMediaLink(String mediaLink) { infoBuilder.setMediaLink(mediaLink); diff --git a/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobInfo.java b/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobInfo.java index bb8864174dfe..b95129c7a40a 100644 --- a/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobInfo.java +++ b/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobInfo.java @@ -226,6 +226,14 @@ public abstract static class Builder { */ public abstract Builder setMd5(String md5); + /** + * Sets the MD5 hash of blob's data from hex string. + * + * @see Hashes and ETags: + * Best Practices + */ + public abstract Builder setMd5FromHexString(String md5HexString); + /** * Sets the CRC32C checksum of blob's data as described in RFC 4960, Appendix B; encoded in @@ -236,6 +244,16 @@ public abstract static class Builder { */ public abstract Builder setCrc32c(String crc32c); + /** + * Sets the CRC32C checksum of blob's data as described in RFC 4960, Appendix B; from hex + * string. + * + * @see Hashes and ETags: + * Best Practices + */ + public abstract Builder setCrc32cFromHexString(String crc32cHexString); + abstract Builder setMediaLink(String mediaLink); /** Sets the blob's storage class. */ @@ -423,12 +441,37 @@ public Builder setMd5(String md5) { return this; } + public Builder setMd5FromHexString(String md5HexString) { + if (md5HexString == null) { + return this; + } + if (md5HexString.startsWith("0x")) { + md5HexString = md5HexString.replaceFirst("0x", ""); + } + byte[] bytes = new BigInteger(md5HexString, 16).toByteArray(); + this.md5 = "0x" + BaseEncoding.base64().omitPadding().encode(bytes); + return this; + } + @Override public Builder setCrc32c(String crc32c) { this.crc32c = firstNonNull(crc32c, Data.nullOf(String.class)); return this; } + @Override + public Builder setCrc32cFromHexString(String crc32cHexString) { + if (crc32cHexString == null) { + return this; + } + if (crc32cHexString.startsWith("0x")) { + crc32cHexString = crc32cHexString.replaceFirst("0x", ""); + } + byte[] bytes = new BigInteger(crc32cHexString, 16).toByteArray(); + this.crc32c = "0x" + BaseEncoding.base64().omitPadding().encode(bytes); + return this; + } + @Override Builder setMediaLink(String mediaLink) { this.mediaLink = mediaLink; @@ -684,7 +727,8 @@ public String getMd5ToHexString() { if (md5 == null) { return null; } - byte[] decodedMd5 = BaseEncoding.base64().decode(md5); + String md5withoutPrefix = md5.startsWith("0x") ? md5.replaceFirst("0x", "") : md5; + byte[] decodedMd5 = BaseEncoding.base64().decode(md5withoutPrefix); StringBuilder stringBuilder = new StringBuilder(); for (byte b : decodedMd5) { stringBuilder.append(String.format("%02x", b & 0xff)); @@ -716,7 +760,8 @@ public String getCrc32cToHexString() { if (crc32c == null) { return null; } - byte[] decodeCrc32c = BaseEncoding.base64().decode(crc32c); + String crc32cWithoutPrefix = crc32c.startsWith("0x") ? crc32c.replaceFirst("0x", "") : crc32c; + byte[] decodeCrc32c = BaseEncoding.base64().decode(crc32cWithoutPrefix); StringBuilder stringBuilder = new StringBuilder(); for (byte b : decodeCrc32c) { stringBuilder.append(String.format("%02x", b & 0xff)); diff --git a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobInfoTest.java b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobInfoTest.java index 3fa1bae7e6d5..331b5b3d0ccb 100644 --- a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobInfoTest.java +++ b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobInfoTest.java @@ -48,13 +48,13 @@ public class BlobInfoTest { private static final String CONTENT_ENCODING = "UTF-8"; private static final String CONTENT_LANGUAGE = "En"; private static final String CRC32 = "0xFF00"; - private static final String CRC32_STRING = "d31145d3"; + private static final String CRC32_STRING = "145d34"; private static final Long DELETE_TIME = System.currentTimeMillis(); private static final String ETAG = "0xFF00"; private static final Long GENERATION = 1L; private static final String GENERATED_ID = "B/N:1"; private static final String MD5 = "0xFF00"; - private static final String MD5_STRING = "d31145d3"; + private static final String MD5_STRING = "145d34"; private static final String MEDIA_LINK = "http://media/b/n"; private static final Map METADATA = ImmutableMap.of("n1", "v1", "n2", "v2"); private static final Long META_GENERATION = 10L; @@ -125,6 +125,20 @@ public void testToBuilder() { compareBlobs(BLOB_INFO, blobInfo); } + @Test + public void testToBuilderSetMd5FromHexString() { + BlobInfo blobInfo = + BlobInfo.newBuilder(BlobId.of("b2", "n2")).setMd5FromHexString(MD5_STRING).build(); + assertEquals(MD5, blobInfo.getMd5()); + } + + @Test + public void testToBuilderSetCrc32cFromHexString() { + BlobInfo blobInfo = + BlobInfo.newBuilder(BlobId.of("b2", "n2")).setCrc32cFromHexString(CRC32_STRING).build(); + assertEquals(CRC32, blobInfo.getCrc32c()); + } + @Test public void testToBuilderIncomplete() { BlobInfo incompleteBlobInfo = BlobInfo.newBuilder(BlobId.of("b2", "n2")).build(); diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/StorageSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/StorageSnippets.java index 837756368421..30a603c4b66c 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/StorageSnippets.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/StorageSnippets.java @@ -1136,13 +1136,13 @@ public void getBlobMetadata(String bucketName, String blobName) throws StorageEx System.out.println("ContentLanguage: " + blob.getContentLanguage()); System.out.println("ContentType: " + blob.getContentType()); System.out.println("Crc32c: " + blob.getCrc32c()); - System.out.println("Crc32cString: " + blob.getCrc32cToHexString()); + System.out.println("Crc32cHexString: " + blob.getCrc32cToHexString()); System.out.println("ETag: " + blob.getEtag()); System.out.println("Generation: " + blob.getGeneration()); System.out.println("Id: " + blob.getBlobId()); System.out.println("KmsKeyName: " + blob.getKmsKeyName()); System.out.println("Md5Hash: " + blob.getMd5()); - System.out.println("Md5String: " + blob.getMd5ToHexString()); + System.out.println("Md5HexString: " + blob.getMd5ToHexString()); System.out.println("MediaLink: " + blob.getMediaLink()); System.out.println("Metageneration: " + blob.getMetageneration()); System.out.println("Name: " + blob.getName()); diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITStorageSnippets.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITStorageSnippets.java index 2f02cd3896c7..183b68679775 100644 --- a/google-cloud-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITStorageSnippets.java +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITStorageSnippets.java @@ -458,13 +458,13 @@ public void testGetBlobMetadata() { assertTrue(snippetOutput.contains("ContentLanguage: " + remoteBlob.getContentLanguage())); assertTrue(snippetOutput.contains("ContentType: " + remoteBlob.getContentType())); assertTrue(snippetOutput.contains("Crc32c: " + remoteBlob.getCrc32c())); - assertTrue(snippetOutput.contains("Crc32cString: " + remoteBlob.getCrc32cToHexString())); + assertTrue(snippetOutput.contains("Crc32cHexString: " + remoteBlob.getCrc32cToHexString())); assertTrue(snippetOutput.contains("ETag: " + remoteBlob.getEtag())); assertTrue(snippetOutput.contains("Generation: " + remoteBlob.getGeneration())); assertTrue(snippetOutput.contains("Id: " + remoteBlob.getBlobId())); assertTrue(snippetOutput.contains("KmsKeyName: " + remoteBlob.getKmsKeyName())); assertTrue(snippetOutput.contains("Md5Hash: " + remoteBlob.getMd5())); - assertTrue(snippetOutput.contains("Md5HashString: " + remoteBlob.getMd5ToHexString())); + assertTrue(snippetOutput.contains("Md5HexString: " + remoteBlob.getMd5ToHexString())); assertTrue(snippetOutput.contains("MediaLink: " + remoteBlob.getMediaLink())); assertTrue(snippetOutput.contains("Metageneration: " + remoteBlob.getMetageneration())); assertTrue(snippetOutput.contains("Name: " + remoteBlob.getName())); From 645304864d6f3cb567a74d21c723f535fb3fbf85 Mon Sep 17 00:00:00 2001 From: andrey-qlogic Date: Mon, 4 Feb 2019 14:30:54 +0000 Subject: [PATCH 05/11] 4117: Renamed predefined HEX strings for Md5 and Crc32c. Changed value for BlobTest. --- .../java/com/google/cloud/storage/BlobInfoTest.java | 12 ++++++------ .../test/java/com/google/cloud/storage/BlobTest.java | 8 ++++---- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobInfoTest.java b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobInfoTest.java index 331b5b3d0ccb..6be2fa504a12 100644 --- a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobInfoTest.java +++ b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobInfoTest.java @@ -48,13 +48,13 @@ public class BlobInfoTest { private static final String CONTENT_ENCODING = "UTF-8"; private static final String CONTENT_LANGUAGE = "En"; private static final String CRC32 = "0xFF00"; - private static final String CRC32_STRING = "145d34"; + private static final String CRC32_HEX_STRING = "145d34"; private static final Long DELETE_TIME = System.currentTimeMillis(); private static final String ETAG = "0xFF00"; private static final Long GENERATION = 1L; private static final String GENERATED_ID = "B/N:1"; private static final String MD5 = "0xFF00"; - private static final String MD5_STRING = "145d34"; + private static final String MD5_HEX_STRING = "145d34"; private static final String MEDIA_LINK = "http://media/b/n"; private static final Map METADATA = ImmutableMap.of("n1", "v1", "n2", "v2"); private static final Long META_GENERATION = 10L; @@ -128,14 +128,14 @@ public void testToBuilder() { @Test public void testToBuilderSetMd5FromHexString() { BlobInfo blobInfo = - BlobInfo.newBuilder(BlobId.of("b2", "n2")).setMd5FromHexString(MD5_STRING).build(); + BlobInfo.newBuilder(BlobId.of("b2", "n2")).setMd5FromHexString(MD5_HEX_STRING).build(); assertEquals(MD5, blobInfo.getMd5()); } @Test public void testToBuilderSetCrc32cFromHexString() { BlobInfo blobInfo = - BlobInfo.newBuilder(BlobId.of("b2", "n2")).setCrc32cFromHexString(CRC32_STRING).build(); + BlobInfo.newBuilder(BlobId.of("b2", "n2")).setCrc32cFromHexString(CRC32_HEX_STRING).build(); assertEquals(CRC32, blobInfo.getCrc32c()); } @@ -158,13 +158,13 @@ public void testBuilder() { assertEquals(CONTENT_LANGUAGE, BLOB_INFO.getContentLanguage()); assertEquals(CUSTOMER_ENCRYPTION, BLOB_INFO.getCustomerEncryption()); assertEquals(CRC32, BLOB_INFO.getCrc32c()); - assertEquals(CRC32_STRING, BLOB_INFO.getCrc32cToHexString()); + assertEquals(CRC32_HEX_STRING, BLOB_INFO.getCrc32cToHexString()); assertEquals(DELETE_TIME, BLOB_INFO.getDeleteTime()); assertEquals(ETAG, BLOB_INFO.getEtag()); assertEquals(GENERATION, BLOB_INFO.getGeneration()); assertEquals(GENERATED_ID, BLOB_INFO.getGeneratedId()); assertEquals(MD5, BLOB_INFO.getMd5()); - assertEquals(MD5_STRING, BLOB_INFO.getMd5ToHexString()); + assertEquals(MD5_HEX_STRING, BLOB_INFO.getMd5ToHexString()); assertEquals(MEDIA_LINK, BLOB_INFO.getMediaLink()); assertEquals(METADATA, BLOB_INFO.getMetadata()); assertEquals(META_GENERATION, BLOB_INFO.getMetageneration()); diff --git a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobTest.java b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobTest.java index 5861f9d67c0b..a124eb580258 100644 --- a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobTest.java +++ b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobTest.java @@ -71,13 +71,13 @@ public class BlobTest { private static final String CONTENT_ENCODING = "UTF-8"; private static final String CONTENT_LANGUAGE = "En"; private static final String CRC32 = "0xFF00"; - private static final String CRC32_STRING = "d31145d3"; + private static final String CRC32_HEX_STRING = "145d34"; private static final Long DELETE_TIME = System.currentTimeMillis(); private static final String ETAG = "0xFF00"; private static final Long GENERATION = 1L; private static final String GENERATED_ID = "B/N:1"; private static final String MD5 = "0xFF00"; - private static final String MD5_STRING = "d31145d3"; + private static final String MD5_HEX_STRING = "145d34"; private static final String MEDIA_LINK = "http://media/b/n"; private static final Map METADATA = ImmutableMap.of("n1", "v1", "n2", "v2"); private static final Long META_GENERATION = 10L; @@ -508,7 +508,7 @@ public void testBuilder() { assertEquals(CONTENT_ENCODING, blob.getContentEncoding()); assertEquals(CONTENT_LANGUAGE, blob.getContentLanguage()); assertEquals(CRC32, blob.getCrc32c()); - assertEquals(CRC32_STRING, blob.getCrc32cToHexString()); + assertEquals(CRC32_HEX_STRING, blob.getCrc32cToHexString()); assertEquals(CREATE_TIME, blob.getCreateTime()); assertEquals(CUSTOMER_ENCRYPTION, blob.getCustomerEncryption()); assertEquals(KMS_KEY_NAME, blob.getKmsKeyName()); @@ -519,7 +519,7 @@ public void testBuilder() { assertEquals(ETAG, blob.getEtag()); assertEquals(GENERATED_ID, blob.getGeneratedId()); assertEquals(MD5, blob.getMd5()); - assertEquals(MD5_STRING, blob.getMd5ToHexString()); + assertEquals(MD5_HEX_STRING, blob.getMd5ToHexString()); assertEquals(MEDIA_LINK, blob.getMediaLink()); assertEquals(METADATA, blob.getMetadata()); assertEquals(META_GENERATION, blob.getMetageneration()); From 2d762563e795a2886a75cc3a1f45d38adff3d909 Mon Sep 17 00:00:00 2001 From: andrey-qlogic Date: Mon, 4 Feb 2019 16:34:09 +0000 Subject: [PATCH 06/11] 4117: Fixed code formatting error. --- .../automl/v1beta1/ImageClassificationDatasetMetadata.java | 6 ++++-- .../automl/v1beta1/TextClassificationDatasetMetadata.java | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/google-api-grpc/proto-google-cloud-automl-v1beta1/src/main/java/com/google/cloud/automl/v1beta1/ImageClassificationDatasetMetadata.java b/google-api-grpc/proto-google-cloud-automl-v1beta1/src/main/java/com/google/cloud/automl/v1beta1/ImageClassificationDatasetMetadata.java index 6ab33d07c60d..9933ff53fadc 100644 --- a/google-api-grpc/proto-google-cloud-automl-v1beta1/src/main/java/com/google/cloud/automl/v1beta1/ImageClassificationDatasetMetadata.java +++ b/google-api-grpc/proto-google-cloud-automl-v1beta1/src/main/java/com/google/cloud/automl/v1beta1/ImageClassificationDatasetMetadata.java @@ -144,7 +144,8 @@ public final boolean isInitialized() { public void writeTo(com.google.protobuf.CodedOutputStream output) throws java.io.IOException { if (classificationType_ != com.google.cloud.automl.v1beta1.ClassificationProto.ClassificationType - .CLASSIFICATION_TYPE_UNSPECIFIED.getNumber()) { + .CLASSIFICATION_TYPE_UNSPECIFIED + .getNumber()) { output.writeEnum(1, classificationType_); } unknownFields.writeTo(output); @@ -158,7 +159,8 @@ public int getSerializedSize() { size = 0; if (classificationType_ != com.google.cloud.automl.v1beta1.ClassificationProto.ClassificationType - .CLASSIFICATION_TYPE_UNSPECIFIED.getNumber()) { + .CLASSIFICATION_TYPE_UNSPECIFIED + .getNumber()) { size += com.google.protobuf.CodedOutputStream.computeEnumSize(1, classificationType_); } size += unknownFields.getSerializedSize(); diff --git a/google-api-grpc/proto-google-cloud-automl-v1beta1/src/main/java/com/google/cloud/automl/v1beta1/TextClassificationDatasetMetadata.java b/google-api-grpc/proto-google-cloud-automl-v1beta1/src/main/java/com/google/cloud/automl/v1beta1/TextClassificationDatasetMetadata.java index b6b7d5de8130..c93dd37ee2e0 100644 --- a/google-api-grpc/proto-google-cloud-automl-v1beta1/src/main/java/com/google/cloud/automl/v1beta1/TextClassificationDatasetMetadata.java +++ b/google-api-grpc/proto-google-cloud-automl-v1beta1/src/main/java/com/google/cloud/automl/v1beta1/TextClassificationDatasetMetadata.java @@ -144,7 +144,8 @@ public final boolean isInitialized() { public void writeTo(com.google.protobuf.CodedOutputStream output) throws java.io.IOException { if (classificationType_ != com.google.cloud.automl.v1beta1.ClassificationProto.ClassificationType - .CLASSIFICATION_TYPE_UNSPECIFIED.getNumber()) { + .CLASSIFICATION_TYPE_UNSPECIFIED + .getNumber()) { output.writeEnum(1, classificationType_); } unknownFields.writeTo(output); @@ -158,7 +159,8 @@ public int getSerializedSize() { size = 0; if (classificationType_ != com.google.cloud.automl.v1beta1.ClassificationProto.ClassificationType - .CLASSIFICATION_TYPE_UNSPECIFIED.getNumber()) { + .CLASSIFICATION_TYPE_UNSPECIFIED + .getNumber()) { size += com.google.protobuf.CodedOutputStream.computeEnumSize(1, classificationType_); } size += unknownFields.getSerializedSize(); From 7f19e33c3516c119f1a2775d0eb0fd07533a06c7 Mon Sep 17 00:00:00 2001 From: andrey-qlogic Date: Wed, 6 Feb 2019 11:39:42 +0000 Subject: [PATCH 07/11] 4117: Added method to remove 0x prefix from hex string. Added integration tests, --- .../com/google/cloud/storage/BlobInfo.java | 30 ++++++++-------- .../google/cloud/storage/BlobInfoTest.java | 8 ++--- .../com/google/cloud/storage/BlobTest.java | 4 +-- .../cloud/storage/it/ITStorageTest.java | 34 +++++++++++++++++++ 4 files changed, 54 insertions(+), 22 deletions(-) diff --git a/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobInfo.java b/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobInfo.java index b95129c7a40a..b9c419c0da91 100644 --- a/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobInfo.java +++ b/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobInfo.java @@ -88,6 +88,11 @@ public StorageObject apply(BlobInfo blobInfo) { private final Boolean temporaryHold; private final Long retentionExpirationTime; + /** Helper method to remove 0x prefix from the hex string */ + public static String remove0xPrefixFromHexString(String hex) { + return hex != null && hex.startsWith("0x") ? hex.replaceFirst("0x", "") : hex; + } + /** This class is meant for internal use only. Users are discouraged from using this class. */ public static final class ImmutableEmptyMap extends AbstractMap { @@ -437,7 +442,7 @@ Builder setSelfLink(String selfLink) { @Override public Builder setMd5(String md5) { - this.md5 = firstNonNull(md5, Data.nullOf(String.class)); + this.md5 = firstNonNull(remove0xPrefixFromHexString(md5), Data.nullOf(String.class)); return this; } @@ -445,17 +450,15 @@ public Builder setMd5FromHexString(String md5HexString) { if (md5HexString == null) { return this; } - if (md5HexString.startsWith("0x")) { - md5HexString = md5HexString.replaceFirst("0x", ""); - } - byte[] bytes = new BigInteger(md5HexString, 16).toByteArray(); - this.md5 = "0x" + BaseEncoding.base64().omitPadding().encode(bytes); + byte[] bytes = new BigInteger(remove0xPrefixFromHexString(md5HexString), 16).toByteArray(); + this.md5 = BaseEncoding.base64().encode(bytes); return this; } @Override public Builder setCrc32c(String crc32c) { - this.crc32c = firstNonNull(crc32c, Data.nullOf(String.class)); + this.crc32c = + firstNonNull(remove0xPrefixFromHexString(crc32c), Data.nullOf(String.class)); return this; } @@ -464,11 +467,8 @@ public Builder setCrc32cFromHexString(String crc32cHexString) { if (crc32cHexString == null) { return this; } - if (crc32cHexString.startsWith("0x")) { - crc32cHexString = crc32cHexString.replaceFirst("0x", ""); - } - byte[] bytes = new BigInteger(crc32cHexString, 16).toByteArray(); - this.crc32c = "0x" + BaseEncoding.base64().omitPadding().encode(bytes); + byte[] bytes = new BigInteger(remove0xPrefixFromHexString(crc32cHexString), 16).toByteArray(); + this.crc32c = BaseEncoding.base64().encode(bytes); return this; } @@ -727,8 +727,7 @@ public String getMd5ToHexString() { if (md5 == null) { return null; } - String md5withoutPrefix = md5.startsWith("0x") ? md5.replaceFirst("0x", "") : md5; - byte[] decodedMd5 = BaseEncoding.base64().decode(md5withoutPrefix); + byte[] decodedMd5 = BaseEncoding.base64().decode(remove0xPrefixFromHexString(md5)); StringBuilder stringBuilder = new StringBuilder(); for (byte b : decodedMd5) { stringBuilder.append(String.format("%02x", b & 0xff)); @@ -760,8 +759,7 @@ public String getCrc32cToHexString() { if (crc32c == null) { return null; } - String crc32cWithoutPrefix = crc32c.startsWith("0x") ? crc32c.replaceFirst("0x", "") : crc32c; - byte[] decodeCrc32c = BaseEncoding.base64().decode(crc32cWithoutPrefix); + byte[] decodeCrc32c = BaseEncoding.base64().decode(remove0xPrefixFromHexString(crc32c)); StringBuilder stringBuilder = new StringBuilder(); for (byte b : decodeCrc32c) { stringBuilder.append(String.format("%02x", b & 0xff)); diff --git a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobInfoTest.java b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobInfoTest.java index 6be2fa504a12..8b40efe2a1a4 100644 --- a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobInfoTest.java +++ b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobInfoTest.java @@ -129,14 +129,14 @@ public void testToBuilder() { public void testToBuilderSetMd5FromHexString() { BlobInfo blobInfo = BlobInfo.newBuilder(BlobId.of("b2", "n2")).setMd5FromHexString(MD5_HEX_STRING).build(); - assertEquals(MD5, blobInfo.getMd5()); + assertEquals(BlobInfo.remove0xPrefixFromHexString(MD5), blobInfo.getMd5()); } @Test public void testToBuilderSetCrc32cFromHexString() { BlobInfo blobInfo = BlobInfo.newBuilder(BlobId.of("b2", "n2")).setCrc32cFromHexString(CRC32_HEX_STRING).build(); - assertEquals(CRC32, blobInfo.getCrc32c()); + assertEquals(BlobInfo.remove0xPrefixFromHexString(CRC32), blobInfo.getCrc32c()); } @Test @@ -157,13 +157,13 @@ public void testBuilder() { assertEquals(CONTENT_ENCODING, BLOB_INFO.getContentEncoding()); assertEquals(CONTENT_LANGUAGE, BLOB_INFO.getContentLanguage()); assertEquals(CUSTOMER_ENCRYPTION, BLOB_INFO.getCustomerEncryption()); - assertEquals(CRC32, BLOB_INFO.getCrc32c()); + assertEquals(BlobInfo.remove0xPrefixFromHexString(CRC32), BLOB_INFO.getCrc32c()); assertEquals(CRC32_HEX_STRING, BLOB_INFO.getCrc32cToHexString()); assertEquals(DELETE_TIME, BLOB_INFO.getDeleteTime()); assertEquals(ETAG, BLOB_INFO.getEtag()); assertEquals(GENERATION, BLOB_INFO.getGeneration()); assertEquals(GENERATED_ID, BLOB_INFO.getGeneratedId()); - assertEquals(MD5, BLOB_INFO.getMd5()); + assertEquals(BlobInfo.remove0xPrefixFromHexString(MD5), BLOB_INFO.getMd5()); assertEquals(MD5_HEX_STRING, BLOB_INFO.getMd5ToHexString()); assertEquals(MEDIA_LINK, BLOB_INFO.getMediaLink()); assertEquals(METADATA, BLOB_INFO.getMetadata()); diff --git a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobTest.java b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobTest.java index a124eb580258..13d5c312d8d2 100644 --- a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobTest.java +++ b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobTest.java @@ -507,7 +507,7 @@ public void testBuilder() { assertEquals(CONTENT_DISPOSITION, blob.getContentDisposition()); assertEquals(CONTENT_ENCODING, blob.getContentEncoding()); assertEquals(CONTENT_LANGUAGE, blob.getContentLanguage()); - assertEquals(CRC32, blob.getCrc32c()); + assertEquals(BlobInfo.remove0xPrefixFromHexString(CRC32), blob.getCrc32c()); assertEquals(CRC32_HEX_STRING, blob.getCrc32cToHexString()); assertEquals(CREATE_TIME, blob.getCreateTime()); assertEquals(CUSTOMER_ENCRYPTION, blob.getCustomerEncryption()); @@ -518,7 +518,7 @@ public void testBuilder() { assertEquals(DELETE_TIME, blob.getDeleteTime()); assertEquals(ETAG, blob.getEtag()); assertEquals(GENERATED_ID, blob.getGeneratedId()); - assertEquals(MD5, blob.getMd5()); + assertEquals(BlobInfo.remove0xPrefixFromHexString(MD5), blob.getMd5()); assertEquals(MD5_HEX_STRING, blob.getMd5ToHexString()); assertEquals(MEDIA_LINK, blob.getMediaLink()); assertEquals(METADATA, blob.getMetadata()); diff --git a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java index 3a97dab2800d..94e31e1616f2 100644 --- a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java +++ b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java @@ -460,6 +460,40 @@ public void testCreateBlob() { assertTrue(remoteBlob.delete()); } + @Test + public void testCreateBlobMd5FromHexString() { + String blobName = "test-create-blob-md5-from-hex-string"; + BlobInfo blob = + BlobInfo.newBuilder(BUCKET, blobName) + .setContentType(CONTENT_TYPE) + .setMd5FromHexString("3b54781b51c94835084898e821899585") + .build(); + Blob remoteBlob = storage.create(blob, BLOB_BYTE_CONTENT); + assertNotNull(remoteBlob); + assertEquals(blob.getBucket(), remoteBlob.getBucket()); + assertEquals(blob.getName(), remoteBlob.getName()); + byte[] readBytes = storage.readAllBytes(BUCKET, blobName); + assertArrayEquals(BLOB_BYTE_CONTENT, readBytes); + assertTrue(remoteBlob.delete()); + } + + @Test + public void testCreateBlobCrc32cFromHexString() { + String blobName = "test-create-blob-crc32c-from-hex-string"; + BlobInfo blob = + BlobInfo.newBuilder(BUCKET, blobName) + .setContentType(CONTENT_TYPE) + .setCrc32cFromHexString("f4ddc43d") + .build(); + Blob remoteBlob = storage.create(blob, BLOB_BYTE_CONTENT); + assertNotNull(remoteBlob); + assertEquals(blob.getBucket(), remoteBlob.getBucket()); + assertEquals(blob.getName(), remoteBlob.getName()); + byte[] readBytes = storage.readAllBytes(BUCKET, blobName); + assertArrayEquals(BLOB_BYTE_CONTENT, readBytes); + assertTrue(remoteBlob.delete()); + } + @Test public void testCreateBlobWithEncryptionKey() { String blobName = "test-create-with-customer-key-blob"; From 646c7cce032145bc4df5c842e2e83c818de8cf30 Mon Sep 17 00:00:00 2001 From: andrey-qlogic Date: Thu, 7 Feb 2019 08:24:53 +0000 Subject: [PATCH 08/11] 4117: Reverted back previous commit except integration tests. --- .../com/google/cloud/storage/BlobInfo.java | 30 ++++++++++--------- .../google/cloud/storage/BlobInfoTest.java | 8 ++--- .../com/google/cloud/storage/BlobTest.java | 4 +-- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobInfo.java b/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobInfo.java index b9c419c0da91..b95129c7a40a 100644 --- a/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobInfo.java +++ b/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobInfo.java @@ -88,11 +88,6 @@ public StorageObject apply(BlobInfo blobInfo) { private final Boolean temporaryHold; private final Long retentionExpirationTime; - /** Helper method to remove 0x prefix from the hex string */ - public static String remove0xPrefixFromHexString(String hex) { - return hex != null && hex.startsWith("0x") ? hex.replaceFirst("0x", "") : hex; - } - /** This class is meant for internal use only. Users are discouraged from using this class. */ public static final class ImmutableEmptyMap extends AbstractMap { @@ -442,7 +437,7 @@ Builder setSelfLink(String selfLink) { @Override public Builder setMd5(String md5) { - this.md5 = firstNonNull(remove0xPrefixFromHexString(md5), Data.nullOf(String.class)); + this.md5 = firstNonNull(md5, Data.nullOf(String.class)); return this; } @@ -450,15 +445,17 @@ public Builder setMd5FromHexString(String md5HexString) { if (md5HexString == null) { return this; } - byte[] bytes = new BigInteger(remove0xPrefixFromHexString(md5HexString), 16).toByteArray(); - this.md5 = BaseEncoding.base64().encode(bytes); + if (md5HexString.startsWith("0x")) { + md5HexString = md5HexString.replaceFirst("0x", ""); + } + byte[] bytes = new BigInteger(md5HexString, 16).toByteArray(); + this.md5 = "0x" + BaseEncoding.base64().omitPadding().encode(bytes); return this; } @Override public Builder setCrc32c(String crc32c) { - this.crc32c = - firstNonNull(remove0xPrefixFromHexString(crc32c), Data.nullOf(String.class)); + this.crc32c = firstNonNull(crc32c, Data.nullOf(String.class)); return this; } @@ -467,8 +464,11 @@ public Builder setCrc32cFromHexString(String crc32cHexString) { if (crc32cHexString == null) { return this; } - byte[] bytes = new BigInteger(remove0xPrefixFromHexString(crc32cHexString), 16).toByteArray(); - this.crc32c = BaseEncoding.base64().encode(bytes); + if (crc32cHexString.startsWith("0x")) { + crc32cHexString = crc32cHexString.replaceFirst("0x", ""); + } + byte[] bytes = new BigInteger(crc32cHexString, 16).toByteArray(); + this.crc32c = "0x" + BaseEncoding.base64().omitPadding().encode(bytes); return this; } @@ -727,7 +727,8 @@ public String getMd5ToHexString() { if (md5 == null) { return null; } - byte[] decodedMd5 = BaseEncoding.base64().decode(remove0xPrefixFromHexString(md5)); + String md5withoutPrefix = md5.startsWith("0x") ? md5.replaceFirst("0x", "") : md5; + byte[] decodedMd5 = BaseEncoding.base64().decode(md5withoutPrefix); StringBuilder stringBuilder = new StringBuilder(); for (byte b : decodedMd5) { stringBuilder.append(String.format("%02x", b & 0xff)); @@ -759,7 +760,8 @@ public String getCrc32cToHexString() { if (crc32c == null) { return null; } - byte[] decodeCrc32c = BaseEncoding.base64().decode(remove0xPrefixFromHexString(crc32c)); + String crc32cWithoutPrefix = crc32c.startsWith("0x") ? crc32c.replaceFirst("0x", "") : crc32c; + byte[] decodeCrc32c = BaseEncoding.base64().decode(crc32cWithoutPrefix); StringBuilder stringBuilder = new StringBuilder(); for (byte b : decodeCrc32c) { stringBuilder.append(String.format("%02x", b & 0xff)); diff --git a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobInfoTest.java b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobInfoTest.java index 8b40efe2a1a4..6be2fa504a12 100644 --- a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobInfoTest.java +++ b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobInfoTest.java @@ -129,14 +129,14 @@ public void testToBuilder() { public void testToBuilderSetMd5FromHexString() { BlobInfo blobInfo = BlobInfo.newBuilder(BlobId.of("b2", "n2")).setMd5FromHexString(MD5_HEX_STRING).build(); - assertEquals(BlobInfo.remove0xPrefixFromHexString(MD5), blobInfo.getMd5()); + assertEquals(MD5, blobInfo.getMd5()); } @Test public void testToBuilderSetCrc32cFromHexString() { BlobInfo blobInfo = BlobInfo.newBuilder(BlobId.of("b2", "n2")).setCrc32cFromHexString(CRC32_HEX_STRING).build(); - assertEquals(BlobInfo.remove0xPrefixFromHexString(CRC32), blobInfo.getCrc32c()); + assertEquals(CRC32, blobInfo.getCrc32c()); } @Test @@ -157,13 +157,13 @@ public void testBuilder() { assertEquals(CONTENT_ENCODING, BLOB_INFO.getContentEncoding()); assertEquals(CONTENT_LANGUAGE, BLOB_INFO.getContentLanguage()); assertEquals(CUSTOMER_ENCRYPTION, BLOB_INFO.getCustomerEncryption()); - assertEquals(BlobInfo.remove0xPrefixFromHexString(CRC32), BLOB_INFO.getCrc32c()); + assertEquals(CRC32, BLOB_INFO.getCrc32c()); assertEquals(CRC32_HEX_STRING, BLOB_INFO.getCrc32cToHexString()); assertEquals(DELETE_TIME, BLOB_INFO.getDeleteTime()); assertEquals(ETAG, BLOB_INFO.getEtag()); assertEquals(GENERATION, BLOB_INFO.getGeneration()); assertEquals(GENERATED_ID, BLOB_INFO.getGeneratedId()); - assertEquals(BlobInfo.remove0xPrefixFromHexString(MD5), BLOB_INFO.getMd5()); + assertEquals(MD5, BLOB_INFO.getMd5()); assertEquals(MD5_HEX_STRING, BLOB_INFO.getMd5ToHexString()); assertEquals(MEDIA_LINK, BLOB_INFO.getMediaLink()); assertEquals(METADATA, BLOB_INFO.getMetadata()); diff --git a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobTest.java b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobTest.java index 13d5c312d8d2..a124eb580258 100644 --- a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobTest.java +++ b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobTest.java @@ -507,7 +507,7 @@ public void testBuilder() { assertEquals(CONTENT_DISPOSITION, blob.getContentDisposition()); assertEquals(CONTENT_ENCODING, blob.getContentEncoding()); assertEquals(CONTENT_LANGUAGE, blob.getContentLanguage()); - assertEquals(BlobInfo.remove0xPrefixFromHexString(CRC32), blob.getCrc32c()); + assertEquals(CRC32, blob.getCrc32c()); assertEquals(CRC32_HEX_STRING, blob.getCrc32cToHexString()); assertEquals(CREATE_TIME, blob.getCreateTime()); assertEquals(CUSTOMER_ENCRYPTION, blob.getCustomerEncryption()); @@ -518,7 +518,7 @@ public void testBuilder() { assertEquals(DELETE_TIME, blob.getDeleteTime()); assertEquals(ETAG, blob.getEtag()); assertEquals(GENERATED_ID, blob.getGeneratedId()); - assertEquals(BlobInfo.remove0xPrefixFromHexString(MD5), blob.getMd5()); + assertEquals(MD5, blob.getMd5()); assertEquals(MD5_HEX_STRING, blob.getMd5ToHexString()); assertEquals(MEDIA_LINK, blob.getMediaLink()); assertEquals(METADATA, blob.getMetadata()); From e21485369b0df44f5f397db7dbbfb118ac7ceb11 Mon Sep 17 00:00:00 2001 From: andrey-qlogic Date: Thu, 7 Feb 2019 17:56:04 +0000 Subject: [PATCH 09/11] 4117: Removed code that removes 0x from hex string and code that adds prepending 0x. OmitPadding removed --- .../main/java/com/google/cloud/storage/BlobInfo.java | 10 ++-------- .../java/com/google/cloud/storage/BlobInfoTest.java | 6 ++++-- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobInfo.java b/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobInfo.java index b95129c7a40a..b49fe270ad1f 100644 --- a/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobInfo.java +++ b/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobInfo.java @@ -445,11 +445,8 @@ public Builder setMd5FromHexString(String md5HexString) { if (md5HexString == null) { return this; } - if (md5HexString.startsWith("0x")) { - md5HexString = md5HexString.replaceFirst("0x", ""); - } byte[] bytes = new BigInteger(md5HexString, 16).toByteArray(); - this.md5 = "0x" + BaseEncoding.base64().omitPadding().encode(bytes); + this.md5 = BaseEncoding.base64().encode(bytes); return this; } @@ -464,11 +461,8 @@ public Builder setCrc32cFromHexString(String crc32cHexString) { if (crc32cHexString == null) { return this; } - if (crc32cHexString.startsWith("0x")) { - crc32cHexString = crc32cHexString.replaceFirst("0x", ""); - } byte[] bytes = new BigInteger(crc32cHexString, 16).toByteArray(); - this.crc32c = "0x" + BaseEncoding.base64().omitPadding().encode(bytes); + this.crc32c = BaseEncoding.base64().encode(bytes); return this; } diff --git a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobInfoTest.java b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobInfoTest.java index 6be2fa504a12..984a3541c3e0 100644 --- a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobInfoTest.java +++ b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobInfoTest.java @@ -48,12 +48,14 @@ public class BlobInfoTest { private static final String CONTENT_ENCODING = "UTF-8"; private static final String CONTENT_LANGUAGE = "En"; private static final String CRC32 = "0xFF00"; + private static final String CRC32_NO_PREFIX = "FF00"; private static final String CRC32_HEX_STRING = "145d34"; private static final Long DELETE_TIME = System.currentTimeMillis(); private static final String ETAG = "0xFF00"; private static final Long GENERATION = 1L; private static final String GENERATED_ID = "B/N:1"; private static final String MD5 = "0xFF00"; + private static final String MD5_NO_PREFIX = "FF00"; private static final String MD5_HEX_STRING = "145d34"; private static final String MEDIA_LINK = "http://media/b/n"; private static final Map METADATA = ImmutableMap.of("n1", "v1", "n2", "v2"); @@ -129,14 +131,14 @@ public void testToBuilder() { public void testToBuilderSetMd5FromHexString() { BlobInfo blobInfo = BlobInfo.newBuilder(BlobId.of("b2", "n2")).setMd5FromHexString(MD5_HEX_STRING).build(); - assertEquals(MD5, blobInfo.getMd5()); + assertEquals(MD5_NO_PREFIX, blobInfo.getMd5()); } @Test public void testToBuilderSetCrc32cFromHexString() { BlobInfo blobInfo = BlobInfo.newBuilder(BlobId.of("b2", "n2")).setCrc32cFromHexString(CRC32_HEX_STRING).build(); - assertEquals(CRC32, blobInfo.getCrc32c()); + assertEquals(CRC32_NO_PREFIX, blobInfo.getCrc32c()); } @Test From 3630c82abbac5c88193f465450b06803e8d8e93d Mon Sep 17 00:00:00 2001 From: andrey-qlogic Date: Fri, 8 Feb 2019 10:00:33 +0000 Subject: [PATCH 10/11] 4117: Changes after review. --- .../com/google/cloud/storage/BlobInfo.java | 12 +++++----- .../google/cloud/storage/BlobInfoTest.java | 10 ++++----- .../com/google/cloud/storage/BlobTest.java | 4 ++-- .../cloud/storage/it/ITStorageTest.java | 22 ++++--------------- 4 files changed, 16 insertions(+), 32 deletions(-) diff --git a/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobInfo.java b/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobInfo.java index b49fe270ad1f..6014948bfdc2 100644 --- a/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobInfo.java +++ b/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobInfo.java @@ -41,6 +41,7 @@ import java.util.Map; import java.util.Objects; import java.util.Set; +import javax.xml.bind.DatatypeConverter; /** * Google Storage object metadata. @@ -442,10 +443,11 @@ public Builder setMd5(String md5) { } public Builder setMd5FromHexString(String md5HexString) { + System.out.println("crc32cHexString: " + md5HexString); if (md5HexString == null) { return this; } - byte[] bytes = new BigInteger(md5HexString, 16).toByteArray(); + byte[] bytes = DatatypeConverter.parseHexBinary(md5HexString); this.md5 = BaseEncoding.base64().encode(bytes); return this; } @@ -461,7 +463,7 @@ public Builder setCrc32cFromHexString(String crc32cHexString) { if (crc32cHexString == null) { return this; } - byte[] bytes = new BigInteger(crc32cHexString, 16).toByteArray(); + byte[] bytes = DatatypeConverter.parseHexBinary(crc32cHexString); this.crc32c = BaseEncoding.base64().encode(bytes); return this; } @@ -721,8 +723,7 @@ public String getMd5ToHexString() { if (md5 == null) { return null; } - String md5withoutPrefix = md5.startsWith("0x") ? md5.replaceFirst("0x", "") : md5; - byte[] decodedMd5 = BaseEncoding.base64().decode(md5withoutPrefix); + byte[] decodedMd5 = BaseEncoding.base64().decode(md5); StringBuilder stringBuilder = new StringBuilder(); for (byte b : decodedMd5) { stringBuilder.append(String.format("%02x", b & 0xff)); @@ -754,8 +755,7 @@ public String getCrc32cToHexString() { if (crc32c == null) { return null; } - String crc32cWithoutPrefix = crc32c.startsWith("0x") ? crc32c.replaceFirst("0x", "") : crc32c; - byte[] decodeCrc32c = BaseEncoding.base64().decode(crc32cWithoutPrefix); + byte[] decodeCrc32c = BaseEncoding.base64().decode(crc32c); StringBuilder stringBuilder = new StringBuilder(); for (byte b : decodeCrc32c) { stringBuilder.append(String.format("%02x", b & 0xff)); diff --git a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobInfoTest.java b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobInfoTest.java index 984a3541c3e0..a57be6df6358 100644 --- a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobInfoTest.java +++ b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobInfoTest.java @@ -47,15 +47,13 @@ public class BlobInfoTest { private static final String CONTENT_DISPOSITION = "content-disposition"; private static final String CONTENT_ENCODING = "UTF-8"; private static final String CONTENT_LANGUAGE = "En"; - private static final String CRC32 = "0xFF00"; - private static final String CRC32_NO_PREFIX = "FF00"; + private static final String CRC32 = "FF00"; private static final String CRC32_HEX_STRING = "145d34"; private static final Long DELETE_TIME = System.currentTimeMillis(); private static final String ETAG = "0xFF00"; private static final Long GENERATION = 1L; private static final String GENERATED_ID = "B/N:1"; - private static final String MD5 = "0xFF00"; - private static final String MD5_NO_PREFIX = "FF00"; + private static final String MD5 = "FF00"; private static final String MD5_HEX_STRING = "145d34"; private static final String MEDIA_LINK = "http://media/b/n"; private static final Map METADATA = ImmutableMap.of("n1", "v1", "n2", "v2"); @@ -131,14 +129,14 @@ public void testToBuilder() { public void testToBuilderSetMd5FromHexString() { BlobInfo blobInfo = BlobInfo.newBuilder(BlobId.of("b2", "n2")).setMd5FromHexString(MD5_HEX_STRING).build(); - assertEquals(MD5_NO_PREFIX, blobInfo.getMd5()); + assertEquals(MD5, blobInfo.getMd5()); } @Test public void testToBuilderSetCrc32cFromHexString() { BlobInfo blobInfo = BlobInfo.newBuilder(BlobId.of("b2", "n2")).setCrc32cFromHexString(CRC32_HEX_STRING).build(); - assertEquals(CRC32_NO_PREFIX, blobInfo.getCrc32c()); + assertEquals(CRC32, blobInfo.getCrc32c()); } @Test diff --git a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobTest.java b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobTest.java index a124eb580258..d68bf965f1f4 100644 --- a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobTest.java +++ b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobTest.java @@ -70,13 +70,13 @@ public class BlobTest { private static final String CONTENT_DISPOSITION = "content-disposition"; private static final String CONTENT_ENCODING = "UTF-8"; private static final String CONTENT_LANGUAGE = "En"; - private static final String CRC32 = "0xFF00"; + private static final String CRC32 = "FF00"; private static final String CRC32_HEX_STRING = "145d34"; private static final Long DELETE_TIME = System.currentTimeMillis(); private static final String ETAG = "0xFF00"; private static final Long GENERATION = 1L; private static final String GENERATED_ID = "B/N:1"; - private static final String MD5 = "0xFF00"; + private static final String MD5 = "FF00"; private static final String MD5_HEX_STRING = "145d34"; private static final String MEDIA_LINK = "http://media/b/n"; private static final Map METADATA = ImmutableMap.of("n1", "v1", "n2", "v2"); diff --git a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java index 94e31e1616f2..900ba462f705 100644 --- a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java +++ b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java @@ -461,34 +461,20 @@ public void testCreateBlob() { } @Test - public void testCreateBlobMd5FromHexString() { - String blobName = "test-create-blob-md5-from-hex-string"; + public void testCreateBlobMd5Crc32cFromHexString() { + String blobName = "test-create-blob-md5-crc32c-from-hex-string"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName) .setContentType(CONTENT_TYPE) .setMd5FromHexString("3b54781b51c94835084898e821899585") - .build(); - Blob remoteBlob = storage.create(blob, BLOB_BYTE_CONTENT); - assertNotNull(remoteBlob); - assertEquals(blob.getBucket(), remoteBlob.getBucket()); - assertEquals(blob.getName(), remoteBlob.getName()); - byte[] readBytes = storage.readAllBytes(BUCKET, blobName); - assertArrayEquals(BLOB_BYTE_CONTENT, readBytes); - assertTrue(remoteBlob.delete()); - } - - @Test - public void testCreateBlobCrc32cFromHexString() { - String blobName = "test-create-blob-crc32c-from-hex-string"; - BlobInfo blob = - BlobInfo.newBuilder(BUCKET, blobName) - .setContentType(CONTENT_TYPE) .setCrc32cFromHexString("f4ddc43d") .build(); Blob remoteBlob = storage.create(blob, BLOB_BYTE_CONTENT); assertNotNull(remoteBlob); assertEquals(blob.getBucket(), remoteBlob.getBucket()); assertEquals(blob.getName(), remoteBlob.getName()); + assertEquals(blob.getMd5ToHexString(), remoteBlob.getMd5ToHexString()); + assertEquals(blob.getCrc32cToHexString(), remoteBlob.getCrc32cToHexString()); byte[] readBytes = storage.readAllBytes(BUCKET, blobName); assertArrayEquals(BLOB_BYTE_CONTENT, readBytes); assertTrue(remoteBlob.delete()); From 400b62478d894932fcbc467f2194ea21351c41de Mon Sep 17 00:00:00 2001 From: andrey-qlogic Date: Fri, 8 Feb 2019 11:33:05 +0000 Subject: [PATCH 11/11] 4117: Removed javax.xml.bind.DatatypeConverter as not supported by Java 11. --- .../java/com/google/cloud/storage/BlobInfo.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobInfo.java b/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobInfo.java index 6014948bfdc2..d1bea35a9487 100644 --- a/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobInfo.java +++ b/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobInfo.java @@ -35,13 +35,13 @@ import java.io.Serializable; import java.math.BigInteger; import java.util.AbstractMap; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Set; -import javax.xml.bind.DatatypeConverter; /** * Google Storage object metadata. @@ -443,11 +443,14 @@ public Builder setMd5(String md5) { } public Builder setMd5FromHexString(String md5HexString) { - System.out.println("crc32cHexString: " + md5HexString); if (md5HexString == null) { return this; } - byte[] bytes = DatatypeConverter.parseHexBinary(md5HexString); + byte[] bytes = new BigInteger(md5HexString, 16).toByteArray(); + int leadingEmptyBytes = bytes.length - md5HexString.length() / 2; + if (leadingEmptyBytes > 0) { + bytes = Arrays.copyOfRange(bytes, leadingEmptyBytes, bytes.length); + } this.md5 = BaseEncoding.base64().encode(bytes); return this; } @@ -463,7 +466,11 @@ public Builder setCrc32cFromHexString(String crc32cHexString) { if (crc32cHexString == null) { return this; } - byte[] bytes = DatatypeConverter.parseHexBinary(crc32cHexString); + byte[] bytes = new BigInteger(crc32cHexString, 16).toByteArray(); + int leadingEmptyBytes = bytes.length - crc32cHexString.length() / 2; + if (leadingEmptyBytes > 0) { + bytes = Arrays.copyOfRange(bytes, leadingEmptyBytes, bytes.length); + } this.crc32c = BaseEncoding.base64().encode(bytes); return this; }