Example usage for java.nio ByteBuffer getInt

List of usage examples for java.nio ByteBuffer getInt

Introduction

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

Prototype

public abstract int getInt(int index);

Source Link

Document

Returns the int at the specified index.

Usage

From source file:com.koda.integ.hbase.storage.FileExtStorage.java

/**
 * Tries to store batch of blocks into a current buffer.
 *
 * @param buf the buf/* w  w w.j av  a  2s  .com*/
 * @return the list
 */
private List<StorageHandle> storeDataNoReleaseLock(ByteBuffer buf) {

    List<StorageHandle> handles = new ArrayList<StorageHandle>();
    writeLock.writeLock().lock();
    try {

        if (activeBuffer.get() == null) {
            return null;
        }
        int size = buf.getInt(0);
        long off = bufferOffset.get();
        if (off + size > bufferSize) {
            return null;
        }

        long currentFileLength = currentFileOffsetForWrites.get();
        if (bufferOffset.get() == 0 && currentFileLength + bufferSize > fileSizeLimit) {
            // previous buffer was flushed
            currentFileOffsetForWrites.set(0);
            maxIdForWrites.incrementAndGet();
        }

        buf.position(4);

        while (buf.position() < size + 4) {
            buf.limit(buf.capacity());
            int pos = buf.position();
            int blockSize = buf.getInt();
            buf.position(pos);
            buf.limit(pos + 4 + blockSize);
            activeBuffer.get().put(buf);
            FileStorageHandle fsh = new FileStorageHandle(maxIdForWrites.get(),
                    (int) (currentFileOffsetForWrites.get()), blockSize);
            handles.add(fsh);
            // Increase offset in current file for writes;
            currentFileOffsetForWrites.addAndGet(blockSize + 4);
            bufferOffset.getAndAdd(blockSize + 4);
        }
        return handles;
    } finally {
        WriteLock lock = writeLock.writeLock();
        if (lock.isHeldByCurrentThread()) {
            lock.unlock();
        }
    }
}

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

/**
 * Creates an IndexData appropriate for the given table, using information
 * from the given table definition buffer.
 *//*from w  w w. ja v  a  2  s .  c  o  m*/
public static IndexData create(TableImpl table, ByteBuffer tableBuffer, int number, JetFormat format)
        throws IOException {
    int uniqueEntryCountOffset = (format.OFFSET_INDEX_DEF_BLOCK + (number * format.SIZE_INDEX_DEFINITION) + 4);
    int uniqueEntryCount = tableBuffer.getInt(uniqueEntryCountOffset);

    return new IndexData(table, number, uniqueEntryCount, uniqueEntryCountOffset);
}

From source file:x10.x10rt.yarn.ApplicationMaster.java

protected void handleX10() {
    // handle X10 place requests
    Iterator<SelectionKey> events = null;
    while (running) {
        try {//  w  w w .jav a2s . c  o  m
            SelectionKey key;
            // check for previously unhandled events
            if (events != null && events.hasNext()) {
                key = events.next();
                events.remove();
            } else if (selector.select() == 0) // check for new events
                continue; // nothing to process, go back and block on select again
            else { // select returned some events
                events = selector.selectedKeys().iterator();
                key = events.next();
                events.remove();
            }

            // process the selectionkey
            if (key.isAcceptable()) {
                LOG.info("New connection from X10 detected");
                // accept any connections on the server socket, and look for things to read from it
                ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
                SocketChannel sc = ssc.accept();
                sc.configureBlocking(false);
                sc.register(selector, SelectionKey.OP_READ);
            }
            if (key.isReadable()) {
                SocketChannel sc = (SocketChannel) key.channel();

                ByteBuffer incomingMsg;
                if (pendingReads.containsKey(sc))
                    incomingMsg = pendingReads.remove(sc);
                else
                    incomingMsg = ByteBuffer.allocateDirect(headerLength).order(ByteOrder.nativeOrder());

                LOG.info("Reading message from X10");
                try {
                    if (sc.read(incomingMsg) == -1) {
                        // socket closed
                        sc.close();
                        key.cancel();
                        pendingReads.remove(sc);
                    } else if (incomingMsg.hasRemaining()) {
                        LOG.info("Message header partially read. " + incomingMsg.remaining()
                                + " bytes remaining");
                        pendingReads.put(sc, incomingMsg);
                    } else { // buffer is full
                        if (incomingMsg.capacity() == headerLength) {
                            // check to see if there is a body associated with this message header
                            int datalen = incomingMsg.getInt(headerLength - 4);
                            //System.err.println("Byte order is "+incomingMsg.order()+" datalen="+datalen);
                            if (datalen == 0)
                                processMessage(incomingMsg, sc);
                            else { // create a larger array to hold the header+body
                                ByteBuffer newBuffer = ByteBuffer.allocateDirect(headerLength + datalen)
                                        .order(ByteOrder.nativeOrder());
                                incomingMsg.rewind();
                                newBuffer.put(incomingMsg);
                                incomingMsg = newBuffer;
                                sc.read(incomingMsg); // read in the body, if available
                                if (incomingMsg.hasRemaining()) {
                                    LOG.info("Message partially read. " + incomingMsg.remaining()
                                            + " bytes remaining");
                                    pendingReads.put(sc, incomingMsg);
                                } else
                                    processMessage(incomingMsg, sc);
                            }
                        }
                    }
                } catch (IOException e) {
                    LOG.warn("Error reading in message from socket channel", e);
                }
            }
        } catch (IOException e) {
            LOG.warn("Error handling X10 links", e);
        }
    }
}

