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:org.apache.bookkeeper.bookie.Bookie.java

/**
 * Retrieve the ledger descriptor for the ledger which entry should be added to.
 * The LedgerDescriptor returned from this method should be eventually freed with
 * #putHandle().//  w w  w. j a v  a2 s. co m
 *
 * @throws BookieException if masterKey does not match the master key of the ledger
 */
private LedgerDescriptor getLedgerForEntry(ByteBuffer entry, byte[] masterKey)
        throws IOException, BookieException {
    long ledgerId = entry.getLong();
    LedgerDescriptor l = handles.getHandle(ledgerId, masterKey);
    if (!masterKeyCache.containsKey(ledgerId)) {
        // new handle, we should add the key to journal ensure we can rebuild
        ByteBuffer bb = ByteBuffer.allocate(8 + 8 + 4 + masterKey.length);
        bb.putLong(ledgerId);
        bb.putLong(METAENTRY_ID_LEDGER_KEY);
        bb.putInt(masterKey.length);
        bb.put(masterKey);
        bb.flip();

        if (null == masterKeyCache.putIfAbsent(ledgerId, masterKey)) {
            journal.logAddEntry(bb, new NopWriteCallback(), null);
        }
    }
    return l;
}

From source file:org.openhab.binding.ulux.internal.ump.messages.ControlMessage.java

@Override
protected void addData(final ByteBuffer buffer) {
    BigInteger controlFlags = BigInteger.valueOf(0);

    // TODO this.lockMode
    // TODO this.backgroundLight

    if (this.i2cPlugAndPlay) {
        controlFlags = controlFlags.setBit(31);
    }//from  w ww.j  a  v a  2s .  c o  m
    if (this.i2cHumidityChangeRequest) {
        controlFlags = controlFlags.setBit(25);
    }
    if (this.i2cTemperatureChangeRequest) {
        controlFlags = controlFlags.setBit(24);
    }
    if (this.motionSensorChangeRequest) {
        controlFlags = controlFlags.setBit(11);
    }
    if (this.keepAlive) {
        controlFlags = controlFlags.setBit(10);
    }
    if (this.changeFilter) {
        controlFlags = controlFlags.setBit(9);
    }
    if (this.frameAcknowledgement) {
        controlFlags = controlFlags.setBit(8);
    }
    if (this.volumeChangeRequest) {
        controlFlags = controlFlags.setBit(5);
    }
    if (this.pageChangeRequest) {
        controlFlags = controlFlags.setBit(4);
    }
    if (this.audioActiveChangeRequest) {
        controlFlags = controlFlags.setBit(3);
    }
    if (this.displayActiveChangeRequest) {
        controlFlags = controlFlags.setBit(2);
    }
    if (this.proximitySensorChangeRequest) {
        controlFlags = controlFlags.setBit(1);
    }
    if (this.lightSensorChangeRequest) {
        controlFlags = controlFlags.setBit(0);
    }

    buffer.putInt(controlFlags.intValue());
}

From source file:com.healthmarketscience.jackcess.impl.IndexData.java

/**
 * Writes the data page info to the given buffer.
 *///from  ww w.  j a  v  a2  s .  c  om
protected static void writeDataPage(ByteBuffer buffer, DataPage dataPage, int tdefPageNumber, JetFormat format)
        throws IOException {
    buffer.put(dataPage.isLeaf() ? PageTypes.INDEX_LEAF : PageTypes.INDEX_NODE); //Page type
    buffer.put((byte) 0x01); //Unknown
    buffer.putShort((short) 0); //Free space
    buffer.putInt(tdefPageNumber);

    buffer.putInt(0); //Unknown
    buffer.putInt(dataPage.getPrevPageNumber()); //Prev page
    buffer.putInt(dataPage.getNextPageNumber()); //Next page
    buffer.putInt(dataPage.getChildTailPageNumber()); //ChildTail page

    byte[] entryPrefix = dataPage.getEntryPrefix();
    buffer.putShort((short) entryPrefix.length); // entry prefix byte count
    buffer.put((byte) 0); //Unknown

    byte[] entryMask = new byte[format.SIZE_INDEX_ENTRY_MASK];
    // first entry includes the prefix
    int totalSize = entryPrefix.length;
    for (Entry entry : dataPage.getEntries()) {
        totalSize += (entry.size() - entryPrefix.length);
        int idx = totalSize / 8;
        entryMask[idx] |= (1 << (totalSize % 8));
    }
    buffer.put(entryMask);

    // first entry includes the prefix
    buffer.put(entryPrefix);

    for (Entry entry : dataPage.getEntries()) {
        entry.write(buffer, entryPrefix);
    }

    // update free space
    buffer.putShort(2, (short) (format.PAGE_SIZE - buffer.position()));
}

