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 ByteBuf writerIndex(int writerIndex);

Source Link

Document

Sets the writerIndex of this buffer.

Usage

From source file:org.dcache.xrootd.stream.ChunkedFileReadvResponse.java

License:Open Source License

@Override
protected ByteBuf read(ByteBufAllocator alloc, int fd, long position, int length)
        throws IOException, XrootdException {
    if (fd < 0 || fd >= files.size() || files.get(fd) == null) {
        throw new XrootdException(kXR_FileNotOpen, "Invalid file descriptor");
    }/*ww  w .  j  av  a  2 s .c o  m*/

    FileChannel channel = files.get(fd).getChannel();

    ByteBuf chunk = alloc.ioBuffer(length);
    try {
        chunk.writerIndex(length);
        ByteBuffer buffer = chunk.nioBuffer();

        while (length > 0) {
            /* use position independent thread safe call */
            int bytes = channel.read(buffer, position);
            if (bytes < 0) {
                break;
            }
            position += bytes;
            length -= bytes;
        }
        chunk.writerIndex(chunk.writerIndex() - length);
        return chunk;
    } catch (RuntimeException | IOException e) {
        ReferenceCountUtil.release(chunk);
        throw e;
    }
}

From source file:org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamDecoderTest.java

License:Open Source License

@Test
public void testReadVariantStackOverflow() {
    ByteBuf buffer = Unpooled.buffer();

    for (int i = 0; i < 10000; i++) {
        buffer.writerIndex(5 * i);

        buffer.writeByte(24 | 0x80);//from   w  w w .  j  av  a2  s  .  co m
        buffer.writeByte(1);
        buffer.writeByte(0);
        buffer.writeByte(0);
        buffer.writeByte(0);
    }
    buffer.writeByte(0);

    assertThrows(UaSerializationException.class, () -> new OpcUaBinaryStreamDecoder(buffer).readVariant());
}

From source file:org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamDecoderTest.java

License:Open Source License

@Test
public void testReadVariantStackOverflow2() {
    ByteBuf buffer = Unpooled.buffer();

    for (int i = 0; i < 10000; i++) {
        buffer.writerIndex(2 * i);

        buffer.writeByte(23);/*from  ww  w.j  av a2  s  .  c  om*/
        buffer.writeByte(1);
    }
    buffer.writeByte(0);

    assertThrows(UaSerializationException.class, () -> new OpcUaBinaryStreamDecoder(buffer).readVariant());
}

From source file:org.enderstone.server.packet.NetworkEncrypter.java

License:Open Source License

protected ByteBuf decrypt(ChannelHandlerContext ctx, ByteBuf inbytes) throws ShortBufferException {
    int readableBytes = inbytes.readableBytes();
    byte[] byteArray = fillInBuffer(inbytes);
    ByteBuf localByteBuf = ctx.alloc().heapBuffer(this.cipher.getOutputSize(readableBytes));
    localByteBuf.writerIndex(
            this.cipher.update(byteArray, 0, readableBytes, localByteBuf.array(), localByteBuf.arrayOffset()));
    return localByteBuf;
}

From source file:org.kaazing.messaging.driver.transport.netty.tcp.NettySendingTransport.java

License:Apache License

private ChannelFuture writeAndFlush(DriverMessage driverMessage) {
    //TODO(JAF): Figure out why multiple sends are getting condensed down to a single send
    ByteBuf nettyMessage = tlNettyMessage.get();
    nettyMessage.retain();//  w  w w  . j  a  v  a2s .  c  o  m

    //TODO(JAF): Avoid making a new byte array with each send
    byte[] bytesToSend = new byte[driverMessage.getBufferLength()];
    driverMessage.getBuffer().getBytes(driverMessage.getBufferOffset(), bytesToSend);
    nettyMessage.setBytes(0, bytesToSend);
    nettyMessage.writerIndex(bytesToSend.length);

    return sendingChannel.writeAndFlush(nettyMessage);
}

From source file:org.mashupbots.socko.netty.HttpChunkedFile.java

License:Apache License

