Package org.apache.avro.io

# Class DecoderFactory

java.lang.Object
org.apache.avro.io.DecoderFactory

public class DecoderFactory extends Object
A factory for creating and configuring `Decoder`s.

Factories are thread-safe, and are generally cached by applications for performance reasons. Multiple instances are only required if multiple concurrent configurations are needed.

Although Factories are thread-safe the `Decoder`s they return are not.

See Also:
  - `Decoder`

## Constructor Summary

Constructors
Constructor
Description
`DecoderFactory()`
Constructor for factory instances

## Method Summary

Modifier and Type
Method
Description
`BinaryDecoder`
`binaryDecoder(byte[] bytes, int offset, int length, BinaryDecoder reuse)`
Creates or reinitializes a `BinaryDecoder` with the byte array provided as the source of data.
`BinaryDecoder`
`binaryDecoder(byte[] bytes, BinaryDecoder reuse)`
This method is shorthand for
`BinaryDecoder`
`binaryDecoder(InputStream in, BinaryDecoder reuse)`
Creates or reinitializes a `BinaryDecoder` with the input stream provided as the source of data.
`DecoderFactory`
`configureDecoderBufferSize(int size)`
Configures this factory to use the specified buffer size when creating Decoder instances that buffer their input.
`BinaryDecoder`
`createBinaryDecoder(byte[] bytes, int offset, int length, BinaryDecoder reuse)`
Deprecated.
use `binaryDecoder(byte[], int, int, BinaryDecoder)` instead
`BinaryDecoder`
`createBinaryDecoder(byte[] bytes, BinaryDecoder reuse)`
Deprecated.
use `binaryDecoder(byte[], BinaryDecoder)` instead
`BinaryDecoder`
`createBinaryDecoder(InputStream in, BinaryDecoder reuse)`
Deprecated.
use the equivalent `binaryDecoder(InputStream, BinaryDecoder)` instead
`static DecoderFactory`
`defaultFactory()`
Deprecated.
use the equivalent `get()` instead
`BinaryDecoder`
`directBinaryDecoder(InputStream in, BinaryDecoder reuse)`
Creates or reinitializes a `BinaryDecoder` with the input stream provided as the source of data.
`static DecoderFactory`
`get()`
Returns an immutable static DecoderFactory configured with default settings All mutating methods throw IllegalArgumentExceptions.
`int`
`getConfiguredBufferSize()`
Returns this factory's configured preferred buffer size.
`JsonDecoder`
`jsonDecoder(Schema schema, InputStream input)`
Creates a `JsonDecoder` using the InputStream provided for reading data that conforms to the Schema provided.
`JsonDecoder`
`jsonDecoder(Schema schema, String input)`
Creates a `JsonDecoder` using the String provided for reading data that conforms to the Schema provided.
`ResolvingDecoder`
`resolvingDecoder(Schema writer, Schema reader, Decoder wrapped)`
Creates a `ResolvingDecoder` wrapping the Decoder provided.
`ValidatingDecoder`
`validatingDecoder(Schema schema, Decoder wrapped)`
Creates a `ValidatingDecoder` wrapping the Decoder provided.

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

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

## Constructor Details

### DecoderFactory

public DecoderFactory()
Constructor for factory instances

## Method Details

### defaultFactory

@Deprecated public static DecoderFactory defaultFactory()
Deprecated.
use the equivalent `get()` instead

### get

public static DecoderFactory get()
Returns an immutable static DecoderFactory configured with default settings All mutating methods throw IllegalArgumentExceptions. All creator methods create objects with default settings.

### configureDecoderBufferSize

public DecoderFactory configureDecoderBufferSize(int size)
Configures this factory to use the specified buffer size when creating Decoder instances that buffer their input. The default buffer size is 8192 bytes.

Parameters:
  `size` - The preferred buffer size. Valid values are in the range [32, 16*1024*1024]. Values outside this range are rounded to the nearest value in the range. Values less than 512 or greater than 1024*1024 are not recommended.
Returns:
  This factory, to enable method chaining:

               DecoderFactory myFactory = new DecoderFactory().useBinaryDecoderBufferSize(4096);

### getConfiguredBufferSize

public int getConfiguredBufferSize()
Returns this factory's configured preferred buffer size. Used when creating Decoder instances that buffer. See `configureDecoderBufferSize(int)`

Returns:
  The preferred buffer size, in bytes.

### createBinaryDecoder

@Deprecated public BinaryDecoder createBinaryDecoder(InputStream in, BinaryDecoder reuse)
Deprecated.
use the equivalent `binaryDecoder(InputStream, BinaryDecoder)` instead

### binaryDecoder

public BinaryDecoder binaryDecoder(InputStream in, BinaryDecoder reuse)
Creates or reinitializes a `BinaryDecoder` with the input stream provided as the source of data. If reuse is provided, it will be reinitialized to the given input stream.

`BinaryDecoder` instances returned by this method buffer their input, reading up to `getConfiguredBufferSize()` bytes past the minimum required to satisfy read requests in order to achieve better performance. If the buffering is not desired, use `directBinaryDecoder(InputStream, BinaryDecoder)`.

`BinaryDecoder.inputStream()` provides a view on the data that is buffer-aware, for users that need to interleave access to data with the Decoder API.

Parameters:
  `in` - The InputStream to initialize to
  `reuse` - The BinaryDecoder to attempt to reuse given the factory configuration. A BinaryDecoder implementation may not be compatible with reuse, causing a new instance to be returned. If null, a new instance is returned.
