Example usage for io.netty.buffer ByteBuf isReadable

List of usage examples for io.netty.buffer ByteBuf isReadable

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf isReadable.

Prototype

public abstract boolean isReadable();

Source Link

Document

Returns true if and only if (this.writerIndex - this.readerIndex) is greater than 0 .

Usage

From source file:org.apache.drill.exec.rpc.ProtobufLengthDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    if (!ctx.channel().isOpen()) {
        if (in.readableBytes() > 0) {
            logger.info("Channel is closed, discarding remaining {} byte(s) in buffer.", in.readableBytes());
        }/*from   w  w w.  j a  va 2s.  com*/
        in.skipBytes(in.readableBytes());
        return;
    }

    in.markReaderIndex();
    final byte[] buf = new byte[5];
    for (int i = 0; i < buf.length; i++) {
        if (!in.isReadable()) {
            in.resetReaderIndex();
            return;
        }

        buf[i] = in.readByte();
        if (buf[i] >= 0) {

            int length = CodedInputStream.newInstance(buf, 0, i + 1).readRawVarint32();

            if (length < 0) {
                throw new CorruptedFrameException("negative length: " + length);
            }
            if (length == 0) {
                throw new CorruptedFrameException("Received a message of length 0.");
            }

            if (in.readableBytes() < length) {
                in.resetReaderIndex();
                return;
            } else {
                // need to make buffer copy, otherwise netty will try to refill this buffer if we move the readerIndex forward...
                // TODO: Can we avoid this copy?
                ByteBuf outBuf;
                try {
                    outBuf = allocator.buffer(length);
                } catch (OutOfMemoryException e) {
                    logger.warn(
                            "Failure allocating buffer on incoming stream due to memory limits.  Current Allocation: {}.",
                            allocator.getAllocatedMemory());
                    in.resetReaderIndex();
                    outOfMemoryHandler.handle();
                    return;
                }
                outBuf.writeBytes(in, in.readerIndex(), length);

                in.skipBytes(length);

                if (RpcConstants.EXTRA_DEBUGGING) {
                    logger.debug(String.format(
                            "ReaderIndex is %d after length header of %d bytes and frame body of length %d bytes.",
                            in.readerIndex(), i + 1, length));
                }

                out.add(outBuf);
                return;
            }
        }
    }

    // Couldn't find the byte whose MSB is off.
    throw new CorruptedFrameException("length wider than 32-bit");

}

From source file:org.apache.drill.exec.rpc.ZeroCopyProtobufLengthDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {

    if (!ctx.channel().isOpen()) {
        if (in.readableBytes() > 0)
            logger.info("Channel is closed, discarding remaining {} byte(s) in buffer.", in.readableBytes());
        in.skipBytes(in.readableBytes());
        return;// www.j  a va  2 s . c  o  m
    }

    in.markReaderIndex();
    final byte[] buf = new byte[5];
    for (int i = 0; i < buf.length; i++) {
        if (!in.isReadable()) {
            in.resetReaderIndex();
            return;
        }

        buf[i] = in.readByte();
        if (buf[i] >= 0) {

            int length = CodedInputStream.newInstance(buf, 0, i + 1).readRawVarint32();

            if (length < 0) {
                throw new CorruptedFrameException("negative length: " + length);
            }
            if (length == 0) {
                throw new CorruptedFrameException("Received a message of length 0.");
            }

            if (in.readableBytes() < length) {
                in.resetReaderIndex();
                return;
            } else {
                // need to make buffer copy, otherwise netty will try to refill this buffer if we move the readerIndex forward...
                // TODO: Can we avoid this copy?
                ByteBuf outBuf = in.copy(in.readerIndex(), length);
                in.skipBytes(length);

                if (RpcConstants.EXTRA_DEBUGGING)
                    logger.debug(String.format(
                            "ReaderIndex is %d after length header of %d bytes and frame body of length %d bytes.",
                            in.readerIndex(), i + 1, length));
                out.add(outBuf);
                return;
            }
        }
    }

    // Couldn't find the byte whose MSB is off.
    throw new CorruptedFrameException("length wider than 32-bit");

}

From source file:org.apache.flink.runtime.io.network.netty.OutboundEnvelopeEncoder.java

License:Apache License

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    Envelope env = (Envelope) msg;//from   www .jav a  2s .c  o m

    ByteBuf buf = ctx.alloc().directBuffer();

    encode(env, buf);

    if (buf.isReadable()) {
        ctx.write(buf, promise);
    } else {
        buf.release();
        ctx.write(Unpooled.EMPTY_BUFFER, promise);
    }
}

