Example usage for java.nio ByteBuffer putInt

List of usage examples for java.nio ByteBuffer putInt

Introduction

In this page you can find the example usage for java.nio ByteBuffer putInt.

Prototype

public abstract ByteBuffer putInt(int value);

Source Link

Document

Writes the given int to the current position and increases the position by 4.

Usage

From source file:shardableobjectids.ShardableObjectId.java

public byte[] toByteArray() {
    byte b[] = new byte[12];
    ByteBuffer bb = ByteBuffer.wrap(b);
    // by default BB is big endian like we need
    bb.putInt(_machine);
    bb.putInt(_time);/*from  ww w  .  j a  va2  s  . c om*/
    bb.putInt(_inc);
    return b;
}

From source file:org.apache.kylin.storage.hbase.cube.v1.filter.TestFuzzyRowFilterV2EndToEnd.java

@SuppressWarnings("unchecked")
private void runTest(HTable hTable, int expectedSize) throws IOException {
    // [0, 2, ?, ?, ?, ?, 0, 0, 0, 1]
    byte[] fuzzyKey1 = new byte[10];
    ByteBuffer buf = ByteBuffer.wrap(fuzzyKey1);
    buf.clear();//from  w w w .  j  a  va  2 s . c  o  m
    buf.putShort((short) 2);
    for (int i = 0; i < 4; i++)
        buf.put(fuzzyValue);
    buf.putInt((short) 1);
    byte[] mask1 = new byte[] { 0, 0, 1, 1, 1, 1, 0, 0, 0, 0 };

    byte[] fuzzyKey2 = new byte[10];
    buf = ByteBuffer.wrap(fuzzyKey2);
    buf.clear();
    buf.putShort((short) 2);
    buf.putInt((short) 2);
    for (int i = 0; i < 4; i++)
        buf.put(fuzzyValue);

    byte[] mask2 = new byte[] { 0, 0, 0, 0, 0, 0, 1, 1, 1, 1 };

    Pair<byte[], byte[]> pair1 = Pair.newPair(fuzzyKey1, mask1);
    Pair<byte[], byte[]> pair2 = Pair.newPair(fuzzyKey2, mask2);

    FuzzyRowFilterV2 fuzzyRowFilter1 = new FuzzyRowFilterV2(Lists.newArrayList(pair1));
    FuzzyRowFilterV2 fuzzyRowFilter2 = new FuzzyRowFilterV2(Lists.newArrayList(pair2));
    // regular test - we expect 1 row back (5 KVs)
    runScanner(hTable, expectedSize, fuzzyRowFilter1, fuzzyRowFilter2);
}

From source file:org.apache.hadoop.hbase.filter.TestFuzzyRowFilterEndToEnd.java

@SuppressWarnings("unchecked")
private void runTest(Table hTable, int expectedSize) throws IOException {
    // [0, 2, ?, ?, ?, ?, 0, 0, 0, 1]
    byte[] fuzzyKey1 = new byte[10];
    ByteBuffer buf = ByteBuffer.wrap(fuzzyKey1);
    buf.clear();//ww w  .  j  a v a2  s  . c  o  m
    buf.putShort((short) 2);
    for (int i = 0; i < 4; i++)
        buf.put(fuzzyValue);
    buf.putInt((short) 1);
    byte[] mask1 = new byte[] { 0, 0, 1, 1, 1, 1, 0, 0, 0, 0 };

    byte[] fuzzyKey2 = new byte[10];
    buf = ByteBuffer.wrap(fuzzyKey2);
    buf.clear();
    buf.putShort((short) 2);
    buf.putInt((short) 2);
    for (int i = 0; i < 4; i++)
        buf.put(fuzzyValue);

    byte[] mask2 = new byte[] { 0, 0, 0, 0, 0, 0, 1, 1, 1, 1 };

    Pair<byte[], byte[]> pair1 = new Pair<byte[], byte[]>(fuzzyKey1, mask1);
    Pair<byte[], byte[]> pair2 = new Pair<byte[], byte[]>(fuzzyKey2, mask2);

    FuzzyRowFilter fuzzyRowFilter1 = new FuzzyRowFilter(Lists.newArrayList(pair1));
    FuzzyRowFilter fuzzyRowFilter2 = new FuzzyRowFilter(Lists.newArrayList(pair2));
    // regular test - we expect 1 row back (5 KVs)
    runScanner(hTable, expectedSize, fuzzyRowFilter1, fuzzyRowFilter2);
}

