Example usage for java.nio ByteBuffer getShort

List of usage examples for java.nio ByteBuffer getShort

Introduction

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

Prototype

public abstract short getShort(int index);

Source Link

Document

Returns the short at the specified index.

Usage

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

/**
 * @usage _advanced_method_// w ww  . j  av  a 2  s  .  co  m
 */
public static short findRowStart(ByteBuffer buffer, int rowNum, JetFormat format) {
    return cleanRowStart(buffer.getShort(getRowStartOffset(rowNum, format)));
}

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

/**
 * @usage _advanced_method_/*from  w  w w . j  a  v a 2s  . c  o m*/
 */
public static short findRowEnd(ByteBuffer buffer, int rowNum, JetFormat format) {
    return (short) ((rowNum == 0) ? format.PAGE_SIZE
            : cleanRowStart(buffer.getShort(getRowEndOffset(rowNum, format))));
}

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

/**
 * Returns {@code true} if a row of the given size will fit on the given
 * data page, {@code false} otherwise.//from  w ww  . jav  a2  s.  c o m
 * @usage _advanced_method_
 */
public static boolean rowFitsOnDataPage(int rowLength, ByteBuffer dataPage, JetFormat format)
        throws IOException {
    int rowSpaceUsage = getRowSpaceUsage(rowLength, format);
    short freeSpaceInPage = dataPage.getShort(format.OFFSET_FREE_SPACE);
    int rowsOnPage = getRowsOnDataPage(dataPage, format);
    return ((rowSpaceUsage <= freeSpaceInPage) && (rowsOnPage < format.MAX_NUM_ROWS_ON_DATA_PAGE));
}

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

/**
 * Returns the row count for the current page.  If the page is invalid
 * ({@code null}) or the page is not a DATA page, 0 is returned.
 *///from w w w .j av  a2  s.  c o m
static int getRowsOnDataPage(ByteBuffer rowBuffer, JetFormat format) throws IOException {
    int rowsOnPage = 0;
    if ((rowBuffer != null) && (rowBuffer.get(0) == PageTypes.DATA)) {
        rowsOnPage = rowBuffer.getShort(format.OFFSET_NUM_ROWS_ON_DATA_PAGE);
    }
    return rowsOnPage;
}

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

/**
 * Updates free space and row info for a new row of the given size in the
 * given data page.  Positions the page for writing the row data.
 * @return the row number of the new row
 * @usage _advanced_method_// ww  w.  ja va  2s.c  o  m
 */
public static int addDataPageRow(ByteBuffer dataPage, int rowSize, JetFormat format, int rowFlags) {
    int rowSpaceUsage = getRowSpaceUsage(rowSize, format);

    // Decrease free space record.
    short freeSpaceInPage = dataPage.getShort(format.OFFSET_FREE_SPACE);
    dataPage.putShort(format.OFFSET_FREE_SPACE, (short) (freeSpaceInPage - rowSpaceUsage));

    // Increment row count record.
    short rowCount = dataPage.getShort(format.OFFSET_NUM_ROWS_ON_DATA_PAGE);
    dataPage.putShort(format.OFFSET_NUM_ROWS_ON_DATA_PAGE, (short) (rowCount + 1));

    // determine row position
    short rowLocation = findRowEnd(dataPage, rowCount, format);
    rowLocation -= rowSize;

    // write row position
    dataPage.putShort(getRowStartOffset(rowCount, format), (short) (rowLocation | rowFlags));

    // set position for row data
    dataPage.position(rowLocation);

    return rowCount;
}

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

/**
 * Sets a new buffer to the correct row header page using the given rowState
 * according to the given rowId.  Deleted state is
 * determined, but overflow row pointers are not followed.
 * /* ww w. j  a va  2  s  .c  o m*/
 * @return a ByteBuffer of the relevant page, or null if row was invalid
 * @usage _advanced_method_
 */