From source file:com.healthmarketscience.jackcess.Table.java

/**
 * @param database database which owns this table
 * @param tableBuffer Buffer to read the table with
 * @param pageNumber Page number of the table definition
 * @param name Table name//from   w  ww.  j  a  va  2s .co  m
 * @param useBigIndex whether or not "big index support" should be enabled
 *                    for the table
 */
protected Table(Database database, ByteBuffer tableBuffer, int pageNumber, String name, int flags,
        boolean useBigIndex) throws IOException {
    _database = database;
    _tableDefPageNumber = pageNumber;
    _name = name;
    _flags = flags;
    _useBigIndex = useBigIndex;
    int nextPage = tableBuffer.getInt(getFormat().OFFSET_NEXT_TABLE_DEF_PAGE);
    ByteBuffer nextPageBuffer = null;
    while (nextPage != 0) {
        if (nextPageBuffer == null) {
            nextPageBuffer = getPageChannel().createPageBuffer();
        }
        getPageChannel().readPage(nextPageBuffer, nextPage);
        nextPage = nextPageBuffer.getInt(getFormat().OFFSET_NEXT_TABLE_DEF_PAGE);
        ByteBuffer newBuffer = getPageChannel()
                .createBuffer(tableBuffer.capacity() + getFormat().PAGE_SIZE - 8);
        newBuffer.put(tableBuffer);
        newBuffer.put(nextPageBuffer.array(), 8, getFormat().PAGE_SIZE - 8);
        tableBuffer = newBuffer;
        tableBuffer.flip();
    }
    readTableDefinition(tableBuffer);
    tableBuffer = null;
}

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

/**
 * Returns a single ByteBuffer which contains the entire table definition
 * (which may span multiple database pages).
 *//*from  w ww.  j  a  va  2s.c  o m*/
private ByteBuffer loadCompleteTableDefinitionBuffer(ByteBuffer tableBuffer) throws IOException {
    int nextPage = tableBuffer.getInt(getFormat().OFFSET_NEXT_TABLE_DEF_PAGE);
    ByteBuffer nextPageBuffer = null;
    while (nextPage != 0) {
        if (nextPageBuffer == null) {
            nextPageBuffer = getPageChannel().createPageBuffer();
        }
        getPageChannel().readPage(nextPageBuffer, nextPage);
        nextPage = nextPageBuffer.getInt(getFormat().OFFSET_NEXT_TABLE_DEF_PAGE);
        ByteBuffer newBuffer = PageChannel.createBuffer(tableBuffer.capacity() + getFormat().PAGE_SIZE - 8);
        newBuffer.put(tableBuffer);
        newBuffer.put(nextPageBuffer.array(), 8, getFormat().PAGE_SIZE - 8);
        tableBuffer = newBuffer;
        tableBuffer.flip();
    }
    return tableBuffer;
}

From source file:com.healthmarketscience.jackcess.Table.java

/**
 * Read the table definition/*from w  ww .j av  a 2  s .  co m*/
 */
