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.linkedin.databus.core.DbusEventPart.java

/**
 * Decodes a bytebuffer and returns DbusEventPart. Preserves the ByteBuffer position.
 * @param buf//from www . jav a 2s .c  o  m
 * @return
 */
public static DbusEventPart decode(ByteBuffer buf) {
    int pos = buf.position();
    int dataLen = buf.getInt(pos);
    if (dataLen < 0) {
        throw new UnsupportedOperationException("Data length " + dataLen + " not supported");
    }
    short attributes = buf.getShort(pos + AttributesOffset);
    short schemaVersion = (short) (attributes >> VERSION_SHIFT);
    SchemaDigestType schemaDigestType = digestType(attributes);
    int digestLen = digestLen(schemaDigestType);
    byte[] digest = new byte[digestLen];
    for (int i = 0; i < digestLen; i++) {
        digest[i] = buf.get(pos + AttributesOffset + AttributesLen + i);
    }

    // NOTE - this will create a new ByteBuffer object pointing to the
    // same memory. So the position of this new BB is beginning of the data
    // and limit is set to the end of the data.
    ByteBuffer dataBuf = buf.asReadOnlyBuffer();
    dataBuf.position(pos + AttributesOffset + AttributesLen + digestLen);
    dataBuf.limit(dataBuf.position() + dataLen);

    return new DbusEventPart(schemaDigestType, digest, schemaVersion, dataBuf);
}

From source file:Main.java

public static long findCentralDirStartOffset(final FileChannel fileChannel, final long commentLength)
        throws IOException {
    // End of central directory record (EOCD)
    // Offset    Bytes     Description[23]
    // 0           4       End of central directory signature = 0x06054b50
    // 4           2       Number of this disk
    // 6           2       Disk where central directory starts
    // 8           2       Number of central directory records on this disk
    // 10          2       Total number of central directory records
    // 12          4       Size of central directory (bytes)
    // 16          4       Offset of start of central directory, relative to start of archive
    // 20          2       Comment length (n)
    // 22          n       Comment
    // For a zip with no archive comment, the
    // end-of-central-directory record will be 22 bytes long, so
    // we expect to find the EOCD marker 22 bytes from the end.

    final ByteBuffer zipCentralDirectoryStart = ByteBuffer.allocate(4);
    zipCentralDirectoryStart.order(ByteOrder.LITTLE_ENDIAN);
    fileChannel.position(fileChannel.size() - commentLength - 6); // 6 = 2 (Comment length) + 4 (Offset of start of central directory, relative to start of archive)
    fileChannel.read(zipCentralDirectoryStart);
    final long centralDirStartOffset = zipCentralDirectoryStart.getInt(0);
    return centralDirStartOffset;
}

From source file:com.healthmarketscience.jackcess.complex.ComplexColumnInfo.java

public static ComplexColumnInfo<? extends ComplexValue> create(Column column, ByteBuffer buffer, int offset)
        throws IOException {
    int complexTypeId = buffer.getInt(offset + column.getFormat().OFFSET_COLUMN_COMPLEX_ID);

    Database db = column.getDatabase();//from  w w w.j  a v a2 s  .com
    Table complexColumns = db.getSystemComplexColumns();
    IndexCursor cursor = IndexCursor.createCursor(complexColumns, complexColumns.getPrimaryKeyIndex());
    if (!cursor.findFirstRowByEntry(complexTypeId)) {
        throw new IOException("Could not find complex column info for complex column with id " + complexTypeId);
    }
    Map<String, Object> cColRow = cursor.getCurrentRow();
    int tableId = (Integer) cColRow.get(COL_TABLE_ID);
    if (tableId != column.getTable().getTableDefPageNumber()) {
        throw new IOException("Found complex column for table " + tableId + " but expected table "
                + column.getTable().getTableDefPageNumber());
    }
    int flatTableId = (Integer) cColRow.get(COL_FLAT_TABLE_ID);
    int typeObjId = (Integer) cColRow.get(COL_COMPLEX_TYPE_OBJECT_ID);

    Table typeObjTable = db.getTable(typeObjId);
    Table flatTable = db.getTable(flatTableId);

    if ((typeObjTable == null) || (flatTable == null)) {
        throw new IOException("Could not find supporting tables (" + typeObjId + ", " + flatTableId
                + ") for complex column with id " + complexTypeId);
    }

    // we inspect the structore of the "type table" to determine what kind of
    // complex info we are dealing with
    if (MultiValueColumnInfo.isMultiValueColumn(typeObjTable)) {
        return new MultiValueColumnInfo(column, complexTypeId, typeObjTable, flatTable);
    } else if (AttachmentColumnInfo.isAttachmentColumn(typeObjTable)) {
        return new AttachmentColumnInfo(column, complexTypeId, typeObjTable, flatTable);
    } else if (VersionHistoryColumnInfo.isVersionHistoryColumn(typeObjTable)) {
        return new VersionHistoryColumnInfo(column, complexTypeId, typeObjTable, flatTable);
    }

    LOG.warn("Unsupported complex column type " + typeObjTable.getName());
    return new UnsupportedColumnInfo(column, complexTypeId, typeObjTable, flatTable);
}

