Package io.protostuff

Class 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).
    • Constructor Detail

      • ProtobufOutput

        public ProtobufOutput​(LinkedBuffer buffer)
      • ProtobufOutput

        public ProtobufOutput​(LinkedBuffer buffer,
                              int nextBufferSize)
    • Method Detail

      • writeInt32

        public void writeInt32​(int fieldNumber,
                               int value,
                               boolean repeated)
                        throws java.io.IOException
        Description copied from interface: Output
        Writes a variable int field.
        Specified by:
        writeInt32 in interface Output
        Throws:
        java.io.IOException
      • writeUInt32

        public void writeUInt32​(int fieldNumber,
                                int value,
                                boolean repeated)
                         throws java.io.IOException
        Description copied from interface: Output
        Writes an unsigned int field.
        Specified by:
        writeUInt32 in interface Output
        Throws:
        java.io.IOException
      • writeSInt32

        public void writeSInt32​(int fieldNumber,
                                int value,
                                boolean repeated)
                         throws java.io.IOException
        Description copied from interface: Output
        Writes a signed int field.
        Specified by:
        writeSInt32 in interface Output
        Throws:
        java.io.IOException
      • writeFixed32

        public void writeFixed32​(int fieldNumber,
                                 int value,
                                 boolean repeated)
                          throws java.io.IOException
        Description copied from interface: Output
        Writes a fixed int(4 bytes) field.
        Specified by:
        writeFixed32 in interface Output
        Throws:
        java.io.IOException
      • writeSFixed32

        public void writeSFixed32​(int fieldNumber,
                                  int value,
                                  boolean repeated)
                           throws java.io.IOException
        Description copied from interface: Output
        Writes a signed+fixed int(4 bytes) field.
        Specified by:
        writeSFixed32 in interface Output
        Throws:
        java.io.IOException
      • writeInt64

        public void writeInt64​(int fieldNumber,
                               long value,
                               boolean repeated)
                        throws java.io.IOException
        Description copied from interface: Output
        Writes a variable long field.
        Specified by:
        writeInt64 in interface Output
        Throws:
        java.io.IOException
      • writeUInt64

        public void writeUInt64​(int fieldNumber,
                                long value,
                                boolean repeated)
                         throws java.io.IOException
        Description copied from interface: Output
        Writes an unsigned long field.
        Specified by:
        writeUInt64 in interface Output
        Throws:
        java.io.IOException
      • writeSInt64

        public void writeSInt64​(int fieldNumber,
                                long value,
                                boolean repeated)
                         throws java.io.IOException
        Description copied from interface: Output
        Writes a signed long field.
        Specified by:
        writeSInt64 in interface Output
        Throws:
        java.io.IOException
      • writeFixed64

        public void writeFixed64​(int fieldNumber,
                                 long value,
                                 boolean repeated)
                          throws java.io.IOException
        Description copied from interface: Output
        Writes a fixed long(8 bytes) field.
        Specified by:
        writeFixed64 in interface Output
        Throws:
        java.io.IOException
      • writeSFixed64

        public void writeSFixed64​(int fieldNumber,
                                  long value,
                                  boolean repeated)
                           throws java.io.IOException
        Description copied from interface: Output
        Writes a signed+fixed long(8 bytes) field.
        Specified by:
        writeSFixed64 in interface Output
        Throws:
        java.io.IOException
      • writeFloat

        public void writeFloat​(int fieldNumber,
                               float value,
                               boolean repeated)
                        throws java.io.IOException
        Description copied from interface: Output
        Writes a float field.
        Specified by:
        writeFloat in interface Output
        Throws:
        java.io.IOException
      • writeDouble

        public void writeDouble​(int fieldNumber,
                                double value,
                                boolean repeated)
                         throws java.io.IOException
        Description copied from interface: Output
        Writes a double field.
        Specified by:
        writeDouble in interface Output
        Throws:
        java.io.IOException
      • writeBool

        public void writeBool​(int fieldNumber,
                              boolean value,
                              boolean repeated)
                       throws java.io.IOException
        Description copied from interface: Output
        Writes a boolean field.
        Specified by:
        writeBool in interface Output
        Throws:
        java.io.IOException
      • writeEnum

        public void writeEnum​(int fieldNumber,
                              int number,
                              boolean repeated)
                       throws java.io.IOException
        Description copied from interface: Output
        Writes a enum(its number) field.
        Specified by:
        writeEnum in interface Output
        Throws:
        java.io.IOException
      • writeString

        public void writeString​(int fieldNumber,
                                java.lang.CharSequence value,
                                boolean repeated)
                         throws java.io.IOException
        Description copied from interface: Output
        Writes a String field.
        Specified by:
        writeString in interface Output
        Throws:
        java.io.IOException
      • writeBytes

        public void writeBytes​(int fieldNumber,
                               ByteString value,
                               boolean repeated)
                        throws java.io.IOException
        Description copied from interface: Output
        Writes a ByteString(wraps byte array) field.
        Specified by:
        writeBytes in interface Output
        Throws:
        java.io.IOException
      • writeByteArray

        public void writeByteArray​(int fieldNumber,
                                   byte[] bytes,
                                   boolean repeated)
                            throws java.io.IOException
        Description copied from interface: Output
        Writes a byte array field.
        Specified by:
        writeByteArray in interface Output
        Throws:
        java.io.IOException
      • writeByteRange

        public void writeByteRange​(boolean utf8String,
                                   int fieldNumber,
                                   byte[] value,
                                   int offset,
                                   int length,
                                   boolean repeated)
                            throws java.io.IOException
        Description copied from interface: Output
        Writes a binary or a pre-encoded utf8 string.
        Specified by:
        writeByteRange in interface Output
        Throws:
        java.io.IOException
      • writeObject

        public <T> void writeObject​(int fieldNumber,
                                    T value,
                                    Schema<T> schema,
                                    boolean repeated)
                             throws java.io.IOException
        Description copied from interface: Output
        Writes an object(using its schema) field.
        Specified by:
        writeObject in interface Output
        Throws:
        java.io.IOException
      • 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.IOException
        Encode 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.IOException
        Encode and write a varint to the OutputStream
        Throws:
        java.io.IOException
      • writeRawVarInt32Bytes

        public static void writeRawVarInt32Bytes​(java.io.DataOutput out,
                                                 int value)
                                          throws java.io.IOException
        Encode and write a varint to the DataOutput
        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. value is 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.IOException
        Writes a ByteBuffer field.
        Specified by:
        writeBytes in interface Output
        Throws:
        java.io.IOException