Top Description Fields Constructors Methods
org.apache.avro.message

public Class BinaryMessageDecoder<D>

extends MessageDecoder.BaseDecoder<D>
Class Inheritance
Imports
org.apache.avro.Schema, .SchemaNormalization, org.apache.avro.generic.GenericData, org.apache.avro.util.internal.ThreadLocalWithInitial, java.io.IOException, .InputStream, java.nio.ByteBuffer, .ByteOrder, java.util.Map, java.util.concurrent.ConcurrentHashMap

A MessageDecoder that reads a binary-encoded datum. This checks for the datum header and decodes the payload with the schema that corresponds to the 8-byte schema fingerprint.

Instances can decode message payloads for known schemas, which are schemas added using addSchema(Schema), schemas resolved by the SchemaStore passed to the constructor, or the expected schema passed to the constructor. Messages encoded using an unknown schema will cause instances to throw a MissingSchemaException.

It is safe to continue using instances of this class after decode throws BadHeaderException or MissingSchemaException.

This class is thread-safe.

Field Summary

Modifier and TypeField and Description
private final Map<Long, RawMessageDecoder<D>>
private static final ThreadLocal<ByteBuffer>
private static final ThreadLocal<byte[]>
private final GenericData
private final Schema
private final SchemaStore

Constructor Summary

AccessConstructor and Description
public
BinaryMessageDecoder(GenericData
the data model for datum instances
model
,
Schema
the Schema used to construct datum instances
readSchema
)

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

public
BinaryMessageDecoder(GenericData
the data model for datum instances
model
,
Schema
the Schema used to construct datum instances
readSchema
,
SchemaStore
a SchemaStore used to find schemas by fingerprint
resolver
)

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

Method Summary

Modifier and TypeMethod and Description
public void
addSchema(Schema
a Schema to use when decoding buffers
writeSchema
)

Adds a Schema that can be used to decode buffers.

public D
decode(InputStream
stream to read from
stream
,
D
a datum instance to reuse, avoiding instantiation if possible
reuse
)

Implements org.apache.avro.message.MessageDecoder.decode.

Deserialize a single datum from an InputStream.
private RawMessageDecoder<D>
getDecoder(long fp)

private boolean

Returns:

true if the buffer is complete, false otherwise (stream ended)
readFully
(InputStream
an InputStream to read from
stream
,
byte[]
a buffer
bytes
)

Reads a buffer from a stream, making multiple read calls if necessary.

Inherited from org.apache.avro.message.MessageDecoder.BaseDecoder:
decodedecodedecodedecodedecode

Field Detail

codecByFingerprintback to summary
private final Map<Long, RawMessageDecoder<D>> codecByFingerprint
FP_BUFFERback to summary
private static final ThreadLocal<ByteBuffer> FP_BUFFER
HEADER_BUFFERback to summary
private static final ThreadLocal<byte[]> HEADER_BUFFER
modelback to summary
private final GenericData model
readSchemaback to summary
private final Schema readSchema
resolverback to summary
private final SchemaStore resolver

Constructor Detail

BinaryMessageDecoderback to summary
public BinaryMessageDecoder(GenericData model, Schema readSchema)

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

The readSchema is as used the expected schema (read schema). Datum instances created by this class will be described by the expected schema.

If readSchema is null, the write schema of an incoming buffer is used as read schema for that datum instance.

The schema used to decode incoming buffers is determined by the schema fingerprint encoded in the message header. This class can decode messages that were encoded using the readSchema (if any) and other schemas that are added using addSchema(Schema).

Parameters
model:GenericData

the data model for datum instances

readSchema:Schema

the Schema used to construct datum instances

BinaryMessageDecoderback to summary
public BinaryMessageDecoder(GenericData model, Schema readSchema, SchemaStore resolver)

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

The readSchema is used as the expected schema (read schema). Datum instances created by this class will be described by the expected schema.

If readSchema is null, the write schema of an incoming buffer is used as read schema for that datum instance.

The schema used to decode incoming buffers is determined by the schema fingerprint encoded in the message header. This class can decode messages that were encoded using the readSchema (if any), other schemas that are added using addSchema(Schema), or schemas returned by the resolver.

Parameters
model:GenericData

the data model for datum instances

readSchema:Schema

the Schema used to construct datum instances

resolver:SchemaStore

a SchemaStore used to find schemas by fingerprint

Method Detail

addSchemaback to summary
public void addSchema(Schema writeSchema)

Adds a Schema that can be used to decode buffers.

Parameters
writeSchema:Schema

a Schema to use when decoding buffers

decodeback to summary
public D decode(InputStream stream, D reuse) throws IOException

Implements org.apache.avro.message.MessageDecoder.decode.

Doc from org.apache.avro.message.MessageDecoder.decode.

Deserialize a single datum from an InputStream.

Parameters
stream:InputStream

stream to read from

reuse:D

a datum instance to reuse, avoiding instantiation if possible

Returns:D

a datum read from the stream

Annotations
@Override
getDecoderback to summary
private RawMessageDecoder<D> getDecoder(long fp)
readFullyback to summary
private boolean readFully(InputStream stream, byte[] bytes) throws IOException

Reads a buffer from a stream, making multiple read calls if necessary.

Parameters
stream:InputStream

an InputStream to read from

bytes:byte[]

a buffer

Returns:boolean

true if the buffer is complete, false otherwise (stream ended)