From source file:com.healthmarketscience.jackcess.impl.TableImpl.java

/**
 * Writes the page header for a table definition page
 * @param buffer Buffer to write to/*from ww  w .  ja v a  2 s  .  c om*/
 */
private static void writeTablePageHeader(ByteBuffer buffer) {
    buffer.put(PageTypes.TABLE_DEF); //Page type
    buffer.put((byte) 0x01); //Unknown
    buffer.put((byte) 0); //Unknown
    buffer.put((byte) 0); //Unknown
    buffer.putInt(0); //Next TDEF page pointer
}

From source file:com.healthmarketscience.jackcess.impl.IndexData.java

/**
 * Writes the index definitions into a table definition buffer.
 * @param buffer Buffer to write to/*from ww  w  . j  a v  a2s  .c om*/
 * @param indexes List of IndexBuilders to write definitions for
 */
protected static void writeDefinitions(TableCreator creator, ByteBuffer buffer) throws IOException {
    ByteBuffer rootPageBuffer = creator.getPageChannel().createPageBuffer();
    writeDataPage(rootPageBuffer, NEW_ROOT_DATA_PAGE, creator.getTdefPageNumber(), creator.getFormat());

    for (IndexBuilder idx : creator.getIndexes()) {
        buffer.putInt(MAGIC_INDEX_NUMBER); // seemingly constant magic value

        // write column information (always MAX_COLUMNS entries)
        List<IndexBuilder.Column> idxColumns = idx.getColumns();
        for (int i = 0; i < MAX_COLUMNS; ++i) {

            short columnNumber = COLUMN_UNUSED;
            byte flags = 0;

            if (i < idxColumns.size()) {

                // determine column info
                IndexBuilder.Column idxCol = idxColumns.get(i);
                flags = idxCol.getFlags();

                // find actual table column number
                for (ColumnBuilder col : creator.getColumns()) {
                    if (col.getName().equalsIgnoreCase(idxCol.getName())) {
                        columnNumber = col.getColumnNumber();
                        break;
                    }
                }
                if (columnNumber == COLUMN_UNUSED) {
                    // should never happen as this is validated before
                    throw new IllegalArgumentException("Column with name " + idxCol.getName() + " not found");
                }
            }

            buffer.putShort(columnNumber); // table column number
            buffer.put(flags); // column flags (e.g. ordering)
        }

        TableCreator.IndexState idxState = creator.getIndexState(idx);

        buffer.put(idxState.getUmapRowNumber()); // umap row
        ByteUtil.put3ByteInt(buffer, creator.getUmapPageNumber()); // umap page

        // write empty root index page
        creator.getPageChannel().writePage(rootPageBuffer, idxState.getRootPageNumber());

        buffer.putInt(idxState.getRootPageNumber());
        buffer.putInt(0); // unknown
        buffer.put(idx.getFlags()); // index flags (unique, etc.)
        ByteUtil.forward(buffer, 5); // unknown
    }
}

From source file:com.healthmarketscience.jackcess.impl.TableImpl.java

/**
 * @param buffer Buffer to write to//from  w  ww .  ja  va2s .  c om
 * @param columns List of Columns in the table
 */
private static void writeTableDefinitionHeader(TableCreator creator, ByteBuffer buffer, int totalTableDefSize)
        throws IOException {
    List<ColumnBuilder> columns = creator.getColumns();

    //Start writing the tdef
    writeTablePageHeader(buffer);
    buffer.putInt(totalTableDefSize); //Length of table def
    buffer.putInt(MAGIC_TABLE_NUMBER); // seemingly constant magic value
    buffer.putInt(0); //Number of rows
    buffer.putInt(0); //Last Autonumber
    buffer.put((byte) 1); // this makes autonumbering work in access
    for (int i = 0; i < 15; i++) { //Unknown
        buffer.put((byte) 0);
    }
    buffer.put(TYPE_USER); //Table type
    buffer.putShort((short) columns.size()); //Max columns a row will have
    buffer.putShort(ColumnImpl.countVariableLength(columns)); //Number of variable columns in table
    buffer.putShort((short) columns.size()); //Number of columns in table
    buffer.putInt(creator.getLogicalIndexCount()); //Number of logical indexes in table
    buffer.putInt(creator.getIndexCount()); //Number of indexes in table
    buffer.put((byte) 0); //Usage map row number
    ByteUtil.put3ByteInt(buffer, creator.getUmapPageNumber()); //Usage map page number
    buffer.put((byte) 1); //Free map row number
    ByteUtil.put3ByteInt(buffer, creator.getUmapPageNumber()); //Free map page number
}

