@Internal @NotThreadSafe public class BufferedElementCountingOutputStream extends java.io.OutputStream
Iterable
s containing small values by buffering up to
bufferSize
bytes of data before prefixing the count. Note that each element needs to be
encoded in a nested context. See Coder.Context
for more details.
To use this stream:
BufferedElementCountingOutputStream os = ...
for (Element E : elements) {
os.markElementStart();
// write an element to os
}
os.finish();
The resulting output stream is:
countA element(0) element(1) ... element(countA - 1) countB element(0) element(1) ... element(countB - 1) ... countX element(0) element(1) ... element(countX - 1) countY
To read this stream:
InputStream is = ...
long count;
do {
count = VarInt.decodeLong(is);
for (int i = 0; i < count; ++i) {
// read an element from is
}
} while(count > 0);
The counts are encoded as variable length longs. See VarInt.encode(long, OutputStream)
for more details. The end of the iterable is detected by reading a count of 0.
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_BUFFER_SIZE |
Constructor and Description |
---|
BufferedElementCountingOutputStream(java.io.OutputStream os)
Creates an output stream which encodes the number of elements output to it in a streaming
manner.
|
BufferedElementCountingOutputStream(java.io.OutputStream os,
long terminatorValue) |
Modifier and Type | Method and Description |
---|---|
void |
close() |
void |
finish()
Finishes the encoding by flushing any buffered data, and outputting a final count of 0.
|
void |
flush() |
void |
markElementStart()
Marks that a new element is being output.
|
void |
write(byte[] b,
int off,
int len) |
void |
write(int b) |
public static final int DEFAULT_BUFFER_SIZE
public BufferedElementCountingOutputStream(java.io.OutputStream os)
public BufferedElementCountingOutputStream(java.io.OutputStream os, long terminatorValue)
public void finish() throws java.io.IOException
java.io.IOException
public void markElementStart() throws java.io.IOException
java.io.IOException
public void write(int b) throws java.io.IOException
write
in class java.io.OutputStream
java.io.IOException
public void write(byte[] b, int off, int len) throws java.io.IOException
write
in class java.io.OutputStream
java.io.IOException
public void flush() throws java.io.IOException
flush
in interface java.io.Flushable
flush
in class java.io.OutputStream
java.io.IOException
public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.OutputStream
java.io.IOException