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.spongepowered.clean.network.netty.PacketDecryptor.java

License:MIT License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
    int length = msg.readableBytes();
    if (this.dataBuffer.length < length) {
        this.dataBuffer = new byte[length];
    }//w w w  .j a va  2s  . c o  m
    msg.readBytes(this.dataBuffer, 0, length);
    ByteBuf plain = ctx.alloc().heapBuffer(this.cipher.getOutputSize(length));
    int actual = this.cipher.update(this.dataBuffer, 0, length, plain.array(), plain.arrayOffset());
    plain.writerIndex(actual);
    out.add(plain);
}

From source file:org.starnub.starbounddata.packets.Packet.java

License:Open Source License

/**
 * Recommended: For internal use with StarNub packet decoding
 * <p>/* ww  w.ja  v  a  2  s. c om*/
 * Uses: This will write a s{@link org.starnub.starbounddata.types.variants.VLQ} to a {@link io.netty.buffer.ByteBuf}
 * <p>
 * Notes: This will not create a VLQ object and should be used
 * <p>
 *
 * @param out   ByteBuf in which is to be read
 * @param value long representing the VLQ value to be written out
 */
protected static void writeSVLQPacketEncoder(ByteBuf out, long value) {
    if (value < 0) {
        value = ((-(value + 1)) << 1) | 1;
    } else {
        value = value << 1;
    }
    int numRelevantBits = 64 - Long.numberOfLeadingZeros(value);
    int numBytes = (numRelevantBits + 6) / 7;
    if (numBytes == 0) {
        numBytes = 1;
    }
    out.writerIndex(numBytes + 1); /* Sets the write index at the number of bytes + 1 byte for packet id */
    for (int i = numBytes - 1; i >= 0; i--) {
        int curByte = (int) (value & 0x7F);
        if (i != (numBytes - 1)) {
            curByte |= 0x80;
        }
        out.setByte(i + 1, curByte); /* Sets the byte at index + 1 byte for packet id */
        value >>>= 7;
    }
}

From source file:org.traccar.helper.BufferUtil.java

License:Apache License

public static int indexOf(String needle, ByteBuf haystack, int startIndex, int endIndex) {
    ByteBuf wrappedHaystack = Unpooled.wrappedBuffer(haystack);
    wrappedHaystack.readerIndex(startIndex - haystack.readerIndex());
    wrappedHaystack.writerIndex(endIndex - haystack.readerIndex());
    int result = indexOf(needle, wrappedHaystack);
    return result < 0 ? result : haystack.readerIndex() + result;
}

From source file:org.traccar.protocol.At2000FrameDecoder.java

License:Apache License

private void sendResponse(Channel channel) {
    if (channel != null) {
        ByteBuf response = Unpooled.buffer(2 * BLOCK_LENGTH);
        response.writeByte(At2000ProtocolDecoder.MSG_ACKNOWLEDGEMENT);
        response.writeMedium(1);/* w  w  w .j  av a  2s  . c o  m*/
        response.writeByte(0x00); // success
        response.writerIndex(2 * BLOCK_LENGTH);
        channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress()));
    }
}

From source file:org.traccar.protocol.At2000ProtocolDecoder.java

License:Apache License

private static void sendRequest(Channel channel) {
    if (channel != null) {
        ByteBuf response = Unpooled.buffer(BLOCK_LENGTH);
        response.writeByte(MSG_TRACK_REQUEST);
        response.writeMedium(0);/*  w w  w  .j a v a  2  s .  c  o  m*/
        response.writerIndex(BLOCK_LENGTH);
        channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress()));
    }
}