List of usage examples for io.netty.buffer ByteBuf slice
public abstract ByteBuf slice(int index, int length);
From source file:books.netty.protocol.netty.codec.MarshallingDecoder.java
License:Apache License
protected Object decode(ByteBuf in) throws Exception { int objectSize = in.readInt(); ByteBuf buf = in.slice(in.readerIndex(), objectSize); ByteInput input = new ChannelBufferByteInput(buf); try {/*from ww w.j a v a 2s .com*/ unmarshaller.start(input); Object obj = unmarshaller.readObject(); unmarshaller.finish(); in.readerIndex(in.readerIndex() + objectSize); return obj; } finally { unmarshaller.close(); } }
From source file:com.allanbank.mongodb.netty.ByteToMessageDecoder.java
License:Apache License
/** * {@inheritDoc}/* w w w .jav a2 s . c o m*/ * <p> * Overridden to return a slice of the buffer since it will not escape the * current stack. * </p> */ @Override protected ByteBuf extractFrame(final ChannelHandlerContext ctx, final ByteBuf buffer, final int index, final int length) { return buffer.slice(index, length); }
From source file:com.allanbank.mongodb.netty.ByteToMessageDecoderTest.java
License:Apache License
/** * Test method for/*from w ww. jav a2 s. co m*/ * {@link ByteToMessageDecoder#decode(ChannelHandlerContext, ByteBuf)}. * * @throws Exception * On a test failure. */ @Test public void testDecodeInCompleteFrame() throws Exception { final Random rand = new Random(System.currentTimeMillis()); final Message msg = new KillCursors(new long[] { rand.nextLong() }, ReadPreference.PRIMARY); final int msgId = rand.nextInt() & 0xFFFFFF; final ByteBuf buffer = ourAllocator.buffer(); final ByteBufOutputStream out = new ByteBufOutputStream(buffer); final BsonOutputStream bout = new BsonOutputStream(out); msg.write(msgId, bout); final ChannelHandlerContext mockContext = createMock(ChannelHandlerContext.class); replay(mockContext); final ByteToMessageDecoder decoder = new ByteToMessageDecoder(new StringDecoderCache()); final Object result = decoder.decode(mockContext, buffer.slice(0, buffer.writerIndex() - 1)); assertThat(result, nullValue()); verify(mockContext); }
From source file:com.cloudera.livy.client.local.rpc.TestKryoMessageCodec.java
License:Apache License
@Test public void testFragmentation() throws Exception { ByteBuf buf = newBuffer(); Object[] messages = { "msg1", "msg2" }; int[] indices = new int[messages.length]; KryoMessageCodec codec = new KryoMessageCodec(0); for (int i = 0; i < messages.length; i++) { codec.encode(null, messages[i], buf); indices[i] = buf.writerIndex();//from ww w.j a va 2 s. co m } List<Object> objects = Lists.newArrayList(); // Don't read enough data for the first message to be decoded. codec.decode(null, buf.slice(0, indices[0] - 1), objects); assertEquals(0, objects.size()); // Read enough data for just the first message to be decoded. codec.decode(null, buf.slice(0, indices[0] + 1), objects); assertEquals(1, objects.size()); }
From source file:com.cloudera.livy.rsc.rpc.TestKryoMessageCodec.java
License:Apache License
@Test public void testFragmentation() throws Exception { ByteBuf buf = newBuffer(); Object[] messages = { "msg1", "msg2" }; int[] indices = new int[messages.length]; KryoMessageCodec codec = new KryoMessageCodec(0); for (int i = 0; i < messages.length; i++) { codec.encode(null, messages[i], buf); indices[i] = buf.writerIndex();/*w ww .j a v a 2s .co m*/ } List<Object> objects = new ArrayList<>(); // Don't read enough data for the first message to be decoded. codec.decode(null, buf.slice(0, indices[0] - 1), objects); assertEquals(0, objects.size()); // Read enough data for just the first message to be decoded. codec.decode(null, buf.slice(0, indices[0] + 1), objects); assertEquals(1, objects.size()); }
From source file:com.corundumstudio.socketio.parser.Decoder.java
License:Apache License
public Packet decodePackets(ByteBuf buffer, UUID uuid) throws IOException { if (isCurrentDelimiter(buffer, buffer.readerIndex())) { buffer.readerIndex(buffer.readerIndex() + Packet.DELIMITER_BYTES.length); Integer len = extractLength(buffer); int startIndex = buffer.readerIndex(); ByteBuf frame = buffer.slice(startIndex, len); Packet packet = decodePacket(frame, uuid); buffer.readerIndex(startIndex + len); return packet; } else {/*from w w w.ja v a 2 s .co m*/ Packet packet = decodePacket(buffer, uuid); return packet; } }
From source file:com.corundumstudio.socketio.parser.Decoder.java
License:Apache License
private int delimiterIndexOf(ByteBuf buffer, int startIndex, int length) { ByteBuf buf = buffer.slice(startIndex, length); for (int i = 0; i < buf.readableBytes(); i++) { if (isCurrentDelimiter(buf, i)) { return startIndex + i; }/*from ww w. ja va2 s .c om*/ } return -1; }
From source file:com.corundumstudio.socketio.protocol.PacketDecoder.java
License:Apache License
public Packet decodePackets(ByteBuf buffer, ClientHead client) throws IOException { if (isStringPacket(buffer)) { // TODO refactor int headEndIndex = buffer.bytesBefore((byte) -1); int len = (int) readLong(buffer, headEndIndex); ByteBuf frame = buffer.slice(buffer.readerIndex() + 1, len); // skip this frame buffer.readerIndex(buffer.readerIndex() + 1 + len); return decode(client, frame); } else if (hasLengthHeader(buffer)) { // TODO refactor int lengthEndIndex = buffer.bytesBefore((byte) ':'); int lenHeader = (int) readLong(buffer, lengthEndIndex); int len = utf8scanner.getActualLength(buffer, lenHeader); ByteBuf frame = buffer.slice(buffer.readerIndex() + 1, len); if (lenHeader != len) { frame = Unpooled.wrappedBuffer(frame.toString(CharsetUtil.UTF_8).getBytes(CharsetUtil.ISO_8859_1)); }// w ww .j a v a 2s . c om // skip this frame buffer.readerIndex(buffer.readerIndex() + 1 + len); return decode(client, frame); } return decode(client, buffer); }
From source file:com.corundumstudio.socketio.protocol.PacketDecoder.java
License:Apache License
private Packet parseBinary(ClientHead head, ByteBuf frame) throws IOException { if (frame.getByte(0) == 1) { frame.readByte();//from w w w . ja v a2s. c om int headEndIndex = frame.bytesBefore((byte) -1); int len = (int) readLong(frame, headEndIndex); ByteBuf oldFrame = frame; frame = frame.slice(oldFrame.readerIndex() + 1, len); oldFrame.readerIndex(oldFrame.readerIndex() + 1 + len); } if (frame.getByte(0) == 'b' && frame.getByte(1) == '4') { frame.readShort(); } else if (frame.getByte(0) == 4) { frame.readByte(); } Packet binaryPacket = head.getLastBinaryPacket(); if (binaryPacket != null) { ByteBuf attachBuf; if (frame.getByte(0) == 'b' && frame.getByte(1) == '4') { attachBuf = frame; } else { attachBuf = Base64.encode(frame); } binaryPacket.addAttachment(Unpooled.copiedBuffer(attachBuf)); frame.readerIndex(frame.readerIndex() + frame.readableBytes()); if (binaryPacket.isAttachmentsLoaded()) { LinkedList<ByteBuf> slices = new LinkedList<ByteBuf>(); ByteBuf source = binaryPacket.getDataSource(); for (int i = 0; i < binaryPacket.getAttachments().size(); i++) { ByteBuf attachment = binaryPacket.getAttachments().get(i); ByteBuf scanValue = Unpooled.copiedBuffer("{\"_placeholder\":true,\"num\":" + i + "}", CharsetUtil.UTF_8); int pos = PacketEncoder.find(source, scanValue); if (pos == -1) { throw new IllegalStateException( "Can't find attachment by index: " + i + " in packet source"); } ByteBuf prefixBuf = source.slice(source.readerIndex(), pos - source.readerIndex()); slices.add(prefixBuf); slices.add(QUOTES); slices.add(attachment); slices.add(QUOTES); source.readerIndex(pos + scanValue.readableBytes()); } slices.add(source.slice()); ByteBuf compositeBuf = Unpooled.wrappedBuffer(slices.toArray(new ByteBuf[slices.size()])); parseBody(head, compositeBuf, binaryPacket); head.setLastBinaryPacket(null); return binaryPacket; } } return new Packet(PacketType.MESSAGE); }
From source file:com.corundumstudio.socketio.transport.FlashPolicyHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (msg instanceof ByteBuf) { ByteBuf message = (ByteBuf) msg; ByteBuf data = message.slice(0, requestBuffer.readableBytes()); if (data.equals(requestBuffer)) { message.release();// ww w . j av a 2 s . c om ChannelFuture f = ctx.writeAndFlush(Unpooled.copiedBuffer(responseBuffer)); f.addListener(ChannelFutureListener.CLOSE); return; } ctx.pipeline().remove(this); } ctx.fireChannelRead(msg); }