You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@accumulo.apache.org by "cshannon (via GitHub)" <gi...@apache.org> on 2023/04/07 16:52:11 UTC

[GitHub] [accumulo] cshannon opened a new pull request, #3285: Move Gson ByteArrayToBase64TypeAdapter to its own utility class

cshannon opened a new pull request, #3285:
URL: https://github.com/apache/accumulo/pull/3285

   BulkSerialize had a Gson adapter to serialize byte arrays to Base64 encoding that can be re-used by other areas in the code. Also update the uses of the base 64 Gson object to be static and re-used as the objects are thread-safe so can be created just one time.
   
   I noticed this when working on serializing ranges as part of the updates for #1327 and this PR will be the first part of my PR for that task but this was something that could be done separately and pulled out into its own PR.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@accumulo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [accumulo] cshannon commented on a diff in pull request #3285: Move Gson ByteArrayToBase64TypeAdapter to its own utility class

Posted by "cshannon (via GitHub)" <gi...@apache.org>.
cshannon commented on code in PR #3285:
URL: https://github.com/apache/accumulo/pull/3285#discussion_r1160854223


##########
core/src/main/java/org/apache/accumulo/core/util/ByteArrayToBase64TypeAdapter.java:
##########
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.accumulo.core.util;
+
+import java.lang.reflect.Type;
+import java.util.Base64;
+import java.util.Base64.Decoder;
+import java.util.Base64.Encoder;
+import java.util.Objects;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.JsonDeserializationContext;
+import com.google.gson.JsonDeserializer;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonParseException;
+import com.google.gson.JsonPrimitive;
+import com.google.gson.JsonSerializationContext;
+import com.google.gson.JsonSerializer;
+
+/**
+ * Gson adapter to handle serializing and deserializing byte arrays using Base64 encoding.
+ */
+public class ByteArrayToBase64TypeAdapter
+    implements JsonSerializer<byte[]>, JsonDeserializer<byte[]> {
+
+  private static final Decoder decoder = Base64.getUrlDecoder();
+  private static final Encoder encoder = Base64.getUrlEncoder();

Review Comment:
   They are indeed threadsafe, from the docs: https://docs.oracle.com/javase/8/docs/api/java/util/Base64.Encoder.html
   
   > Instances of [Base64.Encoder](https://docs.oracle.com/javase/8/docs/api/java/util/Base64.Encoder.html) class are safe for use by multiple concurrent threads.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@accumulo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [accumulo] DomGarguilo commented on a diff in pull request #3285: Move Gson ByteArrayToBase64TypeAdapter to its own utility class

Posted by "DomGarguilo (via GitHub)" <gi...@apache.org>.
DomGarguilo commented on code in PR #3285:
URL: https://github.com/apache/accumulo/pull/3285#discussion_r1160846550


##########
core/src/test/java/org/apache/accumulo/core/util/ByteArrayToBase64TypeAdapterTest.java:
##########
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.accumulo.core.util;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+
+import org.apache.hadoop.io.Text;
+import org.junit.jupiter.api.Test;
+
+import com.google.gson.Gson;
+
+public class ByteArrayToBase64TypeAdapterTest {
+
+  private static final Gson gson = ByteArrayToBase64TypeAdapter.createBase64Gson();
+
+  @Test
+  public void testSerializeText() throws IOException {
+    final Text original = new Text("This is a test");
+    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+    original.write(new DataOutputStream(baos));
+
+    final String encoded = gson.toJson(baos.toByteArray());
+    final Text decoded = new Text();
+    decoded.readFields(
+        new DataInputStream(new ByteArrayInputStream(gson.fromJson(encoded, byte[].class))));

Review Comment:
   ```suggestion
       String encoded;
   
       try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
           DataOutputStream dos = new DataOutputStream(baos)) {
         original.write(dos);
         encoded = gson.toJson(baos.toByteArray());
       }
       final Text decoded = new Text();
       try (ByteArrayInputStream bais = new ByteArrayInputStream(gson.fromJson(encoded, byte[].class));
           DataInputStream dis = new DataInputStream(bais)) {
         decoded.readFields(dis);
       }
   ```
   Minor suggestion to add resources to try-with-resources blocks.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@accumulo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [accumulo] keith-turner commented on a diff in pull request #3285: Move Gson ByteArrayToBase64TypeAdapter to its own utility class

Posted by "keith-turner (via GitHub)" <gi...@apache.org>.
keith-turner commented on code in PR #3285:
URL: https://github.com/apache/accumulo/pull/3285#discussion_r1160849628


##########
core/src/main/java/org/apache/accumulo/core/util/ByteArrayToBase64TypeAdapter.java:
##########
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.accumulo.core.util;
+
+import java.lang.reflect.Type;
+import java.util.Base64;
+import java.util.Base64.Decoder;
+import java.util.Base64.Encoder;
+import java.util.Objects;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.JsonDeserializationContext;
+import com.google.gson.JsonDeserializer;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonParseException;
+import com.google.gson.JsonPrimitive;
+import com.google.gson.JsonSerializationContext;
+import com.google.gson.JsonSerializer;
+
+/**
+ * Gson adapter to handle serializing and deserializing byte arrays using Base64 encoding.
+ */
+public class ByteArrayToBase64TypeAdapter
+    implements JsonSerializer<byte[]>, JsonDeserializer<byte[]> {
+
+  private static final Decoder decoder = Base64.getUrlDecoder();
+  private static final Encoder encoder = Base64.getUrlEncoder();

Review Comment:
   I was curious if these were thread safe.  I looked at the impl and both return a statically referenced singleton, so would assume that is thread safe.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@accumulo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [accumulo] cshannon merged pull request #3285: Move Gson ByteArrayToBase64TypeAdapter to its own utility class

Posted by "cshannon (via GitHub)" <gi...@apache.org>.
cshannon merged PR #3285:
URL: https://github.com/apache/accumulo/pull/3285


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@accumulo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org