public class BlockingDirectBinaryEncoder extends DirectBinaryEncoder
Encoder
for Avro's binary encoding that does not buffer output.
This encoder does not buffer writes in contrast to
BufferedBinaryEncoder
. However, it is lighter-weight and useful when:
The buffering in BufferedBinaryEncoder is not desired because you buffer a
different level or the Encoder is very short-lived.
The BlockingDirectBinaryEncoder will encode the number of bytes of the Map
and Array blocks. This will allow to postpone the decoding, or skip over it
at all.
To construct, use
EncoderFactory.blockingDirectBinaryEncoder(OutputStream, BinaryEncoder)
BlockingDirectBinaryEncoder
instances returned by this method are not
thread-safeBinaryEncoder
,
EncoderFactory
,
Encoder
,
Decoder
out
Constructor and Description |
---|
BlockingDirectBinaryEncoder(OutputStream out)
Create a writer that sends its output to the underlying stream
out . |
Modifier and Type | Method and Description |
---|---|
void |
setItemCount(long itemCount)
Call this method before writing a batch of items in an array or a map.
|
void |
writeArrayEnd()
Call this method to finish writing an array.
|
void |
writeArrayStart()
Call this method to start writing an array.
|
void |
writeMapEnd()
Call this method to terminate the inner-most, currently-opened map.
|
void |
writeMapStart()
Call this to start a new map.
|
bytesBuffered, flush, writeBoolean, writeDouble, writeFixed, writeFloat, writeInt, writeLong, writeZero
startItem, writeBytes, writeBytes, writeEnum, writeIndex, writeNull, writeString, writeString
writeBytes, writeFixed, writeFixed, writeString
public BlockingDirectBinaryEncoder(OutputStream out)
out
.out
- The Outputstream to write topublic void setItemCount(long itemCount) throws IOException
Encoder
Encoder.startItem()
followed by any of the other write
methods of Encoder
. The number of calls to Encoder.startItem()
must
be equal to the count specified in Encoder.setItemCount(long)
. Once a batch is
completed you can start another batch with Encoder.setItemCount(long)
.setItemCount
in class BinaryEncoder
itemCount
- The number of Encoder.startItem()
calls to follow.IOException
public void writeArrayStart() throws IOException
Encoder
Encoder.writeArrayStart()
. Then,
before writing any data for any item call Encoder.setItemCount(long)
followed by a
sequence of Encoder.startItem()
and the item itself. The number of
Encoder.startItem()
should match the number specified in
Encoder.setItemCount(long)
. When actually writing the data of the item, you can
call any Encoder
method (e.g., Encoder.writeLong(long)
). When all items of
the array have been written, call Encoder.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();
writeArrayStart
in class BinaryEncoder
IOException
public void writeArrayEnd() throws IOException
Encoder
Encoder.writeArrayStart()
for
usage information.writeArrayEnd
in class BinaryEncoder
IOException
public void writeMapStart() throws IOException
Encoder
Encoder.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.Entryentry : map.entrySet()) { out.startItem(); out.writeString(entry.getKey()); out.writeLong(entry.getValue().longField); out.writeBoolean(entry.getValue().boolField); } out.writeMapEnd();
writeMapStart
in class BinaryEncoder
IOException
public void writeMapEnd() throws IOException
Encoder
Encoder.writeArrayStart()
for more details.writeMapEnd
in class BinaryEncoder
IOException
Copyright © 2009–2024 The Apache Software Foundation. All rights reserved.