use of org.jboss.netty.buffer.ChannelBuffer in project weave by continuuity.
the class AbstractCompressedMessageSetEncoder method finish.
@Override
public final ChannelBuffer finish() {
try {
compressedOutput.close();
ChannelBuffer buf = prefixLength(encodePayload(os.buffer(), compression));
compressedOutput = createCompressedStream(os);
os.buffer().clear();
return buf;
} catch (IOException e) {
throw Throwables.propagate(e);
}
}
use of org.jboss.netty.buffer.ChannelBuffer in project weave by continuuity.
the class AbstractCompressedMessageSetEncoder method add.
@Override
public final MessageSetEncoder add(ChannelBuffer payload) {
try {
ChannelBuffer encoded = encodePayload(payload);
encoded.readBytes(compressedOutput, encoded.readableBytes());
} catch (IOException e) {
throw Throwables.propagate(e);
}
return this;
}
use of org.jboss.netty.buffer.ChannelBuffer in project weave by continuuity.
the class AbstractMessageSetEncoder method prefixLength.
protected final ChannelBuffer prefixLength(ChannelBuffer buffer) {
ChannelBuffer sizeBuf = ChannelBuffers.buffer(4);
sizeBuf.writeInt(buffer.readableBytes());
return ChannelBuffers.wrappedBuffer(sizeBuf, buffer);
}
use of org.jboss.netty.buffer.ChannelBuffer in project weave by continuuity.
the class AbstractMessageSetEncoder method encodePayload.
protected final ChannelBuffer encodePayload(ChannelBuffer payload, Compression compression) {
ChannelBuffer header = ChannelBuffers.buffer(10);
int crc = computeCRC32(payload);
int magic = ((compression == Compression.NONE) ? 0 : 1);
// Message length = 1 byte magic + (optional 1 compression byte) + 4 bytes crc + payload length
header.writeInt(5 + magic + payload.readableBytes());
// Magic number = 0 for non-compressed data
header.writeByte(magic);
if (magic > 0) {
header.writeByte(compression.getCode());
}
header.writeInt(crc);
return ChannelBuffers.wrappedBuffer(header, payload);
}
use of org.jboss.netty.buffer.ChannelBuffer in project weave by continuuity.
the class Bufferer method getNext.
/**
* Returns the buffer if the buffer data is ready to be consumed,
* otherwise return {@link ChannelBuffers#EMPTY_BUFFER}.
*/
ChannelBuffer getNext() {
if (currentSize < 0) {
if (currentBuffer.readableBytes() < 4) {
return ChannelBuffers.EMPTY_BUFFER;
}
currentSize = currentBuffer.readInt();
}
// Keep buffering if less then required number of bytes
if (currentBuffer.readableBytes() < currentSize) {
return ChannelBuffers.EMPTY_BUFFER;
}
ChannelBuffer result = currentBuffer.readSlice(currentSize);
currentSize = -1;
return result;
}
Aggregations