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

public Class ValidatingEncoder

extends ParsingEncoder
implements ActionHandler
Class Inheritance
All Implemented Interfaces
org.apache.avro.io.parsing.Parser.ActionHandler
Imports
java.io.IOException, java.nio.ByteBuffer, org.apache.avro.AvroTypeException, .Schema, org.apache.avro.io.parsing.ValidatingGrammarGenerator, .Parser, .Symbol, org.apache.avro.util.Utf8

An implementation of Encoder that wraps another Encoder and ensures that the sequence of operations conforms to the provided schema.

Use EncoderFactory#validatingEncoder(Schema, Encoder) to construct and configure.

ValidatingEncoder is not thread-safe.

See Also
Encoder, EncoderFactory

Field Summary

Modifier and TypeField and Description
protected Encoder
protected final Parser
Inherited from org.apache.avro.io.ParsingEncoder:
pos

Constructor Summary

AccessConstructor and Description
pack-priv
pack-priv

Method Summary

Modifier and TypeMethod and Description
public ValidatingEncoder

Returns:

This ValidatingEncoder.
configure
(Encoder
The Encoder to wrap for validation.
encoder
)

Reconfigures this ValidatingEncoder to wrap the encoder provided.

public Symbol
doAction(Symbol
The input symbol from the caller of advance
input
,
Symbol
The symbol at the top the stack.
top
)

Implements org.apache.avro.io.parsing.Parser.ActionHandler.doAction.

Handle the action symbol top when the input is sought to be taken off the stack.
public void
flush()

Implements java.io.Flushable.flush.

Flushes this stream by writing any buffered output to the underlying stream.
public void
setItemCount(long
The number of startItem() calls to follow.
itemCount
)

Overrides org.apache.avro.io.ParsingEncoder.setItemCount.

Call this method before writing a batch of items in an array or a map.
public void
startItem()

Overrides org.apache.avro.io.ParsingEncoder.startItem.

Start a new item of an array or map.
public void
writeArrayEnd()

Implements abstract org.apache.avro.io.Encoder.writeArrayEnd.

Call this method to finish writing an array.
public void
writeArrayStart()

Implements abstract org.apache.avro.io.Encoder.writeArrayStart.

Call this method to start writing an array.
public void
writeBoolean(boolean b)

Implements abstract org.apache.avro.io.Encoder.writeBoolean.

Write a boolean value.
public void
writeBytes(ByteBuffer bytes)

Implements abstract org.apache.avro.io.Encoder.writeBytes.

Write a byte string.
public void
writeBytes(byte[] bytes, int start, int len)

Implements abstract org.apache.avro.io.Encoder.writeBytes.

Write a byte string.
public void
writeDouble(double d)

Implements abstract org.apache.avro.io.Encoder.writeDouble.

Write a double.
public void
writeEnum(int e)

Implements abstract org.apache.avro.io.Encoder.writeEnum.

Writes an enumeration.
public void
writeFixed(byte[]
The contents to write
bytes
,
int
The position within bytes where the contents start.
start
,
int
The number of bytes to write.
len
)

Implements abstract org.apache.avro.io.Encoder.writeFixed.

Writes a fixed size binary object.
public void
writeFloat(float f)

Implements abstract org.apache.avro.io.Encoder.writeFloat.

Write a float.
public void
writeIndex(int unionIndex)

Implements abstract org.apache.avro.io.Encoder.writeIndex.

Call this method to write the tag of a union.
public void
writeInt(int n)

Implements abstract org.apache.avro.io.Encoder.writeInt.

Writes a 32-bit integer.
public void
writeLong(long n)

Implements abstract org.apache.avro.io.Encoder.writeLong.

Write a 64-bit integer.
public void
writeMapEnd()

Implements abstract org.apache.avro.io.Encoder.writeMapEnd.

Call this method to terminate the inner-most, currently-opened map.
public void
writeMapStart()

Implements abstract org.apache.avro.io.Encoder.writeMapStart.

Call this to start a new map.
public void
writeNull()

Implements abstract org.apache.avro.io.Encoder.writeNull.

"Writes" a null value.
public void
writeString(Utf8 utf8)

Implements abstract org.apache.avro.io.Encoder.writeString.

Write a Unicode character string.
public void
writeString(String str)

Overrides org.apache.avro.io.Encoder.writeString.

Write a Unicode character string.
public void
writeString(CharSequence charSequence)

