Package io.protostuff
Class ProtobufOutput
- java.lang.Object
-
- io.protostuff.WriteSession
-
- io.protostuff.ProtobufOutput
-
- All Implemented Interfaces:
Output
public final class ProtobufOutput extends WriteSession implements Output
Protobuf serialization where the messages must be fully buffered on memory before it can be written to the socket (OutputStream).
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class io.protostuff.WriteSession
WriteSession.FlushHandler
-
-
Field Summary
Fields Modifier and Type Field Description static intLITTLE_ENDIAN_32_SIZEstatic intLITTLE_ENDIAN_64_SIZE-
Fields inherited from class io.protostuff.WriteSession
flushHandler, head, nextBufferSize, out, sink, size, tail
-
-
Constructor Summary
Constructors Constructor Description ProtobufOutput(LinkedBuffer buffer)ProtobufOutput(LinkedBuffer buffer, int nextBufferSize)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ProtobufOutputclear()Resets this output for re-use.static intcomputeRawVarint32Size(int value)Compute the number of bytes that would be needed to encode a varint.static intcomputeRawVarint64Size(long value)Compute the number of bytes that would be needed to encode a varint.static intencodeZigZag32(int n)Encode a ZigZag-encoded 32-bit value.static longencodeZigZag64(long n)Encode a ZigZag-encoded 64-bit value.static byte[]getRawVarInt32Bytes(int value)Returns the byte array computed from the var int 32 sizestatic byte[]getTagAndRawLittleEndian32Bytes(int tag, int value)Returns a byte array encoded with the tag and little endian 32static byte[]getTagAndRawLittleEndian64Bytes(int tag, long value)Returns a byte array encoded with the tag and little endian 64static byte[]getTagAndRawVarInt32Bytes(int tag, int value)Returns a byte array encoded with the tag and var int 32static byte[]getTagAndRawVarInt64Bytes(int tag, long value)Returns a byte array encoded with the tag and var int 64voidwriteBool(int fieldNumber, boolean value, boolean repeated)Writes a boolean field.voidwriteByteArray(int fieldNumber, byte[] bytes, boolean repeated)Writes a byte array field.voidwriteByteRange(boolean utf8String, int fieldNumber, byte[] value, int offset, int length, boolean repeated)Writes a binary or a pre-encoded utf8 string.voidwriteBytes(int fieldNumber, ByteString value, boolean repeated)Writes a ByteString(wraps byte array) field.voidwriteBytes(int fieldNumber, java.nio.ByteBuffer value, boolean repeated)Writes a ByteBuffer field.voidwriteDouble(int fieldNumber, double value, boolean repeated)Writes a double field.voidwriteEnum(int fieldNumber, int number, boolean repeated)Writes a enum(its number) field.voidwriteFixed32(int fieldNumber, int value, boolean repeated)Writes a fixed int(4 bytes) field.voidwriteFixed64(int fieldNumber, long value, boolean repeated)Writes a fixed long(8 bytes) field.voidwriteFloat(int fieldNumber, float value, boolean repeated)Writes a float field.voidwriteInt32(int fieldNumber, int value, boolean repeated)Writes a variable int field.voidwriteInt64(int fieldNumber, long value, boolean repeated)Writes a variable long field.<T> voidwriteObject(int fieldNumber, T value, Schema<T> schema, boolean repeated)Writes an object(using its schema) field.static intwriteRawLittleEndian32(int value, byte[] buffer, int offset)Writes the encoded little endian 32 and returns the bytes writtenstatic intwriteRawLittleEndian64(long value, byte[] buffer, int offset)Writes the encoded little endian 64 and returns the bytes writtenstatic voidwriteRawVarInt32(int value, byte[] buf, int offset)Encode and write a varint to the byte arraystatic LinkedBufferwriteRawVarInt32(int value, WriteSession session, LinkedBuffer lb)Returns the buffer encoded with the variable int 32.static voidwriteRawVarInt32Bytes(java.io.DataOutput out, int value)Encode and write a varint to theDataOutputstatic voidwriteRawVarInt32Bytes(java.io.OutputStream out, int value)Encode and write a varint to theOutputStreamvoidwriteSFixed32(int fieldNumber, int value, boolean repeated)Writes a signed+fixed int(4 bytes) field.voidwriteSFixed64(int fieldNumber, long value, boolean repeated)Writes a signed+fixed long(8 bytes) field.voidwriteSInt32(int fieldNumber, int value, boolean repeated)Writes a signed int field.voidwriteSInt64(int fieldNumber, long value, boolean repeated)Writes a signed long field.voidwriteString(int fieldNumber, java.lang.CharSequence value, boolean repeated)Writes a String field.static LinkedBufferwriteTagAndByteArray(int tag, byte[] value, int offset, int valueLen, WriteSession session, LinkedBuffer lb)Returns the buffer encoded with the tag and byte arraystatic LinkedBufferwriteTagAndLinkedBuffer(int tag, LinkedBuffer buffer, WriteSession session, LinkedBuffer lb)Returns the buffer encoded with the tag and LinkedBuffer (zero-copy)static LinkedBufferwriteTagAndRawLittleEndian32(int tag, int value, WriteSession session, LinkedBuffer lb)Returns the buffer encoded with the tag and little endian 32static LinkedBufferwriteTagAndRawLittleEndian64(int tag, long value, WriteSession session, LinkedBuffer lb)Returns the buffer encoded with the tag and little endian 64static LinkedBufferwriteTagAndRawVarInt32(int tag, int value, WriteSession session, LinkedBuffer lb)Returns the buffer encoded with the tag and var int 32static LinkedBufferwriteTagAndRawVarInt64(int tag, long value, WriteSession session, LinkedBuffer lb)Returns the buffer encoded with the tag and var int 64voidwriteUInt32(int fieldNumber, int value, boolean repeated)Writes an unsigned int field.voidwriteUInt64(int fieldNumber, long value, boolean repeated)Writes an unsigned long field.-
Methods inherited from class io.protostuff.WriteSession
flush, flush, flush, getSize, reset, toByteArray
-
-
-
-
Field Detail
-
LITTLE_ENDIAN_32_SIZE
public static final int LITTLE_ENDIAN_32_SIZE
- See Also:
- Constant Field Values
-
LITTLE_ENDIAN_64_SIZE
public static final int LITTLE_ENDIAN_64_SIZE
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ProtobufOutput
public ProtobufOutput(LinkedBuffer buffer)
-
ProtobufOutput
public ProtobufOutput(LinkedBuffer buffer, int nextBufferSize)
-
-
Method Detail
-
clear
public ProtobufOutput clear()
Resets this output for re-use.- Overrides:
clearin classWriteSession
-
writeInt32
public void writeInt32(int fieldNumber, int value, boolean repeated) throws java.io.IOExceptionDescription copied from interface:OutputWrites a variable int field.- Specified by:
writeInt32in interfaceOutput- Throws:
java.io.IOException
-
writeUInt32
public void writeUInt32(int fieldNumber, int value, boolean repeated) throws java.io.IOExceptionDescription copied from interface:OutputWrites an unsigned int field.- Specified by:
writeUInt32in interfaceOutput- Throws:
java.io.IOException
-
writeSInt32
public void writeSInt32(int fieldNumber, int value, boolean repeated) throws java.io.IOExceptionDescription copied from interface:OutputWrites a signed int field.- Specified by:
writeSInt32in interfaceOutput- Throws:
java.io.IOException
-
writeFixed32
public void writeFixed32(int fieldNumber, int value, boolean repeated) throws java.io.IOExceptionDescription copied from interface:OutputWrites a fixed int(4 bytes) field.- Specified by:
writeFixed32in interfaceOutput- Throws:
java.io.IOException
-
writeSFixed32
public void writeSFixed32(int fieldNumber, int value, boolean repeated) throws java.io.IOExceptionDescription copied from interface:OutputWrites a signed+fixed int(4 bytes) field.- Specified by:
writeSFixed32in interfaceOutput- Throws:
java.io.IOException
-
writeInt64
public void writeInt64(int fieldNumber, long value, boolean repeated) throws java.io.IOExceptionDescription copied from interface:OutputWrites a variable long field.- Specified by:
writeInt64in interfaceOutput- Throws:
java.io.IOException
-
writeUInt64
public void writeUInt64(int fieldNumber, long value, boolean repeated) throws java.io.IOExceptionDescription copied from interface:OutputWrites an unsigned long field.- Specified by:
writeUInt64in interfaceOutput- Throws:
java.io.IOException
-
writeSInt64
public void writeSInt64(int fieldNumber, long value, boolean repeated) throws java.io.IOExceptionDescription copied from interface:OutputWrites a signed long field.- Specified by:
writeSInt64in interfaceOutput- Throws:
java.io.IOException
-
writeFixed64
public void writeFixed64(int fieldNumber, long value, boolean repeated) throws java.io.IOExceptionDescription copied from interface:OutputWrites a fixed long(8 bytes) field.- Specified by:
writeFixed64in interfaceOutput- Throws:
java.io.IOException
-
writeSFixed64
public void writeSFixed64(int fieldNumber, long value, boolean repeated) throws java.io.IOExceptionDescription copied from interface:OutputWrites a signed+fixed long(8 bytes) field.- Specified by:
writeSFixed64in interfaceOutput- Throws:
java.io.IOException
-
writeFloat
public void writeFloat(int fieldNumber, float value, boolean repeated) throws java.io.IOExceptionDescription copied from interface:OutputWrites a float field.- Specified by:
writeFloatin interfaceOutput- Throws:
java.io.IOException
-
writeDouble
public void writeDouble(int fieldNumber, double value, boolean repeated) throws java.io.IOExceptionDescription copied from interface:OutputWrites a double field.- Specified by:
writeDoublein interfaceOutput- Throws:
java.io.IOException
-
writeBool
public void writeBool(int fieldNumber, boolean value, boolean repeated) throws java.io.IOExceptionDescription copied from interface:OutputWrites a boolean field.
-
writeEnum
public void writeEnum(int fieldNumber, int number, boolean repeated) throws java.io.IOExceptionDescription copied from interface:OutputWrites a enum(its number) field.
-
writeString
public void writeString(int fieldNumber, java.lang.CharSequence value, boolean repeated) throws java.io.IOExceptionDescription copied from interface:OutputWrites a String field.- Specified by:
writeStringin interfaceOutput- Throws:
java.io.IOException
-
writeBytes
public void writeBytes(int fieldNumber, ByteString value, boolean repeated) throws java.io.IOExceptionDescription copied from interface:OutputWrites a ByteString(wraps byte array) field.- Specified by:
writeBytesin interfaceOutput- Throws:
java.io.IOException
-
writeByteArray
public void writeByteArray(int fieldNumber, byte[] bytes, boolean repeated) throws java.io.IOExceptionDescription copied from interface:OutputWrites a byte array field.- Specified by:
writeByteArrayin interfaceOutput- Throws:
java.io.IOException
-
writeByteRange
public void writeByteRange(boolean utf8String, int fieldNumber, byte[] value, int offset, int length, boolean repeated) throws java.io.IOExceptionDescription copied from interface:OutputWrites a binary or a pre-encoded utf8 string.- Specified by:
writeByteRangein interfaceOutput- Throws:
java.io.IOException
-
writeObject
public <T> void writeObject(int fieldNumber, T value, Schema<T> schema, boolean repeated) throws java.io.IOExceptionDescription copied from interface:OutputWrites an object(using its schema) field.- Specified by:
writeObjectin interfaceOutput- Throws:
java.io.IOException
-
writeRawVarInt32
public static LinkedBuffer writeRawVarInt32(int value, WriteSession session, LinkedBuffer lb)
Returns the buffer encoded with the variable int 32.
-
writeTagAndLinkedBuffer
public static LinkedBuffer writeTagAndLinkedBuffer(int tag, LinkedBuffer buffer, WriteSession session, LinkedBuffer lb)
Returns the buffer encoded with the tag and LinkedBuffer (zero-copy)
-
writeTagAndByteArray
public static LinkedBuffer writeTagAndByteArray(int tag, byte[] value, int offset, int valueLen, WriteSession session, LinkedBuffer lb)
Returns the buffer encoded with the tag and byte array
-
writeTagAndRawVarInt32
public static LinkedBuffer writeTagAndRawVarInt32(int tag, int value, WriteSession session, LinkedBuffer lb)
Returns the buffer encoded with the tag and var int 32
-
writeTagAndRawVarInt64
public static LinkedBuffer writeTagAndRawVarInt64(int tag, long value, WriteSession session, LinkedBuffer lb)
Returns the buffer encoded with the tag and var int 64
-
writeTagAndRawLittleEndian32
public static LinkedBuffer writeTagAndRawLittleEndian32(int tag, int value, WriteSession session, LinkedBuffer lb)
Returns the buffer encoded with the tag and little endian 32
-
writeTagAndRawLittleEndian64
public static LinkedBuffer writeTagAndRawLittleEndian64(int tag, long value, WriteSession session, LinkedBuffer lb)
Returns the buffer encoded with the tag and little endian 64
-
writeRawVarInt32
public static void writeRawVarInt32(int value, byte[] buf, int offset) throws java.io.IOExceptionEncode and write a varint to the byte array- Throws:
java.io.IOException
-
writeRawVarInt32Bytes
public static void writeRawVarInt32Bytes(java.io.OutputStream out, int value) throws java.io.IOExceptionEncode and write a varint to theOutputStream- Throws:
java.io.IOException
-
writeRawVarInt32Bytes
public static void writeRawVarInt32Bytes(java.io.DataOutput out, int value) throws java.io.IOExceptionEncode and write a varint to theDataOutput- Throws:
java.io.IOException
-
getTagAndRawVarInt32Bytes
public static byte[] getTagAndRawVarInt32Bytes(int tag, int value)Returns a byte array encoded with the tag and var int 32
-
getTagAndRawVarInt64Bytes
public static byte[] getTagAndRawVarInt64Bytes(int tag, long value)Returns a byte array encoded with the tag and var int 64
-
getTagAndRawLittleEndian32Bytes
public static byte[] getTagAndRawLittleEndian32Bytes(int tag, int value)Returns a byte array encoded with the tag and little endian 32
-
getTagAndRawLittleEndian64Bytes
public static byte[] getTagAndRawLittleEndian64Bytes(int tag, long value)Returns a byte array encoded with the tag and little endian 64
-
writeRawLittleEndian32
public static int writeRawLittleEndian32(int value, byte[] buffer, int offset)Writes the encoded little endian 32 and returns the bytes written
-
writeRawLittleEndian64
public static int writeRawLittleEndian64(long value, byte[] buffer, int offset)Writes the encoded little endian 64 and returns the bytes written
-
getRawVarInt32Bytes
public static byte[] getRawVarInt32Bytes(int value)
Returns the byte array computed from the var int 32 size
-
computeRawVarint32Size
public static int computeRawVarint32Size(int value)
Compute the number of bytes that would be needed to encode a varint.valueis treated as unsigned, so it won't be sign-extended if negative.
-
computeRawVarint64Size
public static int computeRawVarint64Size(long value)
Compute the number of bytes that would be needed to encode a varint.
-
encodeZigZag32
public static int encodeZigZag32(int n)
Encode a ZigZag-encoded 32-bit value. ZigZag encodes signed integers into values that can be efficiently encoded with varint. (Otherwise, negative values must be sign-extended to 64 bits to be varint encoded, thus always taking 10 bytes on the wire.)- Parameters:
n- A signed 32-bit integer.- Returns:
- An unsigned 32-bit integer, stored in a signed int because Java has no explicit unsigned support.
-
encodeZigZag64
public static long encodeZigZag64(long n)
Encode a ZigZag-encoded 64-bit value. ZigZag encodes signed integers into values that can be efficiently encoded with varint. (Otherwise, negative values must be sign-extended to 64 bits to be varint encoded, thus always taking 10 bytes on the wire.)- Parameters:
n- A signed 64-bit integer.- Returns:
- An unsigned 64-bit integer, stored in a signed int because Java has no explicit unsigned support.
-
writeBytes
public void writeBytes(int fieldNumber, java.nio.ByteBuffer value, boolean repeated) throws java.io.IOExceptionWrites a ByteBuffer field.- Specified by:
writeBytesin interfaceOutput- Throws:
java.io.IOException
-
-