Example usage for io.netty.buffer ByteBuf setMedium

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

Introduction

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

Prototype

public abstract ByteBuf setMedium(int index, int value);

Source Link

Document

Sets the specified 24-bit medium integer at the specified absolute index in this buffer.

Usage

From source file:com.tesora.dve.db.mysql.portal.protocol.MSPAuthenticateV10MessageMessage.java

License:Open Source License

public static void write(ByteBuf out, String userName, String userPassword, String salt, Charset charset,
        int mysqlCharsetID, int capabilitiesFlag) {
    ByteBuf leBuf = out.order(ByteOrder.LITTLE_ENDIAN);

    int payloadSizeIndex = leBuf.writerIndex();
    leBuf.writeMedium(0);//from   w w  w  . ja v  a 2s  .c  o  m
    leBuf.writeByte(1);
    int payloadStartIndex = leBuf.writerIndex();
    leBuf.writeInt(capabilitiesFlag);
    leBuf.writeInt(MAX_PACKET_SIZE);
    //      leBuf.writeByte(serverGreeting.getServerCharsetId());
    leBuf.writeByte(mysqlCharsetID);
    leBuf.writeZero(23);
    leBuf.writeBytes(userName.getBytes(charset));
    leBuf.writeZero(1);

    if ((capabilitiesFlag & ClientCapabilities.CLIENT_SECURE_CONNECTION) > 0) {

        byte[] securePassword = computeSecurePassword(userPassword, salt);
        leBuf.writeByte(securePassword.length);
        leBuf.writeBytes(securePassword);
    } else {
        leBuf.writeBytes(userPassword.getBytes(charset));
        leBuf.writeZero(1);
    }

    leBuf.setMedium(payloadSizeIndex, leBuf.writerIndex() - payloadStartIndex);
}

From source file:com.tesora.dve.db.mysql.portal.protocol.MSPServerGreetingRequestMessage.java

License:Open Source License

public static void write(ChannelHandlerContext ctx, int connectionId, String salt, int serverCapabilities,
        String serverVersion, byte serverCharSet, String pluginData) {
    ByteBuf out = ctx.channel().alloc().heapBuffer(50).order(ByteOrder.LITTLE_ENDIAN);

    String scrambleBuffer1st = salt.substring(0, 8);
    String scrambleBuffer2nd = salt.substring(8) + '\0';
    Integer scrambleBufferSize = scrambleBuffer1st.length() + scrambleBuffer2nd.length();

    ByteBuf serverCapabilitiesBuf = ctx.channel().alloc().heapBuffer(4).order(ByteOrder.LITTLE_ENDIAN);
    try {//from w  ww  .ja va 2 s  .c  o m
        serverCapabilitiesBuf.writeInt(serverCapabilities);
        out.writeMedium(0);
        out.writeByte(0);
        out.writeByte(MYSQL_PROTOCOL_VERSION);

        out.writeBytes(serverVersion.getBytes());
        out.writeZero(1);
        out.writeInt(connectionId);
        out.writeBytes(scrambleBuffer1st.getBytes()); // Salt
        out.writeZero(1);
        out.writeByte(serverCapabilitiesBuf.getByte(0));
        out.writeByte(serverCapabilitiesBuf.getByte(1));

        out.writeByte(serverCharSet);
        out.writeShort(MyProtocolDefs.SERVER_STATUS_AUTOCOMMIT);
        out.writeByte(serverCapabilitiesBuf.getByte(2));
        out.writeByte(serverCapabilitiesBuf.getByte(3));
        out.writeByte(scrambleBufferSize.byteValue());
        out.writeZero(10); // write 10 unused bytes
        out.writeBytes(scrambleBuffer2nd.getBytes()); // Salt

        out.writeBytes(pluginData.getBytes()); // payload
        out.writeZero(1);

        out.setMedium(0, out.writerIndex() - 4);

        ctx.channel().writeAndFlush(out);
    } finally {
        serverCapabilitiesBuf.release();
    }
}