Example usage for io.netty.buffer ByteBuf writerIndex

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

Introduction

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

Prototype

public abstract int writerIndex();

Source Link

Document

Returns the writerIndex of this buffer.

Usage

From source file:org.apache.activemq.artemis.tests.integration.transports.netty.ActiveMQFrameDecoder2Test.java

License:Apache License

@Test
public void testOrdinaryFragmentation() throws Exception {
    final EmbeddedChannel decoder = new EmbeddedChannel(new ActiveMQFrameDecoder2());
    final byte[] data = new byte[ActiveMQFrameDecoder2Test.MSG_LEN];
    ActiveMQFrameDecoder2Test.rand.nextBytes(data);

    ByteBuf src = Unpooled.buffer(ActiveMQFrameDecoder2Test.MSG_CNT * (ActiveMQFrameDecoder2Test.MSG_LEN + 4));
    while (src.writerIndex() < src.capacity()) {
        src.writeInt(ActiveMQFrameDecoder2Test.MSG_LEN);
        src.writeBytes(data);/*from  w w  w  .j  a v a2  s . c  o m*/
    }

    List<ByteBuf> packets = new ArrayList<ByteBuf>();
    while (src.isReadable()) {
        int length = Math.min(
                ActiveMQFrameDecoder2Test.rand.nextInt(ActiveMQFrameDecoder2Test.FRAGMENT_MAX_LEN),
                src.readableBytes());
        packets.add(src.readBytes(length));
    }

    int cnt = 0;
    for (ByteBuf p : packets) {
        decoder.writeInbound(p);
        for (;;) {
            ByteBuf frame = (ByteBuf) decoder.readInbound();
            if (frame == null) {
                break;
            }
            Assert.assertEquals(4, frame.readerIndex());
            Assert.assertEquals(ActiveMQFrameDecoder2Test.MSG_LEN, frame.readableBytes());
            Assert.assertEquals(Unpooled.wrappedBuffer(data), frame);
            cnt++;
            frame.release();
        }
    }
    Assert.assertEquals(ActiveMQFrameDecoder2Test.MSG_CNT, cnt);
}

From source file:org.apache.activemq.artemis.utils.UTF8Util.java

License:Apache License

public static void saveUTF(final ByteBuf out, final String str) {

    if (str.length() > 0xffff) {
        throw ActiveMQUtilBundle.BUNDLE.stringTooLong(str.length());
    }//from w w w  . j a v  a2  s . c om

    final int len = UTF8Util.calculateUTFSize(str);

    if (len > 0xffff) {
        throw ActiveMQUtilBundle.BUNDLE.stringTooLong(len);
    }

    out.writeShort((short) len);

    final int stringLength = str.length();

    if (UTF8Util.isTrace) {
        // This message is too verbose for debug, that's why we are using trace here
        ActiveMQUtilLogger.LOGGER.trace("Saving string with utfSize=" + len + " stringSize=" + stringLength);
    }

    if (out.hasArray()) {
        out.ensureWritable(len);
        final byte[] bytes = out.array();
        final int writerIndex = out.writerIndex();
        final int index = out.arrayOffset() + writerIndex;
        if (PlatformDependent.hasUnsafe()) {
            unsafeOnHeapWriteUTF(str, bytes, index, stringLength);
        } else {
            writeUTF(str, bytes, index, stringLength);
        }
        out.writerIndex(writerIndex + len);
    } else {
        if (PlatformDependent.hasUnsafe() && out.hasMemoryAddress()) {
            out.ensureWritable(len);
            final long addressBytes = out.memoryAddress();
            final int writerIndex = out.writerIndex();
            unsafeOffHeapWriteUTF(str, addressBytes, writerIndex, stringLength);
            out.writerIndex(writerIndex + len);
        } else {
            final StringUtilBuffer buffer = UTF8Util.getThreadLocalBuffer();
            final byte[] bytes = buffer.borrowByteBuffer(len);
            writeUTF(str, bytes, 0, stringLength);
            out.writeBytes(bytes, 0, len);
        }
    }
}

From source file:org.apache.bookkeeper.proto.BookieProtoEncoding.java

License:Apache License

