Binary serialization, deserialization, and I/O operations for protocol buffer messages. This includes ByteString utilities, coded streams, and wire format support in the shaded akka.protobufv3.internal package.
Immutable sequence of bytes with rich manipulation methods. Provides efficient operations for byte data handling and string conversions.
/**
* Immutable sequence of bytes with efficient manipulation methods.
* Optimized for protocol buffer operations with lazy evaluation and sharing.
*/
class ByteString {
/** Create ByteString from byte array */
static ByteString copyFrom(byte[] bytes);
/** Create ByteString from byte array segment */
static ByteString copyFrom(byte[] bytes, int offset, int size);
/** Create ByteString from UTF-8 encoded string */
static ByteString copyFromUtf8(String text);
/** Create ByteString from string with specific charset */
static ByteString copyFrom(String text, String charsetName)
throws UnsupportedEncodingException;
/** Create ByteString from string with charset */
static ByteString copyFrom(String text, Charset charset);
/** Create ByteString from InputStream */
static ByteString readFrom(InputStream streamToDrain) throws IOException;
/** Create ByteString from InputStream with size limit */
static ByteString readFrom(InputStream streamToDrain, int chunkSize) throws IOException;
/** Empty ByteString constant */
static final ByteString EMPTY;
/** Convert to byte array */
byte[] toByteArray();
/** Convert to UTF-8 string */
String toStringUtf8();
/** Convert to string with specific charset */
String toString(String charsetName) throws UnsupportedEncodingException;
/** Convert to string with charset */
String toString(Charset charset);
/** Get size in bytes */
int size();
/** Check if empty */
boolean isEmpty();
/** Get byte at specific index */
byte byteAt(int index);
/** Create substring from begin index to end */
ByteString substring(int beginIndex);
/** Create substring from begin to end index */
ByteString substring(int beginIndex, int endIndex);
/** Concatenate with another ByteString */
ByteString concat(ByteString other);
/** Check if starts with specific ByteString */
boolean startsWith(ByteString prefix);
/** Check if ends with specific ByteString */
boolean endsWith(ByteString suffix);
/** Create iterator over bytes */
ByteIterator iterator();
/** Copy bytes to existing byte array */
void copyTo(byte[] target, int offset);
/** Copy bytes to ByteBuffer */
void copyTo(ByteBuffer target);
/** Create new CodedInputStream from this ByteString */
CodedInputStream newCodedInput();
/** Create InputStream from this ByteString */
InputStream newInput();
}Reads and decodes protocol message fields from various input sources. Handles variable-length encoding and provides methods for reading all protobuf wire format types.
/**
* Reads and decodes protocol message fields from input sources.
* Handles protobuf wire format including varint encoding and length-delimited fields.
*/
class CodedInputStream {
/** Create from byte array */
static CodedInputStream newInstance(byte[] buf);
/** Create from byte array segment */
static CodedInputStream newInstance(byte[] buf, int off, int len);
/** Create from ByteString */
static CodedInputStream newInstance(ByteString byteString);
/** Create from InputStream */
static CodedInputStream newInstance(InputStream input);
/** Create from InputStream with buffer size */
static CodedInputStream newInstance(InputStream input, int bufferSize);
/** Read a 32-bit integer */
int readInt32() throws IOException;
/** Read a 64-bit integer */
long readInt64() throws IOException;
/** Read an unsigned 32-bit integer */
int readUInt32() throws IOException;
/** Read an unsigned 64-bit integer */
long readUInt64() throws IOException;
/** Read a signed 32-bit integer */
int readSInt32() throws IOException;
/** Read a signed 64-bit integer */
long readSInt64() throws IOException;
/** Read a fixed 32-bit integer */
int readFixed32() throws IOException;
/** Read a fixed 64-bit integer */
long readFixed64() throws IOException;
/** Read a signed fixed 32-bit integer */
int readSFixed32() throws IOException;
/** Read a signed fixed 64-bit integer */
long readSFixed64() throws IOException;
/** Read a float */
float readFloat() throws IOException;
/** Read a double */
double readDouble() throws IOException;
/** Read a boolean */
boolean readBool() throws IOException;
/** Read a string */
String readString() throws IOException;
/** Read bytes as ByteString */
ByteString readBytes() throws IOException;
/** Read a varint field tag */
int readTag() throws IOException;
/** Check if at end of input */
boolean isAtEnd() throws IOException;
/** Skip a field with given tag */
boolean skipField(int tag) throws IOException;
/** Set size limit for messages */
int setSizeLimit(int limit);
/** Set recursion depth limit */
int setRecursionLimit(int limit);
/** Push a new limit for length-delimited messages */
int pushLimit(int byteLimit) throws InvalidProtocolBufferException;
/** Pop the current limit */
void popLimit(int oldLimit);
/** Get the total bytes read */
int getTotalBytesRead();
}Encodes and writes protocol message fields to various output destinations. Handles variable-length encoding and provides methods for writing all protobuf wire format types.
/**
* Encodes and writes protocol message fields to output destinations.
* Handles protobuf wire format encoding including varint and length-delimited fields.
*/
class CodedOutputStream {
/** Create writing to byte array */
static CodedOutputStream newInstance(byte[] flatArray);
/** Create writing to byte array segment */
static CodedOutputStream newInstance(byte[] flatArray, int offset, int length);
/** Create writing to OutputStream */
static CodedOutputStream newInstance(OutputStream output);
/** Create writing to OutputStream with buffer size */
static CodedOutputStream newInstance(OutputStream output, int bufferSize);
/** Write a 32-bit integer field */
void writeInt32(int fieldNumber, int value) throws IOException;
/** Write a 64-bit integer field */
void writeInt64(int fieldNumber, long value) throws IOException;
/** Write an unsigned 32-bit integer field */
void writeUInt32(int fieldNumber, int value) throws IOException;
/** Write an unsigned 64-bit integer field */
void writeUInt64(int fieldNumber, long value) throws IOException;
/** Write a signed 32-bit integer field */
void writeSInt32(int fieldNumber, int value) throws IOException;
/** Write a signed 64-bit integer field */
void writeSInt64(int fieldNumber, long value) throws IOException;
/** Write a fixed 32-bit integer field */
void writeFixed32(int fieldNumber, int value) throws IOException;
/** Write a fixed 64-bit integer field */
void writeFixed64(int fieldNumber, long value) throws IOException;
/** Write a signed fixed 32-bit integer field */
void writeSFixed32(int fieldNumber, int value) throws IOException;
/** Write a signed fixed 64-bit integer field */
void writeSFixed64(int fieldNumber, long value) throws IOException;
/** Write a float field */
void writeFloat(int fieldNumber, float value) throws IOException;
/** Write a double field */
void writeDouble(int fieldNumber, double value) throws IOException;
/** Write a boolean field */
void writeBool(int fieldNumber, boolean value) throws IOException;
/** Write a string field */
void writeString(int fieldNumber, String value) throws IOException;
/** Write bytes field */
void writeBytes(int fieldNumber, ByteString value) throws IOException;
/** Write a message field */
void writeMessage(int fieldNumber, MessageLite value) throws IOException;
/** Write field tag */
void writeTag(int fieldNumber, int wireType) throws IOException;
/** Write raw varint */
void writeRawVarint32(int value) throws IOException;
/** Write raw varint64 */
void writeRawVarint64(long value) throws IOException;
/** Write raw bytes */
void writeRawBytes(ByteString value) throws IOException;
/** Flush any buffered output */
void flush() throws IOException;
/** Get the current buffer space */
int spaceLeft();
/** Compute the size needed for a field */
static int computeInt32Size(int fieldNumber, int value);
static int computeStringSize(int fieldNumber, String value);
static int computeMessageSize(int fieldNumber, MessageLite value);
}Constants and helper functions for protocol buffer wire format. Defines wire types and provides utilities for working with field tags.
/**
* Constants and helper functions for protocol buffer wire format.
* Defines wire types and tag manipulation utilities.
*/
class WireFormat {
/** Wire type constants */
static final int WIRETYPE_VARINT = 0;
static final int WIRETYPE_FIXED64 = 1;
static final int WIRETYPE_LENGTH_DELIMITED = 2;
static final int WIRETYPE_START_GROUP = 3;
static final int WIRETYPE_END_GROUP = 4;
static final int WIRETYPE_FIXED32 = 5;
/** Extract field number from tag */
static int getTagFieldNumber(int tag);
/** Extract wire type from tag */
static int getTagWireType(int tag);
/** Create tag from field number and wire type */
static int makeTag(int fieldNumber, int wireType);
/** JavaType enumeration for field types */
enum JavaType {
INT, LONG, FLOAT, DOUBLE, BOOLEAN, STRING, BYTE_STRING, ENUM, MESSAGE;
}
/** FieldType enumeration with wire format details */
enum FieldType {
DOUBLE(JavaType.DOUBLE, WIRETYPE_FIXED64),
FLOAT(JavaType.FLOAT, WIRETYPE_FIXED32),
INT64(JavaType.LONG, WIRETYPE_VARINT),
UINT64(JavaType.LONG, WIRETYPE_VARINT),
INT32(JavaType.INT, WIRETYPE_VARINT),
FIXED64(JavaType.LONG, WIRETYPE_FIXED64),
FIXED32(JavaType.INT, WIRETYPE_FIXED32),
BOOL(JavaType.BOOLEAN, WIRETYPE_VARINT),
STRING(JavaType.STRING, WIRETYPE_LENGTH_DELIMITED),
MESSAGE(JavaType.MESSAGE, WIRETYPE_LENGTH_DELIMITED),
BYTES(JavaType.BYTE_STRING, WIRETYPE_LENGTH_DELIMITED),
UINT32(JavaType.INT, WIRETYPE_VARINT),
ENUM(JavaType.ENUM, WIRETYPE_VARINT),
SFIXED32(JavaType.INT, WIRETYPE_FIXED32),
SFIXED64(JavaType.LONG, WIRETYPE_FIXED64),
SINT32(JavaType.INT, WIRETYPE_VARINT),
SINT64(JavaType.LONG, WIRETYPE_VARINT);
/** Get the Java type for this field type */
JavaType getJavaType();
/** Get the wire type for this field type */
int getWireType();
}
}Provides text parsing and formatting for protocol buffers. Enables human-readable representation of messages for debugging and configuration.
/**
* Utilities for converting protocol buffer messages to/from text format.
* Useful for debugging, logging, and human-readable configuration files.
*/
class TextFormat {
/** Convert message to text format string */
static String printToString(MessageOrBuilder message);
/** Print message to Appendable */
static void print(MessageOrBuilder message, Appendable output) throws IOException;
/** Parse text format into message builder */
static void merge(Readable input, Message.Builder builder) throws IOException;
/** Parse text format string into message builder */
static void merge(CharSequence input, Message.Builder builder)
throws TextFormatParseException;
/** Parse text format with extension registry */
static void merge(Readable input, ExtensionRegistry extensionRegistry,
Message.Builder builder) throws IOException;
/** Printer class for customizable text format output */
static class Printer {
/** Create printer that includes default value fields */
Printer includingDefaultValueFields();
/** Create printer that preserves proto field names */
Printer preservingProtoFieldNames();
/** Create printer with custom type registry */
Printer usingTypeRegistry(JsonFormat.TypeRegistry registry);
/** Print message to string */
String print(MessageOrBuilder message);
/** Print message to Appendable */
void print(MessageOrBuilder message, Appendable output) throws IOException;
}
/** Parser class for customizable text format parsing */
static class Parser {
/** Create parser that ignores unknown fields */
Parser ignoringUnknownFields();
/** Create parser that allows missing required fields */
Parser allowingMissingRequiredFields();
/** Create parser with custom type registry */
Parser usingTypeRegistry(JsonFormat.TypeRegistry registry);
/** Parse from Readable into builder */
void merge(Readable input, Message.Builder builder) throws IOException;
/** Parse from string into builder */
void merge(CharSequence input, Message.Builder builder)
throws TextFormatParseException;
}
/** Create default printer */
static Printer printer();
/** Create default parser */
static Parser parser();
}import akka.protobufv3.internal.ByteString;
// Create ByteString from string
ByteString data = ByteString.copyFromUtf8("Hello Protocol Buffers");
// Create from byte array
byte[] bytes = {1, 2, 3, 4, 5};
ByteString fromBytes = ByteString.copyFrom(bytes);
// Convert back to string and bytes
String text = data.toStringUtf8();
byte[] backToBytes = data.toByteArray();
// Concatenation and substring
ByteString part1 = ByteString.copyFromUtf8("Hello ");
ByteString part2 = ByteString.copyFromUtf8("World");
ByteString combined = part1.concat(part2);
ByteString substring = combined.substring(0, 5); // "Hello"
// Check properties
boolean isEmpty = data.isEmpty();
int size = data.size();
byte firstByte = data.byteAt(0);import akka.protobufv3.internal.*;
// Read from byte array
byte[] data = getSerializedData();
CodedInputStream input = CodedInputStream.newInstance(data);
try {
while (!input.isAtEnd()) {
int tag = input.readTag();
int fieldNumber = WireFormat.getTagFieldNumber(tag);
int wireType = WireFormat.getTagWireType(tag);
switch (wireType) {
case WireFormat.WIRETYPE_VARINT:
long value = input.readInt64();
break;
case WireFormat.WIRETYPE_LENGTH_DELIMITED:
ByteString bytes = input.readBytes();
break;
default:
input.skipField(tag);
}
}
} catch (IOException e) {
// Handle parsing error
}import akka.protobufv3.internal.*;
// Write to byte array
byte[] buffer = new byte[1024];
CodedOutputStream output = CodedOutputStream.newInstance(buffer);
try {
// Write various field types
output.writeInt32(1, 42); // Field 1: int32
output.writeString(2, "Hello"); // Field 2: string
output.writeBool(3, true); // Field 3: bool
output.writeBytes(4, ByteString.copyFromUtf8("data")); // Field 4: bytes
output.flush();
// Get the actual size written
int bytesWritten = output.getTotalBytesWritten();
} catch (IOException e) {
// Handle write error
}import akka.protobufv3.internal.TextFormat;
// Convert message to text (assumes MyMessage exists)
// MyMessage message = MyMessage.newBuilder()
// .setName("example")
// .setId(123)
// .build();
// String textFormat = TextFormat.printToString(message);
// System.out.println(textFormat);
// Parse text format back to message
// MyMessage.Builder builder = MyMessage.newBuilder();
// TextFormat.merge("name: \"parsed\" id: 456", builder);
// MyMessage parsed = builder.build();
// Use custom printer
TextFormat.Printer printer = TextFormat.printer()
.includingDefaultValueFields()
.preservingProtoFieldNames();
// String customFormat = printer.print(message);