Overrides org.apache.avro.io.Encoder.writeString.

Write a Unicode character string.
Inherited from org.apache.avro.io.ParsingEncoder:
depthpoppush

Field Detail

outback to summary
protected Encoder out
parserback to summary
protected final Parser parser

Constructor Detail

ValidatingEncoderback to summary
pack-priv ValidatingEncoder(Symbol root, Encoder out) throws IOException
ValidatingEncoderback to summary
pack-priv ValidatingEncoder(Schema schema, Encoder in) throws IOException

Method Detail

configureback to summary
public ValidatingEncoder configure(Encoder encoder)

Reconfigures this ValidatingEncoder to wrap the encoder provided.

Parameters
encoder:Encoder

The Encoder to wrap for validation.

Returns:ValidatingEncoder

This ValidatingEncoder.

doActionback to summary
public Symbol doAction(Symbol input, Symbol top) throws IOException

Implements org.apache.avro.io.parsing.Parser.ActionHandler.doAction.

Doc from org.apache.avro.io.parsing.Parser.ActionHandler.doAction.

Handle the action symbol top when the input is sought to be taken off the stack.

Parameters
input:Symbol

The input symbol from the caller of advance

top:Symbol

The symbol at the top the stack.

Returns:Symbol

null if advance() is to continue processing the stack. If not null the return value will be returned by advance().

Annotations
@Override
flushback to summary
public void flush() throws IOException

Implements java.io.Flushable.flush.

Doc from java.io.Flushable.flush.

Flushes this stream by writing any buffered output to the underlying stream.

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs
setItemCountback to summary
public void setItemCount(long itemCount) throws IOException

Overrides org.apache.avro.io.ParsingEncoder.setItemCount.

Doc from org.apache.avro.io.Encoder.setItemCount.

Call this method before writing a batch of items in an array or a map. Then for each item, call startItem() followed by any of the other write methods of Encoder. The number of calls to startItem() must be equal to the count specified in setItemCount. Once a batch is completed you can start another batch with setItemCount.

Parameters
itemCount:long

The number of startItem() calls to follow.

Annotations
@Override
startItemback to summary
public void startItem() throws IOException

Overrides org.apache.avro.io.ParsingEncoder.startItem.

Doc from org.apache.avro.io.Encoder.startItem.

Start a new item of an array or map. See writeArrayStart for usage information.

Annotations
@Override
writeArrayEndback to summary
public void writeArrayEnd() throws IOException

Implements abstract org.apache.avro.io.Encoder.writeArrayEnd.

Doc from org.apache.avro.io.Encoder.writeArrayEnd.

Call this method to finish writing an array. See writeArrayStart for usage information.

Annotations
@Override
writeArrayStartback to summary
public void writeArrayStart() throws IOException

Implements abstract org.apache.avro.io.Encoder.writeArrayStart.

Doc from org.apache.avro.io.Encoder.writeArrayStart.

Call this method to start writing an array. When starting to serialize an array, call writeArrayStart. Then, before writing any data for any item call setItemCount followed by a sequence of startItem() and the item itself. The number of startItem() should match the number specified in setItemCount. When actually writing the data of the item, you can call any Encoder method (e.g., writeLong). When all items of the array have been written, call writeArrayEnd. As an example, let's say you want to write an array of records, the record consisting of an Long field and a Boolean field. Your code would look something like this:

out.writeArrayStart();
out.setItemCount(list.size());
for (Record r : list) {
  out.startItem();
  out.writeLong(r.longField);
  out.writeBoolean(r.boolField);
}
out.writeArrayEnd();
Annotations
@Override
writeBooleanback to summary
public void writeBoolean(boolean b) throws IOException

Implements abstract org.apache.avro.io.Encoder.writeBoolean.

Doc from org.apache.avro.io.Encoder.writeBoolean.

Write a boolean value.

Annotations
@Override
writeBytesback to summary
public void writeBytes(ByteBuffer bytes) throws IOException

Implements abstract org.apache.avro.io.Encoder.writeBytes.

Doc from org.apache.avro.io.Encoder.writeBytes.

Write a byte string.

Annotations
@Override
writeBytesback to summary
public void writeBytes(byte[] bytes, int start, int len) throws IOException

Implements abstract org.apache.avro.io.Encoder.writeBytes.

Doc from org.apache.avro.io.Encoder.writeBytes.

Write a byte string.

Annotations
@Override
writeDoubleback to summary
public void writeDouble(double d) throws IOException

