org.apache.avro.message

## Class BinaryMessageEncoder<D>

- java.lang.Object
- - org.apache.avro.message.BinaryMessageEncoder<D>

- All Implemented Interfaces:
    MessageEncoder<D>

      public class BinaryMessageEncoder<D>
      extends Object
      implements MessageEncoder<D>

  A `MessageEncoder` that adds a header and 8-byte schema fingerprint to each datum encoded as binary.

  This class is thread-safe.

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `BinaryMessageEncoder(GenericData model, Schema schema)` Creates a new `BinaryMessageEncoder` that uses the given `data model` to deconstruct datum instances described by the `schema`.
    `BinaryMessageEncoder(GenericData model, Schema schema, boolean shouldCopy)` Creates a new `BinaryMessageEncoder` that uses the given `data model` to deconstruct datum instances described by the `schema`.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `ByteBuffer` | `encode(D datum)` Serialize a single datum to a ByteBuffer.
    `void` | `encode(D datum, OutputStream stream)` Serialize a single datum to an OutputStream.

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

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

- - ### Constructor Detail

    - #### BinaryMessageEncoder

          public BinaryMessageEncoder(GenericData model,
                                      Schema schema)

      Creates a new `BinaryMessageEncoder` that uses the given `data model` to deconstruct datum instances described by the `schema`.

      Buffers returned by `encode(D)` are copied and will not be modified by future calls to `encode`.

      Parameters:
        `model` - the `data model` for datum instances
        `schema` - the `Schema` for datum instances

    - #### BinaryMessageEncoder

          public BinaryMessageEncoder(GenericData model,
                                      Schema schema,
                                      boolean shouldCopy)

      Creates a new `BinaryMessageEncoder` that uses the given `data model` to deconstruct datum instances described by the `schema`.

      If `shouldCopy` is true, then buffers returned by `encode(D)` are copied and will not be modified by future calls to `encode`.

      If `shouldCopy` is false, then buffers returned by `encode` wrap a thread-local buffer that can be reused by future calls to `encode`, but may not be. Callers should only set `shouldCopy` to false if the buffer will be copied before the current thread's next call to `encode`.

      Parameters:
        `model` - the `data model` for datum instances
        `schema` - the `Schema` for datum instances
        `shouldCopy` - whether to copy buffers before returning encoded results

  - ### Method Detail

    - #### encode

          public ByteBuffer encode(D datum)
                            throws IOException

      Description copied from interface: `MessageEncoder`
      Serialize a single datum to a ByteBuffer.
      Specified by:
        `encode` in interface `MessageEncoder<D>`
      Parameters:
        `datum` - a datum
      Returns:
        a ByteBuffer containing the serialized datum
      Throws:
        `IOException`

    - #### encode

          public void encode(D datum,
                             OutputStream stream)
                      throws IOException

      Description copied from interface: `MessageEncoder`
      Serialize a single datum to an OutputStream.
      Specified by:
        `encode` in interface `MessageEncoder<D>`
      Parameters:
        `datum` - a datum
        `stream` - an OutputStream to serialize the datum to
      Throws:
        `IOException`

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