Example usage for io.netty.buffer ByteBuf setBytes

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

Introduction

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

Prototype

public abstract ByteBuf setBytes(int index, ByteBuffer src);

Source Link

Document

Transfers the specified source buffer's data to this buffer starting at the specified absolute index until the source buffer's position reaches its limit.

Usage

From source file:org.glassfish.jersey.netty.connector.internal.JerseyChunkedInput.java

License:Open Source License

@Override
public ByteBuf readChunk(ByteBufAllocator allocator) throws Exception {

    if (!open) {/*  w ww.  j a va2s .c  o m*/
        return null;
    }

    ByteBuffer top = queue.poll(READ_TIMEOUT, TimeUnit.MILLISECONDS);

    if (top == null) {
        // returning empty buffer instead of null causes flush (which is needed for BroadcasterTest and others..).
        return Unpooled.EMPTY_BUFFER;
    }

    if (top == VOID) {
        open = false;
        return null;
    }

    int topRemaining = top.remaining();
    ByteBuf buffer = allocator.buffer(topRemaining);

    buffer.setBytes(0, top);
    buffer.setIndex(0, topRemaining);

    if (top.remaining() > 0) {
        queue.addFirst(top);
    }

    offset += topRemaining;

    return buffer;
}

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();/*from  ww w. j a v  a2  s .  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.l2junity.loginserver.network.client.crypt.Crypt.java

License:Open Source License

@Override
public void encrypt(ByteBuf buf) {
    // Ensure that byte order is little endian because we set new packet size in first 2 bytes
    if (buf.order() != ByteOrder.LITTLE_ENDIAN) {
        buf = buf.order(ByteOrder.LITTLE_ENDIAN);
    }//from ww w  . ja  v  a2 s .  c o  m

    // Checksum & XOR Key or Checksum only
    buf.writeZero(_static ? 8 : 4);

    // Padding
    buf.writeZero(8 - (buf.readableBytes() % 8));

    if (_static) {
        _static = false;

        int key = Rnd.nextInt();
        buf.skipBytes(4); // The first 4 bytes are ignored
        while (buf.readerIndex() < (buf.writerIndex() - 8)) {
            int data = buf.readInt();
            key += data;
            data ^= key;
            buf.setInt(buf.readerIndex() - 4, data);
        }
        buf.setInt(buf.readerIndex(), key);

        buf.resetReaderIndex();

        final byte[] block = new byte[8];
        while (buf.isReadable(8)) {
            buf.readBytes(block);
            STATIC_BLOWFISH_ENGINE.encryptBlock(block, 0);
            buf.setBytes(buf.readerIndex() - block.length, block);
        }
    } else {
        int checksum = 0;
        while (buf.isReadable(8)) {
            checksum ^= buf.readInt();
        }
        buf.setInt(buf.readerIndex(), checksum);

        buf.resetReaderIndex();

        final byte[] block = new byte[8];
        while (buf.isReadable(8)) {
            buf.readBytes(block);
            _blowfishEngine.encryptBlock(block, 0);
            buf.setBytes(buf.readerIndex() - block.length, block);
        }
    }
}

From source file:org.l2junity.loginserver.network.client.crypt.Crypt.java

License:Open Source License

@Override
public void decrypt(ByteBuf buf) {
    // Packet size must be multiple of 8
    if ((buf.readableBytes() % 8) != 0) {
        buf.clear();//from  www  .  ja v  a2 s .  c  o  m
        return;
    }

    final byte[] block = new byte[8];
    while (buf.isReadable(8)) {
        buf.readBytes(block);
        _blowfishEngine.decryptBlock(block, 0);
        buf.setBytes(buf.readerIndex() - block.length, block);
    }

    // verify checksum also dont forget!
}