Package org.apache.avro.generic

# Class GenericDatumReader<D>

java.lang.Object
org.apache.avro.generic.GenericDatumReader<D>

All Implemented Interfaces:
  `DatumReader<D>`

Direct Known Subclasses:
  `SpecificDatumReader`

public class GenericDatumReader<D> extends Object implements DatumReader<D>
`DatumReader` for generic Java objects.

## Constructor Summary

Constructors
Modifier
Constructor
Description
``
`GenericDatumReader()`
`protected`
`GenericDatumReader(GenericData data)`
``
`GenericDatumReader(Schema schema)`
Construct where the writer's and reader's schemas are the same.
``
`GenericDatumReader(Schema writer, Schema reader)`
Construct given writer's and reader's schema.
``
`GenericDatumReader(Schema writer, Schema reader, GenericData data)`

## Method Summary

Modifier and Type
Method
Description
`protected void`
`addToArray(Object array, long pos, Object e)`
Called by the default implementation of `readArray(java.lang.Object, org.apache.avro.Schema, org.apache.avro.io.ResolvingDecoder)` to add a value.
`protected void`
`addToMap(Object map, Object key, Object value)`
Called by the default implementation of `readMap(java.lang.Object, org.apache.avro.Schema, org.apache.avro.io.ResolvingDecoder)` to add a key/value pair.
`protected Object`
`convert(Object datum, Schema schema, LogicalType type, Conversion<?> conversion)`
Convert an underlying representation of a logical type (such as a ByteBuffer) to a higher level object (such as a BigDecimal).
`protected Object`
`createBytes(byte[] value)`
Called to create byte arrays from default values.
`protected Object`
`createEnum(String symbol, Schema schema)`
Called to create an enum value.
`protected Object`
`createFixed(Object old, byte[] bytes, Schema schema)`
Deprecated.
As of Avro 1.6.0 this method has been moved to `GenericData.createFixed(Object, byte[], Schema)`
`protected Object`
`createFixed(Object old, Schema schema)`
Deprecated.
As of Avro 1.6.0 this method has been moved to `GenericData.createFixed(Object, Schema)`
`protected Object`
`createString(String value)`
Called to create a string from a default value.
`static void`
`ensureAvailableCollectionBytes(Decoder decoder, long count, Schema elementSchema)`
Validates that the decoder has enough remaining bytes to hold `count` elements of the given schema, assuming each element requires at least `minBytesPerElement(org.apache.avro.Schema)` bytes.
`protected Class`
`findStringClass(Schema schema)`
Determines the class to used to represent a string Schema.
`GenericData`
`getData()`
Return the `GenericData` implementation.
`Schema`
`getExpected()`
Get the reader's schema.
`protected final ResolvingDecoder`
`getResolver(Schema actual, Schema expected)`
Gets a resolving decoder for use by this GenericDatumReader.
`Schema`
`getSchema()`
Return the writer's schema.
`static int`
`initialCollectionCapacity(long count)`
Clamp a declared collection block count to a safe initial allocation size.
`static boolean`
`isZeroByteSchema(Schema schema)`
Whether the minimum encoded size of the given schema is zero, i.e.
`protected Object`
`newArray(Object old, int size, Schema schema)`
Called to create new array instances.
`protected Object`
`newInstanceFromString(Class c, String s)`
`protected Object`
`newMap(Object old, int size)`
Called to create new array instances.
`protected Object`
`newRecord(Object old, Schema schema)`
Deprecated.
As of Avro 1.6.0 this method has been moved to `GenericData.newRecord(Object, Schema)`
`protected Object`
`peekArray(Object array)`
Called by the default implementation of `readArray(java.lang.Object, org.apache.avro.Schema, org.apache.avro.io.ResolvingDecoder)` to retrieve a value from a reused instance.
`D`
`read(D reuse, Decoder in)`
Read a datum.
`protected Object`
`read(Object old, Schema expected, ResolvingDecoder in)`
Called to read data.
`protected Object`
`readArray(Object old, Schema expected, ResolvingDecoder in)`
Called to read an array instance.
`protected Object`
`readBytes(Object old, Decoder in)`
Called to read byte arrays.
`protected Object`
`readBytes(Object old, Schema s, Decoder in)`
Called to read byte arrays.
`protected Object`
`readEnum(Schema expected, Decoder in)`
Called to read an enum value.
`protected void`
`readField(Object record, Schema.Field field, Object oldDatum, ResolvingDecoder in, Object state)`
Called to read a single field of a record.
`protected Object`
`readFixed(Object old, Schema expected, Decoder in)`
Called to read a fixed value.
`protected Object`
`readInt(Object old, Schema expected, Decoder in)`
Called to read integers.
`protected Object`
`readMap(Object old, Schema expected, ResolvingDecoder in)`
Called to read a map instance.
`protected Object`
`readMapKey(Object old, Schema expected, Decoder in)`
Called by the default implementation of `readMap(java.lang.Object, org.apache.avro.Schema, org.apache.avro.io.ResolvingDecoder)` to read a key value.
`protected Object`
`readRecord(Object old, Schema expected, ResolvingDecoder in)`
Called to read a record instance.
`protected Object`
`readString(Object old, Decoder in)`
Called to read strings.
`protected Object`
`readString(Object old, Schema expected, Decoder in)`
Called to read strings.
`protected Object`
`readWithConversion(Object old, Schema expected, LogicalType logicalType, Conversion<?> conversion, ResolvingDecoder in)`
`protected Object`
`readWithoutConversion(Object old, Schema expected, ResolvingDecoder in)`
`void`
`setExpected(Schema reader)`
Set the reader's schema.
`void`
`setSchema(Schema writer)`
Set the writer's schema.
`static void`
`skip(Schema schema, Decoder in)`
Skip an instance of a schema.

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

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

