Package org.apache.avro.io
Class BlockingBinaryEncoder
java.lang.Object
org.apache.avro.io.Encoder
org.apache.avro.io.BinaryEncoder
org.apache.avro.io.BufferedBinaryEncoder
org.apache.avro.io.BlockingBinaryEncoder
- All Implemented Interfaces:
Flushable
A
BinaryEncoder
implementation that writes large arrays and maps as a
sequence of blocks. So long as individual primitive values fit in memory,
arbitrarily long arrays and maps may be written and subsequently read without
exhausting memory. Values are buffered until the specified block size would
be exceeded, minimizing block overhead.
Use EncoderFactory.blockingBinaryEncoder(OutputStream, BinaryEncoder)
to construct and configure.
BlockingBinaryEncoder buffers writes, data may not appear on the output until
flush()
is called.
BlockingBinaryEncoder is not thread-safe- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionint
Returns the number of bytes currently buffered by this encoder.void
flush()
void
setItemCount
(long itemCount) Call this method before writing a batch of items in an array or a map.void
Start a new item of an array or map.void
Call this method to finish writing an array.void
Call this method to start writing an array.void
writeBoolean
(boolean b) Write a boolean value.void
writeDouble
(double d) Write a double.void
writeFixed
(byte[] bytes, int start, int len) Writes a fixed size binary object.void
writeFixed
(ByteBuffer bytes) Writes a fixed from a ByteBuffer.void
writeFloat
(float f) Write a float.void
writeIndex
(int unionIndex) Call this method to write the tag of a union.void
writeInt
(int n) Writes a 32-bit integer.void
writeLong
(long n) Write a 64-bit integer.void
Call this method to terminate the inner-most, currently-opened map.void
Call this to start a new map.protected void
Write a zero byte to the underlying output.Methods inherited from class org.apache.avro.io.BinaryEncoder
writeBytes, writeBytes, writeEnum, writeNull, writeString, writeString
Methods inherited from class org.apache.avro.io.Encoder
writeBytes, writeFixed, writeString
-
Method Details
-
flush
- Specified by:
flush
in interfaceFlushable
- Overrides:
flush
in classBufferedBinaryEncoder
- Throws:
IOException
-
writeBoolean
Description copied from class:Encoder
Write a boolean value.- Overrides:
writeBoolean
in classBufferedBinaryEncoder
- Throws:
IOException
-
writeInt
Description copied from class:Encoder
Writes a 32-bit integer.- Overrides:
writeInt
in classBufferedBinaryEncoder
- Throws:
IOException
-
writeLong
Description copied from class:Encoder
Write a 64-bit integer.- Overrides:
writeLong
in classBufferedBinaryEncoder
- Throws:
IOException
-
writeFloat
Description copied from class:Encoder
Write a float.- Overrides:
writeFloat
in classBufferedBinaryEncoder
- Throws:
IOException
-
writeDouble
Description copied from class:Encoder
Write a double.- Overrides:
writeDouble
in classBufferedBinaryEncoder
- Throws:
IOException
-
writeFixed
Description copied from class:Encoder
Writes a fixed size binary object.- Overrides:
writeFixed
in classBufferedBinaryEncoder
- Parameters:
bytes
- The contents to writestart
- The position within bytes where the contents start.len
- The number of bytes to write.- Throws:
IOException
-
writeFixed
Description copied from class:Encoder
Writes a fixed from a ByteBuffer.- Overrides:
writeFixed
in classBufferedBinaryEncoder
- Throws:
IOException
-
writeZero
Description copied from class:BinaryEncoder
Write a zero byte to the underlying output.- Overrides:
writeZero
in classBufferedBinaryEncoder
- Throws:
IOException
-
writeArrayStart
Description copied from class:Encoder
Call this method to start writing an array. When starting to serialize an array, callEncoder.writeArrayStart()
. Then, before writing any data for any item callEncoder.setItemCount(long)
followed by a sequence ofEncoder.startItem()
and the item itself. The number ofEncoder.startItem()
should match the number specified inEncoder.setItemCount(long)
. When actually writing the data of the item, you can call anyEncoder
method (e.g.,Encoder.writeLong(long)
). When all items of the array have been written, callEncoder.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();
- Overrides:
writeArrayStart
in classBinaryEncoder
- Throws:
IOException
-
setItemCount
Description copied from class:Encoder
Call this method before writing a batch of items in an array or a map. Then for each item, callEncoder.startItem()
followed by any of the other write methods ofEncoder
. The number of calls toEncoder.startItem()
must be equal to the count specified inEncoder.setItemCount(long)
. Once a batch is completed you can start another batch withEncoder.setItemCount(long)
.- Overrides:
setItemCount
in classBinaryEncoder
- Parameters:
itemCount
- The number ofEncoder.startItem()
calls to follow.- Throws:
IOException
-
startItem
Description copied from class:Encoder
Start a new item of an array or map. SeeEncoder.writeArrayStart()
for usage information.- Overrides:
startItem
in classBinaryEncoder
- Throws:
IOException
-
writeArrayEnd
Description copied from class:Encoder
Call this method to finish writing an array. SeeEncoder.writeArrayStart()
for usage information.- Overrides:
writeArrayEnd
in classBinaryEncoder
- Throws:
IOException
-
writeMapStart
Description copied from class:Encoder
Call this to start a new map. SeeEncoder.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.Entryinvalid input: '<'String, Record> entry : map.entrySet()) { out.startItem(); out.writeString(entry.getKey()); out.writeLong(entry.getValue().longField); out.writeBoolean(entry.getValue().boolField); } out.writeMapEnd();
- Overrides:
writeMapStart
in classBinaryEncoder
- Throws:
IOException
-
writeMapEnd
Description copied from class:Encoder
Call this method to terminate the inner-most, currently-opened map. SeeEncoder.writeArrayStart()
for more details.- Overrides:
writeMapEnd
in classBinaryEncoder
- Throws:
IOException
-
writeIndex
Description copied from class:Encoder
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);
- Overrides:
writeIndex
in classBinaryEncoder
- Throws:
IOException
-
bytesBuffered
public int bytesBuffered()Description copied from class:BinaryEncoder
Returns the number of bytes currently buffered by this encoder. If this Encoder does not buffer, this will always return zero. CallFlushable.flush()
to empty the buffer to the underlying output.- Overrides:
bytesBuffered
in classBufferedBinaryEncoder
-