From source file:org.apache.hadoop.hbase.security.SaslClientHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    ByteBuf in = (ByteBuf) msg;

    // If not complete, try to negotiate
    if (!saslClient.isComplete()) {
        while (!saslClient.isComplete() && in.isReadable()) {
            readStatus(in);/*from   w w  w. j av a 2  s .  com*/
            int len = in.readInt();
            if (firstRead) {
                firstRead = false;
                if (len == SaslUtil.SWITCH_TO_SIMPLE_AUTH) {
                    if (!fallbackAllowed) {
                        throw new IOException("Server asks us to fall back to SIMPLE auth, " + "but this "
                                + "client is configured to only allow secure connections.");
                    }
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Server asks us to fall back to simple auth.");
                    }
                    saslClient.dispose();

                    ctx.pipeline().remove(this);
                    successfulConnectHandler.onSuccess(ctx.channel());
                    return;
                }
            }
            saslToken = new byte[len];
            if (LOG.isDebugEnabled()) {
                LOG.debug("Will read input token of size " + saslToken.length
                        + " for processing by initSASLContext");
            }
            in.readBytes(saslToken);

            saslToken = evaluateChallenge(saslToken);
            if (saslToken != null) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Will send token of size " + saslToken.length + " from initSASLContext.");
                }
                writeSaslToken(ctx, saslToken);
            }
        }

        if (saslClient.isComplete()) {
            String qop = (String) saslClient.getNegotiatedProperty(Sasl.QOP);

            if (LOG.isDebugEnabled()) {
                LOG.debug("SASL client context established. Negotiated QoP: " + qop);
            }

            boolean useWrap = qop != null && !"auth".equalsIgnoreCase(qop);

            if (!useWrap) {
                ctx.pipeline().remove(this);
            }
            successfulConnectHandler.onSuccess(ctx.channel());
        }
    }
    // Normal wrapped reading
    else {
        try {
            int length = in.readInt();
            if (LOG.isDebugEnabled()) {
                LOG.debug("Actual length is " + length);
            }
            saslToken = new byte[length];
            in.readBytes(saslToken);
        } catch (IndexOutOfBoundsException e) {
            return;
        }
        try {
            ByteBuf b = ctx.channel().alloc().buffer(saslToken.length);

            b.writeBytes(saslClient.unwrap(saslToken, 0, saslToken.length));
            ctx.fireChannelRead(b);

        } catch (SaslException se) {
            try {
                saslClient.dispose();
            } catch (SaslException ignored) {
                LOG.debug("Ignoring SASL exception", ignored);
            }
            throw se;
        }
    }
}

From source file:org.apache.qpid.jms.provider.amqp.message.AmqpCodec.java

License:Apache License

/**
 * Given an encoded AMQP Section, decode the value previously written there.
 *
 * @param encoded/*ww w  .j  a  v  a  2 s.co m*/
 *      the AMQP Section value to decode.
 *
 * @return a Section object read from its encoded form.
 */
public static Section decode(ByteBuf encoded) {
    if (encoded == null || !encoded.isReadable()) {
        return null;
    }

    DecoderImpl decoder = TLS_CODEC.get().decoder;
    decoder.setByteBuffer(encoded.nioBuffer());
    Section result = (Section) decoder.readObject();
    decoder.setByteBuffer(null);
    encoded.resetReaderIndex();

    return result;
}

From source file:org.apache.spark.network.util.TransportFrameDecoderSuite.java

License:Apache License

/**
 * Creates a number of randomly sized frames and feed them to the given decoder, verifying
 * that the frames were read.//from  w w w  .  ja v  a 2  s . c  o  m
 */
private ByteBuf createAndFeedFrames(int frameCount, TransportFrameDecoder decoder, ChannelHandlerContext ctx)
        throws Exception {
    ByteBuf data = Unpooled.buffer();
    for (int i = 0; i < frameCount; i++) {
        byte[] frame = new byte[1024 * (RND.nextInt(31) + 1)];
        data.writeLong(frame.length + 8);
        data.writeBytes(frame);
    }

    try {
        while (data.isReadable()) {
            int size = RND.nextInt(4 * 1024) + 256;
            decoder.channelRead(ctx, data.readSlice(Math.min(data.readableBytes(), size)).retain());
        }

        verify(ctx, times(frameCount)).fireChannelRead(any(ByteBuf.class));
    } catch (Exception e) {
        release(data);
        throw e;
    }
    return data;
}

From source file:org.apache.tajo.plan.function.stream.TextFieldSerializerDeserializer.java

License:Apache License

private static boolean isNull(ByteBuf val, ByteBuf nullBytes) {
    return !val.isReadable() || nullBytes.equals(val);
}

