@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 void decode(AttributeMap ctx, ByteBuf in, List<Object> out) throws Exception { // Common decoding part in.resetReaderIndex(); PingReqMessage message = new PingReqMessage(); if (!decodeCommonHeader(message, 0x00, in)) { in.resetReaderIndex(); return; } out.add(message); }
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { ChannelPipeline p = ctx.pipeline(); SocksProtocolVersion version = SocksProtocolVersion.valueOf(in.readByte()); System.out.println(version); in.resetReaderIndex(); switch (version) { case SOCKS4a: p.addLast(new Socks4CmdRequestDecoder()); p.addLast(Socks4MessageEncoder.INSTANCE); break; case SOCKS5: p.addLast(new Socks5InitRequestDecoder()); p.addLast(Socks5MessageEncoder.INSTANCE); break; case UNKNOWN: in.clear(); ctx.close(); return; } p.addLast(SocksServerHandler.INSTANCE); p.remove(this); }
@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(); } } }
@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 public String getBodyText() { if (sentResponse) { body.resetReaderIndex(); return body.toString(CharsetUtil.UTF_8); } else { return null; } }
@Test(expected = IllegalArgumentException.class) public void decodeSimulatorMessage_invalidMagicBytes() { buffer = Unpooled.buffer().capacity(8); buffer.writeInt(8); buffer.writeInt(0); buffer.resetReaderIndex(); decodeSimulatorMessage(buffer); }
/** * 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 public byte[] getBodyBytes() { if (sentResponse) { body.resetReaderIndex(); byte[] bytes = new byte[body.writerIndex()]; body.readBytes(bytes, 0, bytes.length); return bytes; } else { return null; } }
@Override public void toXStream(XStreamBuilder builder) throws IOException { builder.writeStartObject(); if (uuid != null) { builder.writeStringField("uuid", uuid.toString()); } if (buffer != null) { buffer.resetReaderIndex(); if (getMessageType() == QueueMessageType.STRING) { builder.writeFieldName("message"); builder.writeString(buffer.toString(defaultCharset)); } else if (getMessageType() == QueueMessageType.JSON) { builder.writeRawField("message", buffer); } } builder.writeEndObject(); }
@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 resetReaderIndex() { return buf.resetReaderIndex(); }
@Override @SuppressWarnings("restriction") public void decode(ByteBuf buf) { id = buf.readLong(); buf.readBytes(bitmask); BitArray bitArray = new BitArray(8 * bitmask.length, Bitops.flipBits(bitmask)); // Size in bits, byte[] // BitArray bitArray = new BitArray(bitmask); if (bitArray.get(0)) { posX = buf.readLong(); posY = buf.readLong(); posZ = buf.readLong(); } if (bitArray.get(1)) { pitch = buf.readFloat(); roll = buf.readFloat(); yaw = buf.readFloat(); } if (bitArray.get(2)) { velocity.decode(buf); } if (bitArray.get(3)) { accel.decode(buf); } if (bitArray.get(4)) { extraVel.decode(buf); } if (bitArray.get(5)) { lookPitch = buf.readFloat(); } if (bitArray.get(6)) { physicsFlags = buf.readUnsignedInt(); } if (bitArray.get(7)) { speedFlags = buf.readByte(); } if (bitArray.get(8)) { entityType = buf.readUnsignedInt(); } if (bitArray.get(9)) { currentMode = buf.readByte(); } if (bitArray.get(10)) { lastShootTime = buf.readUnsignedInt(); } if (bitArray.get(11)) { hitCounter = buf.readUnsignedInt(); } if (bitArray.get(12)) { lastHitTime = buf.readUnsignedInt(); } if (bitArray.get(13)) { app.decode(buf); } if (bitArray.get(14)) { flags1 = buf.readByte(); flags2 = buf.readByte(); } if (bitArray.get(15)) { rollTime = buf.readUnsignedInt(); } if (bitArray.get(16)) { stunTime = buf.readInt(); } if (bitArray.get(17)) { slowedTime = buf.readUnsignedInt(); } if (bitArray.get(18)) { makeBlueTime = buf.readUnsignedInt(); } if (bitArray.get(19)) { speedUpTime = buf.readUnsignedInt(); } if (bitArray.get(20)) { slowPatchTime = buf.readFloat(); } if (bitArray.get(21)) { classType = buf.readByte(); } if (bitArray.get(22)) { specialization = buf.readByte(); } if (bitArray.get(23)) { chargedMP = buf.readFloat(); } if (bitArray.get(24)) { nu1 = buf.readUnsignedInt(); nu2 = buf.readUnsignedInt(); nu3 = buf.readUnsignedInt(); } if (bitArray.get(25)) { nu4 = buf.readUnsignedInt(); nu5 = buf.readUnsignedInt(); nu6 = buf.readUnsignedInt(); } if (bitArray.get(26)) { rayHit.decode(buf); } if (bitArray.get(27)) { HP = buf.readFloat(); } if (bitArray.get(28)) { MP = buf.readFloat(); } if (bitArray.get(29)) { blockPower = buf.readFloat(); } if (bitArray.get(30)) { maxHPMultiplier = buf.readFloat(); shootSpeed = buf.readFloat(); damageMultiplier = buf.readFloat(); armorMultiplier = buf.readFloat(); resistanceMultiplier = buf.readFloat(); } if (bitArray.get(31)) { nu7 = buf.readByte(); } if (bitArray.get(32)) { nu8 = buf.readByte(); } if (bitArray.get(33)) { level = buf.readUnsignedInt(); } if (bitArray.get(34)) { currentXP = buf.readUnsignedInt(); } if (bitArray.get(35)) { parentOwner = buf.readLong(); } if (bitArray.get(36)) { na1 = buf.readUnsignedInt(); na2 = buf.readUnsignedInt(); } if (bitArray.get(37)) { na3 = buf.readByte(); } if (bitArray.get(38)) { na4 = buf.readUnsignedInt(); } if (bitArray.get(39)) { na5 = buf.readUnsignedInt(); nu11 = buf.readUnsignedInt(); nu12 = buf.readUnsignedInt(); } if (bitArray.get(40)) { nu13 = buf.readUnsignedInt(); nu14 = buf.readUnsignedInt(); nu15 = buf.readUnsignedInt(); nu16 = buf.readUnsignedInt(); nu17 = buf.readUnsignedInt(); nu18 = buf.readUnsignedInt(); } if (bitArray.get(41)) { nu20 = buf.readUnsignedInt(); nu21 = buf.readUnsignedInt(); nu22 = buf.readUnsignedInt(); } if (bitArray.get(42)) { nu19 = buf.readByte(); } if (bitArray.get(43)) { itemData.decode(buf); } if (bitArray.get(44)) { for (int i = 0; i < 13; i++) { GItem item = new GItem(); item.decode(buf); equipment[i] = item; } } if (bitArray.get(45)) { name = new String(buf.readBytes(16).array(), Charsets.US_ASCII).trim(); } if (bitArray.get(46)) { for (int i = 0; i < 11; i++) { skills[i] = buf.readUnsignedInt(); } } if (bitArray.get(47)) { iceBlockFour = buf.readUnsignedInt(); } debugCap = buf.capacity(); buf.resetReaderIndex(); buf.resetWriterIndex(); }
@Override public void decode(ByteBuf buf) { id = buf.readLong(); byte[] bitSetBuf = new byte[8]; buf.readBytes(bitSetBuf); this.bitSet = BitSet.valueOf(bitSetBuf); if (bitSet.get(0)) { position = readLongVector3(buf); } if (bitSet.get(1)) { orientation = readOrientation(buf); } if (bitSet.get(2)) { velocity = readFloatVector3(buf); } if (bitSet.get(3)) { accel = readFloatVector3(buf); } if (bitSet.get(4)) { extraVel = readFloatVector3(buf); } if (bitSet.get(5)) { lookPitch = buf.readFloat(); } if (bitSet.get(6)) { physicsFlags = buf.readUnsignedInt(); } if (bitSet.get(7)) { hostileType = buf.readByte(); } if (bitSet.get(8)) { entityType = buf.readUnsignedInt(); } if (bitSet.get(9)) { currentMode = buf.readByte(); } if (bitSet.get(10)) { lastShootTime = buf.readUnsignedInt(); } if (bitSet.get(11)) { hitCounter = buf.readUnsignedInt(); } if (bitSet.get(12)) { lastHitTime = buf.readUnsignedInt(); } if (bitSet.get(13)) { app.decode(buf); } if (bitSet.get(14)) { flags1 = buf.readByte(); flags2 = buf.readByte(); } if (bitSet.get(15)) { rollTime = buf.readUnsignedInt(); } if (bitSet.get(16)) { stunTime = buf.readInt(); } if (bitSet.get(17)) { slowedTime = buf.readUnsignedInt(); } if (bitSet.get(18)) { makeBlueTime = buf.readUnsignedInt(); } if (bitSet.get(19)) { speedUpTime = buf.readUnsignedInt(); } if (bitSet.get(20)) { slowPatchTime = buf.readFloat(); } if (bitSet.get(21)) { classType = buf.readByte(); } if (bitSet.get(22)) { specialization = buf.readByte(); } if (bitSet.get(23)) { chargedMP = buf.readFloat(); } if (bitSet.get(24)) { nu1 = buf.readUnsignedInt(); nu2 = buf.readUnsignedInt(); nu3 = buf.readUnsignedInt(); } if (bitSet.get(25)) { nu4 = buf.readUnsignedInt(); nu5 = buf.readUnsignedInt(); nu6 = buf.readUnsignedInt(); } if (bitSet.get(26)) { rayHit = readFloatVector3(buf); } if (bitSet.get(27)) { HP = buf.readFloat(); } if (bitSet.get(28)) { MP = buf.readFloat(); } if (bitSet.get(29)) { blockPower = buf.readFloat(); } if (bitSet.get(30)) { maxHPMultiplier = buf.readFloat(); shootSpeed = buf.readFloat(); damageMultiplier = buf.readFloat(); armorMultiplier = buf.readFloat(); resistanceMultiplier = buf.readFloat(); } if (bitSet.get(31)) { nu7 = buf.readByte(); } if (bitSet.get(32)) { nu8 = buf.readByte(); } if (bitSet.get(33)) { level = buf.readUnsignedInt(); } if (bitSet.get(34)) { currentXP = buf.readUnsignedInt(); } if (bitSet.get(35)) { parentOwner = buf.readLong(); } if (bitSet.get(36)) { na1 = buf.readUnsignedInt(); na2 = buf.readUnsignedInt(); } if (bitSet.get(37)) { na3 = buf.readByte(); } if (bitSet.get(38)) { na4 = buf.readUnsignedInt(); } if (bitSet.get(39)) { na5 = buf.readUnsignedInt(); nu11 = buf.readUnsignedInt(); nu12 = buf.readUnsignedInt(); } if (bitSet.get(40)) { spawnPosition = readLongVector3(buf); } if (bitSet.get(41)) { nu20 = buf.readUnsignedInt(); nu21 = buf.readUnsignedInt(); nu22 = buf.readUnsignedInt(); } if (bitSet.get(42)) { nu19 = buf.readByte(); } if (bitSet.get(43)) { itemData.decode(buf); } if (bitSet.get(44)) { for (int i = 0; i < 13; i++) { GItem item = new GItem(); item.decode(buf); equipment[i] = item; } } if (bitSet.get(45)) { name = new String(buf.readBytes(16).array(), Charsets.US_ASCII).trim(); } if (bitSet.get(46)) { for (int i = 0; i < 11; i++) { skills[i] = buf.readUnsignedInt(); } } if (bitSet.get(47)) { iceBlockFour = buf.readUnsignedInt(); } debugCap = buf.capacity(); buf.resetReaderIndex(); buf.resetWriterIndex(); }