private void readTableDefinition(ByteBuffer tableBuffer) throws IOException {
    if (LOG.isDebugEnabled()) {
        tableBuffer.rewind();
        LOG.debug("Table def block:\n" + ByteUtil.toHexString(tableBuffer, getFormat().SIZE_TDEF_HEADER));
    }
    _rowCount = tableBuffer.getInt(getFormat().OFFSET_NUM_ROWS);
    _lastLongAutoNumber = tableBuffer.getInt(getFormat().OFFSET_NEXT_AUTO_NUMBER);
    if (getFormat().OFFSET_NEXT_COMPLEX_AUTO_NUMBER >= 0) {
        _lastComplexTypeAutoNumber = tableBuffer.getInt(getFormat().OFFSET_NEXT_COMPLEX_AUTO_NUMBER);
    }
    _tableType = tableBuffer.get(getFormat().OFFSET_TABLE_TYPE);
    _maxColumnCount = tableBuffer.getShort(getFormat().OFFSET_MAX_COLS);
    _maxVarColumnCount = tableBuffer.getShort(getFormat().OFFSET_NUM_VAR_COLS);
    short columnCount = tableBuffer.getShort(getFormat().OFFSET_NUM_COLS);
    _logicalIndexCount = tableBuffer.getInt(getFormat().OFFSET_NUM_INDEX_SLOTS);
    _indexCount = tableBuffer.getInt(getFormat().OFFSET_NUM_INDEXES);

    int rowNum = ByteUtil.getUnsignedByte(tableBuffer, getFormat().OFFSET_OWNED_PAGES);
    int pageNum = ByteUtil.get3ByteInt(tableBuffer, getFormat().OFFSET_OWNED_PAGES + 1);
    _ownedPages = UsageMap.read(getDatabase(), pageNum, rowNum, false);
    rowNum = ByteUtil.getUnsignedByte(tableBuffer, getFormat().OFFSET_FREE_SPACE_PAGES);
    pageNum = ByteUtil.get3ByteInt(tableBuffer, getFormat().OFFSET_FREE_SPACE_PAGES + 1);
    _freeSpacePages = UsageMap.read(getDatabase(), pageNum, rowNum, false);

    for (int i = 0; i < _indexCount; i++) {
        _indexDatas.add(IndexData.create(this, tableBuffer, i, getFormat()));
    }

    int colOffset = getFormat().OFFSET_INDEX_DEF_BLOCK + _indexCount * getFormat().SIZE_INDEX_DEFINITION;
    int dispIndex = 0;
    for (int i = 0; i < columnCount; i++) {
        Column column = new Column(this, tableBuffer, colOffset + (i * getFormat().SIZE_COLUMN_HEADER),
                dispIndex++);
        _columns.add(column);
        if (column.isVariableLength()) {
            // also shove it in the variable columns list, which is ordered
            // differently from the _columns list
            _varColumns.add(column);
        }
    }
    tableBuffer.position(colOffset + (columnCount * getFormat().SIZE_COLUMN_HEADER));
    for (int i = 0; i < columnCount; i++) {
        Column column = _columns.get(i);
        column.setName(readName(tableBuffer));
    }
    Collections.sort(_columns);
    _autoNumColumns = getAutoNumberColumns(_columns);

    // setup the data index for the columns
    int colIdx = 0;
    for (Column col : _columns) {
        col.setColumnIndex(colIdx++);
    }

    // sort variable length columns based on their index into the variable
    // length offset table, because we will write the columns in this order
    Collections.sort(_varColumns, VAR_LEN_COLUMN_COMPARATOR);

    // read index column information
    for (int i = 0; i < _indexCount; i++) {
        _indexDatas.get(i).read(tableBuffer, _columns);
    }

    // read logical index info (may be more logical indexes than index datas)
    for (int i = 0; i < _logicalIndexCount; i++) {
        _indexes.add(new Index(tableBuffer, _indexDatas, getFormat()));
    }

    // read logical index names
    for (int i = 0; i < _logicalIndexCount; i++) {
        _indexes.get(i).setName(readName(tableBuffer));
    }

    Collections.sort(_indexes);

    // re-sort columns if necessary
    if (getDatabase().getColumnOrder() != ColumnOrder.DATA) {
        Collections.sort(_columns, DISPLAY_ORDER_COMPARATOR);
    }

    for (Column col : _columns) {
        // some columns need to do extra work after the table is completely
        // loaded
        col.postTableLoadInit();
    }
}

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

/**
 * CHECKED 2 Checks if is not null.//  ww  w .  j a  va 2  s. co  m
 * 
 * @param buf
 *          the buf
 * @return true, if is not null
 */
private final boolean isNotNull(ByteBuffer buf) {
    return buf.getInt(0) != 0;
}

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

/**
 * @param database database which owns this table
 * @param tableBuffer Buffer to read the table with
 * @param pageNumber Page number of the table definition
 * @param name Table name/*from  w w  w  .  j  a  v  a 2  s. c o  m*/
 */