From source file:au.org.ala.delta.io.BinaryKeyFile.java

public int writeToRecord(int recordNumber, List<Integer> values) {
    ByteBuffer bytes = ByteBuffer.allocate(values.size() * SIZE_INT_IN_BYTES);
    bytes.order(ByteOrder.LITTLE_ENDIAN);
    for (int value : values) {
        bytes.putInt(value);
    }/*ww  w .  jav  a 2  s .  com*/
    return writeToRecord(recordNumber, 0, bytes.array());
}

From source file:au.org.ala.delta.io.BinaryKeyFile.java

public int writeBooleansToRecord(int recordNumber, List<Boolean> values) {
    ByteBuffer bytes = ByteBuffer.allocate(values.size() * SIZE_INT_IN_BYTES);
    bytes.order(ByteOrder.LITTLE_ENDIAN);
    for (boolean value : values) {
        bytes.putInt(value ? 1 : 0);
    }//from w w  w .j a  v  a 2 s. c om
    return writeToRecord(recordNumber, 0, bytes.array());
}

From source file:au.org.ala.delta.io.BinaryKeyFile.java

/**
 * Designed to allow headers and index records to be overwritten.
 * /*from   w ww .  j  a  va2 s . c  om*/
 * @param recordNumber
 *            the first (of possibily many, depending on the number of
 *            values) record to be overwritten.
 * @param values
 *            the values to write, starting at record, recordNumber..
 */
public void overwriteRecord(int recordNumber, List<Integer> values) {
    if (!_occupiedRecords.contains(recordNumber)) {
        throw new IllegalArgumentException("Record " + recordNumber + " has not been allocated.");
    }
    ByteBuffer bytes = ByteBuffer.allocate(values.size() * SIZE_INT_IN_BYTES);
    bytes.order(ByteOrder.LITTLE_ENDIAN);
    for (int value : values) {
        bytes.putInt(value);
    }
    seekToRecord(recordNumber, 0);
    write(bytes.array());
}

From source file:au.org.ala.delta.io.BinFile.java

public void writeInt(int value) {
    ByteBuffer b = ByteBuffer.allocate(4);
    b.order(ByteOrder.LITTLE_ENDIAN);
    b.putInt(value);
    writeBytes(b);//from  w w w  .ja  v a  2  s .  co  m
}

From source file:org.voltdb.HsqlBackend.java

