Top Description Constructors Methods
org.apache.avro.io

public abstract Class BinaryEncoder

extends Encoder
Class Inheritance
Known Direct Subclasses
org.apache.avro.io.BufferedBinaryEncoder, org.apache.avro.io.DirectBinaryEncoder
Imports
java.io.IOException, java.nio.ByteBuffer, java.nio.charset.StandardCharsets, org.apache.avro.util.Utf8

An abstract Encoder for Avro's binary encoding.

To construct and configure instances, use EncoderFactory

See Also
EncoderFactory, BufferedBinaryEncoder, DirectBinaryEncoder, BlockingBinaryEncoder, Encoder, Decoder

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public abstract int
bytesBuffered()

Returns the number of bytes currently buffered by this encoder.

public void
setItemCount(long
The number of startItem() calls to follow.
itemCount
)

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

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

Implements abstract org.apache.avro.io.Encoder.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
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
writeEnum(int e)

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

Writes an enumeration.
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
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 string)

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

Write a Unicode character string.
protected abstract void
writeZero()

Write a zero byte to the underlying output.

Inherited from org.apache.avro.io.Encoder:
writeBooleanwriteByteswriteDoublewriteFixedwriteFixedwriteFixedwriteFloatwriteIntwriteLongwriteString

Constructor Detail

BinaryEncoderback to summary
public BinaryEncoder()

Method Detail

bytesBufferedback to summary
public abstract int bytesBuffered()

Returns the number of bytes currently buffered by this encoder. If this Encoder does not buffer, this will always return zero.

Call flush() to empty the buffer to the underlying output.

setItemCountback to summary
public void setItemCount(long itemCount) throws IOException

Implements abstract org.apache.avro.io.Encoder.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

Implements abstract org.apache.avro.io.Encoder.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
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
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
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
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 string) 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
writeZeroback to summary
protected abstract void writeZero() throws IOException

Write a zero byte to the underlying output.