Example usage for io.netty.buffer ByteBuf readerIndex

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

Introduction

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

Prototype

public abstract ByteBuf readerIndex(int readerIndex);

Source Link

Document

Sets the readerIndex of this buffer.

Usage

From source file:com.yahoo.pulsar.common.api.Commands.java

License:Apache License

private static ByteBuf serializeCommandSendWithSize(BaseCommand.Builder cmdBuilder, ChecksumType checksumType,
        MessageMetadata msgMetadata, ByteBuf payload) {
    // / Wire format
    // [TOTAL_SIZE] [CMD_SIZE][CMD] [MAGIC_NUMBER][CHECKSUM] [METADATA_SIZE][METADATA] [PAYLOAD]

    BaseCommand cmd = cmdBuilder.build();
    int cmdSize = cmd.getSerializedSize();
    int msgMetadataSize = msgMetadata.getSerializedSize();
    int payloadSize = payload.readableBytes();
    int magicAndChecksumLength = ChecksumType.Crc32c.equals(checksumType) ? (2 + 4 /* magic + checksumLength*/)
            : 0;// ww w. j av  a2s.  c  o m
    boolean includeChecksum = magicAndChecksumLength > 0;
    int headerContentSize = 4 + cmdSize + magicAndChecksumLength + 4 + msgMetadataSize; // cmdLength + cmdSize + magicLength +
    // checksumSize + msgMetadataLength +
    // msgMetadataSize
    int totalSize = headerContentSize + payloadSize;
    int headersSize = 4 + headerContentSize; // totalSize + headerLength
    int checksumReaderIndex = -1;

    ByteBuf headers = PooledByteBufAllocator.DEFAULT.buffer(headersSize, headersSize);
    headers.writeInt(totalSize); // External frame

    try {
        // Write cmd
        headers.writeInt(cmdSize);

        ByteBufCodedOutputStream outStream = ByteBufCodedOutputStream.get(headers);
        cmd.writeTo(outStream);
        cmd.recycle();
        cmdBuilder.recycle();

        //Create checksum placeholder
        if (includeChecksum) {
            headers.writeShort(magicCrc32c);
            checksumReaderIndex = headers.writerIndex();
            headers.writerIndex(headers.writerIndex() + checksumSize); //skip 4 bytes of checksum
        }

        // Write metadata
        headers.writeInt(msgMetadataSize);
        msgMetadata.writeTo(outStream);
        outStream.recycle();
    } catch (IOException e) {
        // This is in-memory serialization, should not fail
        throw new RuntimeException(e);
    }

    ByteBuf command = DoubleByteBuf.get(headers, payload);

    // write checksum at created checksum-placeholder
    if (includeChecksum) {
        headers.markReaderIndex();
        headers.readerIndex(checksumReaderIndex + checksumSize);
        int metadataChecksum = computeChecksum(headers);
        int computedChecksum = resumeChecksum(metadataChecksum, payload);
        // set computed checksum
        headers.setInt(checksumReaderIndex, computedChecksum);
        headers.resetReaderIndex();
    }
    return command;
}

From source file:com.yahoo.pulsar.common.compression.CommandsTest.java

License:Apache License

