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();

Source Link

Document

Returns the int at the current position and increases the position by 4.

Usage

From source file:org.broadinstitute.sting.utils.io.IOUtils.java

/**
 * Determines the uncompressed size of a GZIP file. Uses the GZIP ISIZE field in the last
 * 4 bytes of the file to get this information.
 *
 * @param gzipFile GZIP-format file whose uncompressed size to determine
 * @return The uncompressed size (in bytes) of the GZIP file
 *///from   ww w.j  av a 2s  .c  o  m
public static int getGZIPFileUncompressedSize(File gzipFile) {
    if (gzipFile == null) {
        throw new ReviewedStingException("GZIP file to examine was null");
    }

    try {
        // The GZIP ISIZE field holds the uncompressed size of the compressed data.
        // It occupies the last 4 bytes of any GZIP file:
        RandomAccessFile in = new RandomAccessFile(gzipFile, "r");
        in.seek(gzipFile.length() - 4);
        byte[] sizeBytes = new byte[4];
        in.read(sizeBytes, 0, 4);

        ByteBuffer byteBuf = ByteBuffer.wrap(sizeBytes);
        byteBuf.order(ByteOrder.LITTLE_ENDIAN); // The GZIP spec mandates little-endian byte order
        int uncompressedSize = byteBuf.getInt();

        // If the size read in is negative, we've overflowed our signed integer:
        if (uncompressedSize < 0) {
            throw new UserException.CouldNotReadInputFile(String.format(
                    "Cannot accurately determine the uncompressed size of file %s "
                            + "because it's either larger than %d bytes or the GZIP ISIZE field is corrupt",
                    gzipFile.getAbsolutePath(), Integer.MAX_VALUE));
        }

        return uncompressedSize;
    } catch (IOException e) {
        throw new UserException.CouldNotReadInputFile(gzipFile, e);
    }
}

From source file:edu.umass.cs.gigapaxos.paxospackets.PaxosPacket.java

/**
 * @param bytes/* w w  w. j  av  a  2 s  .  c o m*/
 * @return True if first 8 bytes correspond to a legitimate PaxosPacketType.
 */
public static boolean isParseable(byte[] bytes) {
    ByteBuffer bbuf = ByteBuffer.wrap(bytes, 0, 8);
    return bbuf.getInt() == PaxosPacketType.PAXOS_PACKET.getInt()
            && PaxosPacketType.getPaxosPacketType(bbuf.getInt()) != null;
}

From source file:edu.umass.cs.gigapaxos.paxospackets.PaxosPacket.java

/**
 * @param bytes//from w w  w . j a  v  a 2  s  . co  m
 * @return PaxosPacketType if parseable as bytes.
 */
public static PaxosPacketType getType(byte[] bytes) {
    PaxosPacketType type = null;
    ByteBuffer bbuf = ByteBuffer.wrap(bytes, 0, 8);
    return bbuf.getInt() == PaxosPacketType.PAXOS_PACKET.getInt()
            && (type = PaxosPacketType.getPaxosPacketType(bbuf.getInt())) != null ? type : null;
}

From source file:org.midonet.netlink.rtnetlink.Link.java

public static Link buildFrom(ByteBuffer buf) {
    try {//from  w  w  w.  j a  va 2s . co m
        Link link = new Link();
        link.ifi.family = buf.get();
        buf.get(); // pad
        link.ifi.type = buf.getShort();
        link.ifi.index = buf.getInt();
        link.ifi.flags = buf.getInt();
        link.ifi.change = buf.getInt();
        NetlinkMessage.scanAttributes(buf, link);
        return link;
    } catch (BufferUnderflowException ex) {
        return null;
    }
}

From source file:org.apache.hadoop.hbase.io.hfile.FixedFileTrailer.java

/**
 * Reads a file trailer from the given file.
 *
 * @param istream the input stream with the ability to seek. Does not have to
 *          be buffered, as only one read operation is made.
 * @param fileSize the file size. Can be obtained using
 *          {@link org.apache.hadoop.fs.FileSystem#getFileStatus(
 *          org.apache.hadoop.fs.Path)}.
 * @return the fixed file trailer read//w ww .j  a va  2  s  .c  o  m
 * @throws IOException if failed to read from the underlying stream, or the
 *           trailer is corrupted, or the version of the trailer is
 *           unsupported
 */