From source file:com.linkedin.databus.core.DbusEventPart.java

/**
 * @return the length of the DbusEventPart that is encoded in 'buf' at position 'position'.
 * Callers can use this method to advance across the DbusEventPart in a serialized V2 event.
 *//*from   w ww .  ja  va2  s . c om*/
public static int partLength(ByteBuffer buf, int position) {
    DbusEvent.SchemaDigestType digestType = digestType(buf.getShort(position + AttributesOffset));
    int digestLen = digestLen(digestType);
    return AttributesOffset + AttributesLen + digestLen + buf.getInt(position);
}

From source file:com.l2jfree.security.NewCipher.java

public static int getPreCalculatedChecksum(ByteBuffer buf, final int offset, final int size) {
    return buf.getInt(offset + size - 4);
}

From source file:com.glaf.core.util.ByteBufferUtils.java

/**
 * Convert a byte buffer to an integer. Does not change the byte buffer
 * position.//from w  w w . ja  v a 2 s .  c o  m
 * 
 * @param bytes
 *            byte buffer to convert to integer
 * @return int representation of the byte buffer
 */
public static int toInt(ByteBuffer bytes) {
    return bytes.getInt(bytes.position());
}

From source file:jsave.Utils.java

/**
 * Returns an integer./*from  w  ww. ja v a2 s.c  o  m*/
 * 
 * Since java does not provide unsigned primitive types, each unsigned
 * value read from the buffer is promoted up to the next bigger primitive
 * data type :  an int and getUnsignedInt() returns a long.  
 * 
 * @param bb the array of bytes in the buffer
 * @return an long integer
 */
public static long getUnsignedInt(ByteBuffer bb) {
    return ((long) bb.getInt(0) & 0xffffffffL);
}

From source file:org.onlab.packet.ONLabLddp.java

/**
 * Extracts dpid and port from OVX-generated LLDP packet.
 *
 * @param packet packet data/*  w w w .  ja  v a  2s .  co  m*/
 * @return Dpid and port
 */
public static DPIDandPort parseLLDP(final byte[] packet) {
    final ByteBuffer bb = ByteBuffer.wrap(packet);

    // Extra offset due to VLAN tag
    int offset = 0;
    if (bb.getShort(ETHERTYPE_OFFSET) != Ethernet.TYPE_LLDP
            && bb.getShort(ETHERTYPE_OFFSET) != Ethernet.TYPE_BSN) {
        offset = 4;
    }

    final int port = bb.getInt(PORT_OFFSET + offset);
    final long dpid = bb.getLong(DPID_OFFSET + offset);

    return new DPIDandPort(dpid, port);
}

From source file:jsave.Utils.java

public static int read_long(final RandomAccessFile raf) throws IOException {
    byte[] data = new byte[4];
    raf.read(data);/*from  w  w w . j  a  v a  2s.  c o m*/
    ByteBuffer bb = ByteBuffer.allocate(data.length);
    bb.put(data);
    return bb.getInt(0);
}

From source file:org.apache.jackrabbit.oak.segment.Segment.java

/**
 * Determine the gc generation a segment from its data. Note that bulk segments don't have
 * generations (i.e. stay at 0)./*from  ww  w  . j  ava 2 s .  c o m*/
 *
 * @param data         the date of the segment
 * @param segmentId    the id of the segment
 * @return  the gc generation of this segment or 0 if this is bulk segment.
 */
public static int getGcGeneration(ByteBuffer data, UUID segmentId) {
    return isDataSegmentId(segmentId.getLeastSignificantBits()) ? data.getInt(GC_GENERATION_OFFSET) : 0;
}