Top Description Inners Fields Constructors Methods
org.apache.avro.io

public Class EncoderFactory

extends Object
Class Inheritance
Known Direct Subclasses
org.apache.avro.io.EncoderFactory.DefaultEncoderFactory
Imports
java.io.IOException, .OutputStream, org.apache.avro.AvroRuntimeException, .Schema, com.fasterxml.jackson.core.JsonGenerator

A factory for creating and configuring Encoder instances.

Factory methods that create Encoder instances are thread-safe. Multiple instances with different configurations can be cached by an application.

See Also
Encoder, BinaryEncoder, JsonEncoder, ValidatingEncoder, BufferedBinaryEncoder, BlockingBinaryEncoder, DirectBinaryEncoder

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
protected int
protected int
private static final int
private static final int
private static final EncoderFactory
private static final int
private static final int

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public BinaryEncoder

Returns:

A BinaryEncoder that uses out as its data output. If reuse is null, this will be a new instance. If reuse is not null, then the returned instance may be a new instance or reuse reconfigured to use out.
binaryEncoder
(OutputStream
The OutputStream to write to. Cannot be null.
out
,
BinaryEncoder
The BinaryEncoder to attempt to reuse given the factory configuration. A BinaryEncoder implementation may not be compatible with reuse, causing a new instance to be returned. If null, a new instance is returned.
reuse
)

Creates or reinitializes a BinaryEncoder with the OutputStream provided as the destination for written data.

public BinaryEncoder

Returns:

A BinaryEncoder that uses out as its data output. If reuse is null, this will be a new instance. If reuse is not null, then the returned instance may be a new instance or reuse reconfigured to use out.
blockingBinaryEncoder
(OutputStream
The OutputStream to initialize to. Cannot be null.
out
,
BinaryEncoder
The BinaryEncoder to attempt to reuse given the factory configuration. A BinaryEncoder implementation may not be compatible with reuse, causing a new instance to be returned. If null, a new instance is returned.
reuse
)

Creates or reinitializes a BinaryEncoder with the OutputStream provided as the destination for written data.

public EncoderFactory

Returns:

This factory, to enable method chaining:
        EncoderFactory factory = new EncoderFactory().configureBlockSize(8000);
        
configureBlockSize
(int
The preferred block size for array blocking. Arrays larger than this size will be segmented into blocks according to the Avro spec. Valid values are in the range [64, 1024*1024*1024] Values outside this range are set to the nearest value in the range. The encoder will require at least this amount of memory.
size
)

Configures this factory to construct blocking BinaryEncoders with the specified block buffer size.

public EncoderFactory

Returns:

This factory, to enable method chaining:
        EncoderFactory factory = new EncoderFactory().configureBufferSize(4096);
        
configureBufferSize
(int
The buffer size to configure new instances with. Valid values are in the range [32, 16*1024*1024]. Values outside this range are set to the nearest value in the range. Values less than 256 will limit performance but will consume less memory if the BinaryEncoder is short-lived, values greater than 8*1024 are not likely to improve performance but may be useful for the downstream OutputStream.
size
)

Configures this factory to use the specified buffer size when creating Encoder instances that buffer their output.

public BinaryEncoder

Returns:

A BinaryEncoder that uses out as its data output. If reuse is null, this will be a new instance. If reuse is not null, then the returned instance may be a new instance or reuse reconfigured to use out.
directBinaryEncoder
(OutputStream
The OutputStream to initialize to. Cannot be null.
out
,
BinaryEncoder
The BinaryEncoder to attempt to reuse given the factory configuration. A BinaryEncoder implementation may not be compatible with reuse, causing a new instance to be returned. If null, a new instance is returned.
reuse
)

Creates or reinitializes a BinaryEncoder with the OutputStream provided as the destination for written data.

public static EncoderFactory
get()

Returns an immutable static DecoderFactory with default configuration.

public int

Returns:

The preferred block size, in bytes.
getBlockSize
()

Returns this factory's configured default block buffer size.

public int

Returns:

The preferred buffer size, in bytes.
getBufferSize
()

Returns this factory's configured default buffer size.

public JsonEncoder

Returns:

A JsonEncoder configured with out and schema
jsonEncoder
(Schema
The Schema for data written to this JsonEncoder. Cannot be null.
schema
,
OutputStream
The OutputStream to write to. Cannot be null.
out
)

Creates a JsonEncoder using the OutputStream provided for writing data conforming to the Schema provided.

public JsonEncoder

Returns:

A JsonEncoder configured with out, schema and pretty
jsonEncoder
(Schema
The Schema for data written to this JsonEncoder. Cannot be null.
schema
,
OutputStream
The OutputStream to write to. Cannot be null.
out
,
boolean
Pretty print encoding.
pretty
)

