List of usage examples for io.netty.buffer ByteBuf skipBytes
public abstract ByteBuf skipBytes(int length);
From source file:com.shelf.messagepack.MessagePackFrameDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { long frameLength = decodeLength(in, 0); if (frameLength > maxFrameLength) { long discard = frameLength - in.readableBytes(); tooLongFrameLength = frameLength; if (discard < 0) { // buffer contains more bytes then the frameLength so we can discard all now in.skipBytes((int) frameLength); } else {//from w ww . ja va 2 s. com // Enter the discard mode and discard everything received so far. discardingTooLongFrame = true; bytesToDiscard = discard; in.skipBytes(in.readableBytes()); } failIfNecessary(true); } else { int readerIndex = in.readerIndex(); int actualFrameLength = (int) frameLength; ByteBuf buffer = extractFrame(ctx, in, readerIndex, actualFrameLength); in.readerIndex(readerIndex + actualFrameLength); out.add(buffer); } }
From source file:com.spotify.ffwd.protobuf.ProtobufDecoder.java
License:Apache License
private void decodeOne(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { final int version = (int) in.getUnsignedInt(0); final long totalLength = in.getUnsignedInt(4); if (totalLength > MAX_FRAME_SIZE) { log.error("Received frame with length (" + totalLength + ") larger than maximum allowed ( " + MAX_FRAME_SIZE + ")"); in.clear();/*from w ww . j a va 2 s .co m*/ return; } // datagram underflow if (in.readableBytes() < totalLength) { log.error("Received frame of shorter length (" + in.readableBytes() + ") than reported (" + totalLength + ")"); in.clear(); return; } in.skipBytes(8); final Object frame; switch (version) { case 0: frame = decodeFrame0(in); break; default: throw new IllegalArgumentException("Unsupported protocol version: " + version); } if (frame != null) { out.add(frame); } }
From source file:com.spotify.ffwd.riemann.RiemannFrameDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (in.readableBytes() < 4) { return;//from w ww .j av a 2s. c o m } final long length = in.getUnsignedInt(0); if (length > MAX_SIZE) { throw new CorruptedFrameException( String.format("frame size (%s) larger than max (%d)", length, MAX_SIZE)); } final int intLength = (int) length; if (in.readableBytes() < (4 + length)) { return; } in.skipBytes(4); final ByteBuf frame = in.readBytes(intLength); try { out.add(serializer.parse0(frame)); } finally { frame.release(); } }
From source file:com.spotify.folsom.client.binary.BinaryMemcacheDecoder.java
License:Apache License
@Override protected void decode(final ChannelHandlerContext ctx, final ByteBuf buf, final List<Object> out) throws Exception { for (int i = 0; i < BATCH_SIZE; i++) { if (buf.readableBytes() < 24) { return; }/*from w ww . jav a 2 s .co m*/ buf.markReaderIndex(); final int magicNumber = buf.readUnsignedByte(); // byte 0 if (magicNumber != 0x81) { throw fail(buf, String.format("Invalid magic number: 0x%2x", magicNumber)); } final int opcode = buf.readByte(); // byte 1 final int keyLength = buf.readUnsignedShort(); // byte 2-3 final int extrasLength = buf.readUnsignedByte(); // byte 4 buf.skipBytes(1); final int statusCode = buf.readUnsignedShort(); // byte 6-7 final MemcacheStatus status = MemcacheStatus.fromInt(statusCode); final int totalLength = buf.readInt(); // byte 8-11 final int opaque = buf.readInt(); final long cas = buf.readLong(); if (buf.readableBytes() < totalLength) { buf.resetReaderIndex(); return; } buf.skipBytes(extrasLength); buf.skipBytes(keyLength); final int valueLength = totalLength - keyLength - extrasLength; byte[] valueBytes; if (valueLength == 0) { valueBytes = NO_BYTES; } else { valueBytes = new byte[valueLength]; } buf.readBytes(valueBytes); replies.add(new ResponsePacket((byte) opcode, status, opaque, cas, valueBytes)); if ((opaque & 0xFF) == 0) { out.add(replies); replies = new BinaryResponse(); } } }
From source file:com.spotify.netty.handler.codec.zmtp.ZMTPMessageParser.java
License:Apache License
/** * Discard frames for current message.//from ww w . j a v a2 s .c om * * @return A truncated message if done discarding, null if not yet done. */ private ZMTPParsedMessage discardFrames(final ByteBuf buffer) throws ZMTPMessageParsingException { while (buffer.readableBytes() > 0) { // Parse header if necessary if (!headerParsed) { buffer.markReaderIndex(); headerParsed = parseZMTPHeader(buffer); if (!headerParsed) { // Wait for more data to decode buffer.resetReaderIndex(); return null; } size += frameSize; frameRemaining = frameSize; } // Discard bytes final int discardBytes = min(frameRemaining, buffer.readableBytes()); frameRemaining -= discardBytes; buffer.skipBytes(discardBytes); // Check if this frame is completely discarded final boolean done = frameRemaining == 0; if (done) { headerParsed = false; } // Check if this message is done discarding if (done && !hasMore) { // We're done discarding return finish(true); } } return null; }
From source file:com.spotify.netty4.handler.codec.zmtp.benchmarks.CustomReqRepBenchmark.java
License:Apache License
private static CharSequence readMethod(final ByteBuf data, final int size) { for (final AsciiString method : METHODS) { if (asciiEquals(method, data, size)) { data.skipBytes(size); return method; }/*from ww w .j a va2s .c o m*/ } return readAscii(data, size); }
From source file:com.spotify.netty4.handler.codec.zmtp.ZMTP10WireFormat.java
License:Apache License
/** * Read the remote identity octets from a ZMTP/1.0 greeting. *//*from w ww . ja v a 2 s. c om*/ static ByteBuffer readIdentity(final ByteBuf buffer) throws ZMTPParsingException { final long length = readLength(buffer); if (length == -1) { return null; } final long identityLength = length - 1; if (identityLength < 0 || identityLength > 255) { throw new ZMTPParsingException("Bad remote identity length: " + length); } // skip the flags byte buffer.skipBytes(1); final byte[] identity = new byte[(int) identityLength]; buffer.readBytes(identity); return ByteBuffer.wrap(identity); }
From source file:com.spotify.netty4.handler.codec.zmtp.ZMTP20WireFormat.java
License:Apache License
/** * Read a ZMTP/2.0 greeting./*from w w w.ja v a2s . c o m*/ * * @param in The buffer to read the greeting from. * @return A {@link com.spotify.netty4.handler.codec.zmtp.ZMTP20WireFormat.Greeting}. * @throws ZMTPParsingException If the greeting is malformed. * @throws IndexOutOfBoundsException If there is not enough readable bytes to read an entire * greeting. */ static Greeting readGreeting(ByteBuf in) throws ZMTPParsingException { if (in.readByte() != (byte) 0xff) { throw new ZMTPParsingException("Illegal ZMTP/2.0 greeting, first octet not 0xff"); } in.skipBytes(9); return readGreetingBody(in); }
From source file:com.spotify.netty4.handler.codec.zmtp.ZMTP20WireFormat.java
License:Apache License
/** * Read enough bytes from buffer to deduce the remote protocol version in a backwards compatible * ZMTP handshake.// w w w . j a v a 2 s .c o m * * @param in the buffer of data to determine version from. * @return The detected {@link ZMTPVersion}. * @throws IndexOutOfBoundsException if there is not enough data available in buffer. */ static ZMTPVersion detectProtocolVersion(final ByteBuf in) { if (in.readByte() != (byte) 0xff) { return ZMTPVersion.ZMTP10; } in.skipBytes(8); if ((in.readByte() & 0x01) == 0) { return ZMTPVersion.ZMTP10; } return ZMTPVersion.ZMTP20; }
From source file:com.spotify.netty4.handler.codec.zmtp.ZMTPCodec.java
License:Apache License
@Override protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) throws Exception { // Discard input if handshake failed. It is expected that the user will close the channel. if (session.handshakeFuture().isDone()) { assert !session.handshakeFuture().isSuccess(); in.skipBytes(in.readableBytes()); }// w ww. j ava2s.c om // Shake hands final ZMTPHandshake handshake; try { handshake = handshaker.handshake(in, ctx); if (handshake == null) { // Handshake is not yet done. Await more input. return; } } catch (Exception e) { session.handshakeFailure(e); ctx.fireUserEventTriggered(new ZMTPHandshakeFailure(session)); throw e; } // Handshake is done. session.handshakeSuccess(handshake); // Replace this handler with the framing encoder and decoder if (actualReadableBytes() > 0) { out.add(in.readBytes(actualReadableBytes())); } final ZMTPDecoder decoder = config.decoder().decoder(session); final ZMTPEncoder encoder = config.encoder().encoder(session); final ZMTPWireFormat wireFormat = ZMTPWireFormats.wireFormat(session.negotiatedVersion()); final ChannelHandler handler = new CombinedChannelDuplexHandler<ZMTPFramingDecoder, ZMTPFramingEncoder>( new ZMTPFramingDecoder(wireFormat, decoder), new ZMTPFramingEncoder(wireFormat, encoder)); ctx.pipeline().replace(this, ctx.name(), handler); // Tell the user that the handshake is complete ctx.fireUserEventTriggered(new ZMTPHandshakeSuccess(session, handshake)); }