Example usage for io.netty.buffer ByteBuf writeBytes

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

Introduction

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

Prototype

public abstract ByteBuf writeBytes(ByteBuffer src);

Source Link

Document

Transfers the specified source buffer's data to this buffer starting at the current writerIndex until the source buffer's position reaches its limit, and increases the writerIndex by the number of the transferred bytes.

Usage

From source file:com.datastax.driver.core.CBUtil.java

License:Apache License

public static void writeBytes(ByteBuffer bytes, ByteBuf cb) {
    cb.writeShort(bytes.remaining());
    cb.writeBytes(bytes.duplicate());
}

From source file:com.datastax.driver.core.CBUtil.java

License:Apache License

public static void writeValue(byte[] bytes, ByteBuf cb) {
    if (bytes == null) {
        cb.writeInt(-1);//from w  w w  .j a va  2  s  .  c  om
        return;
    }

    cb.writeInt(bytes.length);
    cb.writeBytes(bytes);
}

From source file:com.datastax.driver.core.CBUtil.java

License:Apache License

public static void writeValue(ByteBuffer bytes, ByteBuf cb) {
    if (bytes == null) {
        cb.writeInt(-1);//from w w w.  j  av  a  2s.  c  om
        return;
    }

    if (bytes == BoundStatement.UNSET) {
        cb.writeInt(-2);
        return;
    }

    cb.writeInt(bytes.remaining());
    cb.writeBytes(bytes.duplicate());
}

From source file:com.datastax.driver.core.CBUtil.java

License:Apache License

public static void writeInet(InetSocketAddress inet, ByteBuf cb) {
    byte[] address = inet.getAddress().getAddress();

    cb.writeByte(address.length);//from   w  w  w. java  2  s.c  o  m
    cb.writeBytes(address);
    cb.writeInt(inet.getPort());
}

From source file:com.Da_Technomancer.crossroads.API.packets.Message.java

License:Creative Commons License

private static void writeByte2DArray(byte[][] bytes, ByteBuf buf) {
    buf.writeInt(bytes.length);/*ww w .j  a  v a2  s  .  c o m*/
    buf.writeInt(bytes[0].length);
    for (byte[] inner : bytes) {
        buf.writeBytes(inner);
    }
}

From source file:com.dc.gameserver.ServerCore.Controller.AbstractController.AbstractController.java

License:Apache License

/**
 *  4/*from  www.  ja v  a 2 s.co m*/
 *
 * @param ID          ??
 * @param messageLite
 * @return
 */
public static ByteBuf wrappedBufferInt(int ID, MessageLite messageLite) {
    byte[] src = messageLite.toByteArray();
    int length = 8 + src.length;
    ByteBuf buffer = PooledByteBufAllocator.DEFAULT.heapBuffer(length, length);
    buffer.setIndex(0, 0x4);//writeIndex? //4
    buffer.writeByte(ID); // 4
    buffer.writeBytes(messageLite.toByteArray());
    buffer.setInt(0, buffer.writerIndex() - 0x4);
    messageLite = null;
    return buffer;
}

From source file:com.dc.gameserver.ServerCore.Controller.AbstractController.AbstractController.java

License:Apache License

/**
 * ??       </br>/*from w  w w .jav a 2 s. c o  m*/
 * Encoder buffer          </br>
 * ?4    </br>
 * proto buffer     </br>
 *          </br>
 *
 * @param arg1
 * @param arg2
 * @param messageLite
 * @return + ID+protoBufferData
 */
public static ByteBuf wrappedBufferInt(int arg1, int arg2, MessageLite messageLite) {
    byte[] src = messageLite.toByteArray();
    int length = 12 + src.length;
    ByteBuf buffer = PooledByteBufAllocator.DEFAULT.heapBuffer(length, length);
    buffer.setIndex(0, 0x4);//writeIndex? //4
    buffer.writeByte(arg1); // 4
    buffer.writeByte(arg2); // 4
    buffer.writeBytes(messageLite.toByteArray());
    buffer.setInt(0, buffer.writerIndex() - 0x4);
    messageLite = null;
    return buffer;
}

From source file:com.dc.gameserver.ServerCore.Controller.AbstractController.AbstractController.java

License:Apache License

/**
 * ??     </br>//from  www . j  av a 2 s  .c om
 * Encoder buffer       </br>
 * ?2         </br>
 * proto buffer         </br>
 *             </br>
 *
 * @param arg1
 * @param arg2
 * @param messageLite
 * @return + ID+protoBufferData
 */
public static ByteBuf wrappedBufferShort(int arg1, int arg2, MessageLite messageLite) {
    byte[] src = messageLite.toByteArray();
    int length = 10 + src.length;
    ByteBuf buffer = PooledByteBufAllocator.DEFAULT.heapBuffer(length, length);
    buffer.setIndex(0, 0x2);//writeIndex?
    buffer.writeByte(arg1);
    buffer.writeByte(arg2);
    buffer.writeBytes(messageLite.toByteArray());
    /**?2*/
    buffer.setShort(0, buffer.writerIndex() - 0x2);
    messageLite = null;
    return buffer;
}

From source file:com.dc.gameserver.ServerCore.Controller.AbstractController.AbstractController.java

License:Apache License

/**
 * Encoder buffer            </br>
 * 2            </br>//from w w  w . j  a  v a 2s. co m
 *
 * @param ID          ??
 * @param messageLite byte[]
 * @return
 */
public static ByteBuf wrappedBufferShort(int ID, MessageLite messageLite) {
    byte[] src = messageLite.toByteArray();
    int length = 6 + src.length;
    ByteBuf buffer = PooledByteBufAllocator.DEFAULT.heapBuffer(length, length);
    buffer.setIndex(0, 0x2);//writeIndex?     2
    buffer.writeInt(ID); //?                4
    buffer.writeBytes(src);
    buffer.setShort(0, buffer.writerIndex() - 0x2); //  short
    messageLite = null; //set null ,collection by GC
    return buffer;
}

From source file:com.dempe.chat.common.mqtt.codec.ConnAckEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext chc, ConnAckMessage message, ByteBuf out) {
    out.writeByte(AbstractMessage.CONNACK << 4);
    out.writeBytes(Utils.encodeRemainingLength(2));
    out.writeByte(message.isSessionPresent() ? 0x01 : 0x00);
    out.writeByte(message.getReturnCode());
}