Example usage for io.netty.buffer ByteBuf writeZero

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

Introduction

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

Prototype

public abstract ByteBuf writeZero(int length);

Source Link

Document

Fills this buffer with NUL (0x00) starting at the current writerIndex and increases the writerIndex by the specified length .

Usage

From source file:com.tesora.dve.db.mysql.common.MysqlAPIUtils.java

License:Open Source License

public static void putLengthCodedString(ByteBuf cb, byte[] data, boolean codeNullasZero) {
    if (data != null) {
        // need to handle the case of empty string (NULL and empty string are different)
        // mysql puts (byte)0 in the buffer for empty string
        if (data.length > 0) {
            putLengthCodedLong(cb, data.length);
            cb.writeBytes(data);//w w w.jav  a 2 s  . c  om
        } else
            cb.writeZero(1);
    } else {
        if (!codeNullasZero)
            cb.writeByte(LEN_CODED_NULL); // this indicates the string is NULL
        else
            cb.writeZero(1);
    }
}

From source file:com.tesora.dve.db.mysql.common.MysqlAPIUtils.java

License:Open Source License

public static void putLengthCodedBinary(ByteBuf cb, byte[] data) {
    final int length = data.length;
    if (length > 0) {
        putLengthCodedLong(cb, length);//from  ww w  .  j  av a2s .  c  o m
        cb.writeBytes(data);
    } else {
        cb.writeZero(1);
    }
}

From source file:com.tesora.dve.db.mysql.common.MysqlAPIUtils.java

License:Open Source License

public static void putLengthCodedTime(ByteBuf cb, Time inTime) {
    byte length = 0;
    Calendar cal = null;//w w  w  .  j  a  va2 s.  c o  m
    if (inTime != null) {
        cal = DateUtils.toCalendar(inTime);
        if (cal.get(Calendar.MILLISECOND) > 0) { // indicates we need full 12 byte date
            length = 12;
        } else if (cal.get(Calendar.HOUR_OF_DAY) > 0 || cal.get(Calendar.MINUTE) > 0
                || cal.get(Calendar.SECOND) > 0) { // this is the 8 byte format
            length = 8;
        }
    }
    cb.writeByte(length);
    if (length >= 8) {
        cb.writeZero(5); // this is the sign and days - we are not supporting this
        cb.writeByte(cal.get(Calendar.HOUR_OF_DAY));
        cb.writeByte(cal.get(Calendar.MINUTE));
        cb.writeByte(cal.get(Calendar.SECOND));
    }
    if (length == 12) {
        // 1 millisecond = 1000 microseconds, right?
        int microSeconds = cal.get(Calendar.MILLISECOND) * 1000;
        cb.writeInt(microSeconds);
    }
}

From source file:com.tesora.dve.db.mysql.libmy.BufferedExecute.java

License:Open Source License

public void marshallMessage(ByteBuf stmtExecuteBuf) {
    //header and type are marshalled by parent class.

    int rowsToFlush = this.size();

    MyNullBitmap nullBitmap = new MyNullBitmap(rowsToFlush * columnsPerTuple,
            MyNullBitmap.BitmapType.EXECUTE_REQUEST);

    //            stmtExecuteBuf.writeByte(MSPComStmtExecuteRequestMessage.TYPE_IDENTIFIER);
    stmtExecuteBuf.writeInt(this.stmtID);
    stmtExecuteBuf.writeZero(1); // flags
    stmtExecuteBuf.writeInt(1); // iteration count
    int nullBitmapIndex = stmtExecuteBuf.writerIndex();
    stmtExecuteBuf.writeZero(nullBitmap.length());

    // write the parameter types, as appropriate
    if (this.needsNewParams) {
        stmtExecuteBuf.writeByte(1);//  w ww .j av a  2s  .  c o m

        for (int i = 0; i < rowsToFlush; ++i) {
            stmtExecuteBuf.writeBytes(metadataFragment.slice());
        }

    } else
        stmtExecuteBuf.writeZero(1);

    // Copy the parameter values, updating the null bitmap
    // null bitmap is 1-based
    int rowsWritten = 0;
    int execStmtColIndex = 1;
    for (Iterator<MyBinaryResultRow> i = this.queuedPackets.iterator(); i.hasNext();) {
        MyBinaryResultRow rowPacketData = i.next();

        //                ByteBuf rowSet = Unpooled.buffer(rowPacketData.binRow.sizeInBytes()).order(ByteOrder.LITTLE_ENDIAN);
        //                rowPacketData.binRow.marshallFullMessage(rowSet);

        //                while (rowSet.isReadable() && rowsToWrite-- > 0) {
        //            System.out.println(siteCtx + "/" + myi + ": adding row");
        //                    int payloadLen = rowSet.readMedium();
        //                    rowSet.skipBytes(1);
        //                    byte packetHeader = rowSet.readByte();
        //                    if (packetHeader != 0)
        //                        throw new PEException("Out-of-sync reading redist rowSet");
        int bitmapSize = MyNullBitmap.computeSize(columnsPerTuple, MyNullBitmap.BitmapType.RESULT_ROW);
        int rowFields = rowPacketData.size();
        for (int colIndex = 1; colIndex <= columnsPerTuple; colIndex++, execStmtColIndex++) {
            //we are looping through target columns, which may exceed the source column count.
            if ((colIndex <= rowFields) && rowPacketData.isNull(colIndex - 1 /* zero based indexing */))
                nullBitmap.setBit(execStmtColIndex);
        }
        //                    rowSet.skipBytes(bitmapSize);

        //                    stmtExecuteBuf.writeBytes(rowSet, payloadLen-bitmapSize-1);
        rowPacketData.marshallRawValues(stmtExecuteBuf);
        ++rowsWritten;

    }

    if (rowsWritten != rowsToFlush) {
        //         System.out.println("At failure " + stmtExecuteBuf + "/" + siteCtx);
        throw new PECodingException("Asked to write " + rowsToFlush + " rows, but only " + rowsWritten
                + " were (" + rowsToFlush + " were made available to flushBuffers)");
    }

    // Go back and set the null bitmap and the payload size
    stmtExecuteBuf.setBytes(nullBitmapIndex, nullBitmap.getBitmapArray());
}