public static FixedFileTrailer readFromStream(FSDataInputStream istream, long fileSize) throws IOException {
    int bufferSize = MAX_TRAILER_SIZE;
    long seekPoint = fileSize - bufferSize;
    if (seekPoint < 0) {
        // It is hard to imagine such a small HFile.
        seekPoint = 0;
        bufferSize = (int) fileSize;
    }

    istream.seek(seekPoint);
    ByteBuffer buf = ByteBuffer.allocate(bufferSize);
    istream.readFully(buf.array(), buf.arrayOffset(), buf.arrayOffset() + buf.limit());

    // Read the version from the last int of the file.
    buf.position(buf.limit() - Bytes.SIZEOF_INT);
    int version = buf.getInt();

    // Extract the major and minor versions.
    //version ??major version??minor version
    int majorVersion = extractMajorVersion(version);
    int minorVersion = extractMinorVersion(version);

    HFile.checkFormatVersion(majorVersion); // throws IAE if invalid

    int trailerSize = getTrailerSize(majorVersion);

    FixedFileTrailer fft = new FixedFileTrailer(majorVersion, minorVersion);
    fft.deserialize(new DataInputStream(
            new ByteArrayInputStream(buf.array(), buf.arrayOffset() + bufferSize - trailerSize, trailerSize)));
    return fft;
}

From source file:mil.nga.giat.geowave.datastore.accumulo.AccumuloDataStore.java

protected static GeowaveRowId getRowIdObject(final byte[] row) {
    final byte[] metadata = Arrays.copyOfRange(row, row.length - 12, row.length);
    final ByteBuffer metadataBuf = ByteBuffer.wrap(metadata);
    final int adapterIdLength = metadataBuf.getInt();
    final int dataIdLength = metadataBuf.getInt();
    final int numberOfDuplicates = metadataBuf.getInt();

    final ByteBuffer buf = ByteBuffer.wrap(row, 0, row.length - 12);
    final byte[] indexId = new byte[row.length - 12 - adapterIdLength - dataIdLength];
    final byte[] adapterId = new byte[adapterIdLength];
    final byte[] dataId = new byte[dataIdLength];
    buf.get(indexId);//from  w w w.  java 2 s.com
    buf.get(adapterId);
    buf.get(dataId);
    return new GeowaveRowId(indexId, dataId, adapterId, numberOfDuplicates);
}

From source file:org.grouplens.lenskit.data.dao.packed.BinaryIndexTable.java

/**
 * Create a binary index table.// w w  w.java2s .c  om
 * @param nentries The number of entries in the table.
 * @param buffer The table buffer.  Its position will be advanced to the end of the table.
 * @return The index table.
 */
public static BinaryIndexTable fromBuffer(int nentries, ByteBuffer buffer) {
    logger.debug("reading table of {} entries", nentries);
    long[] keys = new long[nentries];
    int[] offsets = new int[nentries];
    int[] sizes = new int[nentries];
    int nextExpectedOffset = 0;
    for (int i = 0; i < nentries; i++) {
        keys[i] = buffer.getLong();
        if (i > 0 && keys[i - 1] >= keys[i]) {
            logger.error("key {} is not greater than previous key {}", keys[i], keys[i - 1]);
            throw new IllegalArgumentException("corrupted index table");
        }
        offsets[i] = buffer.getInt();
        sizes[i] = buffer.getInt();
        if (offsets[i] != nextExpectedOffset) {
            logger.error("expected offset {}, got {}", nextExpectedOffset, offsets[i]);
            throw new IllegalArgumentException("corrupted index table");
        }
        nextExpectedOffset += sizes[i];
    }
    if (buffer.remaining() < nextExpectedOffset) {
        throw new IllegalArgumentException("buffer not large enough");
    }
    int end = buffer.position() + nextExpectedOffset * 4;
    ByteBuffer dup = buffer.duplicate();
    dup.limit(end);
    buffer.position(end);
    LongKeyDomain dom = LongKeyDomain.wrap(keys, keys.length, true);
    return new BinaryIndexTable(dom, offsets, sizes, dup.asIntBuffer());
}