From source file:org.apache.tinkerpop.gremlin.driver.ser.GryoBaseMessageSerializerV1d0Test.java

License:Apache License

@Test
public void shouldReturnAllBytesInResponse() throws Exception {
    final UUID id = UUID.randomUUID();

    final Map<String, Object> metaData = new HashMap<>();
    metaData.put("test", "this");
    metaData.put("one", 1);

    final Map<String, Object> attributes = new HashMap<>();
    attributes.put("test", "that");
    attributes.put("two", 2);

    final ResponseMessage response = ResponseMessage.build(id).responseMetaData(metaData)
            .code(ResponseStatusCode.SUCCESS).result("some-result").statusAttributes(attributes)
            .statusMessage("worked").create();

    final MessageSerializer binarySerializerWithSmallBuffer = new GryoMessageSerializerV1d0();
    final Map<String, Object> configWithSmallBuffer = new HashMap<String, Object>() {
        {/*from ww  w  .  j  a  v  a  2 s.  co m*/
            // set to bufferSize < total message size but still greater than any individual object requires
            put("bufferSize", 50);
        }
    };
    binarySerializerWithSmallBuffer.configure(configWithSmallBuffer, null);

    final ByteBuf buf = binarySerializerWithSmallBuffer.serializeResponseAsBinary(response, allocator);
    assertTrue(buf.isReadable());
    assertEquals(82, buf.readableBytes());
}

From source file:org.apache.tinkerpop.gremlin.driver.ser.GryoBaseMessageSerializerV1d0Test.java

License:Apache License

@Test
public void shouldReturnAllBytesInRequest() throws Exception {
    final UUID id = UUID.randomUUID();

    final RequestMessage request = RequestMessage.build("try").overrideRequestId(id).processor("pro")
            .addArg("test", "this").create();

    final MessageSerializer binarySerializerWithSmallBuffer = new GryoMessageSerializerV1d0();
    final Map<String, Object> configWithSmallBuffer = new HashMap<String, Object>() {
        {/*from   w w  w  .  j a va2s. c  o m*/
            // set to bufferSize < total message size but still greater than any individual object requires
            put("bufferSize", 50);
        }
    };
    binarySerializerWithSmallBuffer.configure(configWithSmallBuffer, null);

    ByteBuf buf = binarySerializerWithSmallBuffer.serializeRequestAsBinary(request, allocator);
    assertTrue(buf.isReadable());
    assertEquals(71, buf.readableBytes());
}

From source file:org.apache.zookeeper.server.NettyServerCnxn.java

License:Apache License

/**
 * Process incoming message. This should only be called from the event
 * loop thread.//from ww w .j  a v a2 s .co m
 * @param buf the message bytes to process.
 */
void processMessage(ByteBuf buf) {
    assert channel.eventLoop().inEventLoop();
    if (LOG.isDebugEnabled()) {
        LOG.debug("0x{} queuedBuffer: {}", Long.toHexString(sessionId), queuedBuffer);
    }

    if (LOG.isTraceEnabled()) {
        LOG.trace("0x{} buf {}", Long.toHexString(sessionId), ByteBufUtil.hexDump(buf));
    }

    if (throttled.get()) {
        LOG.debug("Received message while throttled");
        // we are throttled, so we need to queue
        if (queuedBuffer == null) {
            LOG.debug("allocating queue");
            queuedBuffer = channel.alloc().buffer(buf.readableBytes());
        }
        queuedBuffer.writeBytes(buf);
        if (LOG.isTraceEnabled()) {
            LOG.trace("0x{} queuedBuffer {}", Long.toHexString(sessionId), ByteBufUtil.hexDump(queuedBuffer));
        }
    } else {
        LOG.debug("not throttled");
        if (queuedBuffer != null) {
            queuedBuffer.writeBytes(buf);
            processQueuedBuffer();
        } else {
            receiveMessage(buf);
            // Have to check !closingChannel, because an error in
            // receiveMessage() could have led to close() being called.
            if (!closingChannel && buf.isReadable()) {
                if (LOG.isTraceEnabled()) {
                    LOG.trace("Before copy {}", buf);
                }
                if (queuedBuffer == null) {
                    queuedBuffer = channel.alloc().buffer(buf.readableBytes());
                }
                queuedBuffer.writeBytes(buf);
                if (LOG.isTraceEnabled()) {
                    LOG.trace("Copy is {}", queuedBuffer);
                    LOG.trace("0x{} queuedBuffer {}", Long.toHexString(sessionId),
                            ByteBufUtil.hexDump(queuedBuffer));
                }
            }
        }
    }
}