private static ByteBuf serializeProtobuf(MessageLite msg, ByteBufAllocator allocator) {
    int size = msg.getSerializedSize();
    ByteBuf buf = allocator.heapBuffer(size, size);

    try {//from w  w  w.  j a v  a  2  s.c  o m
        msg.writeTo(CodedOutputStream.newInstance(buf.array(), buf.arrayOffset() + buf.writerIndex(), size));
    } catch (IOException e) {
        // This is in-memory serialization, should not fail
        throw new RuntimeException(e);
    }

    // Advance writer idx
    buf.writerIndex(buf.capacity());
    return buf;
}

From source file:org.apache.camel.component.netty4.http.DefaultNettyHttpBinding.java

License:Apache License

@Override
public HttpResponse toNettyResponse(Message message, NettyHttpConfiguration configuration) throws Exception {
    LOG.trace("toNettyResponse: {}", message);

    // the message body may already be a Netty HTTP response
    if (message.getBody() instanceof HttpResponse) {
        return (HttpResponse) message.getBody();
    }/*  www .j  a va2 s  .com*/

    Object body = message.getBody();
    Exception cause = message.getExchange().getException();
    // support bodies as native Netty
    ByteBuf buffer;
    // the response code is 200 for OK and 500 for failed
    boolean failed = message.getExchange().isFailed();
    int defaultCode = failed ? 500 : 200;

    int code = message.getHeader(Exchange.HTTP_RESPONSE_CODE, defaultCode, int.class);

    LOG.trace("HTTP Status Code: {}", code);

    // if there was an exception then use that as body
    if (cause != null) {
        if (configuration.isTransferException()) {
            // we failed due an exception, and transfer it as java serialized object
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(cause);
            oos.flush();
            IOHelper.close(oos, bos);

            // the body should be the serialized java object of the exception
            body = NettyConverter.toByteBuffer(bos.toByteArray());
            // force content type to be serialized java object
            message.setHeader(Exchange.CONTENT_TYPE, NettyHttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT);
        } else {
            // we failed due an exception so print it as plain text
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            cause.printStackTrace(pw);

            // the body should then be the stacktrace
            body = NettyConverter.toByteBuffer(sw.toString().getBytes());
            // force content type to be text/plain as that is what the stacktrace is
            message.setHeader(Exchange.CONTENT_TYPE, "text/plain");
        }

        // and mark the exception as failure handled, as we handled it by returning it as the response
        ExchangeHelper.setFailureHandled(message.getExchange());
    }

    if (body instanceof ByteBuf) {
        buffer = (ByteBuf) body;
    } else {
        // try to convert to buffer first
        buffer = message.getBody(ByteBuf.class);
        if (buffer == null) {
            // fallback to byte array as last resort
            byte[] data = message.getBody(byte[].class);
            if (data != null) {
                buffer = NettyConverter.toByteBuffer(data);
            } else {
                // and if byte array fails then try String
                String str;
                if (body != null) {
                    str = message.getMandatoryBody(String.class);
                } else {
                    str = "";
                }
                buffer = NettyConverter.toByteBuffer(str.getBytes());
            }
        }
    }

    HttpResponse response = null;

    if (buffer != null) {
        response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.valueOf(code), buffer);
        // We just need to reset the readerIndex this time
        if (buffer.readerIndex() == buffer.writerIndex()) {
            buffer.setIndex(0, buffer.writerIndex());
        }
        // TODO How to enable the chunk transport 
        int len = buffer.readableBytes();
        // set content-length
        response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, len);
        LOG.trace("Content-Length: {}", len);
    } else {
        response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.valueOf(code));
    }

    TypeConverter tc = message.getExchange().getContext().getTypeConverter();

    // append headers
    // must use entrySet to ensure case of keys is preserved
    for (Map.Entry<String, Object> entry : message.getHeaders().entrySet()) {
        String key = entry.getKey();
        Object value = entry.getValue();
        // use an iterator as there can be multiple values. (must not use a delimiter)
        final Iterator<?> it = ObjectHelper.createIterator(value, null);
        while (it.hasNext()) {
            String headerValue = tc.convertTo(String.class, it.next());
            if (headerValue != null && headerFilterStrategy != null && !headerFilterStrategy
                    .applyFilterToCamelHeaders(key, headerValue, message.getExchange())) {
                LOG.trace("HTTP-Header: {}={}", key, headerValue);
                response.headers().add(key, headerValue);
            }
        }
    }

    // set the content type in the response.
    String contentType = MessageHelper.getContentType(message);
    if (contentType != null) {
        // set content-type
        response.headers().set(HttpHeaders.Names.CONTENT_TYPE, contentType);
        LOG.trace("Content-Type: {}", contentType);
    }

    // configure connection to accordingly to keep alive configuration
    // favor using the header from the message
    String connection = message.getHeader(HttpHeaders.Names.CONNECTION, String.class);
    // Read the connection header from the exchange property
    if (connection == null) {
        connection = message.getExchange().getProperty(HttpHeaders.Names.CONNECTION, String.class);
    }
    if (connection == null) {
        // fallback and use the keep alive from the configuration
        if (configuration.isKeepAlive()) {
            connection = HttpHeaders.Values.KEEP_ALIVE;
        } else {
            connection = HttpHeaders.Values.CLOSE;
        }
    }
    response.headers().set(HttpHeaders.Names.CONNECTION, connection);
    // Just make sure we close the channel when the connection value is close
    if (connection.equalsIgnoreCase(HttpHeaders.Values.CLOSE)) {
        message.setHeader(NettyConstants.NETTY_CLOSE_CHANNEL_WHEN_COMPLETE, true);
    }
    LOG.trace("Connection: {}", connection);

    return response;
}