Creates a JsonEncoder using the OutputStream provided for writing data conforming to the Schema provided with optional pretty printing.

pack-priv JsonEncoder

Returns:

A JsonEncoder configured with gen and schema
jsonEncoder
(Schema
The Schema for data written to this JsonEncoder. Cannot be null.
schema
,
JsonGenerator
The JsonGenerator to write with. Cannot be null.
gen
)

Creates a JsonEncoder using the JsonGenerator provided for output of data conforming to the Schema provided.

public ValidatingEncoder

Returns:

A ValidatingEncoder configured to wrap encoder and validate against schema
validatingEncoder
(Schema
The Schema to validate operations against. Cannot be null.
schema
,
Encoder
The Encoder to wrap. Cannot be be null.
encoder
)

Creates a ValidatingEncoder that wraps the Encoder provided.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

binaryBlockSizeback to summary
protected int binaryBlockSize
binaryBufferSizeback to summary
protected int binaryBufferSize
DEFAULT_BLOCK_BUFFER_SIZEback to summary
private static final int DEFAULT_BLOCK_BUFFER_SIZE
DEFAULT_BUFFER_SIZEback to summary
private static final int DEFAULT_BUFFER_SIZE
DEFAULT_FACTORYback to summary
private static final EncoderFactory DEFAULT_FACTORY
MAX_BLOCK_BUFFER_SIZEback to summary
private static final int MAX_BLOCK_BUFFER_SIZE
MIN_BLOCK_BUFFER_SIZEback to summary
private static final int MIN_BLOCK_BUFFER_SIZE

Constructor Detail

EncoderFactoryback to summary
public EncoderFactory()

Method Detail

binaryEncoderback to summary
public BinaryEncoder binaryEncoder(OutputStream out, BinaryEncoder reuse)

Creates or reinitializes a BinaryEncoder with the OutputStream provided as the destination for written data. If reuse is provided, an attempt will be made to reconfigure reuse rather than construct a new instance, but this is not guaranteed, a new instance may be returned.

The BinaryEncoder implementation returned may buffer its output. Data may not appear on the underlying OutputStream until Encoder#flush() is called. The buffer size is configured with configureBufferSize(int).

If buffering is not desired, and lower performance is acceptable, use directBinaryEncoder(OutputStream, BinaryEncoder)

BinaryEncoder instances returned by this method are not thread-safe

Parameters
out:OutputStream

The OutputStream to write to. Cannot be null.

reuse:BinaryEncoder

The BinaryEncoder to attempt to reuse given the factory configuration. A BinaryEncoder implementation may not be compatible with reuse, causing a new instance to be returned. If null, a new instance is returned.

Returns:BinaryEncoder

A BinaryEncoder that uses out as its data output. If reuse is null, this will be a new instance. If reuse is not null, then the returned instance may be a new instance or reuse reconfigured to use out.

See Also
BufferedBinaryEncoder, Encoder
blockingBinaryEncoderback to summary
public BinaryEncoder blockingBinaryEncoder(OutputStream out, BinaryEncoder reuse)

Creates or reinitializes a BinaryEncoder with the OutputStream provided as the destination for written data. If reuse is provided, an attempt will be made to reconfigure reuse rather than construct a new instance, but this is not guaranteed, a new instance may be returned.

The BinaryEncoder implementation returned buffers its output, calling Encoder#flush() is required for output to appear on the underlying OutputStream.

The returned BinaryEncoder implements the Avro binary encoding using blocks delimited with byte sizes for Arrays and Maps. This allows for some decoders to skip over large Arrays or Maps without decoding the contents, but adds some overhead. The default block size is configured with configureBlockSize(int)

BinaryEncoder instances returned by this method are not thread-safe.

Parameters
out:OutputStream

The OutputStream to initialize to. Cannot be null.

reuse:BinaryEncoder

The BinaryEncoder to attempt to reuse given the factory configuration. A BinaryEncoder implementation may not be compatible with reuse, causing a new instance to be returned. If null, a new instance is returned.

Returns:BinaryEncoder

A BinaryEncoder that uses out as its data output. If reuse is null, this will be a new instance. If reuse is not null, then the returned instance may be a new instance or reuse reconfigured to use out.

See Also
BlockingBinaryEncoder, Encoder
configureBlockSizeback to summary
public EncoderFactory configureBlockSize(int size)

Configures this factory to construct blocking BinaryEncoders with the specified block buffer size. The default buffer size is 64 * 1024 bytes.

Parameters
size:int

