org.apache.avro.io

## Class BlockingDirectBinaryEncoder

- java.lang.Object
- - org.apache.avro.io.Encoder
  - - org.apache.avro.io.BinaryEncoder
    - - org.apache.avro.io.DirectBinaryEncoder
      - - org.apache.avro.io.BlockingDirectBinaryEncoder

- All Implemented Interfaces:
    Flushable

      public class BlockingDirectBinaryEncoder
      extends DirectBinaryEncoder

  An `Encoder` for Avro's binary encoding that does not buffer output.

  This encoder does not buffer writes in contrast to `BufferedBinaryEncoder`. However, it is lighter-weight and useful when: The buffering in BufferedBinaryEncoder is not desired because you buffer a different level or the Encoder is very short-lived. The BlockingDirectBinaryEncoder will encode the number of bytes of the Map and Array blocks. This will allow to postpone the decoding, or skip over it at all.

  To construct, use `EncoderFactory.blockingDirectBinaryEncoder(OutputStream, BinaryEncoder)`

  `BlockingDirectBinaryEncoder` instances returned by this method are not thread-safe
  See Also:
    `BinaryEncoder`, `EncoderFactory`, `Encoder`, `Decoder`

- - ### Field Summary

    - ### Fields inherited from class org.apache.avro.io.DirectBinaryEncoder

      `out`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `BlockingDirectBinaryEncoder(OutputStream out)` Create a writer that sends its output to the underlying stream `out`.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `setItemCount(long itemCount)` Call this method before writing a batch of items in an array or a map.
    `void` | `writeArrayEnd()` Call this method to finish writing an array.
    `void` | `writeArrayStart()` Call this method to start writing an array.
    `void` | `writeMapEnd()` Call this method to terminate the inner-most, currently-opened map.
    `void` | `writeMapStart()` Call this to start a new map.

    - ### Methods inherited from class org.apache.avro.io.DirectBinaryEncoder

      `bytesBuffered, flush, writeBoolean, writeDouble, writeFixed, writeFloat, writeInt, writeLong, writeZero`

    - ### Methods inherited from class org.apache.avro.io.BinaryEncoder

      `startItem, writeBytes, writeBytes, writeEnum, writeIndex, writeNull, writeString, writeString`

    - ### Methods inherited from class org.apache.avro.io.Encoder

      `writeBytes, writeFixed, writeFixed, writeString`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### BlockingDirectBinaryEncoder

          public BlockingDirectBinaryEncoder(OutputStream out)

      Create a writer that sends its output to the underlying stream `out`.
      Parameters:
        `out` - The Outputstream to write to

  - ### Method Detail

    - #### 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)`.
      Overrides:
        `setItemCount` in class `BinaryEncoder`
      Parameters:
        `itemCount` - The number of `Encoder.startItem()` calls to follow.
      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();

      Overrides:
        `writeArrayStart` in class `BinaryEncoder`
      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.
      Overrides:
        `writeArrayEnd` in class `BinaryEncoder`
      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.Entry entry : map.entrySet()) {
             out.startItem();
             out.writeString(entry.getKey());
             out.writeLong(entry.getValue().longField);
             out.writeBoolean(entry.getValue().boolField);
           }
           out.writeMapEnd();

      Overrides:
        `writeMapStart` in class `BinaryEncoder`
      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.
      Overrides:
        `writeMapEnd` in class `BinaryEncoder`
      Throws:
        `IOException`

Copyright © 2009–2024 The Apache Software Foundation. All rights reserved.