From source file:org.apache.bookkeeper.client.BookieWriteLedgerTest.java

/**
 * LedgerHandleAdv out of order writers with ensemble changes.
 * Verify that entry that was written to old ensemble will be
 * written to new enseble too after ensemble change.
 *
 * @throws Exception//from  w  ww  .j av  a 2  s. co  m
 */
@Test
public void testLedgerHandleAdvOutOfOrderWriteAndFrocedEnsembleChange() throws Exception {
    // Create a ledger
    long ledgerId = 0xABCDEF;
    SyncObj syncObj1 = new SyncObj();
    ByteBuffer entry;
    lh = bkc.createLedgerAdv(ledgerId, 3, 3, 3, digestType, ledgerPassword, null);
    entry = ByteBuffer.allocate(4);
    // Add entries 0-4
    for (int i = 0; i < 5; i++) {
        entry.rewind();
        entry.putInt(rng.nextInt(maxInt));
        lh.addEntry(i, entry.array());
    }

    // Add 10 as Async Entry, which goes to first ensemble
    ByteBuffer entry1 = ByteBuffer.allocate(4);
    entry1.putInt(rng.nextInt(maxInt));
    lh.asyncAddEntry(10, entry1.array(), 0, entry1.capacity(), this, syncObj1);

    // Make sure entry-10 goes to the bookies and gets response.
    java.util.Queue<PendingAddOp> myPendingAddOps = Whitebox.getInternalState(lh, "pendingAddOps");
    PendingAddOp addOp = null;
    boolean pendingAddOpReceived = false;

    while (!pendingAddOpReceived) {
        addOp = myPendingAddOps.peek();
        if (addOp.entryId == 10 && addOp.completed) {
            pendingAddOpReceived = true;
        } else {
            Thread.sleep(1000);
        }
    }

    CountDownLatch sleepLatch1 = new CountDownLatch(1);
    List<BookieSocketAddress> ensemble;

    ensemble = lh.getLedgerMetadata().getAllEnsembles().entrySet().iterator().next().getValue();

    // Put all 3 bookies to sleep and start 3 new ones
    sleepBookie(ensemble.get(0), sleepLatch1);
    sleepBookie(ensemble.get(1), sleepLatch1);
    sleepBookie(ensemble.get(2), sleepLatch1);
    startNewBookie();
    startNewBookie();
    startNewBookie();

    // Original bookies are in sleep, new bookies added.
    // Now add entries 5-9 which forces ensemble changes
    // So at this point entries 0-4, 10 went to first
    // ensemble, 5-9 will go to new ensemble.
    for (int i = 5; i < 10; i++) {
        entry.rewind();
        entry.putInt(rng.nextInt(maxInt));
        lh.addEntry(i, entry.array());
    }

    // Wakeup all 3 bookies that went to sleep
    sleepLatch1.countDown();

    // Wait for all entries to be acknowledged for the first ledger
    synchronized (syncObj1) {
        while (syncObj1.counter < 1) {
            syncObj1.wait();
        }
        assertEquals(BKException.Code.OK, syncObj1.rc);
    }

    // Close write handle
    lh.close();

    // Open read handle
    lh = bkc.openLedger(ledgerId, digestType, ledgerPassword);

    // Make sure to read all 10 entries.
    for (int i = 0; i < 11; i++) {
        lh.readEntries(i, i);
    }
    lh.close();
    bkc.deleteLedger(ledgerId);
}

From source file:hivemall.fm.FactorizationMachineUDTF.java

protected void recordTrain(@Nonnull final Feature[] x, final double y) throws HiveException {
    if (_iterations <= 1) {
        return;//from www. ja va2  s .c  o m
    }

    ByteBuffer inputBuf = _inputBuf;
    NioStatefullSegment dst = _fileIO;
    if (inputBuf == null) {
        final File file;
        try {
            file = File.createTempFile("hivemall_fm", ".sgmt");
            file.deleteOnExit();
            if (!file.canWrite()) {
                throw new UDFArgumentException("Cannot write a temporary file: " + file.getAbsolutePath());
            }
            LOG.info("Record training examples to a file: " + file.getAbsolutePath());
        } catch (IOException ioe) {
            throw new UDFArgumentException(ioe);
        } catch (Throwable e) {
            throw new UDFArgumentException(e);
        }

        this._inputBuf = inputBuf = ByteBuffer.allocateDirect(1024 * 1024); // 1 MiB
        this._fileIO = dst = new NioStatefullSegment(file, false);
    }

    int xBytes = Feature.requiredBytes(x);
    int recordBytes = (Integer.SIZE + Double.SIZE) / 8 + xBytes;
    int requiredBytes = (Integer.SIZE / 8) + recordBytes;
    int remain = inputBuf.remaining();
    if (remain < requiredBytes) {
        writeBuffer(inputBuf, dst);
    }

    inputBuf.putInt(recordBytes);
    inputBuf.putInt(x.length);
    for (Feature f : x) {
        f.writeTo(inputBuf);
    }
    inputBuf.putDouble(y);
}

