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 index, int value);

Source Link

Document

Writes the given int to the specified index of this buffer.

Usage

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

/**
 * Updates the table definition after rows are modified.
 *///from  w  w  w  .java  2  s  . c  o  m
private void updateTableDefinition(int rowCountInc) throws IOException {
    // load table definition
    ByteBuffer tdefPage = _tableDefBufferH.setPage(getPageChannel(), _tableDefPageNumber);

    // make sure rowcount and autonumber are up-to-date
    _rowCount += rowCountInc;
    tdefPage.putInt(getFormat().OFFSET_NUM_ROWS, _rowCount);
    tdefPage.putInt(getFormat().OFFSET_NEXT_AUTO_NUMBER, _lastLongAutoNumber);
    int ctypeOff = getFormat().OFFSET_NEXT_COMPLEX_AUTO_NUMBER;
    if (ctypeOff >= 0) {
        tdefPage.putInt(ctypeOff, _lastComplexTypeAutoNumber);
    }

    // write any index changes
    for (IndexData indexData : _indexDatas) {
        // write the unique entry count for the index to the table definition
        // page
        tdefPage.putInt(indexData.getUniqueEntryCountOffset(), indexData.getUniqueEntryCount());
        // write the entry page for the index
        indexData.update();
    }

    // write modified table definition
    getPageChannel().writePage(tdefPage, _tableDefPageNumber);
}

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

/**
 * Writes a new table defined by the given TableCreator to the database.
 * @usage _advanced_method_//from   w  ww  .ja v a 2s .  com
 */
protected static void writeTableDefinition(TableCreator creator) throws IOException {
    // first, create the usage map page
    createUsageMapDefinitionBuffer(creator);

    // next, determine how big the table def will be (in case it will be more
    // than one page)
    JetFormat format = creator.getFormat();
    int idxDataLen = (creator.getIndexCount() * (format.SIZE_INDEX_DEFINITION + format.SIZE_INDEX_COLUMN_BLOCK))
            + (creator.getLogicalIndexCount() * format.SIZE_INDEX_INFO_BLOCK);
    int colUmapLen = creator.getLongValueColumns().size() * 10;
    int totalTableDefSize = format.SIZE_TDEF_HEADER
            + (format.SIZE_COLUMN_DEF_BLOCK * creator.getColumns().size()) + idxDataLen + colUmapLen
            + format.SIZE_TDEF_TRAILER;

    // total up the amount of space used by the column and index names (2
    // bytes per char + 2 bytes for the length)
    for (ColumnBuilder col : creator.getColumns()) {
        int nameByteLen = (col.getName().length() * JetFormat.TEXT_FIELD_UNIT_SIZE);
        totalTableDefSize += nameByteLen + 2;
    }

    for (IndexBuilder idx : creator.getIndexes()) {
        int nameByteLen = (idx.getName().length() * JetFormat.TEXT_FIELD_UNIT_SIZE);
        totalTableDefSize += nameByteLen + 2;
    }

    // now, create the table definition
    PageChannel pageChannel = creator.getPageChannel();
    ByteBuffer buffer = PageChannel.createBuffer(Math.max(totalTableDefSize, format.PAGE_SIZE));
    writeTableDefinitionHeader(creator, buffer, totalTableDefSize);

    if (creator.hasIndexes()) {
        // index row counts
        IndexData.writeRowCountDefinitions(creator, buffer);
    }

    // column definitions
    ColumnImpl.writeDefinitions(creator, buffer);

    if (creator.hasIndexes()) {
        // index and index data definitions
        IndexData.writeDefinitions(creator, buffer);
        IndexImpl.writeDefinitions(creator, buffer);
    }

    // write long value column usage map references
    for (ColumnBuilder lvalCol : creator.getLongValueColumns()) {
        buffer.putShort(lvalCol.getColumnNumber());
        TableCreator.ColumnState colState = creator.getColumnState(lvalCol);

        // owned pages umap (both are on same page)
        buffer.put(colState.getUmapOwnedRowNumber());
        ByteUtil.put3ByteInt(buffer, colState.getUmapPageNumber());
        // free space pages umap
        buffer.put(colState.getUmapFreeRowNumber());
        ByteUtil.put3ByteInt(buffer, colState.getUmapPageNumber());
    }

    //End of tabledef
    buffer.put((byte) 0xff);
    buffer.put((byte) 0xff);

    // write table buffer to database
    if (totalTableDefSize <= format.PAGE_SIZE) {

        // easy case, fits on one page
        buffer.putShort(format.OFFSET_FREE_SPACE, (short) (buffer.remaining() - 8)); // overwrite page free space
        // Write the tdef page to disk.
        pageChannel.writePage(buffer, creator.getTdefPageNumber());

    } else {

        // need to split across multiple pages
        ByteBuffer partialTdef = pageChannel.createPageBuffer();
        buffer.rewind();
        int nextTdefPageNumber = PageChannel.INVALID_PAGE_NUMBER;
        while (buffer.hasRemaining()) {

            // reset for next write
            partialTdef.clear();

            if (nextTdefPageNumber == PageChannel.INVALID_PAGE_NUMBER) {

                // this is the first page.  note, the first page already has the
                // page header, so no need to write it here
                nextTdefPageNumber = creator.getTdefPageNumber();

            } else {

                // write page header
                writeTablePageHeader(partialTdef);
            }

            // copy the next page of tdef bytes
            int curTdefPageNumber = nextTdefPageNumber;
            int writeLen = Math.min(partialTdef.remaining(), buffer.remaining());
            partialTdef.put(buffer.array(), buffer.position(), writeLen);
            ByteUtil.forward(buffer, writeLen);

            if (buffer.hasRemaining()) {
                // need a next page
                nextTdefPageNumber = pageChannel.allocateNewPage();
                partialTdef.putInt(format.OFFSET_NEXT_TABLE_DEF_PAGE, nextTdefPageNumber);
            }

            // update page free space
            partialTdef.putShort(format.OFFSET_FREE_SPACE, (short) (partialTdef.remaining() - 8)); // overwrite page free space

            // write partial page to disk
            pageChannel.writePage(partialTdef, curTdefPageNumber);
        }

    }
}