The preferred block size for array blocking. Arrays larger than this size will be segmented into blocks according to the Avro spec. Valid values are in the range [64, 1024*1024*1024] Values outside this range are set to the nearest value in the range. The encoder will require at least this amount of memory.

Returns:EncoderFactory

This factory, to enable method chaining:

        EncoderFactory factory = new EncoderFactory().configureBlockSize(8000);
        
See Also
blockingBinaryEncoder(OutputStream, BinaryEncoder)
configureBufferSizeback to summary
public EncoderFactory configureBufferSize(int size)

Configures this factory to use the specified buffer size when creating Encoder instances that buffer their output. The default buffer size is 2048 bytes.

Parameters
size:int

The buffer size to configure new instances with. Valid values are in the range [32, 16*1024*1024]. Values outside this range are set to the nearest value in the range. Values less than 256 will limit performance but will consume less memory if the BinaryEncoder is short-lived, values greater than 8*1024 are not likely to improve performance but may be useful for the downstream OutputStream.

Returns:EncoderFactory

This factory, to enable method chaining:

        EncoderFactory factory = new EncoderFactory().configureBufferSize(4096);
        
See Also
binaryEncoder(OutputStream, BinaryEncoder)
directBinaryEncoderback to summary
public BinaryEncoder directBinaryEncoder(OutputStream out, BinaryEncoder reuse)

Creates or reinitializes a BinaryEncoder with the OutputStream provided as the destination for written data. If reuse is provided, an attempt will be made to reconfigure reuse rather than construct a new instance, but this is not guaranteed, a new instance may be returned.

The BinaryEncoder implementation returned does not buffer its output, calling Encoder#flush() will simply cause the wrapped OutputStream to be flushed.

Performance of unbuffered writes can be significantly slower than buffered writes. binaryEncoder(OutputStream, BinaryEncoder) returns BinaryEncoder instances that are tuned for performance but may buffer output. The unbuffered, 'direct' encoder may be desired when buffering semantics are problematic, or if the lifetime of the encoder is so short that the buffer would not be useful.

BinaryEncoder instances returned by this method are not thread-safe.

Parameters
out:OutputStream

The OutputStream to initialize to. Cannot be null.

reuse:BinaryEncoder

The BinaryEncoder to attempt to reuse given the factory configuration. A BinaryEncoder implementation may not be compatible with reuse, causing a new instance to be returned. If null, a new instance is returned.

Returns:BinaryEncoder

A BinaryEncoder that uses out as its data output. If reuse is null, this will be a new instance. If reuse is not null, then the returned instance may be a new instance or reuse reconfigured to use out.

See Also
DirectBinaryEncoder, Encoder
getback to summary
public static EncoderFactory get()

Returns an immutable static DecoderFactory with default configuration. All configuration methods throw AvroRuntimeExceptions if called.

getBlockSizeback to summary
public int getBlockSize()

Returns this factory's configured default block buffer size. BinaryEncoder instances created with #blockingBinaryEncoder(OutputStream, BinaryEncoder) will have block buffers of this size.

Returns:int

The preferred block size, in bytes.

See Also
configureBlockSize(int), blockingBinaryEncoder(OutputStream, BinaryEncoder)
getBufferSizeback to summary
public int getBufferSize()

Returns this factory's configured default buffer size. Used when creating Encoder instances that buffer writes.

Returns:int

The preferred buffer size, in bytes.

See Also
configureBufferSize(int), binaryEncoder(OutputStream, BinaryEncoder)
jsonEncoderback to summary
public JsonEncoder jsonEncoder(Schema schema, OutputStream out) throws IOException

Creates a JsonEncoder using the OutputStream provided for writing data conforming to the Schema provided.

JsonEncoder buffers its output. Data may not appear on the underlying OutputStream until Encoder#flush() is called.

JsonEncoder is not thread-safe.

Parameters
schema:Schema

The Schema for data written to this JsonEncoder. Cannot be null.

out:OutputStream

The OutputStream to write to. Cannot be null.

Returns:JsonEncoder

A JsonEncoder configured with out and schema

jsonEncoderback to summary
public JsonEncoder jsonEncoder(Schema schema, OutputStream out, boolean pretty) throws IOException

Creates a JsonEncoder using the OutputStream provided for writing data conforming to the Schema provided with optional pretty printing.

JsonEncoder buffers its output. Data may not appear on the underlying OutputStream until Encoder#flush() is called.

JsonEncoder is not thread-safe.

Parameters
schema:Schema

The Schema for data written to this JsonEncoder. Cannot be null.

out:OutputStream

The OutputStream to write to. Cannot be null.

pretty:boolean