From source file:org.apache.zookeeper.server.quorum.QuorumPeerMainTest.java

/**
 * verify if bad packets are being handled properly
 * at the quorum port/*from  w ww  . j  a v  a2 s.c om*/
 * @throws Exception
 */
@Test
public void testBadPackets() throws Exception {
    ClientBase.setupTestEnv();
    final int CLIENT_PORT_QP1 = PortAssignment.unique();
    final int CLIENT_PORT_QP2 = PortAssignment.unique();
    int electionPort1 = PortAssignment.unique();
    int electionPort2 = PortAssignment.unique();
    String quorumCfgSection = "server.1=127.0.0.1:" + PortAssignment.unique() + ":" + electionPort1 + ";"
            + CLIENT_PORT_QP1 + "\nserver.2=127.0.0.1:" + PortAssignment.unique() + ":" + electionPort2 + ";"
            + CLIENT_PORT_QP2;

    MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection);
    MainThread q2 = new MainThread(2, CLIENT_PORT_QP2, quorumCfgSection);
    q1.start();
    q2.start();

    Assert.assertTrue("waiting for server 1 being up",
            ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1, CONNECTION_TIMEOUT));
    Assert.assertTrue("waiting for server 2 being up",
            ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP2, CONNECTION_TIMEOUT));

    byte[] b = new byte[4];
    int length = 1024 * 1024 * 1024;
    ByteBuffer buff = ByteBuffer.wrap(b);
    buff.putInt(length);
    buff.position(0);
    SocketChannel s = SocketChannel.open(new InetSocketAddress("127.0.0.1", electionPort1));
    s.write(buff);
    s.close();
    buff.position(0);
    s = SocketChannel.open(new InetSocketAddress("127.0.0.1", electionPort2));
    s.write(buff);
    s.close();

    ZooKeeper zk = new ZooKeeper("127.0.0.1:" + CLIENT_PORT_QP1, ClientBase.CONNECTION_TIMEOUT, this);
    waitForOne(zk, States.CONNECTED);
    zk.create("/foo_q1", "foobar1".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    Assert.assertEquals(new String(zk.getData("/foo_q1", null, null)), "foobar1");
    zk.close();
    q1.shutdown();
    q2.shutdown();
}

From source file:hivemall.recommend.SlimUDTF.java

private void recordTrainingInput(final int itemI, @Nonnull final Int2ObjectMap<Int2FloatMap> knnItems,
        final int numKNNItems) throws HiveException {
    ByteBuffer buf = this._inputBuf;
    NioStatefulSegment dst = this._fileIO;

    if (buf == null) {
        // invoke only at task node (initialize is also invoked in compilation)
        final File file;
        try {//from w ww.j a  v a 2s.c  o m
            file = File.createTempFile("hivemall_slim", ".sgmt"); // to save KNN data
            file.deleteOnExit();
            if (!file.canWrite()) {
                throw new UDFArgumentException("Cannot write a temporary file: " + file.getAbsolutePath());
            }
        } catch (IOException ioe) {
            throw new UDFArgumentException(ioe);
        }

        this._inputBuf = buf = ByteBuffer.allocateDirect(8 * 1024 * 1024); // 8MB
        this._fileIO = dst = new NioStatefulSegment(file, false);
    }

    int recordBytes = SizeOf.INT + SizeOf.INT + SizeOf.INT * 2 * knnItems.size()
            + (SizeOf.INT + SizeOf.FLOAT) * numKNNItems;
    int requiredBytes = SizeOf.INT + recordBytes; // need to allocate space for "recordBytes" itself

    int remain = buf.remaining();
    if (remain < requiredBytes) {
        writeBuffer(buf, dst);
    }

    buf.putInt(recordBytes);
    buf.putInt(itemI);
    buf.putInt(knnItems.size());

    for (Int2ObjectMap.Entry<Int2FloatMap> e1 : Fastutil.fastIterable(knnItems)) {
        int user = e1.getIntKey();
        buf.putInt(user);

        Int2FloatMap ru = e1.getValue();
        buf.putInt(ru.size());
        for (Int2FloatMap.Entry e2 : Fastutil.fastIterable(ru)) {
            buf.putInt(e2.getIntKey());
            buf.putFloat(e2.getFloatValue());
        }
    }
}