From source file:org.apache.directory.server.dhcp.netty.Dhcp6Handler.java

@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Incomming DHCP : {}, from: {}", ByteBufUtil.hexDump(msg.content()), msg.sender());
    }//from   ww  w  . j a v  a  2 s.  c  om

    final Dhcp6Message incommingMsg;
    try {
        incommingMsg = dhcp6MessageDecoder.decode(msg.content().nioBuffer());
    } catch (final Dhcp6Exception.UnknownMsgException e) {
        LOG.warn("Unknown DHCP message type: {}. Ignoring", ByteBufUtil.hexDump(msg.content()), e);
        return;
    }

    final Optional<Dhcp6Message> reply = dhcpService
            .getReplyFor(new Dhcp6RequestContext(msg.sender().getAddress()), incommingMsg);

    if (reply.isPresent()) {
        LOG.debug("Responding with message: {}", reply.get());

        // TODO what size to allocate the buffer to ?
        ByteBuf buf = ctx.alloc().buffer(1024);
        ByteBuffer buffer = buf.nioBuffer(buf.writerIndex(), buf.writableBytes());
        dhcp6MessageEncoder.encode(buffer, reply.get());
        buffer.flip();
        buf.writerIndex(buf.writerIndex() + buffer.remaining());
        DatagramPacket packet = new DatagramPacket(buf, msg.sender());
        ctx.write(packet);
    } else {
        LOG.warn("No response from DHCP service received for: {}. Ignoring.", incommingMsg);
    }
}

From source file:org.apache.directory.server.dhcp.netty.DhcpHandler.java

