com.dyuproject.protostuff
Class ProtobufOutput

java.lang.Object
  extended by com.dyuproject.protostuff.WriteSession
      extended by com.dyuproject.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).

Author:
David Yu
Date created:
May 18, 2010

Field Summary
static int LITTLE_ENDIAN_32_SIZE
           
static int LITTLE_ENDIAN_64_SIZE
           
 
Fields inherited from class com.dyuproject.protostuff.WriteSession
head, nextBufferSize, out, sink, size, tail
 
Constructor Summary
ProtobufOutput(LinkedBuffer buffer)
           
ProtobufOutput(LinkedBuffer buffer, int nextBufferSize)
           
 
Method Summary
 ProtobufOutput clear()
          Resets this output for re-use.
static int computeRawVarint32Size(int value)
          Compute the number of bytes that would be needed to encode a varint.
static int computeRawVarint64Size(long value)
          Compute the number of bytes that would be needed to encode a varint.
static int encodeZigZag32(int n)
          Encode a ZigZag-encoded 32-bit value.
static long encodeZigZag64(long n)
          Encode a ZigZag-encoded 64-bit value.
static byte[] getRawVarInt32Bytes(int value)
          Returns the byte array computed from the var int 32 size
static byte[] getTagAndRawLittleEndian32Bytes(int tag, int value)
          Returns a byte array encoded with the tag and little endian 32
static byte[] getTagAndRawLittleEndian64Bytes(int tag, long value)
          Returns a byte array encoded with the tag and little endian 64
static byte[] getTagAndRawVarInt32Bytes(int tag, int value)
          Returns a byte array encoded with the tag and var int 32
static byte[] getTagAndRawVarInt64Bytes(int tag, long value)
          Returns a byte array encoded with the tag and var int 64
 void writeBool(int fieldNumber, boolean value, boolean repeated)
           
 void writeByteArray(int fieldNumber, byte[] bytes, boolean repeated)
           
 void writeByteRange(boolean utf8String, int fieldNumber, byte[] value, int offset, int length, boolean repeated)
           
 void writeBytes(int fieldNumber, ByteString value, boolean repeated)
           
 void writeDouble(int fieldNumber, double value, boolean repeated)
           
 void writeEnum(int fieldNumber, int number, boolean repeated)
           
 void writeFixed32(int fieldNumber, int value, boolean repeated)
           
 void writeFixed64(int fieldNumber, long value, boolean repeated)
           
 void writeFloat(int fieldNumber, float value, boolean repeated)
           
 void writeInt32(int fieldNumber, int value, boolean repeated)
           
 void writeInt64(int fieldNumber, long value, boolean repeated)
           
<T> void
writeObject(int fieldNumber, T value, Schema<T> schema, boolean repeated)
           
static int writeRawLittleEndian32(int value, byte[] buffer, int offset)
          Writes the encoded little endian 32 and returns the bytes written
static int writeRawLittleEndian64(long value, byte[] buffer, int offset)
          Writes the encoded little endian 64 and returns the bytes written
static void writeRawVarInt32(int value, byte[] buf, int offset)
          Encode and write a varint to the byte array
static LinkedBuffer writeRawVarInt32(int value, WriteSession session, LinkedBuffer lb)
          Returns the buffer encoded with the variable int 32.
static void writeRawVarInt32Bytes(java.io.DataOutput out, int value)
          Encode and write a varint to the DataOutput
static void writeRawVarInt32Bytes(java.io.OutputStream out, int value)
          Encode and write a varint to the OutputStream
 void writeSFixed32(int fieldNumber, int value, boolean repeated)
           
 void writeSFixed64(int fieldNumber, long value, boolean repeated)
           
 void writeSInt32(int fieldNumber, int value, boolean repeated)
           
 void writeSInt64(int fieldNumber, long value, boolean repeated)
           
 void writeString(int fieldNumber, java.lang.String value, boolean repeated)
           
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
static LinkedBuffer writeTagAndLinkedBuffer(int tag, LinkedBuffer buffer, WriteSession session, LinkedBuffer lb)
          Returns the buffer encoded with the tag and LinkedBuffer (zero-copy)
static LinkedBuffer writeTagAndRawLittleEndian32(int tag, int value, WriteSession session, LinkedBuffer lb)
          Returns the buffer encoded with the tag and little endian 32