@Override
public HttpContent readChunk(ChannelHandlerContext ctx) throws Exception {
    long offset = this.offset;
    if (offset >= endOffset) {
        if (sentLastChunk) {
            return null;
        } else {/*  ww  w  . j  a  va  2 s .c om*/
            // Send last chunk for this file
            sentLastChunk = true;
            return new DefaultLastHttpContent();
        }
    }

    int chunkSize = (int) Math.min(this.chunkSize, endOffset - offset);
    // Check if the buffer is backed by an byte array. If so we can optimize it a bit an safe a copy

    ByteBuf buf = ctx.alloc().heapBuffer(chunkSize);
    boolean release = true;
    try {
        file.readFully(buf.array(), buf.arrayOffset(), chunkSize);
        buf.writerIndex(chunkSize);
        this.offset = offset + chunkSize;
        release = false;
        return new DefaultHttpContent(buf);
    } finally {
        if (release) {
            buf.release();
        }
    }

}

From source file:org.mobicents.protocols.ss7.m3ua.impl.message.MessageFactoryImpl.java

License:Open Source License

public M3UAMessageImpl createMessage(ByteBuf message) {
    int dataLen;/* www  . j a  v a 2  s.  c o  m*/
    if (message.readableBytes() < 8) {
        return null;
    }

    // obtain message class and type from header
    message.markReaderIndex();
    message.skipBytes(2);
    int messageClass = message.readUnsignedByte();
    int messageType = message.readUnsignedByte();

    // obtain remaining length of the message and prepare buffer
    dataLen = message.readInt() - 8;
    if (message.readableBytes() < dataLen) {
        message.resetReaderIndex();
        return null;
    }

    // construct new message instance
    M3UAMessageImpl messageTemp = this.createMessage(messageClass, messageType);

    // parsing params of this message
    message.markWriterIndex();
    message.writerIndex(message.readerIndex() + dataLen);
    messageTemp.decode(message);
    message.resetWriterIndex();

    return messageTemp;
}

From source file:org.opencloudb.net.mysql.FieldPacket.java

License:Open Source License

private void writeBody(ByteBuf buffer) {
    byte nullVal = 0;
    BufferUtil.writeWithLength(buffer, catalog, nullVal);
    BufferUtil.writeWithLength(buffer, db, nullVal);
    BufferUtil.writeWithLength(buffer, table, nullVal);
    BufferUtil.writeWithLength(buffer, orgTable, nullVal);
    BufferUtil.writeWithLength(buffer, name, nullVal);
    BufferUtil.writeWithLength(buffer, orgName, nullVal);
    buffer.writeByte((byte) 0x0C);
    BufferUtil.writeUB2(buffer, charsetIndex);
    BufferUtil.writeUB4(buffer, length);
    buffer.writeByte((byte) (type & 0xff));
    BufferUtil.writeUB2(buffer, flags);/*from  w ww. ja  v a 2  s. c  o m*/
    buffer.writeByte(decimals);
    buffer.writerIndex(buffer.writerIndex() + FILLER.length);
    if (definition != null) {
        BufferUtil.writeWithLength(buffer, definition);
    }
}

From source file:org.opendaylight.protocol.pcep.segment.routing.SrPceCapabilityTlvParser.java

License:Open Source License

@Override
public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
    Preconditions.checkArgument(tlv instanceof SrPceCapability, "SrPceCapability is mandatory.");
    final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
    body.writerIndex(OFFSET);
    writeUnsignedByte(((SrPceCapability) tlv).getMsd(), body);
    TlvUtil.formatTlv(TYPE, body, buffer);
}

From source file:org.rzo.yajsw.controller.jvm.MessageDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext paramChannelHandlerContext, ByteBuf b, List<Object> out)
        throws Exception {
    if (!b.isReadable())
        return;//from w  w  w  . j  a  v a2s  .c  o  m
    byte code = b.readByte();
    b.writerIndex(b.writerIndex());
    String msg = b.toString(Charset.defaultCharset());
    Message result = new Message(code, msg);
    out.add(result);

}