From source file:org.carbondata.query.util.CacheUtil.java

private static Member[][] populateMemberCache(DataInputStream fileChannel, CarbonFile memberFile,
        String fileName, String dataType) throws IOException {
    // ByteBuffer toltalLength, memberLength, surrogateKey, bf3;
    // subtracted 4 as last 4 bytes will have the max value for no of
    // records//from  ww w  . ja  va  2 s .c  om
    long currPositionIndex = 0;
    long size = memberFile.getSize() - 4;
    long skipSize = size;
    long actualSkipSize = 0;
    while (actualSkipSize != size) {
        actualSkipSize += fileChannel.skip(skipSize);
        skipSize = skipSize - actualSkipSize;
    }
    //        LOGGER.debug(CarbonEngineLogEvent.UNIBI_CARBONENGINE_MSG, "Bytes skipped " +
    // skipSize);
    int maxVal = fileChannel.readInt();
    CarbonUtil.closeStreams(fileChannel);
    fileChannel = FileFactory.getDataInputStream(fileName, FileFactory.getFileType(fileName));
    // CHECKSTYLE:OFF Approval No:Approval-V1R2C10_005
    ByteBuffer buffer = ByteBuffer.allocate((int) size);
    // CHECKSTYLE:OFF
    fileChannel.readFully(buffer.array());
    int minVal = buffer.getInt();
    int totalArraySize = maxVal - minVal + 1;
    Member[][] surogateKeyArrays = null;
    if (totalArraySize > CarbonCommonConstants.LEVEL_ARRAY_SIZE) {
        int div = totalArraySize / CarbonCommonConstants.LEVEL_ARRAY_SIZE;
        int rem = totalArraySize % CarbonCommonConstants.LEVEL_ARRAY_SIZE;
        if (rem > 0) {
            div++;
        }
        surogateKeyArrays = new Member[div][];

        for (int i = 0; i < div - 1; i++) {
            surogateKeyArrays[i] = new Member[CarbonCommonConstants.LEVEL_ARRAY_SIZE];
        }

        if (rem > 0) {
            surogateKeyArrays[surogateKeyArrays.length - 1] = new Member[rem];
        } else {
            surogateKeyArrays[surogateKeyArrays.length
                    - 1] = new Member[CarbonCommonConstants.LEVEL_ARRAY_SIZE];
        }
    } else {
        surogateKeyArrays = new Member[1][totalArraySize];
    }
    //        Member[] surogateKeyArrays = new Member[maxVal-minVal+1];
    //        int surrogateKeyIndex = minVal;
    currPositionIndex += 4;
    //
    int current = 0;
    // CHECKSTYLE:OFF Approval No:Approval-V1R2C10_005
    boolean enableEncoding = Boolean
            .valueOf(CarbonProperties.getInstance().getProperty(CarbonCommonConstants.ENABLE_BASE64_ENCODING,
                    CarbonCommonConstants.ENABLE_BASE64_ENCODING_DEFAULT));
    // CHECKSTYLE:ON
    int index = 0;
    int prvArrayIndex = 0;
    while (currPositionIndex < size) {
        int len = buffer.getInt();
        // CHECKSTYLE:OFF Approval No:Approval-V1R2C10_005
        // CHECKSTYLE:ON
        currPositionIndex += 4;
        byte[] rowBytes = new byte[len];
        buffer.get(rowBytes);
        currPositionIndex += len;
        // No:Approval-361
        if (enableEncoding) {
            rowBytes = Base64.decodeBase64(rowBytes);
        }
        surogateKeyArrays[current / CarbonCommonConstants.LEVEL_ARRAY_SIZE][index] = new Member(rowBytes);
        current++;
        if (current / CarbonCommonConstants.LEVEL_ARRAY_SIZE > prvArrayIndex) {
            prvArrayIndex++;
            index = 0;
        } else {
            index++;
        }
    }
    return surogateKeyArrays;
}

From source file:org.wso2.carbon.analytics.datasource.core.util.GenericUtils.java