static LinkedBuffer writeTagAndRawLittleEndian64(int tag, long value, WriteSession session, LinkedBuffer lb)
          Returns the buffer encoded with the tag and little endian 64
static LinkedBuffer writeTagAndRawVarInt32(int tag, int value, WriteSession session, LinkedBuffer lb)
          Returns the buffer encoded with the tag and var int 32
static LinkedBuffer writeTagAndRawVarInt64(int tag, long value, WriteSession session, LinkedBuffer lb)
          Returns the buffer encoded with the tag and var int 64
 void writeUInt32(int fieldNumber, int value, boolean repeated)
           
 void writeUInt64(int fieldNumber, long value, boolean repeated)
           
 
Methods inherited from class com.dyuproject.protostuff.WriteSession
getSize, toByteArray
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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:
clear in class WriteSession

writeInt32

public void writeInt32(int fieldNumber,
                       int value,
                       boolean repeated)
                throws java.io.IOException
Specified by:
writeInt32 in interface Output
Throws:
java.io.IOException

writeUInt32

public void writeUInt32(int fieldNumber,
                        int value,
                        boolean repeated)
                 throws java.io.IOException
Specified by:
writeUInt32 in interface Output
Throws:
java.io.IOException

writeSInt32

public void writeSInt32(int fieldNumber,
                        int value,
                        boolean repeated)
                 throws java.io.IOException
Specified by:
writeSInt32 in interface Output
Throws:
java.io.IOException

writeFixed32

public void writeFixed32(int fieldNumber,
                         int value,
                         boolean repeated)
                  throws java.io.IOException
Specified by:
writeFixed32 in interface Output
Throws:
java.io.IOException

writeSFixed32

public void writeSFixed32(int fieldNumber,
                          int value,
                          boolean repeated)
                   throws java.io.IOException
Specified by:
writeSFixed32 in interface Output
Throws:
java.io.IOException

writeInt64

public void writeInt64(int fieldNumber,
                       long value,
                       boolean repeated)
                throws java.io.IOException
Specified by:
writeInt64 in interface Output
Throws:
java.io.IOException

writeUInt64

public void writeUInt64(int fieldNumber,
                        long value,
                        boolean repeated)
                 throws java.io.IOException
Specified by:
writeUInt64 in interface Output
Throws:
java.io.IOException

writeSInt64

public void writeSInt64(int fieldNumber,
                        long value,
                        boolean repeated)
                 throws java.io.IOException
Specified by:
writeSInt64 in interface Output
Throws:
java.io.IOException

writeFixed64

public void writeFixed64(int fieldNumber,
                         long value,
                         boolean repeated)
                  throws java.io.IOException
Specified by:
writeFixed64 in interface Output
Throws:
java.io.IOException

writeSFixed64

public void writeSFixed64(int fieldNumber,
                          long value,
                          boolean repeated)
                   throws java.io.IOException
Specified by:
writeSFixed64 in interface Output
Throws:
java.io.IOException

writeFloat

public void writeFloat(int fieldNumber,
                       float value,
                       boolean repeated)
                throws java.io.IOException
Specified by:
writeFloat in interface Output
Throws:
java.io.IOException

writeDouble

public void writeDouble(int fieldNumber,
                        double value,
                        boolean repeated)
                 throws java.io.IOException
Specified by:
writeDouble in interface Output
Throws:
java.io.IOException

writeBool

public void writeBool(int fieldNumber,
                      boolean value,
                      boolean repeated)
               throws java.io.IOException
Specified by:
writeBool in interface Output
Throws:
java.io.IOException

writeEnum

public void writeEnum(int fieldNumber,
                      int number,
                      boolean repeated)
               throws java.io.IOException
Specified by:
writeEnum in interface Output
Throws:
java.io.IOException

writeString

public void writeString(int fieldNumber,
                        java.lang.String value,
                        boolean repeated)
                 throws java.io.IOException
Specified by:
writeString in interface Output
Throws:
java.io.IOException

writeBytes

public void writeBytes(int fieldNumber,
                       ByteString value,
                       boolean repeated)
                throws java.io.IOException
Specified by:
writeBytes in interface Output
Throws:
java.io.IOException

writeByteArray

public void writeByteArray(int fieldNumber,
                           byte[] bytes,
                           boolean repeated)
                    throws java.io.IOException
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
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
Specified by:
writeObject in interface Output
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.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.


Copyright © 2009-2014. All Rights Reserved.