Google's Protocol Buffers implementation for Java, providing comprehensive serialization of structured data with efficient encoding and extensive API coverage
—
Low-level binary I/O operations for reading and writing protocol buffer wire format data, supporting all protocol buffer data types with high-performance streaming capabilities.
High-performance binary input stream for reading protocol buffer wire format data with support for all protocol buffer data types and streaming operations.
/**
* Reads protocol buffer wire format data
*/
public abstract class CodedInputStream {
// Static factory methods
/** Creates from InputStream */
public static CodedInputStream newInstance(InputStream input);
/** Creates from InputStream with buffer size */
public static CodedInputStream newInstance(InputStream input, int bufferSize);
/** Creates from ByteBuffer iterable */
public static CodedInputStream newInstance(Iterable<ByteBuffer> input);
/** Creates from byte array */
public static CodedInputStream newInstance(byte[] buf);
/** Creates from byte array slice */
public static CodedInputStream newInstance(byte[] buf, int off, int len);
/** Creates from ByteBuffer */
public static CodedInputStream newInstance(ByteBuffer buf);
// Tag and field operations
/** Reads and returns tag (field number and wire type) */
public abstract int readTag() throws IOException;
/** Skips field with given tag */
public abstract boolean skipField(int tag) throws IOException;
/** Skips field with given tag and extension registry */
public abstract boolean skipField(int tag, CodedOutputStream output) throws IOException;
// Integer reading methods
/** Reads int32 field */
public abstract int readInt32() throws IOException;
/** Reads int64 field */
public abstract long readInt64() throws IOException;
/** Reads uint32 field */
public abstract int readUInt32() throws IOException;
/** Reads uint64 field */
public abstract long readUInt64() throws IOException;
/** Reads sint32 field (ZigZag encoded) */
public abstract int readSInt32() throws IOException;
/** Reads sint64 field (ZigZag encoded) */
public abstract long readSInt64() throws IOException;
/** Reads fixed32 field */
public abstract int readFixed32() throws IOException;
/** Reads fixed64 field */
public abstract long readFixed64() throws IOException;
/** Reads sfixed32 field */
public abstract int readSFixed32() throws IOException;
/** Reads sfixed64 field */
public abstract long readSFixed64() throws IOException;
// Floating point reading methods
/** Reads float field */
public abstract float readFloat() throws IOException;
/** Reads double field */
public abstract double readDouble() throws IOException;
// Boolean reading
/** Reads bool field */
public abstract boolean readBool() throws IOException;
// String and bytes reading
/** Reads string field */
public abstract String readString() throws IOException;
/** Reads UTF-8 string field with validation */
public abstract String readStringRequireUtf8() throws IOException;
/** Reads bytes field as ByteString */
public abstract ByteString readBytes() throws IOException;
/** Reads bytes field as byte array */
public abstract byte[] readByteArray() throws IOException;
/** Reads bytes field as ByteBuffer */
public abstract ByteBuffer readByteBuffer() throws IOException;
// Advanced reading operations
/** Reads enum field */
public abstract int readEnum() throws IOException;
/** Reads raw varint32 */
public abstract int readRawVarint32() throws IOException;
/** Reads raw varint64 */
public abstract long readRawVarint64() throws IOException;
/** Reads raw little-endian 32-bit integer */
public abstract int readRawLittleEndian32() throws IOException;
/** Reads raw little-endian 64-bit integer */
public abstract long readRawLittleEndian64() throws IOException;
// Stream control methods
/** Sets recursion limit for nested messages */
public void setRecursionLimit(int limit);
/** Sets size limit for individual messages */
public void setSizeLimit(int limit);
/** Resets size counter */
public void resetSizeCounter();
/** Gets bytes read since last reset */
public int getBytesUntilLimit();
/** Checks if at end of stream */
public abstract boolean isAtEnd() throws IOException;
/** Gets current position in stream */
public abstract int getTotalBytesRead();
/** Pushes limit for length-delimited fields */
public abstract int pushLimit(int byteLimit) throws InvalidProtocolBufferException;
/** Pops limit after reading length-delimited field */
public abstract void popLimit(int oldLimit);
// Message reading
/** Reads embedded message */
public abstract void readMessage(MessageLite.Builder messageBuilder,
ExtensionRegistryLite extensionRegistry) throws IOException;
/** Reads group (deprecated wire type) */
public abstract void readGroup(int fieldNumber, MessageLite.Builder messageBuilder,
ExtensionRegistryLite extensionRegistry) throws IOException;
}High-performance binary output stream for writing protocol buffer wire format data with support for all data types and efficient encoding.
/**
* Writes protocol buffer wire format data
*/
public abstract class CodedOutputStream {
/** Default buffer size constant */
public static final int DEFAULT_BUFFER_SIZE = 4096;
// Static factory methods
/** Creates from OutputStream */
public static CodedOutputStream newInstance(OutputStream output);
/** Creates from OutputStream with buffer size */
public static CodedOutputStream newInstance(OutputStream output, int bufferSize);
/** Creates from byte array */
public static CodedOutputStream newInstance(byte[] flatArray);
/** Creates from byte array slice */
public static CodedOutputStream newInstance(byte[] flatArray, int offset, int length);
/** Creates from ByteBuffer */
public static CodedOutputStream newInstance(ByteBuffer buffer);
// Tag and field writing
/** Writes tag (field number and wire type) */
public abstract void writeTag(int fieldNumber, int wireType) throws IOException;
/** Writes raw varint32 */
public abstract void writeRawVarint32(int value) throws IOException;
/** Writes raw varint64 */
public abstract void writeRawVarint64(long value) throws IOException;
// Integer writing methods
/** Writes int32 field */
public abstract void writeInt32(int fieldNumber, int value) throws IOException;
/** Writes int64 field */
public abstract void writeInt64(int fieldNumber, long value) throws IOException;
/** Writes uint32 field */
public abstract void writeUInt32(int fieldNumber, int value) throws IOException;
/** Writes uint64 field */
public abstract void writeUInt64(int fieldNumber, long value) throws IOException;
/** Writes sint32 field (ZigZag encoded) */
public void writeSInt32(int fieldNumber, int value) throws IOException;
/** Writes sint64 field (ZigZag encoded) */
public void writeSInt64(int fieldNumber, long value) throws IOException;
/** Writes fixed32 field */
public abstract void writeFixed32(int fieldNumber, int value) throws IOException;
/** Writes fixed64 field */
public abstract void writeFixed64(int fieldNumber, long value) throws IOException;
/** Writes sfixed32 field */
public void writeSFixed32(int fieldNumber, int value) throws IOException;
/** Writes sfixed64 field */
public void writeSFixed64(int fieldNumber, long value) throws IOException;
// Floating point writing methods
/** Writes float field */
public void writeFloat(int fieldNumber, float value) throws IOException;
/** Writes double field */
public void writeDouble(int fieldNumber, double value) throws IOException;
// Boolean writing
/** Writes bool field */
public abstract void writeBool(int fieldNumber, boolean value) throws IOException;
// Enum writing
/** Writes enum field */
public void writeEnum(int fieldNumber, int value) throws IOException;
// String and bytes writing
/** Writes string field */
public abstract void writeString(int fieldNumber, String value) throws IOException;
/** Writes bytes field from ByteString */
public abstract void writeBytes(int fieldNumber, ByteString value) throws IOException;
/** Writes bytes field from byte array */
public abstract void writeByteArray(int fieldNumber, byte[] value) throws IOException;
/** Writes bytes field from byte array slice */
public abstract void writeByteArray(int fieldNumber, byte[] value, int offset, int length) throws IOException;
/** Writes bytes field from ByteBuffer */
public abstract void writeByteBuffer(int fieldNumber, ByteBuffer value) throws IOException;
// Message writing
/** Writes embedded message */
public abstract void writeMessage(int fieldNumber, MessageLite value) throws IOException;
/** Writes group (deprecated wire type) */
public abstract void writeGroup(int fieldNumber, MessageLite value) throws IOException;
// Raw writing methods
/** Writes raw bytes */
public abstract void writeRawBytes(ByteString value) throws IOException;
/** Writes raw byte array */
public abstract void writeRawBytes(byte[] value) throws IOException;
/** Writes raw byte array slice */
public abstract void writeRawBytes(byte[] value, int offset, int length) throws IOException;
/** Writes raw ByteBuffer */
public abstract void writeRawBytes(ByteBuffer value) throws IOException;
/** Writes raw little-endian 32-bit integer */
public abstract void writeRawLittleEndian32(int value) throws IOException;
/** Writes raw little-endian 64-bit integer */
public abstract void writeRawLittleEndian64(long value) throws IOException;
// Stream control methods
/** Enables deterministic serialization */
public void useDeterministicSerialization();
/** Flushes output */
public abstract void flush() throws IOException;
/** Returns remaining space in buffer */
public abstract int spaceLeft();
/** Verifies no space left (for fixed-size outputs) */
public void checkNoSpaceLeft();
/** Gets total bytes written */
public abstract int getTotalBytesWritten();
// No-tag writing methods (for packed repeated fields)
/** Writes int32 without tag */
public abstract void writeInt32NoTag(int value) throws IOException;
/** Writes uint32 without tag */
public abstract void writeUInt32NoTag(int value) throws IOException;
/** Writes int64 without tag */
public abstract void writeInt64NoTag(long value) throws IOException;
/** Writes uint64 without tag */
public abstract void writeUInt64NoTag(long value) throws IOException;
/** Writes sint32 without tag */
public void writeSInt32NoTag(int value) throws IOException;
/** Writes sint64 without tag */
public void writeSInt64NoTag(long value) throws IOException;
/** Writes fixed32 without tag */
public abstract void writeFixed32NoTag(int value) throws IOException;
/** Writes fixed64 without tag */
public abstract void writeFixed64NoTag(long value) throws IOException;
/** Writes sfixed32 without tag */
public void writeSFixed32NoTag(int value) throws IOException;
/** Writes sfixed64 without tag */
public void writeSFixed64NoTag(long value) throws IOException;
/** Writes float without tag */
public void writeFloatNoTag(float value) throws IOException;
/** Writes double without tag */
public void writeDoubleNoTag(double value) throws IOException;
/** Writes bool without tag */
public abstract void writeBoolNoTag(boolean value) throws IOException;
/** Writes enum without tag */
public void writeEnumNoTag(int value) throws IOException;
/** Writes string without tag */
public abstract void writeStringNoTag(String value) throws IOException;
/** Writes bytes without tag */
public abstract void writeBytesNoTag(ByteString value) throws IOException;
}Static methods for computing the serialized size of protocol buffer fields without actually serializing them.
/**
* Static methods for computing field sizes in CodedOutputStream
*/
public abstract class CodedOutputStream {
// Tag size computation
/** Computes tag size */
public static int computeTagSize(int fieldNumber);
// Integer field size computation
/** Computes int32 field size */
public static int computeInt32Size(int fieldNumber, int value);
/** Computes int64 field size */
public static int computeInt64Size(int fieldNumber, long value);
/** Computes uint32 field size */
public static int computeUInt32Size(int fieldNumber, int value);
/** Computes uint64 field size */
public static int computeUInt64Size(int fieldNumber, long value);
/** Computes sint32 field size */
public static int computeSInt32Size(int fieldNumber, int value);
/** Computes sint64 field size */
public static int computeSInt64Size(int fieldNumber, long value);
/** Computes fixed32 field size */
public static int computeFixed32Size(int fieldNumber, int value);
/** Computes fixed64 field size */
public static int computeFixed64Size(int fieldNumber, long value);
/** Computes sfixed32 field size */
public static int computeSFixed32Size(int fieldNumber, int value);
/** Computes sfixed64 field size */
public static int computeSFixed64Size(int fieldNumber, long value);
// Floating point field size computation
/** Computes float field size */
public static int computeFloatSize(int fieldNumber, float value);
/** Computes double field size */
public static int computeDoubleSize(int fieldNumber, double value);
// Boolean field size computation
/** Computes bool field size */
public static int computeBoolSize(int fieldNumber, boolean value);
// Enum field size computation
/** Computes enum field size */
public static int computeEnumSize(int fieldNumber, int value);
// String and bytes field size computation
/** Computes string field size */
public static int computeStringSize(int fieldNumber, String value);
/** Computes bytes field size */
public static int computeBytesSize(int fieldNumber, ByteString value);
/** Computes byte array field size */
public static int computeByteArraySize(int fieldNumber, byte[] value);
/** Computes ByteBuffer field size */
public static int computeByteBufferSize(int fieldNumber, ByteBuffer value);
// Message field size computation
/** Computes message field size */
public static int computeMessageSize(int fieldNumber, MessageLite value);
/** Computes group field size */
public static int computeGroupSize(int fieldNumber, MessageLite value);
// Raw size computation methods
/** Computes raw varint32 size */
public static int computeRawVarint32Size(int value);
/** Computes raw varint64 size */
public static int computeRawVarint64Size(long value);
/** Computes raw string size (UTF-8 encoded) */
public static int computeRawStringSize(String str);
// No-tag size computation (for packed repeated fields)
/** Computes int32 size without tag */
public static int computeInt32SizeNoTag(int value);
/** Computes uint32 size without tag */
public static int computeUInt32SizeNoTag(int value);
/** Computes int64 size without tag */
public static int computeInt64SizeNoTag(long value);
/** Computes uint64 size without tag */
public static int computeUInt64SizeNoTag(long value);
/** Computes sint32 size without tag */
public static int computeSInt32SizeNoTag(int value);
/** Computes sint64 size without tag */
public static int computeSInt64SizeNoTag(long value);
}Static utility methods for ZigZag encoding and other wire format operations.
/**
* Encoding utility methods in CodedOutputStream
*/
public abstract class CodedOutputStream {
/** Encodes signed 32-bit integer using ZigZag encoding */
public static int encodeZigZag32(int n);
/** Encodes signed 64-bit integer using ZigZag encoding */
public static long encodeZigZag64(long n);
}
/**
* Decoding utility methods in CodedInputStream
*/
public abstract class CodedInputStream {
/** Decodes ZigZag-encoded 32-bit integer */
public static int decodeZigZag32(int n);
/** Decodes ZigZag-encoded 64-bit integer */
public static long decodeZigZag64(long n);
}Wire type constants and utilities for working with protocol buffer wire format.
/**
* Constants and utilities for protocol buffer wire format
*/
public final class WireFormat {
// Wire type constants
/** Varint wire type (int32, int64, uint32, uint64, sint32, sint64, bool, enum) */
public static final int WIRETYPE_VARINT = 0;
/** Fixed64 wire type (fixed64, sfixed64, double) */
public static final int WIRETYPE_FIXED64 = 1;
/** Length-delimited wire type (string, bytes, embedded messages, packed repeated fields) */
public static final int WIRETYPE_LENGTH_DELIMITED = 2;
/** Start group wire type (deprecated) */
public static final int WIRETYPE_START_GROUP = 3;
/** End group wire type (deprecated) */
public static final int WIRETYPE_END_GROUP = 4;
/** Fixed32 wire type (fixed32, sfixed32, float) */
public static final int WIRETYPE_FIXED32 = 5;
// Utility methods
/** Extract wire type from tag */
public static int getTagWireType(int tag);
/** Extract field number from tag */
public static int getTagFieldNumber(int tag);
/** Create tag from field number and wire type */
public static int makeTag(int fieldNumber, int wireType);
// Enums for type information
public enum JavaType {
INT, LONG, FLOAT, DOUBLE, BOOLEAN, STRING, BYTE_STRING, ENUM, MESSAGE
}
public enum FieldType {
DOUBLE, FLOAT, INT64, UINT64, INT32, FIXED64, FIXED32, BOOL, STRING,
GROUP, MESSAGE, BYTES, UINT32, ENUM, SFIXED32, SFIXED64, SINT32, SINT64
}
}Usage Examples:
import com.google.protobuf.*;
import java.io.*;
// Writing protocol buffer data
try (OutputStream output = new FileOutputStream("message.pb")) {
CodedOutputStream codedOutput = CodedOutputStream.newInstance(output);
// Write fields manually
codedOutput.writeString(1, "Hello Protocol Buffers");
codedOutput.writeInt32(2, 42);
codedOutput.writeBool(3, true);
codedOutput.writeBytes(4, ByteString.copyFromUtf8("Binary data"));
codedOutput.flush();
} catch (IOException e) {
System.err.println("Write error: " + e.getMessage());
}
// Reading protocol buffer data
try (InputStream input = new FileInputStream("message.pb")) {
CodedInputStream codedInput = CodedInputStream.newInstance(input);
while (!codedInput.isAtEnd()) {
int tag = codedInput.readTag();
int fieldNumber = WireFormat.getTagFieldNumber(tag);
int wireType = WireFormat.getTagWireType(tag);
switch (fieldNumber) {
case 1:
String stringValue = codedInput.readString();
System.out.println("String field: " + stringValue);
break;
case 2:
int intValue = codedInput.readInt32();
System.out.println("Int field: " + intValue);
break;
case 3:
boolean boolValue = codedInput.readBool();
System.out.println("Bool field: " + boolValue);
break;
case 4:
ByteString bytesValue = codedInput.readBytes();
System.out.println("Bytes field: " + bytesValue.toStringUtf8());
break;
default:
codedInput.skipField(tag);
break;
}
}
} catch (IOException e) {
System.err.println("Read error: " + e.getMessage());
}
// Size computation example
String text = "Hello Protocol Buffers";
int fieldSize = CodedOutputStream.computeStringSize(1, text);
int totalSize = fieldSize; // Add sizes of other fields
byte[] buffer = new byte[totalSize];
CodedOutputStream output = CodedOutputStream.newInstance(buffer);
output.writeString(1, text);/**
* Advanced I/O configuration and limits
*/
public abstract class CodedInputStream {
/** Default recursion limit for nested messages */
public static final int DEFAULT_RECURSION_LIMIT = 100;
/** Default size limit for individual messages */
public static final int DEFAULT_SIZE_LIMIT = 64 << 20; // 64MB
/** Sets recursion limit to prevent stack overflow */
public void setRecursionLimit(int limit);
/** Gets current recursion limit */
public int getRecursionLimit();
/** Sets size limit for individual messages */
public void setSizeLimit(int limit);
/** Gets current size limit */
public int getSizeLimit();
}
/**
* Advanced output configuration
*/
public abstract class CodedOutputStream {
/** Enables deterministic serialization (affects map ordering) */
public void useDeterministicSerialization();
/** Checks if deterministic serialization is enabled */
public boolean isSerializationDeterministic();
}// Input/Output buffer management
public abstract class AllocatedBuffer {
// Buffer allocation interface for advanced memory management
}
public abstract class BufferAllocator {
// Buffer allocator interface for custom memory management
}
// Unsafe operations for extreme performance (use with caution)
public final class UnsafeByteOperations {
/** Wraps byte array without copying (unsafe) */
public static ByteString unsafeWrap(byte[] buffer);
/** Wraps byte array region without copying (unsafe) */
public static ByteString unsafeWrap(byte[] buffer, int offset, int length);
/** Wraps ByteBuffer without copying (unsafe) */
public static ByteString unsafeWrap(ByteBuffer buffer);
/** Writes ByteString without copying (unsafe) */
public static void unsafeWriteTo(ByteString bytes, ByteOutput output) throws IOException;
}Install with Tessl CLI
npx tessl i tessl/maven-com-google-protobuf--protobuf-java