@Override public void split(ChannelHandlerContext ctx, ByteBuf input, List<Object> list) throws Exception { input.markReaderIndex(); final byte[] array = new byte[3]; for (int i = 0; i < array.length; ++i) { if (!input.isReadable()) { input.resetReaderIndex(); return; } array[i] = input.readByte(); if (array[i] >= 0) { final PacketDataSerializer packetDataSerializer = new PacketDataSerializer( Unpooled.wrappedBuffer(array), ProtocolVersion.MINECRAFT_1_7_10); try { final int length = packetDataSerializer.readVarInt(); if (input.readableBytes() < length) { input.resetReaderIndex(); return; } list.add(input.readBytes(length)); return; } finally { packetDataSerializer.release(); } } } throw new CorruptedFrameException("length wider than 21-bit"); }
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { // System.out.println("decode:" + in.readableBytes()); if (in.readableBytes() > 4) { in.markReaderIndex(); int needBytes = in.readInt(); // System.out.println("needBytes:" + needBytes); if (in.readableBytes() >= needBytes) { byte[] content = new byte[in.readableBytes()]; in.readBytes(content); // byte[] data= ZLipHelper.decompress(content); // System.out.println("data:" + new String(data)); Amf3Input amf3Input = new Amf3Input(SerializationContext.getSerializationContext()); // amf3Input = new Amf3Input(SerializationContext.getSerializationContext()); InputStream bais = new ByteArrayInputStream(content); amf3Input.setInputStream(bais); try { Object decoded = amf3Input.readObject(); if (decoded != null) { out.add(decoded); // System.out.println("decoded:" + decoded); } } catch (Exception e) { } // amf3Input.close(); } else { in.resetReaderIndex(); } } }
/** * Puts the bytes from the specified buffer into this packet's buffer, in reverse. * * @param buffer The source {@link ByteBuf}. * @throws IllegalStateException if the builder is not in byte access mode. */ public void putBytesReverse(ByteBuf buffer) { byte[] bytes = new byte[buffer.readableBytes()]; buffer.markReaderIndex(); try { buffer.readBytes(bytes); } finally { buffer.resetReaderIndex(); } putBytesReverse(bytes); }
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { in.markReaderIndex(); if (!Utils.checkHeaderAvailability(in)) { in.resetReaderIndex(); return; } in.resetReaderIndex(); byte messageType = Utils.readMessageType(in); DemuxDecoder decoder = m_decoderMap.get(messageType); if (decoder == null) { throw new CorruptedFrameException( "Can't find any suitable decoder for message type: " + messageType); } decoder.decode(ctx, in, out); }
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { // mark the reader index to be able to start decoding from the same position if there is not // enough data to finish the frame decoding in.markReaderIndex(); boolean frameDecoded = false; try { if (!in.isReadable(FRAME_HEADER_LENGTH)) { return; } int frameVersion = in.readUnsignedByte(); sessionHandler.versionRead(frameVersion); int frameType = in.readUnsignedByte(); LOG.debug("Received a lumberjack frame of type {}", (char) frameType); switch (frameType) { case TYPE_JSON: frameDecoded = handleJsonFrame(in, out); break; case TYPE_DATA: frameDecoded = handleDataFrame(in, out); break; case TYPE_WINDOW: frameDecoded = handleWindowFrame(in); break; case TYPE_COMPRESS: frameDecoded = handleCompressedFrame(ctx, in, out); break; default: throw new RuntimeException("Unsupported frame type=" + frameType); } } finally { if (!frameDecoded) { LOG.debug( "Not enough data to decode a complete frame, retry when more data is available. Reader index was {}", in.readerIndex()); in.resetReaderIndex(); } } }
@Override public ByteBuf markReaderIndex() { return buf.markReaderIndex(); }