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.antsdb.saltedfish.server.mysql.PacketEncoder.java

License:Open Source License

/**
 * /*from   w w  w .j a  v  a  2s.c  om*/
 * From server to client. One packet for each row in the result set.
 * 
 * <pre>
 * Bytes                   Name
 * -----                   ----
 * n (Length Coded String) (column value)
 * ...
 * 
 * (column value):         The data in the column, as a character string.
 *                         If a column is defined as non-character, the
 *                         server converts the value into a character
 *                         before sending it. Since the value is a Length
 *                         Coded String, a NULL can be represented with a
 *                         single byte containing 251(see the description
 *                         of Length Coded Strings in section "Elements" above).
 * 
 * @see http://forge.mysql.com/wiki/MySQL_Internals_ClientServer_Protocol#Row_Data_Packet
 * </pre>
 * 
 * @param buffer
 * @param fieldValues
 */
public void writeRowBinaryBody(ByteBuf buffer, long pRecord, CursorMeta meta, int nColumns) {
    if ((pRecord != 0) && (nColumns > 0)) {
        // start of package
        buffer.writeByte(0);

        int nullByteCnt = (nColumns + 7 + 2) / 8;

        byte[] nullBitmap = new byte[nullByteCnt];
        int nullPos = buffer.writerIndex();

        buffer.writeBytes(nullBitmap);

        for (int i = 0; i < nColumns; i++) {
            long pValue = Record.getValueAddress(pRecord, i);
            if (pValue != 0) {
                writeValue(buffer, meta.getColumn(i), pValue);
            } else {
                nullBitmap[(i + 2) / 8] |= 1 << (i + 2) % 8;
            }
        }

        int endPos = buffer.writerIndex();

        buffer.writerIndex(nullPos);
        buffer.writeBytes(nullBitmap);

        buffer.writerIndex(endPos);
    }
}

From source file:com.antsdb.saltedfish.server.mysql.PacketEncoder.java

License:Open Source License

/**
 * //from  w  ww .jav a2s.  c o  m
 * From server to client during initial handshake.
 * 
 * <pre>
 * Bytes                        Name
 * -----                        ----
 * 1                            protocol_version
 * n (Null-Terminated String)   server_version
 * 4                            thread_id
 * 8                            scramble_buff
 * 1                            (filler) always 0x00
 * 2                            server_capabilities
 * 1                            server_language
 * 2                            server_status
 * 13                           (filler) always 0x00 ...
 * 13                           rest of scramble_buff (4.1)
 * 
 * @see http://forge.mysql.com/wiki/MySQL_Internals_ClientServer_Protocol#Handshake_Initialization_Packet
 * </pre>
 * 
 * @param buffer
 * @param serverVersion
 * @param protocolVersion
 * @param threadId
 * @param capability
 * @param charSet
 * @param status
 */
public static void writeHandshakeBody(ByteBuf buffer, String serverVersion, byte protocolVersion, long threadId,
        int capability, byte charSet, int status) {
    buffer.writeByte(protocolVersion);
    BufferUtils.writeString(buffer, serverVersion);
    BufferUtils.writeUB4(buffer, threadId);
    // seed
    byte[] seed = new byte[] { 0x50, 0x3a, 0x6e, 0x3d, 0x25, 0x40, 0x51, 0x56 };
    buffer.writeBytes(seed);
    buffer.writeByte(0);
    // lower 16 bits of sever capacity
    BufferUtils.writeInt(buffer, capability);
    // serverCharsetIndex
    buffer.writeByte(charSet);
    // server status 
    BufferUtils.writeInt(buffer, status);
    // upper 16 bits of server capacity
    BufferUtils.writeInt(buffer, capability >>> 16);
    // plugin data length
    if ((capability & MysqlServerHandler.CLIENT_PLUGIN_AUTH) != 0) {
        buffer.writeByte(0x15);
    } else {
        buffer.writeByte(0);
    }
    // fill the rest 10 bytes with 0
    buffer.writeZero(10);
    if ((capability & MysqlServerHandler.CLIENT_PLUGIN_AUTH) != 0) {
        // no idea what this means, copied from trace
        buffer.writeBytes(
                new byte[] { 0x73, 0x68, 0x2f, 0x50, 0x27, 0x6f, 0x7a, 0x38, 0x46, 0x38, 0x26, 0x51, 0x00 });
        BufferUtils.writeString(buffer, MysqlServerHandler.AUTH_MYSQL_NATIVE);
    }
}

From source file:com.antsdb.saltedfish.server.mysql.PacketEncoder.java

License:Open Source License

/**
 * /*from w w w.j  av  a2s .  c  om*/
 * From server to client in response to command, if error.
 * 
 * <pre>
 * Bytes                       Name
 * -----                       ----
 * 1                           field_count, always = 0xff
 * 2                           errno
 * 1                           (sqlstate marker), always '#'
 * 5                           sqlstate (5 characters)
 * n                           message
 * 
 * @see http://forge.mysql.com/wiki/MySQL_Internals_ClientServer_Protocol#Error_Packet
 * </pre>
 * 
 * @param buffer
 * @param errno
 * @param message
 */