public static ByteBuffer positionAtRowHeader(RowState rowState, RowId rowId) throws IOException {
    ByteBuffer rowBuffer = rowState.setHeaderRow(rowId);

    if (rowState.isAtHeaderRow()) {
        // this task has already been accomplished
        return rowBuffer;
    }

    if (!rowState.isValid()) {
        // this was an invalid page/row
        rowState.setStatus(RowStateStatus.AT_HEADER);
        return null;
    }

    // note, we don't use findRowStart here cause we need the unmasked value
    short rowStart = rowBuffer
            .getShort(getRowStartOffset(rowId.getRowNumber(), rowState.getTable().getFormat()));

    // check the deleted, overflow flags for the row (the "real" flags are
    // always set on the header row)
    RowStatus rowStatus = RowStatus.NORMAL;
    if (isDeletedRow(rowStart)) {
        rowStatus = RowStatus.DELETED;
    } else if (isOverflowRow(rowStart)) {
        rowStatus = RowStatus.OVERFLOW;
    }

    rowState.setRowStatus(rowStatus);
    rowState.setStatus(RowStateStatus.AT_HEADER);
    return rowBuffer;
}

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

private TaggedImage readTaggedImage(IFDData data) throws IOException {
    ByteBuffer pixelBuffer = ByteBuffer.allocate((int) data.bytesPerImage);
    ByteBuffer mdBuffer = ByteBuffer.allocate((int) data.mdLength);
    fileChannel_.read(pixelBuffer, data.pixelOffset);
    fileChannel_.read(mdBuffer, data.mdOffset);
    JSONObject md = null;//from   w ww .  j  a  v  a 2s  .c om
    try {
        md = new JSONObject(getString(mdBuffer));
    } catch (JSONException ex) {
        ReportingUtils.logError("Error reading image metadata from file");
    }

    if (byteDepth_ == 0) {
        getRGBAndByteDepth(md);
    }

    if (rgb_) {
        if (byteDepth_ == 1) {
            byte[] pixels = new byte[(int) (4 * data.bytesPerImage / 3)];
            int i = 0;
            for (byte b : pixelBuffer.array()) {
                pixels[i] = b;
                i++;
                if ((i + 1) % 4 == 0) {
                    pixels[i] = 0;
                    i++;
                }
            }
            return new TaggedImage(pixels, md);
        } else {
            short[] pixels = new short[(int) (2 * (data.bytesPerImage / 3))];
            int i = 0;
            while (i < pixels.length) {
                pixels[i] = pixelBuffer.getShort(2 * ((i / 4) * 3 + (i % 4)));
                i++;
                if ((i + 1) % 4 == 0) {
                    pixels[i] = 0;
                    i++;
                }
            }
            return new TaggedImage(pixels, md);
        }
    } else {
        if (byteDepth_ == 1) {
            return new TaggedImage(pixelBuffer.array(), md);
        } else {
            short[] pix = new short[pixelBuffer.capacity() / 2];
            for (int i = 0; i < pix.length; i++) {
                pix[i] = pixelBuffer.getShort(i * 2);
            }
            return new TaggedImage(pix, md);
        }
    }
}

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

/**
 * Sets the position and limit in a new buffer using the given rowState
 * according to the given row number and row end, following overflow row
 * pointers as necessary./*from w w w .  j a v a2  s  .  c  o  m*/
 * 
 * @return a ByteBuffer narrowed to the actual row data, or null if row was
 *         invalid or deleted
 * @usage _advanced_method_
 */
public static ByteBuffer positionAtRowData(RowState rowState, RowId rowId) throws IOException {
    positionAtRowHeader(rowState, rowId);
    if (!rowState.isValid() || rowState.isDeleted()) {
        // row is invalid or deleted
        rowState.setStatus(RowStateStatus.AT_FINAL);
        return null;
    }

    ByteBuffer rowBuffer = rowState.getFinalPage();
    int rowNum = rowState.getFinalRowId().getRowNumber();
    JetFormat format = rowState.getTable().getFormat();

    if (rowState.isAtFinalRow()) {
        // we've already found the final row data
        return PageChannel.narrowBuffer(rowBuffer, findRowStart(rowBuffer, rowNum, format),
                findRowEnd(rowBuffer, rowNum, format));
    }

    while (true) {

        // note, we don't use findRowStart here cause we need the unmasked value
        short rowStart = rowBuffer.getShort(getRowStartOffset(rowNum, format));
        short rowEnd = findRowEnd(rowBuffer, rowNum, format);

        // note, at this point we know the row is not deleted, so ignore any
        // subsequent deleted flags (as overflow rows are always marked deleted
        // anyway)
        boolean overflowRow = isOverflowRow(rowStart);

        // now, strip flags from rowStart offset
        rowStart = (short) (rowStart & OFFSET_MASK);

        if (overflowRow) {

            if ((rowEnd - rowStart) < 4) {
                throw new IOException("invalid overflow row info");
            }

            // Overflow page.  the "row" data in the current page points to
            // another page/row
            int overflowRowNum = ByteUtil.getUnsignedByte(rowBuffer, rowStart);
            int overflowPageNum = ByteUtil.get3ByteInt(rowBuffer, rowStart + 1);
            rowBuffer = rowState.setOverflowRow(new RowId(overflowPageNum, overflowRowNum));
            rowNum = overflowRowNum;

        } else {

            rowState.setStatus(RowStateStatus.AT_FINAL);
            return PageChannel.narrowBuffer(rowBuffer, rowStart, rowEnd);
        }
    }
}

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

