public class PackedByteBuf
extends Object
Modifier and Type | Class and Description |
---|---|
private class |
PackedByteBuf.CompressInfo
Keeps tracks of inflate/deflate stuff on a thread-local basis.
|
Modifier and Type | Field and Description |
---|---|
private byte[] |
bytes
Byte buffer to read from or write to
|
private boolean |
compressed
Tells whether compression succeeded
|
private static ThreadLocal<WeakReference<PackedByteBuf.CompressInfo>> |
compressInfo
Used to compress/decompress data
|
private static int |
compressLimitMax
Maximum buffer size to compress (above this, we wouldn't have
enough bytes to store the length.)
|
private static int |
compressLimitMin
Minimum buffer size to compress (below this, it's probably not
worth even trying).
|
private static byte |
compressMarker
Special marker used to denote a compressed buffer
|
private boolean |
compressTried
Tells whether we've attempted to compress the buffer
|
private boolean |
doNotCompress
Special flag to avoid compression (if buffer will be included in another)
|
private int |
pos
Current position within the byte buffer,
bytes |
private char[] |
strChars
Temporary buffer used when decoding strings
|
private int |
uncompLen
Original (uncompressed) length of the buffer
|
Constructor and Description |
---|
PackedByteBuf(byte[] rawBytes)
Construct a byte buffer from packed data that has been read somehow.
|
PackedByteBuf(DataInput in,
int len)
Construct a byte buffer for reading from.
|
PackedByteBuf(int initialSize)
Construct a byte buffer for writing into.
|
PackedByteBuf(SubStoreReader in,
int len)
Construct a byte buffer for reading from.
|
Modifier and Type | Method and Description |
---|---|
Object |
clone()
Obtain a copy of this buffer (with only the valid bytes)
|
void |
compact()
Makes the buffer as small as possible to hold the existing data; after
this operation, no more data may be added.
|
private void |
compress()
If the buffer hasn't been compressed yet, do so.
|
private void |
decompress()
Given a raw buffer, this method determines if it is compressed, and if
so, decompresses it.
|
void |
doNotCompress()
Call this to avoid compressing the contents of the buffer (for instance, if
it will be copied into another buffer.)
|
private void |
ensureSize(int nBytes)
Make sure that the buffer has room to hold at least nBytes bytes.
|
private PackedByteBuf.CompressInfo |
getCompressInfo()
Obtain thread-local compression info, cached for speed.
|
int |
length()
Returns the number of bytes currently in the buffer.
|
void |
output(DataOutput out)
Copy the entire contents of the buffer to an output sink.
|
void |
output(DataOutput out,
int len)
Copy some or all of the buffer to an output sink.
|
void |
output(SubStoreWriter out)
Copy the entire contents of the buffer to an output sink.
|
void |
output(SubStoreWriter out,
int len)
Copy some or all of the buffer to an output sink.
|
PackedByteBuf |
readBuffer()
Read a buffer that was previously packed into this one with
writeBuffer().
|
byte |
readByte()
Read in a single byte from the buffer.
|
void |
readBytes(byte[] bytes)
Read in a bunch of bytes.
|
void |
readBytes(byte[] outBytes,
int start,
int length)
Read in a bunch of bytes.
|
int |
readInt()
Read an integer from a buffer that was previously made with writeInt().
|
String |
readString()
Read a string from a buffer that was previously made with writeString().
|
void |
reset()
Resets the buffer so that reads/writes occur at the start.
|
void |
setBytes(byte[] rawBytes)
Take a chunk of raw data for unpacking.
|
void |
skipBuffer()
Skip over a buffer that was written with writeBuffer()
|
void |
skipBytes(int num)
Skip a bunch of bytes
|
void |
skipInt()
Skip over an integer made with writeInt()
|
void |
skipString()
Skip over a string that was written with writeString()
|
void |
writeBuffer(PackedByteBuf b)
Write another buffer into this one.
|
void |
writeByte(byte b)
Write out a single byte.
|
void |
writeBytes(byte[] b)
Write out a bunch of bytes
|
void |
writeBytes(byte[] b,
int offset,
int length)
Write out a bunch of bytes
|
void |
writeCharSequence(CharSequence s)
Write a general character sequence to the buffer, using an efficient
format.
|
void |
writeInt(int n)
Write a (non-negative) integer to the buffer.
|
void |
writeString(String s)
Write a string to the buffer, using an efficient format.
|
private byte[] bytes
private int pos
bytes
private char[] strChars
private boolean compressTried
private boolean compressed
private boolean doNotCompress
private int uncompLen
private static ThreadLocal<WeakReference<PackedByteBuf.CompressInfo>> compressInfo
private static final byte compressMarker
private static final int compressLimitMin
private static final int compressLimitMax
public PackedByteBuf(int initialSize)
initialSize
- Hint as to the likely maximum amount of data bytes.
If this is exceeded, the buffer will expand itself
automatically.public PackedByteBuf(SubStoreReader in, int len) throws IOException
in
- Source for datalen
- How many bytes to readIOException
public PackedByteBuf(DataInput in, int len) throws IOException
in
- Source for datalen
- How many bytes to readIOException
public PackedByteBuf(byte[] rawBytes)
rawBytes
- The raw datapublic void setBytes(byte[] rawBytes)
rawBytes
- The raw datapublic void doNotCompress()
private void decompress()
private PackedByteBuf.CompressInfo getCompressInfo()
public Object clone()
clone
in class Object
public void writeByte(byte b)
public void writeBytes(byte[] b)
public void writeBytes(byte[] b, int offset, int length)
public void writeInt(int n)
n
- The number to writepublic void writeString(String s)
s
- The string to write.public void writeCharSequence(CharSequence s)
s
- The sequence to write.public void writeBuffer(PackedByteBuf b)
b
- The buffer to write.private void ensureSize(int nBytes)
public void reset()
private void compress()
public void compact()
public int length()
public void output(SubStoreWriter out) throws IOException
out
- Where to write the data to.IOException
public void output(DataOutput out) throws IOException
out
- Where to write the data to.IOException
public void output(SubStoreWriter out, int len) throws IOException
out
- Where to write the data tolen
- How many bytes to write (okay to exceed buffer length)IOException
public void output(DataOutput out, int len) throws IOException
out
- Where to write the data tolen
- How many bytes to write (okay to exceed buffer length)IOException
public byte readByte()
public void readBytes(byte[] bytes)
public void readBytes(byte[] outBytes, int start, int length)
public void skipBytes(int num)
public int readInt()
public void skipInt()
public String readString()
public void skipString()
public PackedByteBuf readBuffer()
public void skipBuffer()