From source file:com.inclouds.hbase.rowcache.RowCache.java

/**
 * CHECKED 2/*from  w  w  w  .  j  av a2 s. c  om*/
 * 
 * Sets the family column. Format:
 * 
 * // List of columns: // 4 bytes - total columns // Column: // 4 bytes -
 * qualifier length // qualifier // 4 bytes - total versions // list of
 * versions: // { 8 bytes - timestamp // 4 bytes - value length // value // }
 * 
 * @param tableName
 *          the table name
 * @param family
 *          the family
 * @param bundle
 *          the bundle
 * @throws IOException
 *           Signals that an I/O exception has occurred.
 */
private void upsertFamilyColumns(byte[] tableName, byte[] row, byte[] family, List<KeyValue> bundle)
        throws IOException {
    if (bundle.size() == 0)
        return;
    // Get first
    ByteBuffer buf = getLocalByteBuffer();// bufTLS.get();

    try {
        prepareKeyForPut(buf, tableName, row, 0, row.length, family, null);

        // buffer position is at beginning of a value block
        int valueSize = Bytes.SIZEOF_INT;
        int numColumns = 0;
        int startPosition = buf.position();
        // Skip numColumns
        skip(buf, Bytes.SIZEOF_INT);
        while (bundle.size() > 0) {
            valueSize += addColumn(buf, bundle);
            numColumns++;
        }
        buf.putInt(startPosition, numColumns);
        // Update value len
        buf.putInt(4, valueSize);
        // Now we have K-V pair in a buffer - put it into cache
    } catch (BufferOverflowException e) {
        LOG.error("[row-cache] Ignore put op. The row:family is too large and exceeds the limit " + ioBufferSize
                + " bytes.");
        buf.clear();
        return;
    }
    doPut(buf);

}

From source file:edu.mbl.jif.imaging.mmtiff.MultipageTiffWriter.java