## Constructor Details

### GenericDatumReader

public GenericDatumReader()

### GenericDatumReader

public GenericDatumReader(Schema schema)
Construct where the writer's and reader's schemas are the same.

### GenericDatumReader

public GenericDatumReader(Schema writer, Schema reader)
Construct given writer's and reader's schema.

### GenericDatumReader

public GenericDatumReader(Schema writer, Schema reader, GenericData data)

### GenericDatumReader

protected GenericDatumReader(GenericData data)

## Method Details

### getData

public GenericData getData()
Return the `GenericData` implementation.

### getSchema

public Schema getSchema()
Return the writer's schema.

### setSchema

public void setSchema(Schema writer)
Description copied from interface: `DatumReader`
Set the writer's schema.

Specified by:
  `setSchema` in interface `DatumReader<D>`

### getExpected

public Schema getExpected()
Get the reader's schema.

### setExpected

public void setExpected(Schema reader)
Set the reader's schema.

### initialCollectionCapacity

public static int initialCollectionCapacity(long count)
Clamp a declared collection block count to a safe initial allocation size.

Parameters:
  `count` - the declared (already limit-checked) block count
Returns:
  `count` capped at `MAX_COLLECTION_PREALLOC`

### getResolver

protected final ResolvingDecoder getResolver(Schema actual, Schema expected) throws IOException
Gets a resolving decoder for use by this GenericDatumReader. Unstable API. Currently uses a thread local cache to prevent constructing the resolvers too often, because that is very expensive.

Throws:
  `IOException`

### read

public D read(D reuse, Decoder in) throws IOException
Description copied from interface: `DatumReader`
Read a datum. Traverse the schema, depth-first, reading all leaf values in the schema into a datum that is returned. If the provided datum is non-null it may be reused and returned.

Specified by:
  `read` in interface `DatumReader<D>`
Throws:
  `IOException`

### read

protected Object read(Object old, Schema expected, ResolvingDecoder in) throws IOException
Called to read data.

Throws:
  `IOException`

### readWithConversion

protected Object readWithConversion(Object old, Schema expected, LogicalType logicalType, Conversion<?> conversion, ResolvingDecoder in) throws IOException