public VoltTable runDML(String dml) {
    dml = dml.trim();//from   w  w w .j ava 2s  .c  om
    String indicator = dml.substring(0, 1).toLowerCase();
    if (indicator.equals("s") || // "s" is for "select ..."
            indicator.equals("(")) { // "(" is for "(select ... UNION ...)" et. al.
        try {
            Statement stmt = dbconn.createStatement();
            sqlLog.l7dlog(Level.DEBUG, LogKeys.sql_Backend_ExecutingDML.name(), new Object[] { dml }, null);
            sqlLog.debug("Executing " + dml);
            ResultSet rs = stmt.executeQuery(dml);
            ResultSetMetaData rsmd = rs.getMetaData();

            // note the index values here carefully
            VoltTable.ColumnInfo[] columns = new VoltTable.ColumnInfo[rsmd.getColumnCount()];
            for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                String colname = rsmd.getColumnLabel(i);
                String type = rsmd.getColumnTypeName(i);
                //LOG.fine("Column type: " + type);
                if (type.equals("VARCHAR"))
                    columns[i - 1] = new VoltTable.ColumnInfo(colname, VoltType.STRING);
                else if (type.equals("TINYINT"))
                    columns[i - 1] = new VoltTable.ColumnInfo(colname, VoltType.TINYINT);
                else if (type.equals("SMALLINT"))
                    columns[i - 1] = new VoltTable.ColumnInfo(colname, VoltType.SMALLINT);
                else if (type.equals("INTEGER"))
                    columns[i - 1] = new VoltTable.ColumnInfo(colname, VoltType.INTEGER);
                else if (type.equals("BIGINT"))
                    columns[i - 1] = new VoltTable.ColumnInfo(colname, VoltType.BIGINT);
                else if (type.equals("DECIMAL"))
                    columns[i - 1] = new VoltTable.ColumnInfo(colname, VoltType.DECIMAL);
                else if (type.equals("FLOAT"))
                    columns[i - 1] = new VoltTable.ColumnInfo(colname, VoltType.FLOAT);
                else if (type.equals("TIMESTAMP"))
                    columns[i - 1] = new VoltTable.ColumnInfo(colname, VoltType.TIMESTAMP);
                else if (type.equals("VARBINARY"))
                    columns[i - 1] = new VoltTable.ColumnInfo(colname, VoltType.VARBINARY);
                else if (type.equals("CHARACTER"))
                    columns[i - 1] = new VoltTable.ColumnInfo(colname, VoltType.STRING);
                else
                    throw new ExpectedProcedureException(
                            "Trying to create a column in Backend with a (currently) unsupported type: "
                                    + type);
            }
            VoltTable table = new VoltTable(columns);
            while (rs.next()) {
                Object[] row = new Object[table.getColumnCount()];
                for (int i = 0; i < table.getColumnCount(); i++) {
                    if (table.getColumnType(i) == VoltType.STRING)
                        row[i] = rs.getString(i + 1);
                    else if (table.getColumnType(i) == VoltType.TINYINT)
                        row[i] = rs.getByte(i + 1);
                    else if (table.getColumnType(i) == VoltType.SMALLINT)
                        row[i] = rs.getShort(i + 1);
                    else if (table.getColumnType(i) == VoltType.INTEGER)
                        row[i] = rs.getInt(i + 1);
                    else if (table.getColumnType(i) == VoltType.BIGINT)
                        row[i] = rs.getLong(i + 1);
                    else if (table.getColumnType(i) == VoltType.DECIMAL)
                        row[i] = rs.getBigDecimal(i + 1);
                    else if (table.getColumnType(i) == VoltType.FLOAT)
                        row[i] = rs.getDouble(i + 1);
                    else if (table.getColumnType(i) == VoltType.VARBINARY)
                        row[i] = rs.getBytes(i + 1);
                    else if (table.getColumnType(i) == VoltType.TIMESTAMP) {
                        Timestamp t = rs.getTimestamp(i + 1);
                        if (t == null) {
                            row[i] = null;
                        } else {
                            // convert from millisecond to microsecond granularity
                            row[i] = new org.voltdb.types.TimestampType(t.getTime() * 1000);
                        }
                    } else {
                        throw new ExpectedProcedureException(
                                "Trying to read a (currently) unsupported type from a JDBC resultset.");
                    }
                    if (rs.wasNull()) {
                        // JDBC returns 0/0.0 instead of null. Put null into the row.
                        row[i] = null;
                    }
                }

                table.addRow(row);
            }
            stmt.close();
            rs.close();
            return table;
        } catch (Exception e) {
            if (e instanceof ExpectedProcedureException) {
                throw (ExpectedProcedureException) e;
            }
            sqlLog.l7dlog(Level.TRACE, LogKeys.sql_Backend_DmlError.name(), e);
            throw new ExpectedProcedureException("HSQLDB Backend DML Error ", e);
        }
    } else {
        try {
            Statement stmt = dbconn.createStatement();
            sqlLog.debug("Executing: " + dml);
            long ucount = stmt.executeUpdate(dml);
            sqlLog.debug("  result: " + String.valueOf(ucount));
            VoltTable table = new VoltTable(new VoltTable.ColumnInfo("", VoltType.BIGINT));
            table.addRow(ucount);
            return table;
        } catch (SQLException e) {
            // glorious hack to determine if the error is a constraint failure
            if (e.getMessage().contains("constraint")) {
                sqlLog.l7dlog(Level.TRACE, LogKeys.sql_Backend_ConvertingHSQLExtoCFEx.name(), e);
                final byte messageBytes[] = e.getMessage().getBytes();
                ByteBuffer b = ByteBuffer.allocate(25 + messageBytes.length);
                b.putInt(messageBytes.length);
                b.put(messageBytes);
                b.put(e.getSQLState().getBytes());
                b.putInt(0); // ConstraintFailure.type
                try {
                    FastSerializer.writeString("HSQL", b);
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                b.putInt(0);//Table size is 0
                b.rewind();
                throw new ConstraintFailureException(b);
            } else {
                sqlLog.l7dlog(Level.TRACE, LogKeys.sql_Backend_DmlError.name(), e);
                throw new ExpectedProcedureException("HSQLDB Backend DML Error ", e);
            }

        } catch (Exception e) {
            // rethrow an expected exception
            sqlLog.l7dlog(Level.TRACE, LogKeys.sql_Backend_DmlError.name(), e);
            throw new ExpectedProcedureException("HSQLDB Backend DML Error ", e);
        }
    }
}

