Class CodedOutputStream

java.lang.Object
java.io.OutputStream
java.io.FilterOutputStream
org.apache.activemq.protobuf.CodedOutputStream
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public final class CodedOutputStream extends FilterOutputStream
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.

Author:
kneton@google.com Kenton Varda
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
     
    static final int
     

    Fields inherited from class java.io.FilterOutputStream

    out
  • Constructor Summary

    Constructors
    Constructor
    Description
    CodedOutputStream(byte[] data)
     
     
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
     
    static int
    computeBoolSize(int fieldNumber, boolean value)
    Compute the number of bytes that would be needed to encode a bool field, including tag.
    static int
    computeBytesSize(int fieldNumber, Buffer value)
    Compute the number of bytes that would be needed to encode a bytes field, including tag.
    static int
    computeDoubleSize(int fieldNumber, double value)
    Compute the number of bytes that would be needed to encode a double field, including tag.
    static int
    computeEnumSize(int fieldNumber, int value)
    Compute the number of bytes that would be needed to encode an enum field, including tag.
    static int
    computeFixed32Size(int fieldNumber, int value)
    Compute the number of bytes that would be needed to encode a fixed32 field, including tag.
    static int
    computeFixed64Size(int fieldNumber, long value)
    Compute the number of bytes that would be needed to encode a fixed64 field, including tag.
    static int
    computeFloatSize(int fieldNumber, float value)
    Compute the number of bytes that would be needed to encode a float field, including tag.
    static int
    computeInt32Size(int fieldNumber, int value)
    Compute the number of bytes that would be needed to encode an int32 field, including tag.
    static int
    computeInt64Size(int fieldNumber, long value)
    Compute the number of bytes that would be needed to encode an int64 field, including tag.
    static int
    Compute the number of bytes that would be needed to encode a varint.
    static int
    Compute the number of bytes that would be needed to encode a varint.
    static int
    computeSFixed32Size(int fieldNumber, int value)
    Compute the number of bytes that would be needed to encode an sfixed32 field, including tag.
    static int
    computeSFixed64Size(int fieldNumber, long value)
    Compute the number of bytes that would be needed to encode an sfixed64 field, including tag.
    static int
    computeSInt32Size(int fieldNumber, int value)
    Compute the number of bytes that would be needed to encode an sint32 field, including tag.
    static int
    computeSInt64Size(int fieldNumber, long value)
    Compute the number of bytes that would be needed to encode an sint64 field, including tag.
    static int
    computeStringSize(int fieldNumber, String value)
    Compute the number of bytes that would be needed to encode a string field, including tag.
    static int
    computeTagSize(int fieldNumber)
    Compute the number of bytes that would be needed to encode a tag.
    static int
    computeUInt32Size(int fieldNumber, int value)
    Compute the number of bytes that would be needed to encode a uint32 field, including tag.
    static int
    computeUInt64Size(int fieldNumber, long value)
    Compute the number of bytes that would be needed to encode a uint64 field, including tag.
    static int
    Encode a ZigZag-encoded 32-bit value.
    static long
    Encode a ZigZag-encoded 64-bit value.
    getNextBuffer(int size)
     
    void
    writeBool(int fieldNumber, boolean value)
    Write a bool field, including tag, to the stream.
    void
    writeBytes(int fieldNumber, Buffer value)
    Write a bytes field, including tag, to the stream.
    void
    writeDouble(int fieldNumber, double value)
    Write a double field, including tag, to the stream.
    void
    writeEnum(int fieldNumber, int value)
    Write an enum field, including tag, to the stream.
    void
    writeFixed32(int fieldNumber, int value)
    Write a fixed32 field, including tag, to the stream.
    void
    writeFixed64(int fieldNumber, long value)
    Write a fixed64 field, including tag, to the stream.
    void
    writeFloat(int fieldNumber, float value)
    Write a float field, including tag, to the stream.
    void
    writeInt32(int fieldNumber, int value)
    Write an int32 field, including tag, to the stream.
    void
    writeInt64(int fieldNumber, long value)
    Write an int64 field, including tag, to the stream.
    void
    writeRawByte(byte value)
    Write a single byte.
    void
    writeRawByte(int value)
    Write a single byte, represented by an integer value.
    void
    writeRawBytes(byte[] value)
    Write an array of bytes.
    void
    writeRawBytes(byte[] value, int offset, int length)
    Write part of an array of bytes.
    void
     
    void
    Write a little-endian 32-bit integer.
    void
    Write a little-endian 64-bit integer.
    void
    writeRawVarint32(int value)
    Encode and write a varint.
    void
    writeRawVarint64(long value)
    Encode and write a varint.
    void
    writeSFixed32(int fieldNumber, int value)
    Write an sfixed32 field, including tag, to the stream.
    void
    writeSFixed64(int fieldNumber, long value)
    Write an sfixed64 field, including tag, to the stream.
    void
    writeSInt32(int fieldNumber, int value)
    Write an sint32 field, including tag, to the stream.
    void
    writeSInt64(int fieldNumber, long value)
    Write an sint64 field, including tag, to the stream.
    void
    writeString(int fieldNumber, String value)
    Write a string field, including tag, to the stream.
    void
    writeTag(int fieldNumber, int wireType)
    Encode and write a tag.
    void
    writeUInt32(int fieldNumber, int value)
    Write a uint32 field, including tag, to the stream.
    void
    writeUInt64(int fieldNumber, long value)
    Write a uint64 field, including tag, to the stream.

    Methods inherited from class java.io.FilterOutputStream

    close, flush, write, write, write

    Methods inherited from class java.io.OutputStream

    nullOutputStream

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • CodedOutputStream

      public CodedOutputStream(OutputStream os)
    • CodedOutputStream

      public CodedOutputStream(byte[] data)
    • CodedOutputStream

      public CodedOutputStream(Buffer data)
  • Method Details

    • writeDouble

      public void writeDouble(int fieldNumber, double value) throws IOException
      Write a double field, including tag, to the stream.
      Throws:
      IOException
    • writeFloat

      public void writeFloat(int fieldNumber, float value) throws IOException
      Write a float field, including tag, to the stream.
      Throws:
      IOException
    • writeUInt64

      public void writeUInt64(int fieldNumber, long value) throws IOException
      Write a uint64 field, including tag, to the stream.
      Throws:
      IOException
    • writeInt64

      public void writeInt64(int fieldNumber, long value) throws IOException
      Write an int64 field, including tag, to the stream.
      Throws:
      IOException
    • writeInt32

      public void writeInt32(int fieldNumber, int value) throws IOException
      Write an int32 field, including tag, to the stream.
      Throws:
      IOException
    • writeFixed64

      public void writeFixed64(int fieldNumber, long value) throws IOException
      Write a fixed64 field, including tag, to the stream.
      Throws:
      IOException
    • writeFixed32

      public void writeFixed32(int fieldNumber, int value) throws IOException
      Write a fixed32 field, including tag, to the stream.
      Throws:
      IOException
    • writeBool

      public void writeBool(int fieldNumber, boolean value) throws IOException
      Write a bool field, including tag, to the stream.
      Throws:
      IOException
    • writeString

      public void writeString(int fieldNumber, String value) throws IOException
      Write a string field, including tag, to the stream.
      Throws:
      IOException
    • writeBytes

      public void writeBytes(int fieldNumber, Buffer value) throws IOException
      Write a bytes field, including tag, to the stream.
      Throws:
      IOException
    • writeUInt32

      public void writeUInt32(int fieldNumber, int value) throws IOException
      Write a uint32 field, including tag, to the stream.
      Throws:
      IOException
    • writeEnum

      public void writeEnum(int fieldNumber, int value) throws IOException
      Write an enum field, including tag, to the stream. Caller is responsible for converting the enum value to its numeric value.
      Throws:
      IOException
    • writeSFixed32

      public void writeSFixed32(int fieldNumber, int value) throws IOException
      Write an sfixed32 field, including tag, to the stream.
      Throws:
      IOException
    • writeSFixed64

      public void writeSFixed64(int fieldNumber, long value) throws IOException
      Write an sfixed64 field, including tag, to the stream.
      Throws:
      IOException
    • writeSInt32

      public void writeSInt32(int fieldNumber, int value) throws IOException
      Write an sint32 field, including tag, to the stream.
      Throws:
      IOException
    • writeSInt64

      public void writeSInt64(int fieldNumber, long value) throws IOException
      Write an sint64 field, 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 a double field, including tag.
    • computeFloatSize

      public static int computeFloatSize(int fieldNumber, float value)
      Compute the number of bytes that would be needed to encode a float field, including tag.
    • computeUInt64Size

      public static int computeUInt64Size(int fieldNumber, long value)
      Compute the number of bytes that would be needed to encode a uint64 field, including tag.
    • computeInt64Size

      public static int computeInt64Size(int fieldNumber, long value)
      Compute the number of bytes that would be needed to encode an int64 field, including tag.
    • computeInt32Size

      public static int computeInt32Size(int fieldNumber, int value)
      Compute the number of bytes that would be needed to encode an int32 field, including tag.
    • computeFixed64Size

      public static int computeFixed64Size(int fieldNumber, long value)
      Compute the number of bytes that would be needed to encode a fixed64 field, including tag.
    • computeFixed32Size

      public static int computeFixed32Size(int fieldNumber, int value)
      Compute the number of bytes that would be needed to encode a fixed32 field, including tag.
    • computeBoolSize

      public static int computeBoolSize(int fieldNumber, boolean value)
      Compute the number of bytes that would be needed to encode a bool field, including tag.
    • computeStringSize

      public static int computeStringSize(int fieldNumber, String value)
      Compute the number of bytes that would be needed to encode a string field, including tag.
    • computeBytesSize

      public static int computeBytesSize(int fieldNumber, Buffer value)
      Compute the number of bytes that would be needed to encode a bytes field, including tag.
    • computeUInt32Size

      public static int computeUInt32Size(int fieldNumber, int value)
      Compute the number of bytes that would be needed to encode a uint32 field, 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 an sfixed32 field, including tag.
    • computeSFixed64Size

      public static int computeSFixed64Size(int fieldNumber, long value)
      Compute the number of bytes that would be needed to encode an sfixed64 field, including tag.
    • computeSInt32Size

      public static int computeSInt32Size(int fieldNumber, int value)
      Compute the number of bytes that would be needed to encode an sint32 field, including tag.
    • computeSInt64Size

      public static int computeSInt64Size(int fieldNumber, long value)
      Compute the number of bytes that would be needed to encode an sint64 field, including tag.
    • writeRawByte

      public void writeRawByte(byte value) throws IOException
      Write a single byte.
      Throws:
      IOException
    • writeRawByte

      public void writeRawByte(int value) throws IOException
      Write a single byte, represented by an integer value.
      Throws:
      IOException
    • writeRawBytes

      public void writeRawBytes(byte[] value) throws IOException
      Write an array of bytes.
      Throws:
      IOException
    • writeRawBytes

      public void writeRawBytes(byte[] value, int offset, int length) throws IOException
      Write part of an array of bytes.
      Throws:
      IOException
    • writeRawBytes

      public void writeRawBytes(Buffer data) throws IOException
      Throws:
      IOException
    • writeTag

      public void writeTag(int fieldNumber, int wireType) throws IOException
      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

      public void writeRawVarint32(int value) throws IOException
      Encode and write a varint. value is 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. value is treated as unsigned, so it won't be sign-extended if negative.
    • writeRawVarint64

      public void writeRawVarint64(long value) throws IOException
      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

      public void writeRawLittleEndian32(int value) throws IOException
      Write a little-endian 32-bit integer.
      Throws:
      IOException
    • writeRawLittleEndian64

      public void writeRawLittleEndian64(long value) throws IOException
      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

      public Buffer getNextBuffer(int size) throws IOException
      Throws:
      IOException