Class BinaryEncoder

java.lang.Object
org.apache.avro.io.Encoder
org.apache.avro.io.BinaryEncoder
All Implemented Interfaces:
Flushable
Direct Known Subclasses:
BufferedBinaryEncoder, DirectBinaryEncoder

public abstract class BinaryEncoder extends Encoder
An abstract Encoder for Avro's binary encoding.

To construct and configure instances, use EncoderFactory

See Also:
  • Constructor Details

    • BinaryEncoder

      public BinaryEncoder()
  • Method Details

    • writeNull

      public void writeNull() throws IOException
      Description copied from class: Encoder
      "Writes" a null value. (Doesn't actually write anything, but advances the state of the parser if this class is stateful.)
      Specified by:
      writeNull in class Encoder
      Throws:
      IOException
    • writeString

      public void writeString(Utf8 utf8) throws IOException
      Description copied from class: Encoder
      Write a Unicode character string.
      Specified by:
      writeString in class Encoder
      Throws:
      IOException
    • writeString

      public void writeString(String string) throws IOException
      Description copied from class: Encoder
      Write a Unicode character string. The default implementation converts the String to a Utf8. Some Encoder implementations may want to do something different as a performance optimization.
      Overrides:
      writeString in class Encoder
      Throws:
      IOException
    • writeBytes

      public void writeBytes(ByteBuffer bytes) throws IOException
      Description copied from class: Encoder
      Write a byte string.
      Specified by:
      writeBytes in class Encoder
      Throws:
      IOException
    • writeBytes

      public void writeBytes(byte[] bytes, int start, int len) throws IOException
      Description copied from class: Encoder
      Write a byte string.
      Specified by:
      writeBytes in class Encoder
      Throws:
      IOException
    • writeEnum

      public void writeEnum(int e) throws IOException
      Description copied from class: Encoder
      Writes an enumeration.
      Specified by:
      writeEnum in class Encoder
      Parameters:
      e -
      Throws:
      IOException
    • writeArrayStart

      public void writeArrayStart() throws IOException
      Description copied from class: Encoder
      Call this method to start writing an array. When starting to serialize an array, call Encoder.writeArrayStart(). Then, before writing any data for any item call Encoder.setItemCount(long) followed by a sequence of Encoder.startItem() and the item itself. The number of Encoder.startItem() should match the number specified in Encoder.setItemCount(long). When actually writing the data of the item, you can call any Encoder method (e.g., Encoder.writeLong(long)). When all items of the array have been written, call Encoder.writeArrayEnd(). As an example, let's say you want to write an array of records, the record consisting of an Long field and a Boolean field. Your code would look something like this:
       out.writeArrayStart();
       out.setItemCount(list.size());
       for (Record r : list) {
         out.startItem();
         out.writeLong(r.longField);
         out.writeBoolean(r.boolField);
       }
       out.writeArrayEnd();
       
      Specified by:
      writeArrayStart in class Encoder
      Throws:
      IOException
    • setItemCount

      public void setItemCount(long itemCount) throws IOException
      Description copied from class: Encoder
      Call this method before writing a batch of items in an array or a map. Then for each item, call Encoder.startItem() followed by any of the other write methods of Encoder. The number of calls to Encoder.startItem() must be equal to the count specified in Encoder.setItemCount(long). Once a batch is completed you can start another batch with Encoder.setItemCount(long).
      Specified by:
      setItemCount in class Encoder
      Parameters:
      itemCount - The number of Encoder.startItem() calls to follow.
      Throws:
      IOException
    • startItem

      public void startItem() throws IOException
      Description copied from class: Encoder
      Start a new item of an array or map. See Encoder.writeArrayStart() for usage information.
      Specified by:
      startItem in class Encoder
      Throws:
      IOException
    • writeArrayEnd

      public void writeArrayEnd() throws IOException
      Description copied from class: Encoder
      Call this method to finish writing an array. See Encoder.writeArrayStart() for usage information.
      Specified by:
      writeArrayEnd in class Encoder
      Throws:
      IOException
    • writeMapStart

      public void writeMapStart() throws IOException
      Description copied from class: Encoder
      Call this to start a new map. See Encoder.writeArrayStart() for details on usage. As an example of usage, let's say you want to write a map of records, the record consisting of an Long field and a Boolean field. Your code would look something like this:
       out.writeMapStart();
       out.setItemCount(list.size());
       for (Map.Entryinvalid input: '<'String, Record> entry : map.entrySet()) {
         out.startItem();
         out.writeString(entry.getKey());
         out.writeLong(entry.getValue().longField);
         out.writeBoolean(entry.getValue().boolField);
       }
       out.writeMapEnd();
       
      Specified by:
      writeMapStart in class Encoder
      Throws:
      IOException
    • writeMapEnd

      public void writeMapEnd() throws IOException
      Description copied from class: Encoder
      Call this method to terminate the inner-most, currently-opened map. See Encoder.writeArrayStart() for more details.
      Specified by:
      writeMapEnd in class Encoder
      Throws:
      IOException
    • writeIndex

      public void writeIndex(int unionIndex) throws IOException
      Description copied from class: Encoder
      Call this method to write the tag of a union. As an example of usage, let's say you want to write a union, whose second branch is a record consisting of an Long field and a Boolean field. Your code would look something like this:
       out.writeIndex(1);
       out.writeLong(record.longField);
       out.writeBoolean(record.boolField);
       
      Specified by:
      writeIndex in class Encoder
      Throws:
      IOException
    • writeZero

      protected abstract void writeZero() throws IOException
      Write a zero byte to the underlying output.
      Throws:
      IOException
    • bytesBuffered

      public abstract int bytesBuffered()
      Returns the number of bytes currently buffered by this encoder. If this Encoder does not buffer, this will always return zero.

      Call Flushable.flush() to empty the buffer to the underlying output.