List of usage examples for io.netty.buffer ByteBuf skipBytes
public abstract ByteBuf skipBytes(int length);
From source file:io.awacs.protocol.binary.BinaryMessageDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { int readable = in.readableBytes(); if (readable > BinaryMessage.MAX_PACKET_SIZE) { in.skipBytes(readable); throw new TooLongFrameException(); }/*from w w w. j a v a 2s. c o m*/ //??? if (readable < 16) { return; } byte[] headerBytes = new byte[16]; in.readBytes(headerBytes, 0, 16); int bodyLength = BinaryMessage.bodyLength(headerBytes); //body??body if (in.readableBytes() < bodyLength) { in.resetReaderIndex(); return; } byte[] bodyBytes = new byte[bodyLength]; in.readBytes(bodyBytes, 0, bodyLength); // byte[] copy = new byte[16 + bodyLength]; // System.arraycopy(headerBytes, 0, copy, 0, 16); // System.arraycopy(bodyBytes, 0, copy, 16, bodyLength); Message m = BinaryMessage.parse(headerBytes, bodyBytes); in.markReaderIndex(); out.add(m); }
From source file:io.codis.nedis.handler.RedisResponseDecoder.java
License:Apache License
private boolean decode(ByteBuf in, List<Object> out, Object nullValue) throws Exception { if (in.readableBytes() < 2) { return false; }// w w w . j a v a2s . c om byte b = in.readByte(); switch (b) { case '+': { String reply = decodeString(in); if (reply == null) { return false; } out.add(reply); return true; } case '-': { String reply = decodeString(in); if (reply == null) { return false; } out.add(new RedisResponseException(reply)); return true; } case ':': { Long reply = decodeLong(in); if (reply == null) { return false; } out.add(reply); return true; } case '$': { Long numBytes = decodeLong(in); if (numBytes == null) { return false; } if (numBytes.intValue() == -1) { out.add(nullValue); return true; } if (in.readableBytes() < numBytes.intValue() + 2) { return false; } if (in.getByte(in.readerIndex() + numBytes.intValue()) != '\r' || in.getByte(in.readerIndex() + numBytes.intValue() + 1) != '\n') { throw new ProtocolException("Response is not ended by CRLF"); } byte[] reply = new byte[numBytes.intValue()]; in.readBytes(reply); // skip CRLF in.skipBytes(2); out.add(reply); return true; } case '*': { Long numReplies = decodeLong(in); if (numReplies == null) { return false; } if (numReplies.intValue() == -1) { out.add(nullValue); return true; } List<Object> replies = new ArrayList<>(); for (int i = 0; i < numReplies.intValue(); i++) { if (!decode(in, replies, null)) { return false; } } out.add(replies); return true; } default: throw new ProtocolException("Unknown leading char: " + (char) b); } }
From source file:io.datty.msgpack.core.AbstractMessageReader.java
License:Apache License
public void skipBytes(int numberOfBytes, ByteBuf buffer) { buffer.skipBytes(numberOfBytes); }
From source file:io.datty.msgpack.core.ValueMessageReader.java
License:Apache License
public ByteBuf readBinary(ByteBuf source, boolean copy) { int length = readBinaryHeader(source); if (length > source.readableBytes()) { throw new MessageParseException( "insufficient buffer length: " + source.readableBytes() + ", required length: " + length); }// w w w .ja v a2 s.c om if (copy) { ByteBuf dst = source.alloc().buffer(length); source.readBytes(dst, length); return dst; } else { ByteBuf slice = source.slice(source.readerIndex(), length); source.skipBytes(length); return slice; } }
From source file:io.gomint.proxprox.network.tcp.PacketDecompressor.java
License:BSD License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { int size = in.readInt(); if (size == 0) { out.add(in.slice().retain());/*from ww w . jav a 2 s . co m*/ in.skipBytes(in.readableBytes()); } else { ByteBuf decompressed = ctx.alloc().directBuffer(); try { zlib.process(in, decompressed); Preconditions.checkState(decompressed.readableBytes() == size, "Decompressed packet size mismatch"); out.add(decompressed); decompressed = null; } finally { if (decompressed != null) { decompressed.release(); } } } }
From source file:io.grpc.netty.GrpcHpackHuffmanDecoder.java
License:Apache License
/** * Decompresses the given Huffman coded string literal. * * @param buf the string literal to be decoded * @return the output stream for the compressed data * @throws Http2Exception EOS Decoded/*from w w w . j a v a 2s . c om*/ */ public AsciiString decode(ByteBuf buf, int length) throws Http2Exception { processor.reset(); buf.forEachByte(buf.readerIndex(), length, processor); buf.skipBytes(length); return processor.end(); }
From source file:io.hekate.network.netty.NettyMessage.java
License:Apache License
static String utf(ByteBuf buf) throws IOException { try {//w w w .j av a2 s .com int len = buf.readInt(); String string = buf.toString(buf.readerIndex(), len, StandardCharsets.UTF_8); buf.skipBytes(len); return string; } catch (IndexOutOfBoundsException e) { throw endOfStream(e); } }
From source file:io.hydramq.core.type.ConversionContext.java
License:Open Source License
public <T> T read(Class<T> type, ByteBuf buffer) { if (Command.class.isAssignableFrom(type)) { buffer.skipBytes(Integer.BYTES); // swallow type field }/* www . j ava 2s . c o m*/ return lookup(type).read(this, buffer); }
From source file:io.hydramq.core.type.converters.StringConverter.java
License:Open Source License
@Override public String read(final ConversionContext context, final ByteBuf buffer) { int size = buffer.readInt(); String value = buffer.toString(buffer.readerIndex(), size, CharsetUtil.UTF_8); buffer.skipBytes(size); return value; }
From source file:io.moquette.parser.netty.ConnAckEncoderTest.java
License:Open Source License
@Test public void testHeaderEncode() throws Exception { ConnAckMessage msg = new ConnAckMessage(); //Exercise//from w ww. j a v a 2 s . c o m ByteBuf out = Unpooled.buffer(); //Exercise m_encoder.encode(m_mockedContext, msg, out); //Verify assertEquals(0x20, out.readByte()); //1 byte assertEquals(0x02, out.readByte()); //2 byte, length assertEquals(ConnAckMessage.CONNECTION_ACCEPTED, out.skipBytes(1).readByte()); }