public class BlockingBinaryEncoder extends BufferedBinaryEncoder
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-safeBinaryEncoder,
EncoderFactory,
Encoder| Modifier and Type | Method and Description |
|---|---|
int |
bytesBuffered()
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 |
startItem()
Start a new item of an array or map.
|
void |
writeArrayEnd()
Call this method to finish writing an array.
|
void |
writeArrayStart()
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 |
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 |
writeMapEnd()
Call this method to terminate the inner-most, currently-opened
map.
|
void |
writeMapStart()
Call this to start a new map.
|
protected void |
writeZero()
Write a zero byte to the underlying output.
|
writeFixedwriteBytes, writeBytes, writeEnum, writeNull, writeString, writeStringwriteBytes, writeFixed, writeStringpublic void flush()
throws IOException
flush in interface Flushableflush in class BufferedBinaryEncoderIOExceptionpublic void writeBoolean(boolean b)
throws IOException
EncoderwriteBoolean in class BufferedBinaryEncoderIOExceptionpublic void writeInt(int n)
throws IOException
EncoderwriteInt in class BufferedBinaryEncoderIOExceptionpublic void writeLong(long n)
throws IOException
EncoderwriteLong in class BufferedBinaryEncoderIOExceptionpublic void writeFloat(float f)
throws IOException
EncoderwriteFloat in class BufferedBinaryEncoderIOExceptionpublic void writeDouble(double d)
throws IOException
EncoderwriteDouble in class BufferedBinaryEncoderIOExceptionpublic void writeFixed(byte[] bytes,
int start,
int len)
throws IOException
EncoderwriteFixed in class BufferedBinaryEncoderbytes - The contents to writestart - The position within bytes where the contents
start.len - The number of bytes to write.IOExceptionprotected void writeZero()
throws IOException
BinaryEncoderwriteZero in class BufferedBinaryEncoderIOExceptionpublic void writeArrayStart()
throws IOException
EncoderEncoder.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 BinaryEncoderIOExceptionpublic void setItemCount(long itemCount)
throws IOException
EncoderEncoder.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 BinaryEncoderitemCount - The number of Encoder.startItem() calls to follow.IOExceptionpublic void startItem()
throws IOException
EncoderEncoder.writeArrayStart() for usage information.startItem in class BinaryEncoderIOExceptionpublic void writeArrayEnd()
throws IOException
EncoderEncoder.writeArrayStart() for usage information.writeArrayEnd in class BinaryEncoderIOExceptionpublic void writeMapStart()
throws IOException
EncoderEncoder.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 BinaryEncoderIOExceptionpublic void writeMapEnd()
throws IOException
EncoderEncoder.writeArrayStart() for more details.writeMapEnd in class BinaryEncoderIOExceptionpublic void writeIndex(int unionIndex)
throws IOException
Encoderout.writeIndex(1); out.writeLong(record.longField); out.writeBoolean(record.boolField);
writeIndex in class BinaryEncoderIOExceptionpublic int bytesBuffered()
BinaryEncoderFlushable.flush() to empty the buffer to the underlying output.bytesBuffered in class BufferedBinaryEncoderCopyright © 2009-2012 The Apache Software Foundation. All Rights Reserved.