public static Map<String, Object> decodeRecordValues(byte[] data, Set<String> columns)
        throws AnalyticsException {
    /* using LinkedHashMap to retain the column order */
    Map<String, Object> result = new LinkedHashMap<>();
    int type, size;
    String colName;// w w w.  j  a  va2s  .com
    Object value;
    byte[] buff;
    byte boolVal;
    byte[] binData;
    try {
        ByteBuffer buffer = ByteBuffer.wrap(data);
        while (buffer.remaining() > 0) {
            size = buffer.getInt();
            if (size == 0) {
                break;
            }
            buff = new byte[size];
            buffer.get(buff, 0, size);
            colName = new String(buff, StandardCharsets.UTF_8);
            type = buffer.get();
            switch (type) {
            case DATA_TYPE_STRING:
                size = buffer.getInt();
                buff = new byte[size];
                buffer.get(buff, 0, size);
                value = new String(buff, StandardCharsets.UTF_8);
                break;
            case DATA_TYPE_LONG:
                value = buffer.getLong();
                break;
            case DATA_TYPE_DOUBLE:
                value = buffer.getDouble();
                break;
            case DATA_TYPE_BOOLEAN:
                boolVal = buffer.get();
                if (boolVal == BOOLEAN_TRUE) {
                    value = true;
                } else if (boolVal == BOOLEAN_FALSE) {
                    value = false;
                } else {
                    throw new AnalyticsException("Invalid encoded boolean value: " + boolVal);
                }
                break;
            case DATA_TYPE_INTEGER:
                value = buffer.getInt();
                break;
            case DATA_TYPE_FLOAT:
                value = buffer.getFloat();
                break;
            case DATA_TYPE_BINARY:
                size = buffer.getInt();
                binData = new byte[size];
                buffer.get(binData);
                value = binData;
                break;
            case DATA_TYPE_OBJECT:
                size = buffer.getInt();
                binData = new byte[size];
                buffer.get(binData);
                value = GenericUtils.deserializeObject(binData);
                break;
            case DATA_TYPE_NULL:
                value = null;
                break;
            default:
                throw new AnalyticsException("Unknown encoded data source type : " + type);
            }
            if (columns == null || columns.contains(colName)) {
                result.put(colName, value);
            }
        }
    } catch (Exception e) {
        throw new AnalyticsException("Error in decoding record values: " + e.getMessage(), e);
    }
    return result;
}

From source file:org.lenskit.data.packed.BinaryIndexTable.java

/**
 * Create a binary index table./*from www . jav a  2 s  .  c  o  m*/
 * @param nentries The number of entries in the table.
 * @param buffer The table buffer.  Its position will be advanced to the end of the table.
 * @return The index table.
 */
public static BinaryIndexTable fromBuffer(int nentries, ByteBuffer buffer) {
    logger.debug("reading table of {} entries", nentries);
    long[] keys = new long[nentries];
    int[] offsets = new int[nentries];
    int[] sizes = new int[nentries];
    // Read the index table's header (IDs, offsets, and counts/sizes).
    int nextExpectedOffset = 0;
    for (int i = 0; i < nentries; i++) {
        keys[i] = buffer.getLong();
        if (i > 0 && keys[i - 1] >= keys[i]) {
            logger.error("key {} is not greater than previous key {}", keys[i], keys[i - 1]);
            throw new IllegalArgumentException("corrupted index table");
        }
        offsets[i] = buffer.getInt();
        sizes[i] = buffer.getInt();
        if (offsets[i] != nextExpectedOffset) {
            logger.error("expected offset {}, got {}", nextExpectedOffset, offsets[i]);
            throw new IllegalArgumentException("corrupted index table");
        }
        nextExpectedOffset += sizes[i];
    }

    // Set up the integer store
    if (buffer.remaining() < nextExpectedOffset * 4) {
        throw new IllegalArgumentException("buffer not large enough");
    }
    int end = buffer.position() + nextExpectedOffset * 4;
    ByteBuffer dup = buffer.duplicate();
    dup.limit(end);
    // update input indexStore's position
    buffer.position(end);
    // create index table object
    LongKeyDomain dom = LongKeyDomain.wrap(keys, keys.length, true);
    return new BinaryIndexTable(dom, offsets, sizes, dup.asIntBuffer());
}