public abstract class BasicPacket extends Object
Modifier and Type | Field and Description |
---|---|
static int |
CODE_MASK |
private int |
compressionLimit |
private com.yahoo.compress.CompressionType |
compressionType |
private com.yahoo.compress.Compressor |
compressor |
private static int |
DEFAULT_WRITE_BUFFER_SIZE |
protected byte[] |
encodedBody |
protected ByteBuffer |
encodingBuffer |
protected int |
length
The length of this packet in bytes or -1 if not known
|
private static Logger |
log |
private long |
timeStamp
A timestamp which can be set or inspected by clients of this class
but which is never updated by the class itself.
|
Constructor and Description |
---|
BasicPacket() |
Modifier and Type | Method and Description |
---|---|
void |
allocateAndEncode(int channelId)
Allocate the needed buffers and encode the packet using the given
channel ID (if pertinent).
|
private void |
allocateAndEncode(int channelId,
int initialSize) |
protected void |
codeDecodedHook(int code)
Called when the packet code is decoded.
|
BasicPacket |
decode(ByteBuffer buffer)
Fills this package from a byte buffer positioned at the first byte of the package
|
protected void |
decodeAndDecompressBody(ByteBuffer buffer,
int code,
int packetLength) |
void |
decodeBody(ByteBuffer buffer)
Decodes the body of this package from a byte buffer
positioned at the first byte of the package.
|
BasicPacket |
encode(ByteBuffer buffer)
Encodes this package onto the given buffer at the current position.
|
Packet |
encode(ByteBuffer buffer,
int channel)
Just a place holder to make the APIs simpler.
|
protected void |
encodeAndCompressBody(ByteBuffer buffer,
int startPosition) |
protected void |
encodeBody(ByteBuffer buffer)
Encodes the body of this package onto the given buffer at the current position.
|
abstract int |
getCode()
Returns the code of this package
|
private int |
getCompressedCode(com.yahoo.compress.CompressionType compression) |
int |
getLength()
Returns the length of this body (including header (8 bytes) and body),
or -1 if not known.
|
long |
getTimestamp()
Get the timestamp field of this packet.
|
ByteBuffer |
grantEncodingBuffer(int channelId)
Return buffer containing the encoded form of this package and
remove internal reference to it.
|
ByteBuffer |
grantEncodingBuffer(int channelId,
int initialSize) |
boolean |
hasChannelId()
Whether this is a packets which can encode a channel ID.
|
boolean |
isEncoded() |
protected void |
patchChannelId(ByteBuffer buf,
int channelId) |
void |
setCompressionLimit(int limit)
Sets the number of bytes the package must be before activating compression.
|
void |
setCompressionType(String type) |
protected void |
setEncodedBody(ByteBuffer b,
int start,
int length) |
void |
setTimestamp(long timeStamp)
Set the timestamp field of the packet.
|
String |
toString() |
private final com.yahoo.compress.Compressor compressor
private static Logger log
private static int DEFAULT_WRITE_BUFFER_SIZE
public static final int CODE_MASK
protected byte[] encodedBody
protected ByteBuffer encodingBuffer
protected int length
private long timeStamp
private int compressionLimit
private com.yahoo.compress.CompressionType compressionType
public void setCompressionLimit(int limit)
limit
- smallest package size that triggers compression.public void setCompressionType(String type)
public BasicPacket decode(ByteBuffer buffer)
UnsupportedOperationException
- if not implemented in the subclassprotected void decodeAndDecompressBody(ByteBuffer buffer, int code, int packetLength)
public void decodeBody(ByteBuffer buffer)
UnsupportedOperationException
- if not implemented in the subclassprotected void codeDecodedHook(int code)
public BasicPacket encode(ByteBuffer buffer) throws BufferTooSmallException
Encodes this package onto the given buffer at the current position. The position of the buffer after encoding is the byte following the last encoded byte.
This method will ensure that everything is written provided sufficient capacity regardless of the buffer limit. When returning, the limit is at the end of the package (qual to the position).
UnsupportedOperationException
- if not implemented in the subclassBufferTooSmallException
protected void encodeAndCompressBody(ByteBuffer buffer, int startPosition)
private int getCompressedCode(com.yahoo.compress.CompressionType compression)
protected void encodeBody(ByteBuffer buffer)
UnsupportedOperationException
- if not implemented in the subclassprotected void setEncodedBody(ByteBuffer b, int start, int length)
public boolean isEncoded()
public Packet encode(ByteBuffer buffer, int channel) throws BufferTooSmallException
BufferTooSmallException
public final void allocateAndEncode(int channelId)
private void allocateAndEncode(int channelId, int initialSize)
protected void patchChannelId(ByteBuffer buf, int channelId)
public final ByteBuffer grantEncodingBuffer(int channelId)
public final ByteBuffer grantEncodingBuffer(int channelId, int initialSize)
public abstract int getCode()
public int getLength()
public void setTimestamp(long timeStamp)
public long getTimestamp()
public boolean hasChannelId()
Copyright © 2018. All rights reserved.