Protostuff Core is a high-performance Java serialization library that provides efficient Protocol Buffer-compatible serialization for Java objects. It offers both protobuf-compatible format and protostuff's own optimized format, with support for object graphs, cyclic dependencies, and forward/backward compatibility for schema evolution.
This documentation covers both the core serialization implementations (protostuff-core module) and the foundational APIs they depend on (protostuff-api module).
<dependency>
<groupId>io.protostuff</groupId>
<artifactId>protostuff-core</artifactId>
<version>1.8.0</version>
</dependency>// Core serialization utilities (protostuff-core)
import io.protostuff.ProtostuffIOUtil;
import io.protostuff.ProtobufIOUtil;
import io.protostuff.GraphIOUtil;
// Foundational APIs (protostuff-api, included as dependency)
import io.protostuff.Schema;
import io.protostuff.LinkedBuffer;
import io.protostuff.Input;
import io.protostuff.Output;import io.protostuff.*;
// Example message class with schema
public class Person {
String name;
int age;
String email;
// Constructors, getters, setters...
}
// Serialize using Protostuff format
LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);
byte[] data = ProtostuffIOUtil.toByteArray(person, schema, buffer);
// Deserialize using Protostuff format
Person deserializedPerson = schema.newMessage();
ProtostuffIOUtil.mergeFrom(data, deserializedPerson, schema);
// Or use Protocol Buffer format
byte[] protobufData = ProtobufIOUtil.toByteArray(person, schema, buffer);
Person protobufPerson = schema.newMessage();
ProtobufIOUtil.mergeFrom(protobufData, protobufPerson, schema);Protostuff Core is built around several key components:
ProtostuffIOUtil, ProtobufIOUtil, GraphIOUtil) providing high-level serialization/deserialization operationsHigh-level utilities for common serialization tasks including byte array conversion, stream I/O, and message merging. The primary entry points for most applications.
// Protostuff format utilities
public final class ProtostuffIOUtil {
// Serialization
public static <T> byte[] toByteArray(T message, Schema<T> schema, LinkedBuffer buffer);
public static <T> int writeTo(OutputStream out, T message, Schema<T> schema, LinkedBuffer buffer);
public static <T> int writeDelimitedTo(OutputStream out, T message, Schema<T> schema, LinkedBuffer buffer);
public static <T> int writeListTo(OutputStream out, List<T> messages, Schema<T> schema, LinkedBuffer buffer);
// Deserialization
public static <T> void mergeFrom(byte[] data, T message, Schema<T> schema);
public static <T> void mergeFrom(byte[] data, int offset, int length, T message, Schema<T> schema);
public static <T> void mergeFrom(InputStream in, T message, Schema<T> schema);
public static <T> int mergeDelimitedFrom(InputStream in, T message, Schema<T> schema);
public static <T> List<T> parseListFrom(InputStream in, Schema<T> schema);
}
// Protocol Buffer format utilities
public final class ProtobufIOUtil {
public static <T> byte[] toByteArray(T message, Schema<T> schema, LinkedBuffer buffer);
public static <T> void mergeFrom(byte[] data, T message, Schema<T> schema);
public static <T> int writeTo(OutputStream out, T message, Schema<T> schema, LinkedBuffer buffer);
}Low-level streaming interfaces for reading and writing protobuf-encoded data from various sources including byte arrays, ByteBuffers, and InputStreams.
public abstract class CodedInput {
public abstract int readRawByte() throws IOException;
public abstract String readString() throws IOException;
public abstract int readTag() throws IOException;
public abstract boolean isAtEnd() throws IOException;
}
public final class ProtobufOutput implements Output {
public ProtobufOutput(LinkedBuffer buffer);
public void writeString(int fieldNumber, CharSequence value, boolean repeated) throws IOException;
public void writeInt32(int fieldNumber, int value, boolean repeated) throws IOException;
}Specialized serialization for object graphs containing references and cyclic dependencies, which standard protocol buffers cannot handle natively.
public final class GraphIOUtil {
// Serialization
public static <T> byte[] toByteArray(T message, Schema<T> schema, LinkedBuffer buffer);
public static <T> int writeTo(OutputStream out, T message, Schema<T> schema, LinkedBuffer buffer);
public static <T> int writeDelimitedTo(OutputStream out, T message, Schema<T> schema, LinkedBuffer buffer);
// Deserialization
public static <T> void mergeFrom(byte[] data, T message, Schema<T> schema);
public static <T> void mergeFrom(byte[] data, int offset, int length, T message, Schema<T> schema);
public static <T> void mergeFrom(InputStream in, T message, Schema<T> schema);
public static <T> int mergeDelimitedFrom(InputStream in, T message, Schema<T> schema);
}Efficient memory management through linked buffer structures that minimize memory allocations and provide reusable buffer pools.
public final class LinkedBuffer {
public static final int MIN_BUFFER_SIZE = 256;
public static final int DEFAULT_BUFFER_SIZE = 512;
// Allocation methods
public static LinkedBuffer allocate();
public static LinkedBuffer allocate(int size);
public static LinkedBuffer allocate(int size, LinkedBuffer previous);
public static LinkedBuffer wrap(byte[] array, int offset, int length);
public static LinkedBuffer use(byte[] buffer);
// Utility methods
public static int writeTo(OutputStream out, LinkedBuffer node) throws IOException;
public LinkedBuffer clear();
}Specialized input and output implementations for different data sources and memory optimization scenarios.
public final class ByteBufferInput extends CodedInput;
public final class ProtostuffOutput implements Output;
public final class LowCopyProtostuffOutput implements Output;
public final class LowCopyProtobufOutput implements Output;These classes provide optimized implementations for specific use cases like ByteBuffer sources and memory-efficient output operations.
public class ProtobufException extends ProtostuffException {
public ProtobufException(String description);
public ProtobufException(String description, Throwable cause);
}Protostuff Core throws ProtobufException for protocol-level errors such as malformed data, invalid tags, or truncated messages. I/O related errors are propagated as standard IOException.