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:net.tomp2p.storage.AlternativeCompositeByteBuf.java

License:Apache License

@Override
public ByteBuf readBytes(ByteBuf dst, int length) {
    if (length > dst.writableBytes()) {
        throw new IndexOutOfBoundsException(String.format(
                "length(%d) exceeds dst.writableBytes(%d) where dst is: %s", length, dst.writableBytes(), dst));
    }/*  w w w. j av  a2  s  .com*/
    readBytes(dst, dst.writerIndex(), length);
    dst.writerIndex(dst.writerIndex() + length);
    return this;
}

From source file:nettyClient4.clientImpl.java

License:Apache License

private ByteBuf getWriteBuffer(int arg1, int arg2, ByteBuf buffer, Object... paras) {
    if (buffer == null) {
        buffer = PooledByteBufAllocator.DEFAULT.heapBuffer(10);
    }/*from  www.  j av a 2  s  .  co m*/
    buffer.writeShort(Short.MIN_VALUE);//?2
    buffer.writeByte(arg1);
    if (arg2 != 0)
        buffer.writeByte(arg2);
    for (Object para : paras) {
        if (para instanceof Byte) {
            buffer.writeByte((Byte) para); // ?1
        } else if ((para instanceof String)) {
            buffer.writeBytes(((String) para).getBytes());
        } else if (para instanceof Integer) {
            buffer.writeInt((Integer) para); //?4
        } else if (para instanceof Short) {
            buffer.writeShort((Short) para); //?2
        }
    }
    /**?2setShort*/
    buffer.setShort(0, buffer.writerIndex() - 0x2);
    return buffer;
}

From source file:nl.thijsalders.spigotproxy.haproxy.HAProxyMessageDecoder.java

License:Apache License

/**
 * Returns the index in the buffer of the end of line found.
 * Returns -1 if no end of line was found in the buffer.
 *//*from  w w w. j a  va2 s  . c  o  m*/
private static int findEndOfLine(final ByteBuf buffer) {
    final int n = buffer.writerIndex();
    for (int i = buffer.readerIndex(); i < n; i++) {
        final byte b = buffer.getByte(i);
        if (b == '\r' && i < n - 1 && buffer.getByte(i + 1) == '\n') {
            return i; // \r\n
        }
    }
    return -1; // Not found.
}

From source file:org.apache.activemq.artemis.core.io.mapped.MappedSequentialFile.java

License:Apache License

@Override
public void write(ActiveMQBuffer bytes, boolean sync, IOCallback callback) throws IOException {
    if (callback == null) {
        throw new NullPointerException("callback parameter need to be set");
    }/*from w ww  .  j  a  v a  2s .c o  m*/
    checkIsOpen(callback);
    try {
        final ByteBuf byteBuf = bytes.byteBuf();
        final int writerIndex = byteBuf.writerIndex();
        final int readerIndex = byteBuf.readerIndex();
        final int readableBytes = writerIndex - readerIndex;
        if (readableBytes > 0) {
            this.mappedFile.write(byteBuf, readerIndex, readableBytes);
            if (factory.isDatasync() && sync) {
                this.mappedFile.force();
            }
        }
        callback.done();
    } catch (IOException e) {
        if (this.criticalErrorListener != null) {
            this.criticalErrorListener.onIOException(new ActiveMQIOErrorException(e.getMessage(), e),
                    e.getMessage(), this);
        }
        callback.onError(ActiveMQExceptionType.IO_ERROR.getCode(), e.getMessage());
        throw e;
    }
}

From source file:org.apache.activemq.artemis.core.io.mapped.MappedSequentialFile.java

License:Apache License

@Override
public void write(ActiveMQBuffer bytes, boolean sync) throws IOException {
    checkIsOpen();//from  w w  w .j  av a 2  s .  com
    final ByteBuf byteBuf = bytes.byteBuf();
    final int writerIndex = byteBuf.writerIndex();
    final int readerIndex = byteBuf.readerIndex();
    final int readableBytes = writerIndex - readerIndex;
    if (readableBytes > 0) {
        this.mappedFile.write(byteBuf, readerIndex, readableBytes);
        if (factory.isDatasync() && sync) {
            this.mappedFile.force();
        }
    }
}

From source file:org.apache.activemq.artemis.core.message.impl.CoreMessage.java

License:Apache License

/**
 * Recast the message as an 1.4 message/*w w w  .  ja v  a2s. co  m*/
 */