@Test
public void testChecksumSendCommand() throws Exception {

    // test checksum in send command
    String producerName = "prod-name";
    int sequenceId = 0;
    ByteBuf data = Unpooled.buffer(1024);
    MessageMetadata messageMetadata = MessageMetadata.newBuilder().setPublishTime(System.currentTimeMillis())
            .setProducerName(producerName).setSequenceId(sequenceId).build();
    int expectedChecksum = computeChecksum(messageMetadata, data);
    ByteBuf clientCommand = Commands.newSend(1, 0, 1, ChecksumType.Crc32c, messageMetadata, data);
    clientCommand.retain();/*ww w  .  j a va2s  .co  m*/
    ByteBuffer inputBytes = clientCommand.nioBuffer();
    ByteBuf receivedBuf = Unpooled.wrappedBuffer(inputBytes);
    receivedBuf.skipBytes(4); //skip [total-size]
    int cmdSize = (int) receivedBuf.readUnsignedInt();
    receivedBuf.readerIndex(8 + cmdSize);
    int startMessagePos = receivedBuf.readerIndex();

    /*** 1. verify checksum and metadataParsing ***/
    boolean hasChecksum = Commands.hasChecksum(receivedBuf);
    int checksum = Commands.readChecksum(receivedBuf).intValue();

    // verify checksum is present
    assertTrue(hasChecksum);
    // verify checksum value
    assertEquals(expectedChecksum, checksum);
    MessageMetadata metadata = Commands.parseMessageMetadata(receivedBuf);
    // verify metadata parsing
    assertEquals(metadata.getProducerName(), producerName);

    /** 2. parseMessageMetadata should skip checksum if present **/
    receivedBuf.readerIndex(startMessagePos);
    metadata = Commands.parseMessageMetadata(receivedBuf);
    // verify metadata parsing
    assertEquals(metadata.getProducerName(), producerName);

}

From source file:com.zextras.modules.chat.server.xmpp.netty.XmlSubTagTokenizer.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> objects)
        throws Exception {
    int idx = byteBuf.bytesBefore(sTokenEnd);
    if (idx == -1) {
        return;//w w w .j a va2s  .  c  o  m
    }

    String token = byteBuf.toString(0, idx + 1, mCharset);
    byteBuf.readerIndex(byteBuf.readerIndex() + idx + 1);
    byteBuf.discardReadBytes();
    objects.add(token);
}

From source file:cubicchunks.network.WorldEncoder.java

License:MIT License

public static ByteBuf createByteBufForRead(byte[] data) {
    ByteBuf bytebuf = Unpooled.wrappedBuffer(data);
    bytebuf.readerIndex(0);
    return bytebuf;
}

From source file:de.unipassau.isl.evs.ssh.core.network.handler.SignatureGenerator.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception {
    final int dataLength = msg.readableBytes();
    msg.markReaderIndex();/*from  w w w.j  av a 2s .  c  o  m*/
    out.writeInt(dataLength);
    out.writeBytes(msg);
    msg.resetReaderIndex();

    signSignature.update(msg.nioBuffer());
    msg.readerIndex(msg.writerIndex());

    final byte[] signature = signSignature.sign();
    final int signatureLength = signature.length;
    out.writeInt(signatureLength);
    out.writeBytes(signature);

    //Log.v(TAG, "Signed " + dataLength + "b of data with " + signatureLength + "b signature" +
    //        (Log.isLoggable(TAG, Log.VERBOSE) ? ": " + Arrays.toString(signature) : ""));
}

From source file:divconq.api.internal.UploadStream.java

License:Open Source License

@Override
public ByteBuf getChunk(int length) throws IOException {
    if (this.in == null || length == 0)
        return Unpooled.EMPTY_BUFFER;

    // indicate that we are keeping busy and not hung
    this.ops.touch();

    //System.out.println("Get last activity after touch: " + this.ops.getLastActivity());

    int read = 0;

    ByteBuffer byteBuffer = ByteBuffer.allocate(length);

    while (read < length) {
        int readnow = this.in.read(byteBuffer);

        if (readnow == -1) {
            this.in.close();
            this.in = null;
            break;
        } else {//from   w  w w .  j  a  v a2s. c o  m
            read += readnow;
        }
    }

    if (read == 0)
        return Unpooled.EMPTY_BUFFER;

    byteBuffer.flip();
    ByteBuf buffer = Unpooled.wrappedBuffer(byteBuffer);
    buffer.readerIndex(0);
    buffer.writerIndex(read);
    return buffer;
}

From source file:divconq.bus.net.StreamEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext ctx, StreamMessage msg, ByteBuf out) throws Exception {
    msg.toSerial(out);/*from  w  ww.ja  v a  2s. com*/

    if (msg.hasData()) {
        ByteBuf bb = msg.getData();

        try {
            bb.readerIndex(0);
            out.writeInt(bb.readableBytes());
            out.writeBytes(bb);
        } finally {
            bb.release(); // buffer no longer needed
        }
    } else
        out.writeInt(0);
}