From source file:edu.cmu.cylab.starslinger.transaction.WebEngine.java

/**
 * check for iOS activation/*  w w  w. j a  va  2  s  . c  om*/
 * 
 * @throws MessageNotFoundException
 */
public byte[] checkStatusAppleUA(String recipientToken) throws ExchangeException, MessageNotFoundException {

    int capacity = mVersionLen //
            + 4 + recipientToken.length() //
            + 4;
    ByteBuffer msg = ByteBuffer.allocate(capacity);
    msg.putInt(mVersion);
    msg.putInt(recipientToken.length());
    msg.put(recipientToken.getBytes());
    msg.putInt(SafeSlingerConfig.NOTIFY_APPLEUA);

    mNotRegistered = false;

    byte[] resp = doPost(mUrlPrefix + mHost + "/checkStatus" + mUrlSuffix, msg.array());

    mNotRegistered = isNotRegisteredErrorCodes(resp);

    resp = handleResponseExceptions(resp, 0);

    return resp;
}

From source file:edu.cmu.cylab.starslinger.transaction.WebEngine.java

/**
 * put a file, its meta-data, and retrieval id on the server
 * //from  www  . j av  a 2  s.c o m
 * @throws MessageNotFoundException
 */
@Deprecated
public byte[] postFileAndroidC2DM(byte[] msgHashBytes, byte[] msgData, byte[] fileData, String recipientToken)
        throws ExchangeException, MessageNotFoundException {
    handleSizeRestictions(fileData);

    int capacity = mVersionLen //
            + 4 + msgHashBytes.length //
            + 4 + recipientToken.length() //
            + 4 + msgData.length //
            + 4 + fileData.length;
    ByteBuffer msg = ByteBuffer.allocate(capacity);
    msg.putInt(mVersion);
    msg.putInt(msgHashBytes.length);
    msg.put(msgHashBytes);
    msg.putInt(recipientToken.length());
    msg.put(recipientToken.getBytes());
    msg.putInt(msgData.length);
    msg.put(msgData);
    msg.putInt(fileData.length);
    msg.put(fileData);

    mNotRegistered = false;

    byte[] resp = doPost(mUrlPrefix + mHost + "/postFile1" + mUrlSuffix, msg.array());

    mNotRegistered = isNotRegisteredErrorCodes(resp);

    resp = handleResponseExceptions(resp, 0);

    return resp;
}