use of io.netty.handler.codec.memcache.DefaultMemcacheContent in project netty by netty.
the class BinaryMemcacheEncoderTest method shouldFailWithoutLastContent.
@Test(expected = EncoderException.class)
public void shouldFailWithoutLastContent() {
channel.writeOutbound(new DefaultMemcacheContent(Unpooled.EMPTY_BUFFER));
channel.writeOutbound(new DefaultBinaryMemcacheRequest());
}
use of io.netty.handler.codec.memcache.DefaultMemcacheContent in project netty by netty.
the class BinaryMemcacheEncoderTest method shouldEncodeContent.
@Test
public void shouldEncodeContent() {
DefaultMemcacheContent content1 = new DefaultMemcacheContent(Unpooled.copiedBuffer("Netty", CharsetUtil.UTF_8));
DefaultLastMemcacheContent content2 = new DefaultLastMemcacheContent(Unpooled.copiedBuffer(" Rocks!", CharsetUtil.UTF_8));
int totalBodyLength = content1.content().readableBytes() + content2.content().readableBytes();
BinaryMemcacheRequest request = new DefaultBinaryMemcacheRequest();
request.setTotalBodyLength(totalBodyLength);
boolean result = channel.writeOutbound(request);
assertThat(result, is(true));
result = channel.writeOutbound(content1);
assertThat(result, is(true));
result = channel.writeOutbound(content2);
assertThat(result, is(true));
ByteBuf written = channel.readOutbound();
assertThat(written.readableBytes(), is(DEFAULT_HEADER_SIZE));
written.release();
written = channel.readOutbound();
assertThat(written.readableBytes(), is(content1.content().readableBytes()));
assertThat(written.readSlice(content1.content().readableBytes()).toString(CharsetUtil.UTF_8), is("Netty"));
written.release();
written = channel.readOutbound();
assertThat(written.readableBytes(), is(content2.content().readableBytes()));
assertThat(written.readSlice(content2.content().readableBytes()).toString(CharsetUtil.UTF_8), is(" Rocks!"));
written.release();
}
use of io.netty.handler.codec.memcache.DefaultMemcacheContent in project netty by netty.
the class BinaryMemcacheObjectAggregatorTest method shouldRetainByteBufWhenAggregating.
@Test
public void shouldRetainByteBufWhenAggregating() {
channel = new EmbeddedChannel(new BinaryMemcacheRequestEncoder(), new BinaryMemcacheRequestDecoder(), new BinaryMemcacheObjectAggregator(MAX_CONTENT_SIZE));
ByteBuf key = Unpooled.copiedBuffer("Netty", CharsetUtil.UTF_8);
ByteBuf extras = Unpooled.copiedBuffer("extras", CharsetUtil.UTF_8);
BinaryMemcacheRequest request = new DefaultBinaryMemcacheRequest(key, extras);
DefaultMemcacheContent content1 = new DefaultMemcacheContent(Unpooled.copiedBuffer("Netty", CharsetUtil.UTF_8));
DefaultLastMemcacheContent content2 = new DefaultLastMemcacheContent(Unpooled.copiedBuffer(" Rocks!", CharsetUtil.UTF_8));
int totalBodyLength = key.readableBytes() + extras.readableBytes() + content1.content().readableBytes() + content2.content().readableBytes();
request.setTotalBodyLength(totalBodyLength);
assertTrue(channel.writeOutbound(request, content1, content2));
assertThat(channel.outboundMessages().size(), is(3));
assertTrue(channel.writeInbound(channel.readOutbound(), channel.readOutbound(), channel.readOutbound()));
FullBinaryMemcacheRequest read = channel.readInbound();
assertThat(read, notNullValue());
assertThat(read.key().toString(CharsetUtil.UTF_8), is("Netty"));
assertThat(read.extras().toString(CharsetUtil.UTF_8), is("extras"));
assertThat(read.content().toString(CharsetUtil.UTF_8), is("Netty Rocks!"));
read.release();
assertFalse(channel.finish());
}
use of io.netty.handler.codec.memcache.DefaultMemcacheContent in project netty by netty.
the class AbstractBinaryMemcacheDecoder method decode.
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
switch(state) {
case READ_HEADER:
try {
if (in.readableBytes() < 24) {
return;
}
resetDecoder();
currentMessage = decodeHeader(in);
state = State.READ_EXTRAS;
} catch (Exception e) {
resetDecoder();
out.add(invalidMessage(e));
return;
}
case READ_EXTRAS:
try {
byte extrasLength = currentMessage.extrasLength();
if (extrasLength > 0) {
if (in.readableBytes() < extrasLength) {
return;
}
currentMessage.setExtras(in.readRetainedSlice(extrasLength));
}
state = State.READ_KEY;
} catch (Exception e) {
resetDecoder();
out.add(invalidMessage(e));
return;
}
case READ_KEY:
try {
short keyLength = currentMessage.keyLength();
if (keyLength > 0) {
if (in.readableBytes() < keyLength) {
return;
}
currentMessage.setKey(in.readRetainedSlice(keyLength));
}
out.add(currentMessage.retain());
state = State.READ_CONTENT;
} catch (Exception e) {
resetDecoder();
out.add(invalidMessage(e));
return;
}
case READ_CONTENT:
try {
int valueLength = currentMessage.totalBodyLength() - currentMessage.keyLength() - currentMessage.extrasLength();
int toRead = in.readableBytes();
if (valueLength > 0) {
if (toRead == 0) {
return;
}
if (toRead > chunkSize) {
toRead = chunkSize;
}
int remainingLength = valueLength - alreadyReadChunkSize;
if (toRead > remainingLength) {
toRead = remainingLength;
}
ByteBuf chunkBuffer = in.readRetainedSlice(toRead);
MemcacheContent chunk;
if ((alreadyReadChunkSize += toRead) >= valueLength) {
chunk = new DefaultLastMemcacheContent(chunkBuffer);
} else {
chunk = new DefaultMemcacheContent(chunkBuffer);
}
out.add(chunk);
if (alreadyReadChunkSize < valueLength) {
return;
}
} else {
out.add(LastMemcacheContent.EMPTY_LAST_CONTENT);
}
resetDecoder();
state = State.READ_HEADER;
return;
} catch (Exception e) {
resetDecoder();
out.add(invalidChunk(e));
return;
}
case BAD_MESSAGE:
in.skipBytes(actualReadableBytes());
return;
default:
throw new Error("Unknown state reached: " + state);
}
}
Aggregations