protected TableImpl(DatabaseImpl database, ByteBuffer tableBuffer, int pageNumber, String name, int flags)
        throws IOException {
    _database = database;
    _tableDefPageNumber = pageNumber;
    _name = name;
    _flags = flags;

    // read table definition
    tableBuffer = loadCompleteTableDefinitionBuffer(tableBuffer);
    _rowCount = tableBuffer.getInt(getFormat().OFFSET_NUM_ROWS);
    _lastLongAutoNumber = tableBuffer.getInt(getFormat().OFFSET_NEXT_AUTO_NUMBER);
    if (getFormat().OFFSET_NEXT_COMPLEX_AUTO_NUMBER >= 0) {
        _lastComplexTypeAutoNumber = tableBuffer.getInt(getFormat().OFFSET_NEXT_COMPLEX_AUTO_NUMBER);
    }
    _tableType = tableBuffer.get(getFormat().OFFSET_TABLE_TYPE);
    _maxColumnCount = tableBuffer.getShort(getFormat().OFFSET_MAX_COLS);
    _maxVarColumnCount = tableBuffer.getShort(getFormat().OFFSET_NUM_VAR_COLS);
    short columnCount = tableBuffer.getShort(getFormat().OFFSET_NUM_COLS);
    _logicalIndexCount = tableBuffer.getInt(getFormat().OFFSET_NUM_INDEX_SLOTS);
    _indexCount = tableBuffer.getInt(getFormat().OFFSET_NUM_INDEXES);

    tableBuffer.position(getFormat().OFFSET_OWNED_PAGES);
    _ownedPages = UsageMap.read(getDatabase(), tableBuffer, false);
    tableBuffer.position(getFormat().OFFSET_FREE_SPACE_PAGES);
    _freeSpacePages = UsageMap.read(getDatabase(), tableBuffer, false);

    for (int i = 0; i < _indexCount; i++) {
        _indexDatas.add(IndexData.create(this, tableBuffer, i, getFormat()));
    }

    readColumnDefinitions(tableBuffer, columnCount);

    readIndexDefinitions(tableBuffer);

    // read column usage map info
    while (tableBuffer.remaining() >= 2) {

        short umapColNum = tableBuffer.getShort();
        if (umapColNum == IndexData.COLUMN_UNUSED) {
            break;
        }

        int pos = tableBuffer.position();
        UsageMap colOwnedPages = null;
        UsageMap colFreeSpacePages = null;
        try {
            colOwnedPages = UsageMap.read(getDatabase(), tableBuffer, false);
            colFreeSpacePages = UsageMap.read(getDatabase(), tableBuffer, false);
        } catch (IllegalStateException e) {
            // ignore invalid usage map info
            colOwnedPages = null;
            colFreeSpacePages = null;
            tableBuffer.position(pos + 8);
            LOG.warn("Table " + _name + " invalid column " + umapColNum + " usage map definition: " + e);
        }

        for (ColumnImpl col : _columns) {
            if (col.getColumnNumber() == umapColNum) {
                col.setUsageMaps(colOwnedPages, colFreeSpacePages);
                break;
            }
        }
    }

    // re-sort columns if necessary
    if (getDatabase().getColumnOrder() != ColumnOrder.DATA) {
        Collections.sort(_columns, DISPLAY_ORDER_COMPARATOR);
    }

    for (ColumnImpl col : _columns) {
        // some columns need to do extra work after the table is completely
        // loaded
        col.postTableLoadInit();
    }

    _fkEnforcer = new FKEnforcer(this);

    if (!isSystem()) {
        // after fully constructed, allow column validator to be configured (but
        // only for user tables)
        for (ColumnImpl col : _columns) {
            col.setColumnValidator(null);
        }
    }
}

From source file:com.koda.integ.hbase.storage.FileExtStorage.java