Throws:
  `IOException`

### readWithoutConversion

protected Object readWithoutConversion(Object old, Schema expected, ResolvingDecoder in) throws IOException

Throws:
  `IOException`

### convert

protected Object convert(Object datum, Schema schema, LogicalType type, Conversion<?> conversion)
Convert an underlying representation of a logical type (such as a ByteBuffer) to a higher level object (such as a BigDecimal).

Throws:
  `IllegalArgumentException` - if a null schema or logicalType is passed in while datum and conversion are not null. Please be noticed that the exception type has changed. With version 1.8.0 and earlier, in above circumstance, the exception thrown out depends on the implementation of conversion (most likely a NullPointerException). Now, an IllegalArgumentException will be thrown out instead.

### readRecord

protected Object readRecord(Object old, Schema expected, ResolvingDecoder in) throws IOException
Called to read a record instance. May be overridden for alternate record representations.

Throws:
  `IOException`

### readField

protected void readField(Object record, Schema.Field field, Object oldDatum, ResolvingDecoder in, Object state) throws IOException
Called to read a single field of a record. May be overridden for more efficient or alternate implementations.

Throws:
  `IOException`

### readEnum

protected Object readEnum(Schema expected, Decoder in) throws IOException
Called to read an enum value. May be overridden for alternate enum representations. By default, returns a GenericEnumSymbol.

Throws:
  `IOException`

### createEnum

protected Object createEnum(String symbol, Schema schema)
Called to create an enum value. May be overridden for alternate enum representations. By default, returns a GenericEnumSymbol.

### readArray

protected Object readArray(Object old, Schema expected, ResolvingDecoder in) throws IOException
Called to read an array instance. May be overridden for alternate array representations.

Throws:
  `IOException`

### peekArray

protected Object peekArray(Object array)
Called by the default implementation of `readArray(java.lang.Object, org.apache.avro.Schema, org.apache.avro.io.ResolvingDecoder)` to retrieve a value from a reused instance. The default implementation is for `GenericArray`.

### addToArray

protected void addToArray(Object array, long pos, Object e)
Called by the default implementation of `readArray(java.lang.Object, org.apache.avro.Schema, org.apache.avro.io.ResolvingDecoder)` to add a value. The default implementation is for `Collection`.

### readMap

protected Object readMap(Object old, Schema expected, ResolvingDecoder in) throws IOException
Called to read a map instance. May be overridden for alternate map representations.

Throws:
  `IOException`

### readMapKey

protected Object readMapKey(Object old, Schema expected, Decoder in) throws IOException
Called by the default implementation of `readMap(java.lang.Object, org.apache.avro.Schema, org.apache.avro.io.ResolvingDecoder)` to read a key value. The default implementation returns delegates to `readString(Object, org.apache.avro.io.Decoder)`.

Throws:
  `IOException`

### addToMap

protected void addToMap(Object map, Object key, Object value)
Called by the default implementation of `readMap(java.lang.Object, org.apache.avro.Schema, org.apache.avro.io.ResolvingDecoder)` to add a key/value pair. The default implementation is for `Map`.

### isZeroByteSchema

public static boolean isZeroByteSchema(Schema schema)
Whether the minimum encoded size of the given schema is zero, i.e. `minBytesPerElement(Schema)` is `0`. This is true for values that always encode to zero bytes (e.g. `null`, a zero-length `fixed`, or a record whose fields are all zero-byte), and conservatively for recursive schemas, where the cycle is broken by returning a 0 minimum. Such elements cannot be bounded by the number of bytes remaining in the stream, so a collection of them must be bounded by a heap-aware allocation limit instead.

Parameters:
  `schema` - the element (or map value) schema
Returns:
  `true` if the schema's minimum encoded size is zero

### ensureAvailableCollectionBytes