Pretty print encoding.

Returns:JsonEncoder

A JsonEncoder configured with out, schema and pretty

jsonEncoderback to summary
pack-priv JsonEncoder jsonEncoder(Schema schema, JsonGenerator gen) throws IOException

Creates a JsonEncoder using the JsonGenerator provided for output of data conforming to the Schema provided.

JsonEncoder buffers its output. Data may not appear on the underlying output until Encoder#flush() is called.

JsonEncoder is not thread-safe.

Parameters
schema:Schema

The Schema for data written to this JsonEncoder. Cannot be null.

gen:JsonGenerator

The JsonGenerator to write with. Cannot be null.

Returns:JsonEncoder

A JsonEncoder configured with gen and schema

validatingEncoderback to summary
public ValidatingEncoder validatingEncoder(Schema schema, Encoder encoder) throws IOException

Creates a ValidatingEncoder that wraps the Encoder provided. This ValidatingEncoder will ensure that operations against it conform to the schema provided.

Many Encoders buffer their output. Data may not appear on the underlying output until Encoder#flush() is called.

ValidatingEncoder is not thread-safe.

Parameters
schema:Schema

The Schema to validate operations against. Cannot be null.

encoder:Encoder

The Encoder to wrap. Cannot be be null.

Returns:ValidatingEncoder

A ValidatingEncoder configured to wrap encoder and validate against schema

org.apache.avro.io back to summary

private Class EncoderFactory.DefaultEncoderFactory

extends EncoderFactory
Class Inheritance

Field Summary

Inherited from org.apache.avro.io.EncoderFactory:
binaryBlockSizebinaryBufferSize

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public EncoderFactory
configureBlockSize(int
The preferred block size for array blocking. Arrays larger than this size will be segmented into blocks according to the Avro spec. Valid values are in the range [64, 1024*1024*1024] Values outside this range are set to the nearest value in the range. The encoder will require at least this amount of memory.
size
)

Overrides org.apache.avro.io.EncoderFactory.configureBlockSize.

Configures this factory to construct blocking BinaryEncoders with the specified block buffer size.
public EncoderFactory
configureBufferSize(int
The buffer size to configure new instances with. Valid values are in the range [32, 16*1024*1024]. Values outside this range are set to the nearest value in the range. Values less than 256 will limit performance but will consume less memory if the BinaryEncoder is short-lived, values greater than 8*1024 are not likely to improve performance but may be useful for the downstream OutputStream.
size
)

Overrides org.apache.avro.io.EncoderFactory.configureBufferSize.

Configures this factory to use the specified buffer size when creating Encoder instances that buffer their output.
Inherited from org.apache.avro.io.EncoderFactory:
binaryEncoderblockingBinaryEncoderdirectBinaryEncodergetgetBlockSizegetBufferSizejsonEncoderjsonEncoderjsonEncodervalidatingEncoder

Constructor Detail

DefaultEncoderFactoryback to summary
private DefaultEncoderFactory()

Method Detail

configureBlockSizeback to summary
public EncoderFactory configureBlockSize(int size)

Overrides org.apache.avro.io.EncoderFactory.configureBlockSize.

Doc from org.apache.avro.io.EncoderFactory.configureBlockSize.

Configures this factory to construct blocking BinaryEncoders with the specified block buffer size. The default buffer size is 64 * 1024 bytes.

Parameters
size:int

The preferred block size for array blocking. Arrays larger than this size will be segmented into blocks according to the Avro spec. Valid values are in the range [64, 1024*1024*1024] Values outside this range are set to the nearest value in the range. The encoder will require at least this amount of memory.

Returns:EncoderFactory

This factory, to enable method chaining:

        EncoderFactory factory = new EncoderFactory().configureBlockSize(8000);
        
Annotations
@Override
configureBufferSizeback to summary
public EncoderFactory configureBufferSize(int size)

Overrides org.apache.avro.io.EncoderFactory.configureBufferSize.

Doc from org.apache.avro.io.EncoderFactory.configureBufferSize.

Configures this factory to use the specified buffer size when creating Encoder instances that buffer their output. The default buffer size is 2048 bytes.

Parameters
size:int

The buffer size to configure new instances with. Valid values are in the range [32, 16*1024*1024]. Values outside this range are set to the nearest value in the range. Values less than 256 will limit performance but will consume less memory if the BinaryEncoder is short-lived, values greater than 8*1024 are not likely to improve performance but may be useful for the downstream OutputStream.

Returns:EncoderFactory

This factory, to enable method chaining:

        EncoderFactory factory = new EncoderFactory().configureBufferSize(4096);
        
Annotations
@Override