The serialization utilities provide high-level interfaces for converting Java objects to/from byte arrays and streams. These are the primary entry points for most serialization tasks.
Main utility class for serializing objects using the Protostuff format, which is optimized for performance and provides some enhancements over standard Protocol Buffer encoding.
public static <T> byte[] toByteArray(T message, Schema<T> schema, LinkedBuffer buffer)Serializes a message to a byte array using the Protostuff format.
Parameters:
message - The object to serializeschema - The schema defining the message structurebuffer - LinkedBuffer for memory managementReturns: Byte array containing the serialized data
Usage Example:
LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);
byte[] data = ProtostuffIOUtil.toByteArray(person, personSchema, buffer);public static <T> void writeTo(OutputStream out, T message, Schema<T> schema, LinkedBuffer buffer) throws IOExceptionWrites a serialized message directly to an OutputStream.
public static <T> void writeTo(DataOutput out, T message, Schema<T> schema, LinkedBuffer buffer) throws IOExceptionWrites a serialized message to a DataOutput.
public static <T> void writeDelimitedTo(OutputStream out, T message, Schema<T> schema, LinkedBuffer buffer) throws IOExceptionWrites a length-delimited message to an OutputStream, prefixing the data with its length.
public static <T> void mergeFrom(byte[] data, T message, Schema<T> schema) throws IOExceptionDeserializes data from a byte array into an existing message object.
Parameters:
data - Byte array containing serialized datamessage - Target object to populate (must be pre-instantiated)schema - Schema defining the message structurepublic static <T> void mergeFrom(byte[] data, int offset, int length, T message, Schema<T> schema) throws IOExceptionDeserializes data from a specific portion of a byte array.
public static <T> void mergeFrom(InputStream in, T message, Schema<T> schema) throws IOExceptionDeserializes data from an InputStream.
public static <T> void mergeFrom(DataInput in, T message, Schema<T> schema) throws IOExceptionDeserializes data from a DataInput.
public static <T> List<T> parseListFrom(InputStream in, Schema<T> schema) throws IOExceptionParses a list of messages from an InputStream, where each message is length-delimited.
public static Pipe newPipe(byte[] data)Creates a protostuff pipe from a byte array for efficient data transfer operations.
public static Pipe newPipe(byte[] data, int offset, int len)Creates a protostuff pipe from a specific portion of a byte array.
public static Pipe newPipe(InputStream in)Creates a protostuff pipe from an InputStream.
Utility class for serializing objects using the standard Protocol Buffer format, ensuring compatibility with other protobuf implementations.
ProtobufIOUtil provides the same method signatures as ProtostuffIOUtil but uses Protocol Buffer encoding:
public static <T> byte[] toByteArray(T message, Schema<T> schema, LinkedBuffer buffer)
public static <T> void writeTo(OutputStream out, T message, Schema<T> schema, LinkedBuffer buffer) throws IOException
public static <T> void writeTo(CodedOutput output, T message, Schema<T> schema, LinkedBuffer buffer) throws IOException
public static <T> void writeDelimitedTo(OutputStream out, T message, Schema<T> schema, LinkedBuffer buffer) throws IOException
public static <T> void mergeFrom(byte[] data, T message, Schema<T> schema) throws IOException
public static <T> void mergeFrom(byte[] data, int offset, int length, T message, Schema<T> schema) throws IOException
public static <T> void mergeFrom(InputStream in, T message, Schema<T> schema) throws IOException
public static <T> void mergeFrom(CodedInput input, T message, Schema<T> schema) throws IOException
public static <T> List<T> parseListFrom(InputStream in, Schema<T> schema) throws IOException
public static Pipe newPipe(byte[] data)
public static Pipe newPipe(byte[] data, int offset, int len)
public static Pipe newPipe(InputStream in)The key differences between ProtostuffIOUtil and ProtobufIOUtil:
// Standard protobuf serialization
LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);
byte[] protobufData = ProtobufIOUtil.toByteArray(message, schema, buffer);
// Deserialize protobuf data
Message deserializedMessage = schema.newMessage();
ProtobufIOUtil.mergeFrom(protobufData, deserializedMessage, schema);
// Write to stream with length delimiter
ProtobufIOUtil.writeDelimitedTo(outputStream, message, schema, buffer);
// Parse multiple messages from stream
List<Message> messages = ProtobufIOUtil.parseListFrom(inputStream, schema);Both utility classes throw:
IOException for I/O related errorsProtobufException for protocol-level errors (malformed data, invalid encoding)RuntimeException for truncated or corrupted dataLinkedBuffer instances when possible to minimize memory allocations