Returns:
  A BinaryDecoder that uses in as its source of data. If reuse is null, this will be a new instance. If reuse is not null, then it may be reinitialized if compatible, otherwise a new instance will be returned.
See Also:
  - `BinaryDecoder`
  - `Decoder`

### directBinaryDecoder

public BinaryDecoder directBinaryDecoder(InputStream in, BinaryDecoder reuse)
Creates or reinitializes a `BinaryDecoder` with the input stream provided as the source of data. If reuse is provided, it will be reinitialized to the given input stream.

`BinaryDecoder` instances returned by this method do not buffer their input. In most cases a buffering BinaryDecoder is sufficient in combination with `BinaryDecoder.inputStream()` which provides a buffer-aware view on the data.

A "direct" BinaryDecoder does not read ahead from an InputStream or other data source that cannot be rewound. From the perspective of a client, a "direct" decoder must never read beyond the minimum necessary bytes to service a `BinaryDecoder` API read request.

In the case that the improved performance of a buffering implementation does not outweigh the inconvenience of its buffering semantics, a "direct" decoder can be used.

Parameters:
  `in` - The InputStream to initialize to
  `reuse` - The BinaryDecoder to attempt to reuse given the factory configuration. A BinaryDecoder implementation may not be compatible with reuse, causing a new instance to be returned. If null, a new instance is returned.
Returns:
  A BinaryDecoder that uses in as its source of data. If reuse is null, this will be a new instance. If reuse is not null, then it may be reinitialized if compatible, otherwise a new instance will be returned.
See Also:
  - `DirectBinaryDecoder`
  - `Decoder`

### createBinaryDecoder

@Deprecated public BinaryDecoder createBinaryDecoder(byte[] bytes, int offset, int length, BinaryDecoder reuse)
Deprecated.
use `binaryDecoder(byte[], int, int, BinaryDecoder)` instead

### binaryDecoder

public BinaryDecoder binaryDecoder(byte[] bytes, int offset, int length, BinaryDecoder reuse)
Creates or reinitializes a `BinaryDecoder` with the byte array provided as the source of data. If reuse is provided, it will attempt to reinitialize reuse to the new byte array. This instance will use the provided byte array as its buffer.

`BinaryDecoder.inputStream()` provides a view on the data that is buffer-aware and can provide a view of the data not yet read by Decoder API methods.

Parameters:
  `bytes` - The byte array to initialize to
  `offset` - The offset to start reading from
  `length` - The maximum number of bytes to read from the byte array
  `reuse` - The BinaryDecoder to attempt to reinitialize. if null a new BinaryDecoder is created.
Returns:
  A BinaryDecoder that uses bytes as its source of data. If reuse is null, this will be a new instance. reuse may be reinitialized if appropriate, otherwise a new instance is returned. Clients must not assume that reuse is reinitialized and returned.

### createBinaryDecoder

@Deprecated public BinaryDecoder createBinaryDecoder(byte[] bytes, BinaryDecoder reuse)
Deprecated.
use `binaryDecoder(byte[], BinaryDecoder)` instead

### binaryDecoder

public BinaryDecoder binaryDecoder(byte[] bytes, BinaryDecoder reuse)
This method is shorthand for

     createBinaryDecoder(bytes, 0, bytes.length, reuse);

`binaryDecoder(byte[], int, int, BinaryDecoder)`

### jsonDecoder

public JsonDecoder jsonDecoder(Schema schema, InputStream input) throws IOException
Creates a `JsonDecoder` using the InputStream provided for reading data that conforms to the Schema provided.

Parameters:
  `schema` - The Schema for data read from this JsonEncoder. Cannot be null.
  `input` - The InputStream to read from. Cannot be null.
Returns:
  A JsonEncoder configured with input and schema
Throws:
  `IOException`

### jsonDecoder

public JsonDecoder jsonDecoder(Schema schema, String input) throws IOException
Creates a `JsonDecoder` using the String provided for reading data that conforms to the Schema provided.

Parameters:
  `schema` - The Schema for data read from this JsonEncoder. Cannot be null.
  `input` - The String to read from. Cannot be null.
Returns:
  A JsonEncoder configured with input and schema
Throws:
  `IOException`

### validatingDecoder

public ValidatingDecoder validatingDecoder(Schema schema, Decoder wrapped) throws IOException
Creates a `ValidatingDecoder` wrapping the Decoder provided. This ValidatingDecoder will ensure that operations against it conform to the schema provided.

Parameters:
  `schema` - The Schema to validate against. Cannot be null.
  `wrapped` - The Decoder to wrap.
Returns:
  A ValidatingDecoder configured with wrapped and schema
Throws:
  `IOException`

### resolvingDecoder

public ResolvingDecoder resolvingDecoder(Schema writer, Schema reader, Decoder wrapped) throws IOException
Creates a `ResolvingDecoder` wrapping the Decoder provided. This ResolvingDecoder will resolve input conforming to the writer schema from the wrapped Decoder, and present it as the reader schema.

Parameters:
  `writer` - The Schema that the source data is in. Cannot be null.
  `reader` - The Schema that the reader wishes to read the data as. Cannot be null.
  `wrapped` - The Decoder to wrap.
Returns:
  A ResolvingDecoder configured to resolve writer to reader from in
Throws:
  `IOException`