Class BinaryWriter

    • Constructor Detail

      • BinaryWriter

        private BinaryWriter​(BufferAllocator alloc,
                             int chunkSize)
        Only allow subclassing for inner classes.
    • Method Detail

      • newDirectInstance

        public static BinaryWriter newDirectInstance​(BufferAllocator alloc,
                                                     int chunkSize)
        Creates a new BinaryWriter that will allocate direct (i.e. non-heap) buffers of chunkSize as necessary.
      • isUnsafeHeapSupported

        static boolean isUnsafeHeapSupported()
      • isUnsafeDirectSupported

        static boolean isUnsafeDirectSupported()
      • complete

        public final java.util.Queue<AllocatedBuffer> complete()
        Completes the write operation and returns a queue of AllocatedBuffer objects in forward-writing order. This method should only be called once.

        After calling this method, the writer can not be reused. Create a new writer for future writes.

      • writeSFixed32

        public final void writeSFixed32​(int fieldNumber,
                                        int value)
                                 throws java.io.IOException
        Description copied from interface: Writer
        Writes a field of type FieldType.SFIXED32.
        Specified by:
        writeSFixed32 in interface Writer
        Throws:
        java.io.IOException
      • writeInt64

        public final void writeInt64​(int fieldNumber,
                                     long value)
                              throws java.io.IOException
        Description copied from interface: Writer
        Writes a field of type FieldType.INT64.
        Specified by:
        writeInt64 in interface Writer
        Throws:
        java.io.IOException
      • writeSFixed64

        public final void writeSFixed64​(int fieldNumber,
                                        long value)
                                 throws java.io.IOException
        Description copied from interface: Writer
        Writes a field of type FieldType.SFIXED64.
        Specified by:
        writeSFixed64 in interface Writer
        Throws:
        java.io.IOException
      • writeFloat

        public final void writeFloat​(int fieldNumber,
                                     float value)
                              throws java.io.IOException
        Description copied from interface: Writer
        Writes a field of type FieldType.FLOAT.
        Specified by:
        writeFloat in interface Writer
        Throws:
        java.io.IOException
      • writeDouble

        public final void writeDouble​(int fieldNumber,
                                      double value)
                               throws java.io.IOException
        Description copied from interface: Writer
        Writes a field of type FieldType.DOUBLE.
        Specified by:
        writeDouble in interface Writer
        Throws:
        java.io.IOException
      • writeEnum

        public final void writeEnum​(int fieldNumber,
                                    int value)
                             throws java.io.IOException
        Description copied from interface: Writer
        Writes a field of type FieldType.ENUM.
        Specified by:
        writeEnum in interface Writer
        Throws:
        java.io.IOException
      • writeInt32List

        public final void writeInt32List​(int fieldNumber,
                                         java.util.List<java.lang.Integer> list,
                                         boolean packed)
                                  throws java.io.IOException
        Description copied from interface: Writer
        Writes a list field of type FieldType.INT32.
        Specified by:
        writeInt32List in interface Writer
        Throws:
        java.io.IOException
      • writeInt32List_Internal

        private final void writeInt32List_Internal​(int fieldNumber,
                                                   java.util.List<java.lang.Integer> list,
                                                   boolean packed)
                                            throws java.io.IOException
        Throws:
        java.io.IOException
      • writeInt32List_Internal

        private final void writeInt32List_Internal​(int fieldNumber,
                                                   IntArrayList list,
                                                   boolean packed)
                                            throws java.io.IOException
        Throws:
        java.io.IOException
      • writeFixed32List

        public final void writeFixed32List​(int fieldNumber,
                                           java.util.List<java.lang.Integer> list,
                                           boolean packed)
                                    throws java.io.IOException
        Description copied from interface: Writer
        Writes a list field of type FieldType.FIXED32.
        Specified by:
        writeFixed32List in interface Writer
        Throws:
        java.io.IOException
      • writeFixed32List_Internal

        private final void writeFixed32List_Internal​(int fieldNumber,
                                                     java.util.List<java.lang.Integer> list,
                                                     boolean packed)
                                              throws java.io.IOException
        Throws:
        java.io.IOException
      • writeFixed32List_Internal

        private final void writeFixed32List_Internal​(int fieldNumber,
                                                     IntArrayList list,
                                                     boolean packed)
                                              throws java.io.IOException
        Throws:
        java.io.IOException
      • writeInt64List

        public final void writeInt64List​(int fieldNumber,
                                         java.util.List<java.lang.Long> list,
                                         boolean packed)
                                  throws java.io.IOException
        Description copied from interface: Writer
        Writes a list field of type FieldType.INT64.
        Specified by:
        writeInt64List in interface Writer
        Throws:
        java.io.IOException
      • writeUInt64List

        public final void writeUInt64List​(int fieldNumber,
                                          java.util.List<java.lang.Long> list,
                                          boolean packed)
                                   throws java.io.IOException
        Description copied from interface: Writer
        Writes a list field of type FieldType.UINT64.
        Specified by:
        writeUInt64List in interface Writer
        Throws:
        java.io.IOException
      • writeUInt64List_Internal

        private final void writeUInt64List_Internal​(int fieldNumber,
                                                    java.util.List<java.lang.Long> list,
                                                    boolean packed)
                                             throws java.io.IOException
        Throws:
        java.io.IOException
      • writeUInt64List_Internal

        private final void writeUInt64List_Internal​(int fieldNumber,
                                                    LongArrayList list,
                                                    boolean packed)
                                             throws java.io.IOException
        Throws:
        java.io.IOException
      • writeFixed64List

        public final void writeFixed64List​(int fieldNumber,
                                           java.util.List<java.lang.Long> list,
                                           boolean packed)
                                    throws java.io.IOException
        Description copied from interface: Writer
        Writes a list field of type FieldType.FIXED64.
        Specified by:
        writeFixed64List in interface Writer
        Throws:
        java.io.IOException
      • writeFixed64List_Internal

        private final void writeFixed64List_Internal​(int fieldNumber,
                                                     java.util.List<java.lang.Long> list,
                                                     boolean packed)
                                              throws java.io.IOException
        Throws:
        java.io.IOException
      • writeFixed64List_Internal

        private final void writeFixed64List_Internal​(int fieldNumber,
                                                     LongArrayList list,
                                                     boolean packed)
                                              throws java.io.IOException
        Throws:
        java.io.IOException
      • writeFloatList

        public final void writeFloatList​(int fieldNumber,
                                         java.util.List<java.lang.Float> list,
                                         boolean packed)
                                  throws java.io.IOException
        Description copied from interface: Writer
        Writes a list field of type FieldType.FLOAT.
        Specified by:
        writeFloatList in interface Writer
        Throws:
        java.io.IOException
      • writeFloatList_Internal

        private final void writeFloatList_Internal​(int fieldNumber,
                                                   java.util.List<java.lang.Float> list,
                                                   boolean packed)
                                            throws java.io.IOException
        Throws:
        java.io.IOException
      • writeFloatList_Internal

        private final void writeFloatList_Internal​(int fieldNumber,
                                                   FloatArrayList list,
                                                   boolean packed)
                                            throws java.io.IOException
        Throws:
        java.io.IOException
      • writeDoubleList

        public final void writeDoubleList​(int fieldNumber,
                                          java.util.List<java.lang.Double> list,
                                          boolean packed)
                                   throws java.io.IOException
        Description copied from interface: Writer
        Writes a list field of type FieldType.DOUBLE.
        Specified by:
        writeDoubleList in interface Writer
        Throws:
        java.io.IOException
      • writeDoubleList_Internal

        private final void writeDoubleList_Internal​(int fieldNumber,
                                                    java.util.List<java.lang.Double> list,
                                                    boolean packed)
                                             throws java.io.IOException
        Throws:
        java.io.IOException
      • writeDoubleList_Internal

        private final void writeDoubleList_Internal​(int fieldNumber,
                                                    DoubleArrayList list,
                                                    boolean packed)
                                             throws java.io.IOException
        Throws:
        java.io.IOException
      • writeEnumList

        public final void writeEnumList​(int fieldNumber,
                                        java.util.List<java.lang.Integer> list,
                                        boolean packed)
                                 throws java.io.IOException
        Description copied from interface: Writer
        Writes a list field of type FieldType.ENUM.
        Specified by:
        writeEnumList in interface Writer
        Throws:
        java.io.IOException
      • writeBoolList

        public final void writeBoolList​(int fieldNumber,
                                        java.util.List<java.lang.Boolean> list,
                                        boolean packed)
                                 throws java.io.IOException
        Description copied from interface: Writer
        Writes a list field of type FieldType.BOOL.
        Specified by:
        writeBoolList in interface Writer
        Throws:
        java.io.IOException
      • writeBoolList_Internal

        private final void writeBoolList_Internal​(int fieldNumber,
                                                  java.util.List<java.lang.Boolean> list,
                                                  boolean packed)
                                           throws java.io.IOException
        Throws:
        java.io.IOException
      • writeBoolList_Internal

        private final void writeBoolList_Internal​(int fieldNumber,
                                                  BooleanArrayList list,
                                                  boolean packed)
                                           throws java.io.IOException
        Throws:
        java.io.IOException
      • writeStringList

        public final void writeStringList​(int fieldNumber,
                                          java.util.List<java.lang.String> list)
                                   throws java.io.IOException
        Description copied from interface: Writer
        Writes a list field of type FieldType.STRING.
        Specified by:
        writeStringList in interface Writer
        Throws:
        java.io.IOException
      • writeLazyString

        private void writeLazyString​(int fieldNumber,
                                     java.lang.Object value)
                              throws java.io.IOException
        Throws:
        java.io.IOException
      • writeBytesList

        public final void writeBytesList​(int fieldNumber,
                                         java.util.List<ByteString> list)
                                  throws java.io.IOException
        Description copied from interface: Writer
        Writes a list field of type FieldType.BYTES.
        Specified by:
        writeBytesList in interface Writer
        Throws:
        java.io.IOException
      • writeUInt32List

        public final void writeUInt32List​(int fieldNumber,
                                          java.util.List<java.lang.Integer> list,
                                          boolean packed)
                                   throws java.io.IOException
        Description copied from interface: Writer
        Writes a list field of type FieldType.UINT32.
        Specified by:
        writeUInt32List in interface Writer
        Throws:
        java.io.IOException
      • writeUInt32List_Internal

        private final void writeUInt32List_Internal​(int fieldNumber,
                                                    java.util.List<java.lang.Integer> list,
                                                    boolean packed)
                                             throws java.io.IOException
        Throws:
        java.io.IOException
      • writeUInt32List_Internal

        private final void writeUInt32List_Internal​(int fieldNumber,
                                                    IntArrayList list,
                                                    boolean packed)
                                             throws java.io.IOException
        Throws:
        java.io.IOException
      • writeSFixed32List

        public final void writeSFixed32List​(int fieldNumber,
                                            java.util.List<java.lang.Integer> list,
                                            boolean packed)
                                     throws java.io.IOException
        Description copied from interface: Writer
        Writes a list field of type FieldType.SFIXED32.
        Specified by:
        writeSFixed32List in interface Writer
        Throws:
        java.io.IOException
      • writeSFixed64List

        public final void writeSFixed64List​(int fieldNumber,
                                            java.util.List<java.lang.Long> list,
                                            boolean packed)
                                     throws java.io.IOException
        Description copied from interface: Writer
        Writes a list field of type FieldType.SFIXED64.
        Specified by:
        writeSFixed64List in interface Writer
        Throws:
        java.io.IOException
      • writeSInt32List

        public final void writeSInt32List​(int fieldNumber,
                                          java.util.List<java.lang.Integer> list,
                                          boolean packed)
                                   throws java.io.IOException
        Description copied from interface: Writer
        Writes a list field of type FieldType.SINT32.
        Specified by:
        writeSInt32List in interface Writer
        Throws:
        java.io.IOException
      • writeSInt32List_Internal

        private final void writeSInt32List_Internal​(int fieldNumber,
                                                    java.util.List<java.lang.Integer> list,
                                                    boolean packed)
                                             throws java.io.IOException
        Throws:
        java.io.IOException
      • writeSInt32List_Internal

        private final void writeSInt32List_Internal​(int fieldNumber,
                                                    IntArrayList list,
                                                    boolean packed)
                                             throws java.io.IOException
        Throws:
        java.io.IOException
      • writeSInt64List

        public final void writeSInt64List​(int fieldNumber,
                                          java.util.List<java.lang.Long> list,
                                          boolean packed)
                                   throws java.io.IOException
        Description copied from interface: Writer
        Writes a list field of type FieldType.SINT64.
        Specified by:
        writeSInt64List in interface Writer
        Throws:
        java.io.IOException
      • writeMap

        public <K,​V> void writeMap​(int fieldNumber,
                                         MapEntryLite.Metadata<K,​V> metadata,
                                         java.util.Map<K,​V> map)
                                  throws java.io.IOException
        Description copied from interface: Writer
        Writes a map field.
        Specified by:
        writeMap in interface Writer
        Throws:
        java.io.IOException
      • writeMapEntryField

        static final void writeMapEntryField​(Writer writer,
                                             int fieldNumber,
                                             WireFormat.FieldType fieldType,
                                             java.lang.Object object)
                                      throws java.io.IOException
        Throws:
        java.io.IOException
      • writeSInt64List_Internal

        private final void writeSInt64List_Internal​(int fieldNumber,
                                                    java.util.List<java.lang.Long> list,
                                                    boolean packed)
                                             throws java.io.IOException
        Throws:
        java.io.IOException
      • writeSInt64List_Internal

        private final void writeSInt64List_Internal​(int fieldNumber,
                                                    LongArrayList list,
                                                    boolean packed)
                                             throws java.io.IOException
        Throws:
        java.io.IOException
      • writeMessageList

        public final void writeMessageList​(int fieldNumber,
                                           java.util.List<?> list)
                                    throws java.io.IOException
        Description copied from interface: Writer
        Writes a list field of type FieldType.MESSAGE.
        Specified by:
        writeMessageList in interface Writer
        Throws:
        java.io.IOException
      • writeMessageList

        public final void writeMessageList​(int fieldNumber,
                                           java.util.List<?> list,
                                           Schema schema)
                                    throws java.io.IOException
        Description copied from interface: Writer
        Writes a list field of type FieldType.MESSAGE.
        Specified by:
        writeMessageList in interface Writer
        Throws:
        java.io.IOException
      • writeGroupList

        public final void writeGroupList​(int fieldNumber,
                                         java.util.List<?> list)
                                  throws java.io.IOException
        Description copied from interface: Writer
        Writes a list field of type FieldType.GROUP.
        Specified by:
        writeGroupList in interface Writer
        Throws:
        java.io.IOException
      • writeGroupList

        public final void writeGroupList​(int fieldNumber,
                                         java.util.List<?> list,
                                         Schema schema)
                                  throws java.io.IOException
        Description copied from interface: Writer
        Writes a list field of type FieldType.GROUP.
        Specified by:
        writeGroupList in interface Writer
        Throws:
        java.io.IOException
      • writeMessageSetItem

        public final void writeMessageSetItem​(int fieldNumber,
                                              java.lang.Object value)
                                       throws java.io.IOException
        Description copied from interface: Writer
        Writes a message field in MessageSet wire-format.
        Specified by:
        writeMessageSetItem in interface Writer
        value - A message instance or an opaque ByteString for an unknown field.
        Throws:
        java.io.IOException
      • newDirectBuffer

        final AllocatedBuffer newDirectBuffer​(int capacity)
      • getTotalBytesWritten

        public abstract int getTotalBytesWritten()
        Gets the total number of bytes that have been written. This will not be reset by a call to complete().
      • requireSpace

        abstract void requireSpace​(int size)
      • finishCurrentBuffer

        abstract void finishCurrentBuffer()
      • writeTag

        abstract void writeTag​(int fieldNumber,
                               int wireType)
      • writeVarint32

        abstract void writeVarint32​(int value)
      • writeInt32

        abstract void writeInt32​(int value)
      • writeSInt32

        abstract void writeSInt32​(int value)
      • writeFixed32

        abstract void writeFixed32​(int value)
      • writeVarint64

        abstract void writeVarint64​(long value)
      • writeSInt64

        abstract void writeSInt64​(long value)
      • writeFixed64

        abstract void writeFixed64​(long value)
      • writeBool

        abstract void writeBool​(boolean value)
      • writeString

        abstract void writeString​(java.lang.String in)
      • computeUInt64SizeNoTag

        private static byte computeUInt64SizeNoTag​(long value)
        Not using the version in CodedOutputStream due to the fact that benchmarks have shown a performance improvement when returning a byte (rather than an int).