@Override
public void sendBuffer_1X(ByteBuf sendBuffer) {
    checkEncode();
    ByteBuf tmpBuffer = buffer.duplicate();
    sendBuffer.writeInt(endOfBodyPosition + DataConstants.SIZE_INT);
    tmpBuffer.readerIndex(DataConstants.SIZE_INT);
    tmpBuffer.readBytes(sendBuffer, endOfBodyPosition - BUFFER_HEADER_SPACE);
    sendBuffer.writeInt(tmpBuffer.writerIndex() + DataConstants.SIZE_INT + BUFFER_HEADER_SPACE);
    tmpBuffer.readBytes(sendBuffer, tmpBuffer.readableBytes());
    sendBuffer.readerIndex(0);
}

From source file:org.apache.activemq.artemis.core.message.impl.CoreMessage.java

License:Apache License

public void encodeHeadersAndProperties(final ByteBuf buffer) {
    checkProperties();/*from   w  w w . j  a  v  a2 s  . c om*/
    messageIDPosition = buffer.writerIndex();
    buffer.writeLong(messageID);
    SimpleString.writeNullableSimpleString(buffer, address);
    if (userID == null) {
        buffer.writeByte(DataConstants.NULL);
    } else {
        buffer.writeByte(DataConstants.NOT_NULL);
        buffer.writeBytes(userID.asBytes());
    }
    buffer.writeByte(type);
    buffer.writeBoolean(durable);
    buffer.writeLong(expiration);
    buffer.writeLong(timestamp);
    buffer.writeByte(priority);
    properties.encode(buffer);
}

From source file:org.apache.activemq.artemis.core.protocol.core.impl.PacketImpl.java

License:Apache License

protected ByteBuf copyMessageBuffer(ByteBuf buffer, int skipBytes) {

    ByteBuf newNettyBuffer = Unpooled.buffer(buffer.capacity() - PACKET_HEADERS_SIZE - skipBytes);

    int read = buffer.readerIndex();
    int writ = buffer.writerIndex();
    buffer.readerIndex(PACKET_HEADERS_SIZE);

    newNettyBuffer.writeBytes(buffer, buffer.readableBytes() - skipBytes);
    buffer.setIndex(read, writ);//from   w w w  .  ja  v a 2  s.  com
    newNettyBuffer.setIndex(0, writ - PACKET_HEADERS_SIZE - skipBytes);

    return newNettyBuffer;
}

From source file:org.apache.activemq.artemis.core.protocol.proton.plug.ActiveMQProtonConnectionCallback.java

License:Apache License

public void onTransport(ByteBuf byteBuf, AMQPConnectionContext amqpConnection) {
    final int size = byteBuf.writerIndex();

    latch.countUp();/*from   ww w. j a  v a2s  .  co m*/
    connection.write(new ChannelBufferWrapper(byteBuf, true), false, false, new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            latch.countDown();
        }
    });

    if (amqpConnection.isSyncOnFlush()) {
        try {
            latch.await(5, TimeUnit.SECONDS);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    amqpConnection.outputDone(size);
}

From source file:org.apache.activemq.artemis.core.protocol.proton.plug.ProtonSessionIntegrationCallback.java

License:Apache License

@Override
public void serverSend(final Receiver receiver, final Delivery delivery, String address, int messageFormat,
        ByteBuf messageEncoded) throws Exception {
    EncodedMessage encodedMessage = new EncodedMessage(messageFormat, messageEncoded.array(),
            messageEncoded.arrayOffset(), messageEncoded.writerIndex());

    ServerMessage message = manager.getConverter().inbound(encodedMessage);
    //use the address on the receiver if not null, if null let's hope it was set correctly on the message
    if (address != null) {
        message.setAddress(new SimpleString(address));
    }//ww w . jav a2s . c o m

    serverSession.send(message, false);

    manager.getServer().getStorageManager().afterCompleteOperations(new IOAsyncTask() {
        @Override
        public void done() {
            synchronized (connection.getLock()) {
                delivery.settle();
                connection.flush();
            }
        }

        @Override
        public void onError(int errorCode, String errorMessage) {
            synchronized (connection.getLock()) {
                receiver.setCondition(
                        new ErrorCondition(AmqpError.ILLEGAL_STATE, errorCode + ":" + errorMessage));
                connection.flush();
            }
        }
    });
}