@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
    DhcpMessage request = decoder.decode(msg.content().nioBuffer());

    DhcpRequestContext context = interfaceManager.newRequestContext(
            (InetSocketAddress) ctx.channel().localAddress(), msg.sender(), msg.recipient(), request);
    if (context == null) {
        debug("IGNQUERY", msg.sender(), msg.recipient(), request);
        return;/*from w  w w  .  j  a  v  a  2s . c o  m*/
    }
    // debug("READ", msg.sender(), msg.recipient(), request);

    MDCUtils.init(context, request);
    try {
        DhcpMessage reply = dhcpService.getReplyFor(context, request);
        if (reply == null) {
            debug("NOREPLY", msg.sender(), msg.recipient(), request);
            return;
        }

        InterfaceAddress localAddress = interfaceManager.getResponseInterface(request.getRelayAgentAddress(),
                request.getCurrentClientAddress(), msg.sender().getAddress(), reply);
        if (localAddress == null) {
            debug("NOIFACE", msg.recipient(), msg.sender(), reply);
            return;
        }

        debug("READ", msg.sender(), msg.recipient(), request);

        InetSocketAddress isa = DhcpInterfaceUtils.determineMessageDestination(request, reply, localAddress,
                msg.sender().getPort());

        ByteBuf buf = ctx.alloc().buffer(1024);
        ByteBuffer buffer = buf.nioBuffer(buf.writerIndex(), buf.writableBytes());
        encoder.encode(buffer, reply);
        buffer.flip();
        buf.writerIndex(buf.writerIndex() + buffer.remaining());
        DatagramPacket packet = new DatagramPacket(buf, isa);
        debug("WRITE", packet.sender(), packet.recipient(), reply);
        ctx.write(packet, ctx.voidPromise());
    } finally {
        MDCUtils.fini();
    }
}

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

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, OutboundRpcMessage msg, List<Object> out) throws Exception {
    if (RpcConstants.EXTRA_DEBUGGING) {
        logger.debug("Rpc Encoder called with msg {}", msg);
    }/* w w w  .  j a  v  a 2  s.c o  m*/

    if (!ctx.channel().isOpen()) {
        //output.add(ctx.alloc().buffer(0));
        logger.debug("Channel closed, skipping encode.");
        msg.release();
        return;
    }

    try {
        if (RpcConstants.EXTRA_DEBUGGING) {
            logger.debug("Encoding outbound message {}", msg);
        }
        // first we build the RpcHeader
        RpcHeader header = RpcHeader.newBuilder() //
                .setMode(msg.mode) //
                .setCoordinationId(msg.coordinationId) //
                .setRpcType(msg.rpcType).build();

        // figure out the full length
        int headerLength = header.getSerializedSize();
        int protoBodyLength = msg.pBody.getSerializedSize();
        int rawBodyLength = msg.getRawBodySize();
        int fullLength = //
                HEADER_TAG_LENGTH + getRawVarintSize(headerLength) + headerLength + //
                        PROTOBUF_BODY_TAG_LENGTH + getRawVarintSize(protoBodyLength) + protoBodyLength; //

        if (rawBodyLength > 0) {
            fullLength += (RAW_BODY_TAG_LENGTH + getRawVarintSize(rawBodyLength) + rawBodyLength);
        }

        ByteBuf buf = ctx.alloc().buffer();
        OutputStream os = new ByteBufOutputStream(buf);
        CodedOutputStream cos = CodedOutputStream.newInstance(os);

        // write full length first (this is length delimited stream).
        cos.writeRawVarint32(fullLength);

        // write header
        cos.writeRawVarint32(HEADER_TAG);
        cos.writeRawVarint32(headerLength);
        header.writeTo(cos);

        // write protobuf body length and body
        cos.writeRawVarint32(PROTOBUF_BODY_TAG);
        cos.writeRawVarint32(protoBodyLength);
        msg.pBody.writeTo(cos);

        // if exists, write data body and tag.
        if (msg.getRawBodySize() > 0) {
            if (RpcConstants.EXTRA_DEBUGGING) {
                logger.debug("Writing raw body of size {}", msg.getRawBodySize());
            }

            cos.writeRawVarint32(RAW_BODY_TAG);
            cos.writeRawVarint32(rawBodyLength);
            cos.flush(); // need to flush so that dbody goes after if cos is caching.

            CompositeByteBuf cbb = new CompositeByteBuf(buf.alloc(), true, msg.dBodies.length + 1);
            cbb.addComponent(buf);
            int bufLength = buf.readableBytes();
            for (ByteBuf b : msg.dBodies) {
                cbb.addComponent(b);
                bufLength += b.readableBytes();
            }
            cbb.writerIndex(bufLength);
            out.add(cbb);
        } else {
            cos.flush();
            out.add(buf);
        }

        if (RpcConstants.SOME_DEBUGGING) {
            logger.debug("Wrote message length {}:{} bytes (head:body).  Message: " + msg,
                    getRawVarintSize(fullLength), fullLength);
        }
        if (RpcConstants.EXTRA_DEBUGGING) {
            logger.debug("Sent message.  Ending writer index was {}.", buf.writerIndex());
        }
    } finally {
        // make sure to release Rpc Messages underlying byte buffers.
        //msg.release();
    }
}

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

License:Apache License