private void writeIFD(TaggedImage img) throws IOException {
    char numEntries = (char) ((firstIFD_ ? ENTRIES_PER_IFD + 4 : ENTRIES_PER_IFD));
    if (img.tags.has("Summary")) {
        img.tags.remove("Summary");
    }// w ww  .ja  v  a 2 s.c o  m
    String mdString = img.tags.toString() + " ";

    //2 bytes for number of directory entries, 12 bytes per directory entry, 4 byte offset of next IFD
    //6 bytes for bits per sample if RGB, 16 bytes for x and y resolution, 1 byte per character of MD string
    //number of bytes for pixels
    int totalBytes = 2 + numEntries * 12 + 4 + (rgb_ ? 6 : 0) + 16 + mdString.length() + bytesPerImagePixels_;
    int IFDandBitDepthBytes = 2 + numEntries * 12 + 4 + (rgb_ ? 6 : 0);

    ByteBuffer ifdBuffer = ByteBuffer.allocate(IFDandBitDepthBytes).order(BYTE_ORDER);
    CharBuffer charView = ifdBuffer.asCharBuffer();

    long tagDataOffset = filePosition_ + 2 + numEntries * 12 + 4;
    nextIFDOffsetLocation_ = filePosition_ + 2 + numEntries * 12;

    bufferPosition_ = 0;
    charView.put(bufferPosition_, numEntries);
    bufferPosition_ += 2;
    writeIFDEntry(ifdBuffer, charView, WIDTH, (char) 4, 1, imageWidth_);
    writeIFDEntry(ifdBuffer, charView, HEIGHT, (char) 4, 1, imageHeight_);
    writeIFDEntry(ifdBuffer, charView, BITS_PER_SAMPLE, (char) 3, rgb_ ? 3 : 1,
            rgb_ ? tagDataOffset : byteDepth_ * 8);
    if (rgb_) {
        tagDataOffset += 6;
    }
    writeIFDEntry(ifdBuffer, charView, COMPRESSION, (char) 3, 1, 1);
    writeIFDEntry(ifdBuffer, charView, PHOTOMETRIC_INTERPRETATION, (char) 3, 1, rgb_ ? 2 : 1);

    if (firstIFD_) {
        omeDescriptionTagPosition_ = filePosition_ + bufferPosition_;
        writeIFDEntry(ifdBuffer, charView, IMAGE_DESCRIPTION, (char) 2, 0, 0);
        ijDescriptionTagPosition_ = filePosition_ + bufferPosition_;
        writeIFDEntry(ifdBuffer, charView, IMAGE_DESCRIPTION, (char) 2, 0, 0);
    }

    writeIFDEntry(ifdBuffer, charView, STRIP_OFFSETS, (char) 4, 1, tagDataOffset);
    tagDataOffset += bytesPerImagePixels_;
    writeIFDEntry(ifdBuffer, charView, SAMPLES_PER_PIXEL, (char) 3, 1, (rgb_ ? 3 : 1));
    writeIFDEntry(ifdBuffer, charView, ROWS_PER_STRIP, (char) 3, 1, imageHeight_);
    writeIFDEntry(ifdBuffer, charView, STRIP_BYTE_COUNTS, (char) 4, 1, bytesPerImagePixels_);
    writeIFDEntry(ifdBuffer, charView, X_RESOLUTION, (char) 5, 1, tagDataOffset);
    tagDataOffset += 8;
    writeIFDEntry(ifdBuffer, charView, Y_RESOLUTION, (char) 5, 1, tagDataOffset);
    tagDataOffset += 8;
    writeIFDEntry(ifdBuffer, charView, RESOLUTION_UNIT, (char) 3, 1, 3);
    if (firstIFD_) {
        ijMetadataCountsTagPosition_ = filePosition_ + bufferPosition_;
        writeIFDEntry(ifdBuffer, charView, IJ_METADATA_BYTE_COUNTS, (char) 4, 0, 0);
        ijMetadataTagPosition_ = filePosition_ + bufferPosition_;
        writeIFDEntry(ifdBuffer, charView, IJ_METADATA, (char) 1, 0, 0);
    }
    writeIFDEntry(ifdBuffer, charView, MM_METADATA, (char) 2, mdString.length(), tagDataOffset);
    tagDataOffset += mdString.length();
    //NextIFDOffset
    ifdBuffer.putInt(bufferPosition_, (int) tagDataOffset);
    bufferPosition_ += 4;

    if (rgb_) {
        charView.put(bufferPosition_ / 2, (char) (byteDepth_ * 8));
        charView.put(bufferPosition_ / 2 + 1, (char) (byteDepth_ * 8));
        charView.put(bufferPosition_ / 2 + 2, (char) (byteDepth_ * 8));
    }
    buffers_.add(ifdBuffer);
    buffers_.add(getPixelBuffer(img));
    buffers_.add(getResolutionValuesBuffer());
    buffers_.add(ByteBuffer.wrap(getBytesFromString(mdString)));

    filePosition_ += totalBytes;
    firstIFD_ = false;
}

From source file:edu.mbl.jif.imaging.mmtiff.MultipageTiffWriter.java