/**
 * Sets a new buffer to the correct row header page using the given rowState
 * according to the given rowId.  Deleted state is
 * determined, but overflow row pointers are not followed.
 * //from  w  w w.  j  av a2 s  .  com
 * @return a ByteBuffer of the relevant page, or null if row was invalid
 * @usage _advanced_method_
 */
public static ByteBuffer positionAtRowHeader(RowState rowState, RowIdImpl rowId) throws IOException {
    ByteBuffer rowBuffer = rowState.setHeaderRow(rowId);

    if (rowState.isAtHeaderRow()) {
        // this task has already been accomplished
        return rowBuffer;
    }

    if (!rowState.isValid()) {
        // this was an invalid page/row
        rowState.setStatus(RowStateStatus.AT_HEADER);
        return null;
    }

    // note, we don't use findRowStart here cause we need the unmasked value
    short rowStart = rowBuffer
            .getShort(getRowStartOffset(rowId.getRowNumber(), rowState.getTable().getFormat()));

    // check the deleted, overflow flags for the row (the "real" flags are
    // always set on the header row)
    RowStatus rowStatus = RowStatus.NORMAL;
    if (isDeletedRow(rowStart)) {
        rowStatus = RowStatus.DELETED;
    } else if (isOverflowRow(rowStart)) {
        rowStatus = RowStatus.OVERFLOW;
    }

    rowState.setRowStatus(rowStatus);
    rowState.setStatus(RowStateStatus.AT_HEADER);
    return rowBuffer;
}

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

/**
 * Sets the position and limit in a new buffer using the given rowState
 * according to the given row number and row end, following overflow row
 * pointers as necessary.// w w  w . java2s .c o  m
 * 
 * @return a ByteBuffer narrowed to the actual row data, or null if row was
 *         invalid or deleted
 * @usage _advanced_method_
 */
public static ByteBuffer positionAtRowData(RowState rowState, RowIdImpl rowId) throws IOException {
    positionAtRowHeader(rowState, rowId);
    if (!rowState.isValid() || rowState.isDeleted()) {
        // row is invalid or deleted
        rowState.setStatus(RowStateStatus.AT_FINAL);
        return null;
    }

    ByteBuffer rowBuffer = rowState.getFinalPage();
    int rowNum = rowState.getFinalRowId().getRowNumber();
    JetFormat format = rowState.getTable().getFormat();

    if (rowState.isAtFinalRow()) {
        // we've already found the final row data
        return PageChannel.narrowBuffer(rowBuffer, findRowStart(rowBuffer, rowNum, format),
                findRowEnd(rowBuffer, rowNum, format));
    }

    while (true) {

        // note, we don't use findRowStart here cause we need the unmasked value
        short rowStart = rowBuffer.getShort(getRowStartOffset(rowNum, format));
        short rowEnd = findRowEnd(rowBuffer, rowNum, format);

        // note, at this point we know the row is not deleted, so ignore any
        // subsequent deleted flags (as overflow rows are always marked deleted
        // anyway)
        boolean overflowRow = isOverflowRow(rowStart);

        // now, strip flags from rowStart offset
        rowStart = (short) (rowStart & OFFSET_MASK);

        if (overflowRow) {

            if ((rowEnd - rowStart) < 4) {
                throw new IOException("invalid overflow row info");
            }

            // Overflow page.  the "row" data in the current page points to
            // another page/row
            int overflowRowNum = ByteUtil.getUnsignedByte(rowBuffer, rowStart);
            int overflowPageNum = ByteUtil.get3ByteInt(rowBuffer, rowStart + 1);
            rowBuffer = rowState.setOverflowRow(new RowIdImpl(overflowPageNum, overflowRowNum));
            rowNum = overflowRowNum;

        } else {

            rowState.setStatus(RowStateStatus.AT_FINAL);
            return PageChannel.narrowBuffer(rowBuffer, rowStart, rowEnd);
        }
    }
}