From source file:divconq.http.multipart.AbstractDiskHttpData.java

License:Apache License

@Override
public void setContent(ByteBuf buffer) throws IOException {
    if (buffer == null) {
        throw new NullPointerException("buffer");
    }/*from w  ww  . j av  a 2s  . c  o  m*/
    try {
        size = buffer.readableBytes();
        checkSize(size);
        if (definedSize > 0 && definedSize < size) {
            throw new IOException("Out of size: " + size + " > " + definedSize);
        }
        if (file == null) {
            file = tempFile();
        }
        if (buffer.readableBytes() == 0) {
            // empty file
            if (!file.createNewFile()) {
                throw new IOException("file exists already: " + file);
            }
            return;
        }
        FileOutputStream outputStream = new FileOutputStream(file);
        FileChannel localfileChannel = outputStream.getChannel();
        ByteBuffer byteBuffer = buffer.nioBuffer();
        int written = 0;
        while (written < size) {
            written += localfileChannel.write(byteBuffer);
        }
        buffer.readerIndex(buffer.readerIndex() + written);
        localfileChannel.force(false);
        localfileChannel.close();
        outputStream.close();
        setCompleted();
    } finally {
        // Release the buffer as it was retained before and we not need a reference to it at all
        // See https://github.com/netty/netty/issues/1516
        buffer.release();
    }
}

From source file:divconq.http.multipart.AbstractDiskHttpData.java

License:Apache License

@Override
public void addContent(ByteBuf buffer, boolean last) throws IOException {
    if (buffer != null) {
        try {//from  www.ja v  a  2s .  c om
            int localsize = buffer.readableBytes();
            checkSize(size + localsize);
            if (definedSize > 0 && definedSize < size + localsize) {
                throw new IOException("Out of size: " + (size + localsize) + " > " + definedSize);
            }
            ByteBuffer byteBuffer = buffer.nioBufferCount() == 1 ? buffer.nioBuffer()
                    : buffer.copy().nioBuffer();
            int written = 0;
            if (file == null) {
                file = tempFile();
            }
            if (fileChannel == null) {
                FileOutputStream outputStream = new FileOutputStream(file);
                fileChannel = outputStream.getChannel();
            }
            while (written < localsize) {
                written += fileChannel.write(byteBuffer);
            }
            size += localsize;
            buffer.readerIndex(buffer.readerIndex() + written);
        } finally {
            // Release the buffer as it was retained before and we not need a reference to it at all
            // See https://github.com/netty/netty/issues/1516
            buffer.release();
        }
    }
    if (last) {
        if (file == null) {
            file = tempFile();
        }
        if (fileChannel == null) {
            FileOutputStream outputStream = new FileOutputStream(file);
            fileChannel = outputStream.getChannel();
        }
        fileChannel.force(false);
        fileChannel.close();
        fileChannel = null;
        setCompleted();
    } else {
        if (buffer == null) {
            throw new NullPointerException("buffer");
        }
    }
}

From source file:divconq.http.multipart.AbstractDiskHttpData.java

License:Apache License

@Override
public ByteBuf getChunk(int length) throws IOException {
    if (file == null || length == 0) {
        return EMPTY_BUFFER;
    }/*  w  w  w  .  ja  va  2  s.c om*/
    if (fileChannel == null) {
        FileInputStream inputStream = new FileInputStream(file);
        fileChannel = inputStream.getChannel();
    }
    int read = 0;
    ByteBuffer byteBuffer = ByteBuffer.allocate(length);
    while (read < length) {
        int readnow = fileChannel.read(byteBuffer);
        if (readnow == -1) {
            fileChannel.close();
            fileChannel = null;
            break;
        } else {
            read += readnow;
        }
    }
    if (read == 0) {
        return EMPTY_BUFFER;
    }
    byteBuffer.flip();
    ByteBuf buffer = wrappedBuffer(byteBuffer);
    buffer.readerIndex(0);
    buffer.writerIndex(read);
    return buffer;
}