From source file:com.tesora.dve.db.mysql.libmy.MyBinaryResultRow.java

License:Open Source License

@Override
public void marshallMessage(ByteBuf cb) {
    cb.writeZero(1);//binary row marker
    byte[] bitmapArray = constructNullMap();
    cb.writeBytes(bitmapArray);//from   w  w w . j  a  va2  s  .c  o  m
    marshallRawValues(cb);
}

From source file:com.tesora.dve.db.mysql.libmy.MyFieldPktResponse.java

License:Open Source License

public void fullPack(ByteBuf cb) {
    MysqlAPIUtils.putLengthCodedString(cb, catalog, true);
    MysqlAPIUtils.putLengthCodedString(cb, database, true);
    MysqlAPIUtils.putLengthCodedString(cb, table, true);
    MysqlAPIUtils.putLengthCodedString(cb, orig_table, true);
    MysqlAPIUtils.putLengthCodedString(cb, column, true);
    MysqlAPIUtils.putLengthCodedString(cb, orig_column, true);
    // The "spec" I used said this next byte was "filler", thru investigation
    // we determined that it is the number of bytes to the end of
    // the packet (not including the default). It seems to be always 12
    cb.writeByte((byte) 12);
    cb.writeByte(charset);//from   w  ww. j a v a 2s . co  m
    cb.writeZero(1);
    cb.writeInt(column_length);
    cb.writeByte(column_type.getByteValue());
    cb.writeShort(flags);
    cb.writeByte(scale);
    cb.writeZero(2);
}

From source file:com.tesora.dve.db.mysql.libmy.MyHandshakeV10.java

License:Open Source License

@Override
public void marshallMessage(ByteBuf cb) {
    cb.writeByte(protocolVersion);//from w w  w.  j  av  a 2 s.  c  o m
    cb.writeBytes(getServerVersion().getBytes());
    cb.writeZero(1);
    cb.writeInt(getThreadID());
    cb.writeBytes(scrambleBuffer1st.getBytes()); // Salt
    cb.writeZero(1);
    cb.writeByte(getServerCapabilities((byte) 0));
    cb.writeByte(getServerCapabilities((byte) 1));
    cb.writeByte(getServerCharsetId());
    cb.writeShort(serverStatus);
    cb.writeByte(getServerCapabilities((byte) 2));
    cb.writeByte(getServerCapabilities((byte) 3));
    cb.writeByte(scrambleBufferSize.byteValue());
    cb.writeZero(10); // write 10 unused bytes
    cb.writeBytes(scrambleBuffer2nd.getBytes()); // Salt
    cb.writeBytes(getPlugInProvidedData().getBytes()); // payload
    cb.writeZero(1);
}

From source file:com.tesora.dve.db.mysql.libmy.MyLoginRequest.java

License:Open Source License

@Override
public void marshallMessage(ByteBuf cb) {
    boolean hasConnectDatabase = false;
    if (database != null) {
        clientCapabilities = clientCapabilities + ClientCapabilities.CLIENT_CONNECT_WITH_DB;
        hasConnectDatabase = true;//  w  w  w  . ja v  a 2s.c o m
    }
    cb.writeInt((int) clientCapabilities);
    cb.writeInt(maxPacketSize);
    cb.writeByte(clientCharset);
    cb.writeZero(23); // filler
    cb.writeBytes(username.getBytes(CharsetUtil.UTF_8));
    cb.writeZero(1); // null terminator for username
    byte[] passwordBytes = password.getBytes(CharsetUtil.ISO_8859_1);
    MysqlAPIUtils.putLengthCodedString(cb, passwordBytes, false);
    if (hasConnectDatabase) {
        cb.writeBytes(database.getBytes(CharsetUtil.UTF_8));
        cb.writeZero(1); // null terminator for database
    }
    if (plugInData != null) {
        cb.writeBytes(plugInData.getBytes(CharsetUtil.UTF_8));
        cb.writeZero(1); // null terminator for plugInData
    }
}

From source file:com.tesora.dve.db.mysql.libmy.MyPrepareOKResponse.java

License:Open Source License

@Override
public void marshallMessage(ByteBuf cb) {
    cb.writeZero(1); // status
    cb.writeInt((int) stmtId);
    cb.writeShort(numColumns);//from   w  w  w.j a v  a 2 s.c o m
    cb.writeShort(numParams);
    cb.writeZero(1); // filler
    cb.writeShort(warningCount);
}

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.  j  a v  a2s. co 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);
}