private void writeBlankIFD() throws IOException {
    //      boolean blankPixelsAlreadyWritten = blankPixelsOffset_ != -1;
    boolean blankPixelsAlreadyWritten = false;

    char numEntries = (char) (((firstIFD_ && omeTiff_) ? ENTRIES_PER_IFD + 2 : ENTRIES_PER_IFD)
            + (firstIFD_ ? 2 : 0));/*w  w w. j  a  va 2  s  .c o  m*/

    String mdString = "NULL ";

    //2 bytes for number of directory entries, 12 bytes per directory entry, 4 byte offset of next IFD
    //6 bytes for bits per sample if RGB, 16 bytes for x and y resolution, 1 byte per character of MD string
    //number of bytes for pixels
    int totalBytes = 2 + numEntries * 12 + 4 + (rgb_ ? 6 : 0) + 16 + mdString.length()
            + (blankPixelsAlreadyWritten ? 0 : bytesPerImagePixels_);
    int IFDandBitDepthBytes = 2 + numEntries * 12 + 4 + (rgb_ ? 6 : 0);

    ByteBuffer ifdBuffer = ByteBuffer.allocate(IFDandBitDepthBytes).order(BYTE_ORDER);
    CharBuffer charView = ifdBuffer.asCharBuffer();

    long tagDataOffset = filePosition_ + 2 + numEntries * 12 + 4;
    nextIFDOffsetLocation_ = filePosition_ + 2 + numEntries * 12;

    bufferPosition_ = 0;
    charView.put(bufferPosition_, numEntries);
    bufferPosition_ += 2;
    writeIFDEntry(ifdBuffer, charView, WIDTH, (char) 4, 1, imageWidth_);
    writeIFDEntry(ifdBuffer, charView, HEIGHT, (char) 4, 1, imageHeight_);
    writeIFDEntry(ifdBuffer, charView, BITS_PER_SAMPLE, (char) 3, rgb_ ? 3 : 1,
            rgb_ ? tagDataOffset : byteDepth_ * 8);
    if (rgb_) {
        tagDataOffset += 6;
    }
    writeIFDEntry(ifdBuffer, charView, COMPRESSION, (char) 3, 1, 1);
    writeIFDEntry(ifdBuffer, charView, PHOTOMETRIC_INTERPRETATION, (char) 3, 1, rgb_ ? 2 : 1);

    if (firstIFD_ && omeTiff_) {
        omeDescriptionTagPosition_ = filePosition_ + bufferPosition_;
        writeIFDEntry(ifdBuffer, charView, IMAGE_DESCRIPTION, (char) 2, 0, 0);
    }
    if (firstIFD_) {
        ijDescriptionTagPosition_ = filePosition_ + bufferPosition_;
        writeIFDEntry(ifdBuffer, charView, IMAGE_DESCRIPTION, (char) 2, 0, 0);
    }

    if (!blankPixelsAlreadyWritten) { //Write blank pixels
        writeIFDEntry(ifdBuffer, charView, STRIP_OFFSETS, (char) 4, 1, tagDataOffset);
        blankPixelsOffset_ = tagDataOffset;
        tagDataOffset += bytesPerImagePixels_;
    } else {
        writeIFDEntry(ifdBuffer, charView, STRIP_OFFSETS, (char) 4, 1, blankPixelsOffset_);
    }

    writeIFDEntry(ifdBuffer, charView, SAMPLES_PER_PIXEL, (char) 3, 1, (rgb_ ? 3 : 1));
    writeIFDEntry(ifdBuffer, charView, ROWS_PER_STRIP, (char) 3, 1, imageHeight_);
    writeIFDEntry(ifdBuffer, charView, STRIP_BYTE_COUNTS, (char) 4, 1, bytesPerImagePixels_);
    writeIFDEntry(ifdBuffer, charView, X_RESOLUTION, (char) 5, 1, tagDataOffset);
    tagDataOffset += 8;
    writeIFDEntry(ifdBuffer, charView, Y_RESOLUTION, (char) 5, 1, tagDataOffset);
    tagDataOffset += 8;
    writeIFDEntry(ifdBuffer, charView, RESOLUTION_UNIT, (char) 3, 1, 3);
    if (firstIFD_) {
        ijMetadataCountsTagPosition_ = filePosition_ + bufferPosition_;
        writeIFDEntry(ifdBuffer, charView, IJ_METADATA_BYTE_COUNTS, (char) 4, 0, 0);
        ijMetadataTagPosition_ = filePosition_ + bufferPosition_;
        writeIFDEntry(ifdBuffer, charView, IJ_METADATA, (char) 1, 0, 0);
    }
    writeIFDEntry(ifdBuffer, charView, MM_METADATA, (char) 2, mdString.length(), tagDataOffset);
    tagDataOffset += mdString.length();
    //NextIFDOffset
    ifdBuffer.putInt(bufferPosition_, (int) tagDataOffset);
    bufferPosition_ += 4;

    if (rgb_) {
        charView.put(bufferPosition_ / 2, (char) (byteDepth_ * 8));
        charView.put(bufferPosition_ / 2 + 1, (char) (byteDepth_ * 8));
        charView.put(bufferPosition_ / 2 + 2, (char) (byteDepth_ * 8));
    }
    buffers_.add(ifdBuffer);
    if (!blankPixelsAlreadyWritten) {
        buffers_.add(ByteBuffer.wrap(new byte[bytesPerImagePixels_]));
    }
    buffers_.add(getResolutionValuesBuffer());
    buffers_.add(ByteBuffer.wrap(getBytesFromString(mdString)));

    filePosition_ += totalBytes;
    firstIFD_ = false;
}