Top Description Constructors Methods
org.apache.avro.io

public abstract Class Encoder

extends Object
implements Flushable
Class Inheritance
All Implemented Interfaces
java.io.Flushable
Known Direct Subclasses
org.apache.avro.io.ParsingEncoder, org.apache.avro.io.BinaryEncoder
Imports
java.io.Flushable, .IOException, java.nio.ByteBuffer, org.apache.avro.util.Utf8

Low-level support for serializing Avro values.

This class has two types of methods. One type of methods support the writing of leaf values (for example, writeLong and writeString). These methods have analogs in Decoder.

The other type of methods support the writing of maps and arrays. These methods are writeArrayStart, startItem, and writeArrayEnd (and similar methods for maps). Some implementations of Encoder handle the buffering required to break large maps and arrays into blocks, which is necessary for applications that want to do streaming. (See writeArrayStart for details on these methods.)

EncoderFactory contains Encoder construction and configuration facilities.

See Also
EncoderFactory, Decoder

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public abstract void
setItemCount(long
The number of startItem() calls to follow.
itemCount
)

Call this method before writing a batch of items in an array or a map.

public abstract void
startItem()

Start a new item of an array or map.

public abstract void
writeArrayEnd()

Call this method to finish writing an array.

public abstract void
writeArrayStart()

Call this method to start writing an array.

public abstract void
writeBoolean(boolean b)

Write a boolean value.

public abstract void
writeBytes(ByteBuffer bytes)

Write a byte string.

public abstract void
writeBytes(byte[] bytes, int start, int len)

Write a byte string.

public void
writeBytes(byte[] bytes)

Writes a byte string.

public abstract void
writeDouble(double d)

Write a double.

public abstract void
writeEnum(int e)

Writes an enumeration.

public abstract 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
)

Writes a fixed size binary object.

public void
writeFixed(byte[] bytes)

A shorthand for writeFixed(bytes, 0, bytes.length)

public void
writeFixed(ByteBuffer bytes)

Writes a fixed from a ByteBuffer.

public abstract void
writeFloat(float f)

Write a float.

public abstract void
writeIndex(int unionIndex)

Call this method to write the tag of a union.

public abstract void
writeInt(int n)

Writes a 32-bit integer.

public abstract void
writeLong(long n)

Write a 64-bit integer.

public abstract void
writeMapEnd()

Call this method to terminate the inner-most, currently-opened map.

public abstract void
writeMapStart()

Call this to start a new map.

public abstract void
writeNull()

"Writes" a null value.

public abstract void
writeString(Utf8 utf8)

Write a Unicode character string.

public void
writeString(String str)

Write a Unicode character string.

public void
writeString(CharSequence charSequence)

Write a Unicode character string.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

Encoderback to summary
public Encoder()

Method Detail

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

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.

startItemback to summary
public abstract void startItem() throws IOException

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

Exceptions
AvroTypeException:
If called outside of an array or map context
writeArrayEndback to summary
public abstract void writeArrayEnd() throws IOException

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

Exceptions
AvroTypeException:
  • If items written does not match count provided to writeArrayStart
  • If not currently inside an array
writeArrayStartback to summary
public abstract void writeArrayStart() throws IOException

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();
Exceptions
AvroTypeException:
If this is a stateful writer and an array is not expected
writeBooleanback to summary
public abstract void writeBoolean(boolean b) throws IOException

Write a boolean value.

Exceptions
AvroTypeException:
If this is a stateful writer and a boolean is not expected
writeBytesback to summary
public abstract void writeBytes(ByteBuffer bytes) throws IOException

Write a byte string.

Exceptions
AvroTypeException:
If this is a stateful writer and a byte-string is not expected
writeBytesback to summary
public abstract void writeBytes(byte[] bytes, int start, int len) throws IOException

Write a byte string.

Exceptions
AvroTypeException:
If this is a stateful writer and a byte-string is not expected
writeBytesback to summary
public void writeBytes(byte[] bytes) throws IOException

Writes a byte string. Equivalent to writeBytes(bytes, 0, bytes.length)

Exceptions
AvroTypeException:
If this is a stateful writer and a byte-string is not expected
writeDoubleback to summary
public abstract void writeDouble(double d) throws IOException

Write a double.

Exceptions
AvroTypeException:
If this is a stateful writer and a double is not expected
writeEnumback to summary
public abstract void writeEnum(int e) throws IOException

Writes an enumeration.

Exceptions
AvroTypeException:
If this is a stateful writer and an enumeration is not expected or the e is out of range.
writeFixedback to summary
public abstract void writeFixed(byte[] bytes, int start, int len) throws IOException

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.

Exceptions
AvroTypeException:
If this is a stateful writer and a byte-string is not expected
writeFixedback to summary
public void writeFixed(byte[] bytes) throws IOException

A shorthand for writeFixed(bytes, 0, bytes.length)

writeFixedback to summary
public void writeFixed(ByteBuffer bytes) throws IOException

Writes a fixed from a ByteBuffer.

writeFloatback to summary
public abstract void writeFloat(float f) throws IOException

Write a float.

Exceptions
AvroTypeException:
If this is a stateful writer and a float is not expected
writeIndexback to summary
public abstract void writeIndex(int unionIndex) throws IOException

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);
Exceptions
AvroTypeException:
If this is a stateful writer and a map is not expected
writeIntback to summary
public abstract void writeInt(int n) throws IOException

Writes a 32-bit integer.

Exceptions
AvroTypeException:
If this is a stateful writer and an integer is not expected
writeLongback to summary
public abstract void writeLong(long n) throws IOException

Write a 64-bit integer.

Exceptions
AvroTypeException:
If this is a stateful writer and a long is not expected
writeMapEndback to summary
public abstract void writeMapEnd() throws IOException

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

Exceptions
AvroTypeException:
  • If items written does not match count provided to writeMapStart
  • If not currently inside a map
writeMapStartback to summary
public abstract void writeMapStart() throws IOException

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();
Exceptions
AvroTypeException:
If this is a stateful writer and a map is not expected
writeNullback to summary
public abstract void writeNull() throws IOException

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

Exceptions
AvroTypeException:
If this is a stateful writer and a null is not expected
writeStringback to summary
public abstract void writeString(Utf8 utf8) throws IOException

Write a Unicode character string.

Exceptions
AvroTypeException:
If this is a stateful writer and a char-string is not expected
writeStringback to summary
public void writeString(String str) throws IOException

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.

Exceptions
AvroTypeException:
If this is a stateful writer and a char-string is not expected
writeStringback to summary
public void writeString(CharSequence charSequence) throws IOException

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.

Exceptions
AvroTypeException:
If this is a stateful writer and a char-string is not expected