public void writeErrorBody(ByteBuf buffer, int errno, ByteBuffer message) {
    // field count
    buffer.writeByte((byte) 0xff);
    // error number
    BufferUtils.writeUB2(buffer, errno);
    // sql state mark
    buffer.writeByte((byte) '#');
    // sql state
    buffer.writeBytes("HY000".getBytes());
    if (message != null) {
        buffer.writeBytes(message);
    }
}

From source file:com.antsdb.saltedfish.server.mysql.PacketEncoder.java

License:Open Source License

/**
 * /*w  ww.  j av  a2  s  .  c  om*/
 * Registers a slave at the master. Should be sent before requesting a binlog events with COM_BINLOG_DUMP.
 * 
 * <pre>
 * Bytes                        Name
 * -----                        ----
 *   1              [15] COM_REGISTER_SLAVE
 *   4              server-id
 *   1              slaves hostname length
 *   string[$len]   slaves hostname
 *   1              slaves user len
 *   string[$len]   slaves user
 *   1              slaves password len
 *   string[$len]   slaves password
 *   2              slaves mysql-port
 *   4              replication rank
 *   4              master-id
 * 
 * @see https://dev.mysql.com/doc/internals/en/com-register-slave.html
 * </pre>
 * 
 */
public void writeRegisterSlave(ByteBuf buffer, int serverId) {
    // code for COM_REGISTER_SLAVE is 0x15 
    buffer.writeByte(0x15);
    BufferUtils.writeUB4(buffer, serverId);
    // usually empty
    BufferUtils.writeLenString(buffer, "", Charsets.UTF_8);
    // usually empty
    BufferUtils.writeLenString(buffer, "", Charsets.UTF_8);
    // usually empty
    BufferUtils.writeLenString(buffer, "", Charsets.UTF_8);
    // usually empty
    BufferUtils.writeUB2(buffer, 0);
    // replication rank to be ignored
    buffer.writeBytes(new byte[4]);
    // master id, usually 0
    BufferUtils.writeUB4(buffer, 0);
}

From source file:com.antsdb.saltedfish.server.mysql.util.BufferUtils.java

License:Open Source License

public static void writeLenString(ByteBuf buf, String s, Charset encoder) {
    if (s == null) {
        writeFieldLength(buf, 0);/*from  www  .  j  a  v  a2  s .c o  m*/
        return;
    }
    ByteBuffer bb = encoder.encode(s);
    writeFieldLength(buf, bb.remaining());
    buf.writeBytes(bb);
}

From source file:com.antsdb.saltedfish.server.mysql.util.BufferUtils.java

License:Open Source License

public static final void writeWithLength(ByteBuf buffer, byte[] src) {
    if (src == null || src.length == 0) {
        buffer.writeByte((byte) 0);
    } else {/*from w  w  w  . j  a va2 s .  co m*/
        int length = src == null ? 0 : src.length;
        if (length < 251) {
            buffer.writeByte((byte) length);
        } else if (length < 0x10000L) {
            buffer.writeByte((byte) 252);
            writeUB2(buffer, length);
        } else if (length < 0x1000000L) {
            buffer.writeByte((byte) 253);
            writeUB3(buffer, length);
        } else {
            buffer.writeByte((byte) 254);
            writeLong(buffer, length);
        }
        buffer.writeBytes(src);
    }
}

From source file:com.antsdb.saltedfish.server.mysql.util.BufferUtils.java

License:Open Source License

public static final void writeWithNull(ByteBuf buffer, byte[] src) {
    if (src != null) {
        buffer.writeBytes(src);
    }//ww w  .jav  a 2  s.  c  o  m
    buffer.writeByte((byte) 0);
}

From source file:com.antsdb.saltedfish.server.mysql.util.BufferUtils.java

License:Open Source License

private static void writeStringNoNull(ByteBuf buf, String s) {
    Charset cs = Charset.defaultCharset();
    buf.writeBytes(cs.encode(s));
}

From source file:com.barchart.http.server.HttpRequestChannelHandler.java

License:BSD License

private void sendServerError(final ChannelHandlerContext ctx, final ServerException cause) throws Exception {

    if (ctx.channel().isActive()) {

        final ByteBuf content = Unpooled.buffer();

        content.writeBytes(
                (cause.getStatus().code() + " " + cause.getStatus().reasonPhrase() + " - " + cause.getMessage())
                        .getBytes());/*from   w  w w.  j a  v a2s  .  co  m*/

        final FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, cause.getStatus());

        response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, content.readableBytes());

        response.content().writeBytes(content);

        ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);

    }

}

From source file:com.basho.riak.client.core.netty.RiakMessageCodec.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, RiakMessage msg, ByteBuf out) throws Exception {
    int length = msg.getData().length + 1;
    out.writeInt(length);//from ww w .j a v a2s  . co  m
    out.writeByte(msg.getCode());
    out.writeBytes(msg.getData());
}