ByteStringUtil.java

  1. /*
  2.  * Copyright 2020 Providence Authors
  3.  *
  4.  * Licensed to the Apache Software Foundation (ASF) under one
  5.  * or more contributor license agreements. See the NOTICE file
  6.  * distributed with this work for additional information
  7.  * regarding copyright ownership. The ASF licenses this file
  8.  * to you under the Apache License, Version 2.0 (the
  9.  * "License"); you may not use this file except in compliance
  10.  * with the License. You may obtain a copy of the License at
  11.  *
  12.  *   http://www.apache.org/licenses/LICENSE-2.0
  13.  *
  14.  * Unless required by applicable law or agreed to in writing,
  15.  * software distributed under the License is distributed on an
  16.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17.  * KIND, either express or implied. See the License for the
  18.  * specific language governing permissions and limitations
  19.  * under the License.
  20.  */
  21. package net.morimekta.proto.utils;

  22. import com.google.protobuf.ByteString;

  23. import java.util.Base64;

  24. /**
  25.  * Most of these are pretty trivial methods, but here to vastly simplify generated code when making builder, mutable
  26.  * values or build a message.
  27.  */
  28. public final class ByteStringUtil {
  29.     /**
  30.      * @param binary Byte string data.
  31.      * @return Base64 encoded string of the binary data.
  32.      */
  33.     public static String toBase64(ByteString binary) {
  34.         if (binary == null) {
  35.             return null;
  36.         }
  37.         if (binary.isEmpty()) {
  38.             return "";
  39.         }
  40.         return Base64.getEncoder()
  41.                      .withoutPadding()
  42.                      .encodeToString(binary.toByteArray());
  43.     }

  44.     /**
  45.      * @param binary Byte string data.
  46.      * @return Hex encoded string of the binary data.
  47.      */
  48.     public static String toHexString(ByteString binary) {
  49.         if (binary == null) {
  50.             return null;
  51.         }
  52.         if (binary.isEmpty()) {
  53.             return "";
  54.         }
  55.         StringBuilder builder = new StringBuilder();
  56.         for (byte b : binary.toByteArray()) {
  57.             builder.append(String.format("%02x", ((int) (b)) % 0x100));
  58.         }
  59.         return builder.toString();
  60.     }

  61.     /**
  62.      * @param content Base64 encoded string.
  63.      * @return Byte string of the encoded data.
  64.      */
  65.     public static ByteString fromBase64(String content) {
  66.         if (content == null) {
  67.             return null;
  68.         }
  69.         if (content.isEmpty()) {
  70.             return ByteString.EMPTY;
  71.         }
  72.         return ByteString.copyFrom(Base64.getDecoder().decode(content));
  73.     }

  74.     /**
  75.      * @param content Hex encoded content.
  76.      * @return Byte string of the encoded data.
  77.      */
  78.     public static ByteString fromHexString(CharSequence content) {
  79.         if (content == null) {
  80.             return null;
  81.         }
  82.         if (content.length() == 0) {
  83.             return ByteString.EMPTY;
  84.         }
  85.         byte[] data = new byte[content.length() / 2];
  86.         for (int i = 0; i < data.length; ++i) {
  87.             int pos = i * 2;
  88.             data[i] = Byte.parseByte(content.subSequence(pos, pos + 2).toString(), 16);
  89.         }
  90.         return ByteString.copyFrom(data);
  91.     }

  92.     // --- Private ---

  93.     private ByteStringUtil() {}
  94. }