Class CodedOutputStream
java.lang.Object
java.io.OutputStream
java.io.FilterOutputStream
org.fusesource.hawtbuf.proto.CodedOutputStream
- All Implemented Interfaces:
Closeable, Flushable, AutoCloseable
Encodes and writes protocol message fields.
This class contains two kinds of methods: methods that write specific
protocol message constructs and field types (e.g. writeTag(int, int) and
writeInt32(int, int)) and methods that write low-level values (e.g.
writeRawVarint32(int) and writeRawBytes(byte[])). If you are writing
encoded protocol messages, you should use the former methods, but if you are
writing some other format of your own design, use the latter.
This class is totally unsynchronized.
-
Field Summary
FieldsFields inherited from class FilterOutputStream
out -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidstatic intcomputeBoolSize(int fieldNumber, boolean value) Compute the number of bytes that would be needed to encode aboolfield, including tag.static intcomputeBytesSize(int fieldNumber, Buffer value) Compute the number of bytes that would be needed to encode abytesfield, including tag.static intcomputeDoubleSize(int fieldNumber, double value) Compute the number of bytes that would be needed to encode adoublefield, including tag.static intcomputeEnumSize(int fieldNumber, int value) Compute the number of bytes that would be needed to encode an enum field, including tag.static intcomputeFixed32Size(int fieldNumber, int value) Compute the number of bytes that would be needed to encode afixed32field, including tag.static intcomputeFixed64Size(int fieldNumber, long value) Compute the number of bytes that would be needed to encode afixed64field, including tag.static intcomputeFloatSize(int fieldNumber, float value) Compute the number of bytes that would be needed to encode afloatfield, including tag.static intcomputeInt32Size(int fieldNumber, int value) Compute the number of bytes that would be needed to encode anint32field, including tag.static intcomputeInt64Size(int fieldNumber, long value) Compute the number of bytes that would be needed to encode anint64field, including tag.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 intcomputeSFixed32Size(int fieldNumber, int value) Compute the number of bytes that would be needed to encode ansfixed32field, including tag.static intcomputeSFixed64Size(int fieldNumber, long value) Compute the number of bytes that would be needed to encode ansfixed64field, including tag.static intcomputeSInt32Size(int fieldNumber, int value) Compute the number of bytes that would be needed to encode ansint32field, including tag.static intcomputeSInt64Size(int fieldNumber, long value) Compute the number of bytes that would be needed to encode ansint64field, including tag.static intcomputeStringSize(int fieldNumber, String value) Compute the number of bytes that would be needed to encode astringfield, including tag.static intcomputeTagSize(int fieldNumber) Compute the number of bytes that would be needed to encode a tag.static intcomputeUInt32Size(int fieldNumber, int value) Compute the number of bytes that would be needed to encode auint32field, including tag.static intcomputeUInt64Size(int fieldNumber, long value) Compute the number of bytes that would be needed to encode auint64field, including tag.static intencodeZigZag32(int n) Encode a ZigZag-encoded 32-bit value.static longencodeZigZag64(long n) Encode a ZigZag-encoded 64-bit value.getNextBuffer(int size) voidwriteBool(int fieldNumber, boolean value) Write aboolfield, including tag, to the stream.voidwriteBytes(int fieldNumber, Buffer value) Write abytesfield, including tag, to the stream.voidwriteDouble(int fieldNumber, double value) Write adoublefield, including tag, to the stream.voidwriteEnum(int fieldNumber, int value) Write an enum field, including tag, to the stream.voidwriteFixed32(int fieldNumber, int value) Write afixed32field, including tag, to the stream.voidwriteFixed64(int fieldNumber, long value) Write afixed64field, including tag, to the stream.voidwriteFloat(int fieldNumber, float value) Write afloatfield, including tag, to the stream.voidwriteInt32(int fieldNumber, int value) Write anint32field, including tag, to the stream.voidwriteInt64(int fieldNumber, long value) Write anint64field, including tag, to the stream.voidwriteRawByte(byte value) Write a single byte.voidwriteRawByte(int value) Write a single byte, represented by an integer value.voidwriteRawBytes(byte[] value) Write an array of bytes.voidwriteRawBytes(byte[] value, int offset, int length) Write part of an array of bytes.voidwriteRawBytes(Buffer data) voidwriteRawLittleEndian32(int value) Write a little-endian 32-bit integer.voidwriteRawLittleEndian64(long value) Write a little-endian 64-bit integer.voidwriteRawVarint32(int value) Encode and write a varint.voidwriteRawVarint64(long value) Encode and write a varint.voidwriteSFixed32(int fieldNumber, int value) Write ansfixed32field, including tag, to the stream.voidwriteSFixed64(int fieldNumber, long value) Write ansfixed64field, including tag, to the stream.voidwriteSInt32(int fieldNumber, int value) Write ansint32field, including tag, to the stream.voidwriteSInt64(int fieldNumber, long value) Write ansint64field, including tag, to the stream.voidwriteString(int fieldNumber, String value) Write astringfield, including tag, to the stream.voidwriteTag(int fieldNumber, int wireType) Encode and write a tag.voidwriteUInt32(int fieldNumber, int value) Write auint32field, including tag, to the stream.voidwriteUInt64(int fieldNumber, long value) Write auint64field, including tag, to the stream.
-
Field Details
-
bos
-
LITTLE_ENDIAN_32_SIZE
public static final int LITTLE_ENDIAN_32_SIZE- See Also:
-
LITTLE_ENDIAN_64_SIZE
public static final int LITTLE_ENDIAN_64_SIZE- See Also:
-
-
Constructor Details
-
CodedOutputStream
-
CodedOutputStream
public CodedOutputStream(byte[] data) -
CodedOutputStream
-
-
Method Details
-
writeDouble
Write adoublefield, including tag, to the stream.- Throws:
IOException
-
writeFloat
Write afloatfield, including tag, to the stream.- Throws:
IOException
-
writeUInt64
Write auint64field, including tag, to the stream.- Throws:
IOException
-
writeInt64
Write anint64field, including tag, to the stream.- Throws:
IOException
-
writeInt32
Write anint32field, including tag, to the stream.- Throws:
IOException
-
writeFixed64
Write afixed64field, including tag, to the stream.- Throws:
IOException
-
writeFixed32
Write afixed32field, including tag, to the stream.- Throws:
IOException
-
writeBool
Write aboolfield, including tag, to the stream.- Throws:
IOException
-
writeString
Write astringfield, including tag, to the stream.- Throws:
IOException
-
writeBytes
Write abytesfield, including tag, to the stream.- Throws:
IOException
-
writeUInt32
Write auint32field, including tag, to the stream.- Throws:
IOException
-
writeEnum
Write an enum field, including tag, to the stream. Caller is responsible for converting the enum value to its numeric value.- Throws:
IOException
-
writeSFixed32
Write ansfixed32field, including tag, to the stream.- Throws:
IOException
-
writeSFixed64
Write ansfixed64field, including tag, to the stream.- Throws:
IOException
-
writeSInt32
Write ansint32field, including tag, to the stream.- Throws:
IOException
-
writeSInt64
Write ansint64field, including tag, to the stream.- Throws:
IOException
-
computeDoubleSize
public static int computeDoubleSize(int fieldNumber, double value) Compute the number of bytes that would be needed to encode adoublefield, including tag. -
computeFloatSize
public static int computeFloatSize(int fieldNumber, float value) Compute the number of bytes that would be needed to encode afloatfield, including tag. -
computeUInt64Size
public static int computeUInt64Size(int fieldNumber, long value) Compute the number of bytes that would be needed to encode auint64field, including tag. -
computeInt64Size
public static int computeInt64Size(int fieldNumber, long value) Compute the number of bytes that would be needed to encode anint64field, including tag. -
computeInt32Size
public static int computeInt32Size(int fieldNumber, int value) Compute the number of bytes that would be needed to encode anint32field, including tag. -
computeFixed64Size
public static int computeFixed64Size(int fieldNumber, long value) Compute the number of bytes that would be needed to encode afixed64field, including tag. -
computeFixed32Size
public static int computeFixed32Size(int fieldNumber, int value) Compute the number of bytes that would be needed to encode afixed32field, including tag. -
computeBoolSize
public static int computeBoolSize(int fieldNumber, boolean value) Compute the number of bytes that would be needed to encode aboolfield, including tag. -
computeStringSize
Compute the number of bytes that would be needed to encode astringfield, including tag. -
computeBytesSize
Compute the number of bytes that would be needed to encode abytesfield, including tag. -
computeUInt32Size
public static int computeUInt32Size(int fieldNumber, int value) Compute the number of bytes that would be needed to encode auint32field, including tag. -
computeEnumSize
public static int computeEnumSize(int fieldNumber, int value) Compute the number of bytes that would be needed to encode an enum field, including tag. Caller is responsible for converting the enum value to its numeric value. -
computeSFixed32Size
public static int computeSFixed32Size(int fieldNumber, int value) Compute the number of bytes that would be needed to encode ansfixed32field, including tag. -
computeSFixed64Size
public static int computeSFixed64Size(int fieldNumber, long value) Compute the number of bytes that would be needed to encode ansfixed64field, including tag. -
computeSInt32Size
public static int computeSInt32Size(int fieldNumber, int value) Compute the number of bytes that would be needed to encode ansint32field, including tag. -
computeSInt64Size
public static int computeSInt64Size(int fieldNumber, long value) Compute the number of bytes that would be needed to encode ansint64field, including tag. -
writeRawByte
-
writeRawByte
Write a single byte, represented by an integer value.- Throws:
IOException
-
writeRawBytes
Write an array of bytes.- Throws:
IOException
-
writeRawBytes
Write part of an array of bytes.- Throws:
IOException
-
writeRawBytes
- Throws:
IOException
-
writeTag
Encode and write a tag.- Throws:
IOException
-
computeTagSize
public static int computeTagSize(int fieldNumber) Compute the number of bytes that would be needed to encode a tag. -
writeRawVarint32
Encode and write a varint.valueis treated as unsigned, so it won't be sign-extended if negative.- Throws:
IOException
-
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. -
writeRawVarint64
Encode and write a varint.- Throws:
IOException
-
computeRawVarint64Size
public static int computeRawVarint64Size(long value) Compute the number of bytes that would be needed to encode a varint. -
writeRawLittleEndian32
Write a little-endian 32-bit integer.- Throws:
IOException
-
writeRawLittleEndian64
Write a little-endian 64-bit integer.- Throws:
IOException
-
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.
-
checkNoSpaceLeft
public void checkNoSpaceLeft() -
getNextBuffer
- Throws:
IOException
-