Class BinaryHelper


  • public final class BinaryHelper
    extends Object
    A helper class of methods to convert between Binary instances and proto-buffer byte values.
    Since:
    20.06
    Author:
    Mahesh Kannan 2019.11.01, Jonathan Knight 2019.11.07
    • Field Detail

      • EMPTY

        public static final com.google.protobuf.Empty EMPTY
        Singleton Empty.
      • EMPTY_BYTE_STRING

        public static final com.google.protobuf.ByteString EMPTY_BYTE_STRING
        Singleton empty ByteString.
    • Method Detail

      • toBinary

        public static Binary toBinary​(com.google.protobuf.BytesValue bytes)
        Convert a BytesValue to a Binary.

        The BytesValue is converted to a list of ByteBuffers which are then converted to ByteBufferReadBuffers and finally to a Binary to avoid copying byte arrays.

        Parameters:
        bytes - the BytesValue to convert
        Returns:
        a Binary containing the bytes of the BytesValue
        Throws:
        NullPointerException - if the BytesValue is null
      • toBinary

        public static Binary toBinary​(com.google.protobuf.ByteString bytes)
        Convert a ByteString to a Binary.

        The ByteString is converted to a list of ByteBuffers which are then converted to ByteBufferReadBuffers and finally to a Binary to avoid copying byte arrays.

        Parameters:
        bytes - the ByteString to convert
        Returns:
        a Binary containing the bytes of the ByteString
        Throws:
        NullPointerException - if the ByteString is null
      • toReadBuffer

        public static ReadBuffer toReadBuffer​(com.google.protobuf.ByteString bytes)
        Convert a ByteString to a ReadBuffer.

        The ByteString is converted to a list of ByteBuffers which are then converted to ByteBufferReadBuffers and finally to a Binary to avoid copying byte arrays.

        Parameters:
        bytes - the ByteString to convert
        Returns:
        a ReadBuffer containing the bytes of the ByteString
        Throws:
        NullPointerException - if the ByteString is null
      • fromBinary

        public static <T> T fromBinary​(Binary binary,
                                       Serializer serializer)
        Deserialize a serialized Binary.
        Type Parameters:
        T - the type of the original object the Binary represents
        Parameters:
        binary - the Binary to deserialize
        serializer - the Serializer to use
        Returns:
        the deserialized Binary value or null if the Binary is null
      • toBytesValue

        public static com.google.protobuf.BytesValue toBytesValue​(Binary binary)
        Convert a Binary to a BytesValue.

        We need to create a ByteString as efficiently as possible from the undecorated Binary so we use the UnsafeByteOperations to avoid copying byte arrays.

        Parameters:
        binary - the Binary to convert
        Returns:
        a BytesValue containing the bytes from the Binary
      • toByteString

        public static com.google.protobuf.ByteString toByteString​(Binary binary)
        Convert a Binary to a ByteString.

        We need to create a ByteString as efficiently as possible from the undecorated Binary so we use the UnsafeByteOperations to avoid copying byte arrays.

        Parameters:
        binary - the Binary to convert
        Returns:
        a BytesValue containing the bytes from the Binary or an empty ByteString if the Binary is null
      • fromBytesValue

        public static <T> T fromBytesValue​(com.google.protobuf.BytesValue bytes,
                                           Serializer serializer)
        A utility method to deserialize a BytesValue to an Object.
        Type Parameters:
        T - the expected value type
        Parameters:
        bytes - the BytesValue to deserialize an Object.
        serializer - the Serializer to use to convert the binary stream to an object
        Returns:
        an object from the specified BytesValue
      • fromByteString

        public static <T> T fromByteString​(com.google.protobuf.ByteString bytes,
                                           Serializer serializer)
        A utility method to deserialize a ByteString to an Object.
        Type Parameters:
        T - the expected value type
        Parameters:
        bytes - the ByteString to deserialize an Object.
        serializer - the Serializer to use to convert the binary stream to an object
        Returns:
        an object from the specified ByteString
      • toBytesValue

        public static com.google.protobuf.BytesValue toBytesValue​(Object value,
                                                                  Serializer serializer)
        Serialize the specified value to a BytesValue.
        Parameters:
        value - the value to serialize
        serializer - the Serializer to use
        Returns:
        the serialized value as a BytesValue
      • toByteString

        public static com.google.protobuf.ByteString toByteString​(Object value,
                                                                  Serializer serializer)
        Serialize the specified value to a ByteString.
        Parameters:
        value - the value to serialize
        serializer - the Serializer to use
        Returns:
        the serialized value as a ByteString
      • toEntryList

        public static List<com.oracle.coherence.grpc.Entry> toEntryList​(Map<?,​?> map,
                                                                        Serializer serializer)
        Create a Map to a list of Entry instances.
        Parameters:
        map - the Map to convert to a list of Entry instances
        serializer - the Serializer to use
        Returns:
        a list of Entry instances
        Throws:
        NullPointerException - if the map is null
      • toEntry

        public static com.oracle.coherence.grpc.Entry toEntry​(Map.Entry<?,​?> entry,
                                                              Serializer serializer)
        Create an Entry from a Map.Entry.
        Parameters:
        entry - the Map.Entry to convert to an Entry
        serializer - the Serializer to use
        Returns:
        an Entry containing the specified key and value
        Throws:
        NullPointerException - if the entry is null
      • toEntry

        public static com.oracle.coherence.grpc.Entry toEntry​(com.google.protobuf.ByteString key,
                                                              com.google.protobuf.ByteString value)
        Create an Entry containing the specified key and value.
        Parameters:
        key - the entry's key
        value - the entry's value
        Returns:
        an Entry containing the specified key and value
      • toStringValueStream

        public static Stream<com.google.protobuf.StringValue> toStringValueStream​(Iterable<?> iterable)
        Return the specified Iterable as a Stream of StringValue instances.
        Parameters:
        iterable - the Iterable to convert
        Returns:
        the contents of the Iterable as a Stream of StringValue instances
      • toKeyByteString

        public static com.google.protobuf.ByteString toKeyByteString​(Binary bin,
                                                                     Object obj,
                                                                     Serializer serializer)