public static void ensureAvailableCollectionBytes(Decoder decoder, long count, Schema elementSchema) throws EOFException
Validates that the decoder has enough remaining bytes to hold `count` elements of the given schema, assuming each element requires at least `minBytesPerElement(org.apache.avro.Schema)` bytes. Throws `EOFException` if the decoder reports fewer remaining bytes than required.

This check prevents out-of-memory errors from pre-allocating huge backing arrays when the source data is truncated or malicious. It is exposed so the fast reader (`FastReaderBuilder`) can apply the same guard as this classic reader.

Throws:
  `EOFException`

### readFixed

protected Object readFixed(Object old, Schema expected, Decoder in) throws IOException
Called to read a fixed value. May be overridden for alternate fixed representations. By default, returns `GenericFixed`.

Throws:
  `IOException`

### createFixed

@Deprecated protected Object createFixed(Object old, Schema schema)
Deprecated.
As of Avro 1.6.0 this method has been moved to `GenericData.createFixed(Object, Schema)`
Called to create an fixed value. May be overridden for alternate fixed representations. By default, returns `GenericFixed`.

### createFixed

@Deprecated protected Object createFixed(Object old, byte[] bytes, Schema schema)
Deprecated.
As of Avro 1.6.0 this method has been moved to `GenericData.createFixed(Object, byte[], Schema)`
Called to create an fixed value. May be overridden for alternate fixed representations. By default, returns `GenericFixed`.

### newRecord

@Deprecated protected Object newRecord(Object old, Schema schema)
Deprecated.
As of Avro 1.6.0 this method has been moved to `GenericData.newRecord(Object, Schema)`
Called to create new record instances. Subclasses may override to use a different record implementation. The returned instance must conform to the schema provided. If the old object contains fields not present in the schema, they should either be removed from the old object, or it should create a new instance that conforms to the schema. By default, this returns a `GenericData.Record`.

### newArray

protected Object newArray(Object old, int size, Schema schema)
Called to create new array instances. Subclasses may override to use a different array implementation. By default, this returns a `GenericData.Array`.

### newMap

protected Object newMap(Object old, int size)
Called to create new array instances. Subclasses may override to use a different map implementation. By default, this returns a `HashMap`.

### readString

protected Object readString(Object old, Schema expected, Decoder in) throws IOException
Called to read strings. Subclasses may override to use a different string representation. By default, this calls `readString(Object,Decoder)`.

Throws:
  `IOException`

### readString

protected Object readString(Object old, Decoder in) throws IOException
Called to read strings. Subclasses may override to use a different string representation. By default, this calls `Decoder.readString(Utf8)`.

Throws:
  `IOException`

### createString

protected Object createString(String value)
Called to create a string from a default value. Subclasses may override to use a different string representation. By default, this calls `Utf8(String)`.

### findStringClass

protected Class findStringClass(Schema schema)
Determines the class to used to represent a string Schema. By default uses `GenericData.STRING_PROP` to determine whether `Utf8` or `String` is used. Subclasses may override for alternate representations.

### newInstanceFromString

protected Object newInstanceFromString(Class c, String s)

### readBytes

protected Object readBytes(Object old, Schema s, Decoder in) throws IOException
Called to read byte arrays. Subclasses may override to use a different byte array representation. By default, this calls `Decoder.readBytes(ByteBuffer)`.

Throws:
  `IOException`

### readBytes

protected Object readBytes(Object old, Decoder in) throws IOException
Called to read byte arrays. Subclasses may override to use a different byte array representation. By default, this calls `Decoder.readBytes(ByteBuffer)`.

Throws:
  `IOException`

### readInt

protected Object readInt(Object old, Schema expected, Decoder in) throws IOException
Called to read integers. Subclasses may override to use a different integer representation. By default, this calls `Decoder.readInt()`.

Throws:
  `IOException`

### createBytes

protected Object createBytes(byte[] value)
Called to create byte arrays from default values. Subclasses may override to use a different byte array representation. By default, this calls `ByteBuffer.wrap(byte[])`.

### skip

public static void skip(Schema schema, Decoder in) throws IOException
Skip an instance of a schema.

Throws:
  `IOException`