Package io.protostuff

Class ProtobufIOUtil

java.lang.Object
io.protostuff.ProtobufIOUtil

public final class ProtobufIOUtil extends Object
Protobuf ser/deser util for messages/objects.
  • Constructor Details

    • ProtobufIOUtil

      private ProtobufIOUtil()
  • Method Details

    • newPipe

      public static Pipe newPipe(byte[] data)
      Creates a protobuf pipe from a byte array.
    • newPipe

      public static Pipe newPipe(byte[] data, int offset, int len)
      Creates a protobuf pipe from a byte array.
    • newPipe

      public static Pipe newPipe(InputStream in)
      Creates a protobuf pipe from an InputStream.
    • mergeFrom

      public static <T> void mergeFrom(byte[] data, T message, Schema<T> schema)
      Merges the message with the byte array using the given schema.
    • mergeFrom

      public static <T> void mergeFrom(byte[] data, int offset, int length, T message, Schema<T> schema)
      Merges the message with the byte array using the given schema.
    • mergeFrom

      public static <T> void mergeFrom(InputStream in, T message, Schema<T> schema) throws IOException
      Merges the message from the InputStream using the given schema.
      Throws:
      IOException
    • mergeFrom

      public static <T> void mergeFrom(InputStream in, T message, Schema<T> schema, LinkedBuffer buffer) throws IOException
      Merges the message from the InputStream using the given schema.

      The buffer's internal byte array will be used for reading the message.

      Throws:
      IOException
    • mergeDelimitedFrom

      public static <T> int mergeDelimitedFrom(InputStream in, T message, Schema<T> schema) throws IOException
      Merges the message (delimited) from the InputStream using the given schema.
      Returns:
      the size of the message
      Throws:
      IOException
    • mergeDelimitedFrom

      public static <T> int mergeDelimitedFrom(InputStream in, T message, Schema<T> schema, LinkedBuffer buffer) throws IOException
      Merges the message (delimited) from the InputStream using the given schema.

      The delimited message size must not be larger than the buffer's size/capacity. ProtobufException "size limit exceeded" is thrown otherwise.

      Returns:
      the size of the message
      Throws:
      IOException
    • mergeDelimitedFrom

      public static <T> int mergeDelimitedFrom(DataInput in, T message, Schema<T> schema) throws IOException
      Used by the code generated messages that implement Externalizable. Merges from the DataInput.
      Returns:
      the size of the message
      Throws:
      IOException
    • toByteArray

      public static <T> byte[] toByteArray(T message, Schema<T> schema, LinkedBuffer buffer)
      Serializes the message into a byte array using the given schema.
      Returns:
      the byte array containing the data.
    • writeTo

      public static <T> int writeTo(LinkedBuffer buffer, T message, Schema<T> schema)
      Writes the message into the LinkedBuffer using the given schema.
      Returns:
      the size of the message
    • writeTo

      public static <T> int writeTo(OutputStream out, T message, Schema<T> schema, LinkedBuffer buffer) throws IOException
      Serializes the message into an OutputStream using the given schema.
      Returns:
      the size of the message
      Throws:
      IOException
    • writeDelimitedTo

      public static <T> int writeDelimitedTo(OutputStream out, T message, Schema<T> schema, LinkedBuffer buffer) throws IOException
      Serializes the message, prefixed with its length, into an OutputStream.
      Returns:
      the size of the message
      Throws:
      IOException
    • writeDelimitedTo

      public static <T> int writeDelimitedTo(DataOutput out, T message, Schema<T> schema) throws IOException
      Used by the code generated messages that implement Externalizable. Writes to the DataOutput .
      Returns:
      the size of the message.
      Throws:
      IOException
    • writeListTo

      public static <T> int writeListTo(OutputStream out, List<T> messages, Schema<T> schema, LinkedBuffer buffer) throws IOException
      Serializes the messages (delimited) into an OutputStream using the given schema.
      Returns:
      the total size of the messages (excluding the length prefix varint)
      Throws:
      IOException
    • parseListFrom

      public static <T> List<T> parseListFrom(InputStream in, Schema<T> schema) throws IOException
      Parses the messages (delimited) from the InputStream using the given schema.
      Returns:
      the list containing the messages.
      Throws:
      IOException
    • optMergeDelimitedFrom

      public static <T> boolean optMergeDelimitedFrom(InputStream in, T message, Schema<T> schema, LinkedBuffer buffer) throws IOException
      Optimal/Optional mergeDelimitedFrom - If the message does not fit the buffer, no merge is done and this method will return false.

      This is strictly for reading a single message from the stream because the buffer is aggressively filled when reading the delimited size (which could result into reading more bytes than it has to).

      The remaining bytes will be drained (consumed and discared) when the message is too large.

      Throws:
      IOException
    • optMergeDelimitedFrom

      public static <T> boolean optMergeDelimitedFrom(InputStream in, T message, Schema<T> schema, boolean drainRemainingBytesIfTooLarge, LinkedBuffer buffer) throws IOException
      Optimal/Optional mergeDelimitedFrom - If the message does not fit the buffer, no merge is done and this method will return false.

      This is strictly for reading a single message from the stream because the buffer is aggressively filled when reading the delimited size (which could result into reading more bytes than it has to).

      Throws:
      IOException
    • optWriteDelimitedTo

      public static <T> int optWriteDelimitedTo(OutputStream out, T message, Schema<T> schema, LinkedBuffer buffer) throws IOException
      Optimal writeDelimitedTo - The varint32 prefix is written to the buffer instead of directly writing to outputstream.
      Returns:
      the size of the message
      Throws:
      IOException