@Override
public StorageHandle getData(StorageHandle storeHandle, ByteBuffer buf) {
    FileStorageHandle fsh = (FileStorageHandle) storeHandle;

    // Check if current file and offset > currentFileOffset
    int id = maxId.get();
    if (fsh.getId() > id || (fsh.getId() == id && fsh.getOffset() >= currentFileOffset.get())) {
        // not found
        buf.putInt(0, 0);/*from w w  w.ja  va  2 s .  c o m*/
        return fsh;
    }

    RandomAccessFile file = getFile(fsh.getId());//openFile(fsh.getId(), "r");

    boolean needSecondChance = needSecondChance(fsh.getId());

    try {
        if (file == null) {
            // return null
            buf.putInt(0, 0);
        } else {
            buf.clear();
            int toRead = fsh.getSize();
            buf.putInt(fsh.getSize());
            buf.limit(4 + toRead);
            try {
                FileChannel fc = file.getChannel();
                int total = 0;
                int c = 0;
                // offset start with overall object length .add +4
                int off = fsh.getOffset() + 4;
                while (total < toRead) {
                    c = fc.read(buf, off);
                    off += c;
                    if (c < 0) {
                        // return not found
                        buf.putInt(0, 0);
                        break;
                    }
                    total += c;
                }
            } catch (IOException e) {
                // return not found
                if (fsh.getId() > minId.get()) {
                    e.printStackTrace();
                }
                buf.putInt(0, 0);
            }
        }
        if (buf.getInt(0) != 0 && needSecondChance) {
            // store again
            fsh = (FileStorageHandle) storeData(buf);
        }
        return fsh;

    } finally {
        if (file != null) {
            // return file back
            // PUT we need for old version
            putFile(fsh.getId(), file);
        }
    }

}

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

/**
 * Reads an index page, populating the correct collection based on the page
 * type (node or leaf)./*from   w w  w  .  j  ava  2s. c  o m*/
 */
protected void readDataPage(DataPage dataPage) throws IOException {
    ByteBuffer buffer = _indexBufferH.getPageBuffer(getPageChannel());
    getPageChannel().readPage(buffer, dataPage.getPageNumber());

    boolean isLeaf = isLeafPage(buffer);
    dataPage.setLeaf(isLeaf);

    // note, "header" data is in LITTLE_ENDIAN format, entry data is in
    // BIG_ENDIAN format
    int entryPrefixLength = ByteUtil.getUnsignedShort(buffer, getFormat().OFFSET_INDEX_COMPRESSED_BYTE_COUNT);
    int entryMaskLength = getFormat().SIZE_INDEX_ENTRY_MASK;
    int entryMaskPos = getFormat().OFFSET_INDEX_ENTRY_MASK;
    int entryPos = entryMaskPos + entryMaskLength;
    int lastStart = 0;
    int totalEntrySize = 0;
    byte[] entryPrefix = null;
    List<Entry> entries = new ArrayList<Entry>();
    TempBufferHolder tmpEntryBufferH = TempBufferHolder.newHolder(TempBufferHolder.Type.HARD, true,
            ENTRY_BYTE_ORDER);

    Entry prevEntry = FIRST_ENTRY;
    for (int i = 0; i < entryMaskLength; i++) {
        byte entryMask = buffer.get(entryMaskPos + i);
        for (int j = 0; j < 8; j++) {
            if ((entryMask & (1 << j)) != 0) {
                int length = (i * 8) + j - lastStart;
                buffer.position(entryPos + lastStart);

                // determine if we can read straight from the index page (if no
                // entryPrefix).  otherwise, create temp buf with complete entry.
                ByteBuffer curEntryBuffer = buffer;
                int curEntryLen = length;
                if (entryPrefix != null) {
                    curEntryBuffer = getTempEntryBuffer(buffer, length, entryPrefix, tmpEntryBufferH);
                    curEntryLen += entryPrefix.length;
                }
                totalEntrySize += curEntryLen;

                Entry entry = newEntry(curEntryBuffer, curEntryLen, isLeaf);
                if (prevEntry.compareTo(entry) >= 0) {
                    throw new IOException("Unexpected order in index entries, " + prevEntry + " >= " + entry);
                }

                entries.add(entry);

                if ((entries.size() == 1) && (entryPrefixLength > 0)) {
                    // read any shared entry prefix
                    entryPrefix = new byte[entryPrefixLength];
                    buffer.position(entryPos + lastStart);
                    buffer.get(entryPrefix);
                }

                lastStart += length;
                prevEntry = entry;
            }
        }
    }

    dataPage.setEntryPrefix(entryPrefix != null ? entryPrefix : EMPTY_PREFIX);
    dataPage.setEntries(entries);
    dataPage.setTotalEntrySize(totalEntrySize);

    int prevPageNumber = buffer.getInt(getFormat().OFFSET_PREV_INDEX_PAGE);
    int nextPageNumber = buffer.getInt(getFormat().OFFSET_NEXT_INDEX_PAGE);
    int childTailPageNumber = buffer.getInt(getFormat().OFFSET_CHILD_TAIL_INDEX_PAGE);

    dataPage.setPrevPageNumber(prevPageNumber);
    dataPage.setNextPageNumber(nextPageNumber);
    dataPage.setChildTailPageNumber(childTailPageNumber);
}