Implements abstract org.apache.avro.io.Encoder.writeDouble.

Doc from org.apache.avro.io.Encoder.writeDouble.

Write a double.

Annotations
@Override
writeEnumback to summary
public void writeEnum(int e) throws IOException

Implements abstract org.apache.avro.io.Encoder.writeEnum.

Doc from org.apache.avro.io.Encoder.writeEnum.

Writes an enumeration.

Annotations
@Override
writeFixedback to summary
public void writeFixed(byte[] bytes, int start, int len) throws IOException

Implements abstract org.apache.avro.io.Encoder.writeFixed.

Doc from org.apache.avro.io.Encoder.writeFixed.

Writes a fixed size binary object.

Parameters
bytes:byte[]

The contents to write

start:int

The position within bytes where the contents start.

len:int

The number of bytes to write.

Annotations
@Override
writeFloatback to summary
public void writeFloat(float f) throws IOException

Implements abstract org.apache.avro.io.Encoder.writeFloat.

Doc from org.apache.avro.io.Encoder.writeFloat.

Write a float.

Annotations
@Override
writeIndexback to summary
public void writeIndex(int unionIndex) throws IOException

Implements abstract org.apache.avro.io.Encoder.writeIndex.

Doc from org.apache.avro.io.Encoder.writeIndex.

Call this method to write the tag of a union. As an example of usage, let's say you want to write a union, whose second branch is a record consisting of an Long field and a Boolean field. Your code would look something like this:

out.writeIndex(1);
out.writeLong(record.longField);
out.writeBoolean(record.boolField);
Annotations
@Override
writeIntback to summary
public void writeInt(int n) throws IOException

Implements abstract org.apache.avro.io.Encoder.writeInt.

Doc from org.apache.avro.io.Encoder.writeInt.

Writes a 32-bit integer.

Annotations
@Override
writeLongback to summary
public void writeLong(long n) throws IOException

Implements abstract org.apache.avro.io.Encoder.writeLong.

Doc from org.apache.avro.io.Encoder.writeLong.

Write a 64-bit integer.

Annotations
@Override
writeMapEndback to summary
public void writeMapEnd() throws IOException

Implements abstract org.apache.avro.io.Encoder.writeMapEnd.

Doc from org.apache.avro.io.Encoder.writeMapEnd.

Call this method to terminate the inner-most, currently-opened map. See writeArrayStart for more details.

Annotations
@Override
writeMapStartback to summary
public void writeMapStart() throws IOException

Implements abstract org.apache.avro.io.Encoder.writeMapStart.

Doc from org.apache.avro.io.Encoder.writeMapStart.

Call this to start a new map. See writeArrayStart for details on usage. As an example of usage, let's say you want to write a map of records, the record consisting of an Long field and a Boolean field. Your code would look something like this:

out.writeMapStart();
out.setItemCount(list.size());
for (Map.Entry entry : map.entrySet()) {
  out.startItem();
  out.writeString(entry.getKey());
  out.writeLong(entry.getValue().longField);
  out.writeBoolean(entry.getValue().boolField);
}
out.writeMapEnd();
Annotations
@Override
writeNullback to summary
public void writeNull() throws IOException

Implements abstract org.apache.avro.io.Encoder.writeNull.

Doc from org.apache.avro.io.Encoder.writeNull.

"Writes" a null value. (Doesn't actually write anything, but advances the state of the parser if this class is stateful.)

Annotations
@Override
writeStringback to summary
public void writeString(Utf8 utf8) throws IOException

Implements abstract org.apache.avro.io.Encoder.writeString.

Doc from org.apache.avro.io.Encoder.writeString.

Write a Unicode character string.

Annotations
@Override
writeStringback to summary
public void writeString(String str) throws IOException

Overrides org.apache.avro.io.Encoder.writeString.

Doc from org.apache.avro.io.Encoder.writeString.

Write a Unicode character string. The default implementation converts the String to a org.apache.avro.util.Utf8. Some Encoder implementations may want to do something different as a performance optimization.

Annotations
@Override
writeStringback to summary
public void writeString(CharSequence charSequence) throws IOException

Overrides org.apache.avro.io.Encoder.writeString.

Doc from org.apache.avro.io.Encoder.writeString.

Write a Unicode character string. If the CharSequence is an org.apache.avro.util.Utf8 it writes this directly, otherwise the CharSequence is converted to a String via toString() and written.

Annotations
@Override