public final class ByteBuffersDataOutput extends DataOutput implements Accountable
DataOutput storing data in a list of ByteBuffers.| Modifier and Type | Class and Description |
|---|---|
static class |
ByteBuffersDataOutput.ByteBufferRecycler
An implementation of a
ByteBuffer allocation and recycling policy. |
| Modifier and Type | Field and Description |
|---|---|
static java.util.function.IntFunction<java.nio.ByteBuffer> |
ALLOCATE_BB_ON_HEAP |
private java.util.function.IntFunction<java.nio.ByteBuffer> |
blockAllocate
ByteBuffer supplier. |
private int |
blockBits
Current block size:
2^bits. |
private java.util.function.Consumer<java.nio.ByteBuffer> |
blockReuse
ByteBuffer recycler on reset(). |
private java.util.ArrayDeque<java.nio.ByteBuffer> |
blocks
Blocks storing data.
|
private java.nio.ByteBuffer |
currentBlock
The current-or-next write block.
|
static int |
DEFAULT_MAX_BITS_PER_BLOCK |
static int |
DEFAULT_MIN_BITS_PER_BLOCK |
private static java.nio.ByteBuffer |
EMPTY |
private static byte[] |
EMPTY_BYTE_ARRAY |
private static long |
HALF_SHIFT |
(package private) static int |
MAX_BLOCKS_BEFORE_BLOCK_EXPANSION
Maximum number of blocks at the current
blockBits block size
before we increase the block size (and thus decrease the number of blocks). |
private int |
maxBitsPerBlock
Maximum block size:
2^bits. |
static java.util.function.Consumer<java.nio.ByteBuffer> |
NO_REUSE
A singleton instance of "no-reuse" buffer strategy.
|
private static int |
SURROGATE_OFFSET |
| Constructor and Description |
|---|
ByteBuffersDataOutput() |
ByteBuffersDataOutput(int minBitsPerBlock,
int maxBitsPerBlock,
java.util.function.IntFunction<java.nio.ByteBuffer> blockAllocate,
java.util.function.Consumer<java.nio.ByteBuffer> blockReuse) |
ByteBuffersDataOutput(long expectedSize) |
| Modifier and Type | Method and Description |
|---|---|
private void |
appendBlock() |
private int |
blockSize() |
private static int |
computeBlockSizeBitsFor(long bytes) |
void |
copyTo(DataOutput output)
Copy the current content of this object into another
DataOutput. |
static ByteBuffersDataOutput |
newResettableInstance() |
long |
ramBytesUsed()
Return the memory usage of this object in bytes.
|
void |
reset()
This method resets this object to a clean (zero-size) state and
publishes any currently allocated buffers for reuse to the reuse strategy
provided in the constructor.
|
private void |
rewriteToBlockSize(int targetBlockBits) |
long |
size() |
byte[] |
toArrayCopy()
Return a contiguous array with the current content written to the output.
|
java.util.ArrayList<java.nio.ByteBuffer> |
toBufferList()
Return a list of read-only view of
ByteBuffer blocks over the
current content written to the output. |
ByteBuffersDataInput |
toDataInput()
Return a
ByteBuffersDataInput for the set of current buffers (toBufferList()). |
java.lang.String |
toString() |
java.util.ArrayList<java.nio.ByteBuffer> |
toWriteableBufferList()
Returns a list of writeable blocks over the (source) content buffers.
|
private static int |
UTF16toUTF8(java.lang.CharSequence s,
int offset,
int length,
byte[] buf,
java.util.function.IntConsumer bufferFlusher)
A consumer-based UTF16-UTF8 encoder (writes the input string in smaller buffers.).
|
void |
writeByte(byte b)
Writes a single byte.
|
void |
writeBytes(byte[] b) |
void |
writeBytes(byte[] b,
int length)
Writes an array of bytes.
|
void |
writeBytes(byte[] src,
int offset,
int length)
Writes an array of bytes.
|
void |
writeBytes(java.nio.ByteBuffer buffer) |
void |
writeInt(int v)
Writes an int as four bytes.
|
void |
writeLong(long v)
Writes a long as eight bytes.
|
void |
writeMapOfStrings(java.util.Map<java.lang.String,java.lang.String> map)
Writes a String map.
|
void |
writeSetOfStrings(java.util.Set<java.lang.String> set)
Writes a String set.
|
void |
writeShort(short v)
Writes a short as two bytes.
|
void |
writeString(java.lang.String v)
Writes a string.
|
copyBytes, writeVInt, writeVLong, writeZInt, writeZLongclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitgetChildResourcesprivate static final java.nio.ByteBuffer EMPTY
private static final byte[] EMPTY_BYTE_ARRAY
public static final java.util.function.IntFunction<java.nio.ByteBuffer> ALLOCATE_BB_ON_HEAP
public static final java.util.function.Consumer<java.nio.ByteBuffer> NO_REUSE
public static final int DEFAULT_MIN_BITS_PER_BLOCK
public static final int DEFAULT_MAX_BITS_PER_BLOCK
static final int MAX_BLOCKS_BEFORE_BLOCK_EXPANSION
blockBits block size
before we increase the block size (and thus decrease the number of blocks).private final int maxBitsPerBlock
2^bits.private final java.util.function.IntFunction<java.nio.ByteBuffer> blockAllocate
ByteBuffer supplier.private final java.util.function.Consumer<java.nio.ByteBuffer> blockReuse
ByteBuffer recycler on reset().private int blockBits
2^bits.private final java.util.ArrayDeque<java.nio.ByteBuffer> blocks
private java.nio.ByteBuffer currentBlock
private static final long HALF_SHIFT
private static final int SURROGATE_OFFSET
public ByteBuffersDataOutput(long expectedSize)
public ByteBuffersDataOutput()
public ByteBuffersDataOutput(int minBitsPerBlock,
int maxBitsPerBlock,
java.util.function.IntFunction<java.nio.ByteBuffer> blockAllocate,
java.util.function.Consumer<java.nio.ByteBuffer> blockReuse)
public void writeByte(byte b)
DataOutputThe most primitive data type is an eight-bit byte. Files are accessed as sequences of bytes. All other data types are defined as sequences of bytes, so file formats are byte-order independent.
writeByte in class DataOutputDataInput.readByte()public void writeBytes(byte[] src,
int offset,
int length)
DataOutputwriteBytes in class DataOutputsrc - the bytes to writeoffset - the offset in the byte arraylength - the number of bytes to writeDataInput.readBytes(byte[],int,int)public void writeBytes(byte[] b,
int length)
DataOutputwriteBytes in class DataOutputb - the bytes to writelength - the number of bytes to writeDataInput.readBytes(byte[],int,int)public void writeBytes(byte[] b)
public void writeBytes(java.nio.ByteBuffer buffer)
public java.util.ArrayList<java.nio.ByteBuffer> toBufferList()
ByteBuffer blocks over the
current content written to the output.public java.util.ArrayList<java.nio.ByteBuffer> toWriteableBufferList()
toBufferList() which returns a read-only view over
the content of the source buffers.
The difference between toBufferList() and toWriteableBufferList() is that
read-only view of source buffers will always return false from ByteBuffer.hasArray()
(which sometimes may be required to avoid double copying).public ByteBuffersDataInput toDataInput()
ByteBuffersDataInput for the set of current buffers (toBufferList()).public byte[] toArrayCopy()
size() of the underlying buffers exceeds maximum size of Java array, an
RuntimeException will be thrown.public void copyTo(DataOutput output) throws java.io.IOException
DataOutput.java.io.IOExceptionpublic long size()
public java.lang.String toString()
toString in class java.lang.Objectpublic void writeShort(short v)
DataOutputwriteShort in class DataOutputDataInput.readShort()public void writeInt(int v)
DataOutput32-bit unsigned integer written as four bytes, high-order bytes first.
writeInt in class DataOutputDataInput.readInt()public void writeLong(long v)
DataOutput64-bit unsigned integer written as eight bytes, high-order bytes first.
writeLong in class DataOutputDataInput.readLong()public void writeString(java.lang.String v)
DataOutput
Writes strings as UTF-8 encoded bytes. First the length, in bytes, is
written as a VInt, followed by the bytes.
writeString in class DataOutputDataInput.readString()public void writeMapOfStrings(java.util.Map<java.lang.String,java.lang.String> map)
DataOutput
First the size is written as an vInt,
followed by each key-value pair written as two consecutive
Strings.
writeMapOfStrings in class DataOutputmap - Input map.public void writeSetOfStrings(java.util.Set<java.lang.String> set)
DataOutput
First the size is written as an vInt,
followed by each value written as a
String.
writeSetOfStrings in class DataOutputset - Input set.public long ramBytesUsed()
AccountableramBytesUsed in interface Accountablepublic void reset()
public static ByteBuffersDataOutput newResettableInstance()
ByteBuffersDataOutput with the reset() capability.private int blockSize()
private void appendBlock()
private void rewriteToBlockSize(int targetBlockBits)
private static int computeBlockSizeBitsFor(long bytes)
private static int UTF16toUTF8(java.lang.CharSequence s,
int offset,
int length,
byte[] buf,
java.util.function.IntConsumer bufferFlusher)