private void encode(Envelope env, ByteBuf out) {
    // --------------------------------------------------------------------
    // (1) header (48 bytes)
    // --------------------------------------------------------------------
    out.writeInt(MAGIC_NUMBER); // 4 bytes

    if (out.getInt(out.writerIndex() - 4) != MAGIC_NUMBER) {
        throw new RuntimeException();
    }/*w w w . j av a 2 s  .  c  o m*/

    out.writeInt(env.getSequenceNumber()); // 4 bytes
    env.getJobID().writeTo(out); // 16 bytes
    env.getSource().writeTo(out); // 16 bytes
    out.writeInt(env.getEventsSerialized() != null ? env.getEventsSerialized().remaining() : 0); // 4 bytes
    out.writeInt(env.getBuffer() != null ? env.getBuffer().size() : 0); // 4 bytes
    // --------------------------------------------------------------------
    // (2) events (var length)
    // --------------------------------------------------------------------
    if (env.getEventsSerialized() != null) {
        out.writeBytes(env.getEventsSerialized());
    }

    // --------------------------------------------------------------------
    // (3) buffer (var length)
    // --------------------------------------------------------------------
    if (env.getBuffer() != null) {
        Buffer envBuffer = env.getBuffer();
        out.writeBytes(envBuffer.getMemorySegment().wrap(0, envBuffer.size()));

        // Recycle the buffer from OUR buffer pool after everything has been
        // copied to Nettys buffer space.
        envBuffer.recycleBuffer();
    }
}

From source file:org.apache.giraph.comm.netty.handler.RequestDecoder.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (!(msg instanceof ByteBuf)) {
        throw new IllegalStateException("decode: Got illegal message " + msg);
    }//  ww w . j  av  a  2  s .  c  o m
    // Output metrics every 1/2 minute
    String metrics = byteCounter.getMetricsWindow(30000);
    if (metrics != null) {
        if (LOG.isInfoEnabled()) {
            LOG.info("decode: Server window metrics " + metrics);
        }
    }

    if (LOG.isDebugEnabled()) {
        startDecodingNanoseconds = TIME.getNanoseconds();
    }

    // Decode the request
    ByteBuf buf = (ByteBuf) msg;
    int enumValue = buf.readByte();
    RequestType type = RequestType.values()[enumValue];
    Class<? extends WritableRequest> requestClass = type.getRequestClass();
    WritableRequest request = ReflectionUtils.newInstance(requestClass, conf);
    request = RequestUtils.decodeWritableRequest(buf, request);

    if (LOG.isDebugEnabled()) {
        LOG.debug("decode: Client " + request.getClientId() + ", requestId " + request.getRequestId() + ", "
                + request.getType() + ", with size " + buf.writerIndex() + " took "
                + Times.getNanosSince(TIME, startDecodingNanoseconds) + " ns");
    }
    ReferenceCountUtil.release(buf);
    // fire writableRequest object to upstream handlers
    ctx.fireChannelRead(request);
}

From source file:org.apache.giraph.comm.netty.handler.RequestEncoder.java

License:Apache License

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    if (!(msg instanceof WritableRequest)) {
        throw new IllegalArgumentException("encode: Got a message of type " + msg.getClass());
    }//ww w .  jav a2 s.c o  m

    // Encode the request
    if (LOG.isDebugEnabled()) {
        startEncodingNanoseconds = TIME.getNanoseconds();
    }

    ByteBuf buf;
    WritableRequest request = (WritableRequest) msg;
    int requestSize = request.getSerializedSize();
    if (requestSize == WritableRequest.UNKNOWN_SIZE) {
        buf = ctx.alloc().buffer(bufferStartingSize);
    } else {
        requestSize += SIZE_OF_INT + SIZE_OF_BYTE;
        buf = ctx.alloc().buffer(requestSize);
    }
    ByteBufOutputStream output = new ByteBufOutputStream(buf);

    // This will later be filled with the correct size of serialized request
    output.writeInt(0);
    output.writeByte(request.getType().ordinal());
    try {
        request.write(output);
    } catch (IndexOutOfBoundsException e) {
        LOG.error("write: Most likely the size of request was not properly "
                + "specified (this buffer is too small) - see getSerializedSize() " + "in "
                + request.getType().getRequestClass());
        throw new IllegalStateException(e);
    }
    output.flush();
    output.close();

    // Set the correct size at the end
    buf.setInt(0, buf.writerIndex() - SIZE_OF_INT);
    if (LOG.isDebugEnabled()) {
        LOG.debug("write: Client " + request.getClientId() + ", " + "requestId " + request.getRequestId()
                + ", size = " + buf.readableBytes() + ", " + request.getType() + " took "
                + Times.getNanosSince(TIME, startEncodingNanoseconds) + " ns");
    }
    ctx.write(buf, promise);
}