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:com.openteach.diamond.network.waverider.network.Packet.java

/**
 * ByteBuffer??/*from   ww w.j  a v  a2 s .  com*/
 * @param buffer
 * @return
 */
public static Packet unmarshall(ByteBuffer buffer) {
    if (buffer.remaining() < getHeaderSize()) {
        throw new RuntimeException("Wrong packet.");
    }

    Packet packet = new Packet();
    byte[] str = new byte[NetWorkConstants.WAVERIDER_MAGIC.getBytes().length];
    buffer.get(str);
    packet.setMagic(new String(str));

    if (!NetWorkConstants.WAVERIDER_MAGIC.equals(packet.getMagic())) {
        throw new RuntimeException("Wrong packet.");
    }

    packet.setSequence(buffer.getLong());
    packet.setType(buffer.getLong());
    packet.setLength(buffer.getInt());
    packet.setPayLoad(buffer.slice());
    return packet;
}

From source file:org.apache.hadoop.hbase.filter.SlicedRowFilter.java

public static SlicedRowFilter parseFrom(final byte[] pbBytes) throws DeserializationException {

    ByteBuffer bb = ByteBuffer.wrap(pbBytes).order(ByteOrder.BIG_ENDIAN);

    SlicedRowFilter filter = new SlicedRowFilter();

    filter.count = bb.getLong();//from   ww  w.  j a va 2 s.  c o  m
    filter.slicesLength = bb.getInt();
    int nbounds = bb.getInt();
    filter.bounds = new int[nbounds];
    for (int i = 0; i < nbounds; i++) {
        filter.bounds[i] = bb.getInt();
    }

    //
    // If the first slice starts at offset 0 then we will be able to provide a key hint
    //

    if (0 == filter.bounds[0]) {
        filter.hasHinting = true;
    } else {
        filter.hasHinting = false;
    }

    filter.rangekeys = new byte[bb.getInt()];
    bb.get(filter.rangekeys);

    filter.slice = new byte[filter.slicesLength];

    return filter;
}

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

/**
 * creates the appropriate ContentImpl for the given blob.
 */// w  ww. j  av  a 2  s.  com
private static ContentImpl parseContent(OleBlobImpl blob) throws IOException {
    ByteBuffer bb = PageChannel.wrap(blob.getBytes());

    if ((bb.remaining() < 2) || (bb.getShort() != PACKAGE_SIGNATURE)) {
        return new UnknownContentImpl(blob);
    }

    // read outer package header
    int headerSize = bb.getShort();
    int objType = bb.getInt();
    int prettyNameLen = bb.getShort();
    int classNameLen = bb.getShort();
    int prettyNameOff = bb.getShort();
    int classNameOff = bb.getShort();
    int objSize = bb.getInt();
    String prettyName = readStr(bb, prettyNameOff, prettyNameLen);
    String className = readStr(bb, classNameOff, classNameLen);
    bb.position(headerSize);

    // read ole header
    int oleVer = bb.getInt();
    int format = bb.getInt();

    if (oleVer != OLE_VERSION) {
        return new UnknownContentImpl(blob);
    }

    int typeNameLen = bb.getInt();
    String typeName = readStr(bb, bb.position(), typeNameLen);
    bb.getLong(); // unused
    int dataBlockLen = bb.getInt();
    int dataBlockPos = bb.position();

    if (SIMPLE_PACKAGE_TYPE.equalsIgnoreCase(typeName)) {
        return createSimplePackageContent(blob, prettyName, className, typeName, bb, dataBlockLen);
    }

    // if COMPOUND_FACTORY is null, the poi library isn't available, so just
    // load compound data as "other"
    if ((COMPOUND_FACTORY != null) && (bb.remaining() >= COMPOUND_STORAGE_SIGNATURE.length)
            && ByteUtil.matchesRange(bb, bb.position(), COMPOUND_STORAGE_SIGNATURE)) {
        return COMPOUND_FACTORY.createCompoundPackageContent(blob, prettyName, className, typeName, bb,
                dataBlockLen);
    }

    // this is either some other "special" (as yet unhandled) format, or it is
    // simply an embedded file (or it is compound data and poi isn't available)
    return new OtherContentImpl(blob, prettyName, className, typeName, dataBlockPos, dataBlockLen);
}

From source file:com.eventsourcing.layout.binary.ByteArrayBinaryDeserializer.java

@Override
public Object deserialize(ByteArrayTypeHandler typeHandler, ByteBuffer buffer) {
    int len = buffer.getInt();
    byte[] bytes = new byte[len];
    buffer.get(bytes);//from  w w w  . j a  va  2 s . c  o  m
    if (typeHandler.isPrimitive()) {
        return bytes;
    } else {
        return toObject(bytes);
    }
}

From source file:io.ecarf.core.utils.Utils.java

/**
 * Get the likely uncompressed size of a Gziped file
 * @param filename//from  www.j  a v  a2s.  co  m
 * @return
 * @throws IOException 
 * @see http://stackoverflow.com/questions/27825927/get-gzipped-file-attributes-like-gzip-l-basically-compression-ratio
 * @throws FileNotFoundException 
 */
public static long getUncompressedFileSize(String filename) throws FileNotFoundException, IOException {

    File f = new File(filename);
    try (RandomAccessFile ra = new RandomAccessFile(f, "r"); FileChannel channel = ra.getChannel()) {

        MappedByteBuffer fileBuffer = channel.map(MapMode.READ_ONLY, f.length() - 4, 4);
        fileBuffer.load();

        ByteBuffer buf = ByteBuffer.allocate(4);
        buf.order(ByteOrder.LITTLE_ENDIAN);

        buf.put(fileBuffer);
        buf.flip();
        //will print the uncompressed size
        //getInt() reads the 4 bytes as a int
        // if the file is between 2GB and 4GB
        // then this will return a negative value
        //and you'll have to do your own converting to an unsigned int
        int size = buf.getInt();

        if (size < 0) {
            return FileUtils.ONE_GB + size;
        } else {
            return size;
        }
    }
}

From source file:io.warp10.continuum.gts.GTSDecoder.java

public static GTSDecoder fromBlock(byte[] block, byte[] key) throws IOException {

    if (block.length < 6) {
        throw new IOException("Invalid block.");
    }/* ww  w  .  j  a v a  2 s .c  o m*/

    ByteBuffer buffer = ByteBuffer.wrap(block);

    //
    // Extract size
    //

    buffer.order(ByteOrder.BIG_ENDIAN);
    int size = buffer.getInt();

    // Check size

    if (block.length != size) {
        throw new IOException("Invalid block size, expected " + size + ", block is " + block.length);
    }

    // Extract compression

    byte comp = buffer.get();

    boolean compress = false;

    if (0 == comp) {
        compress = false;
    } else if (1 == comp) {
        compress = true;
    } else {
        throw new IOException("Invalid compression flag");
    }

    // Extract base timestamp

    long base = Varint.decodeSignedLong(buffer);

    InputStream in;

    ByteArrayInputStream bain = new ByteArrayInputStream(block, buffer.position(), buffer.remaining());

    if (compress) {
        in = new GZIPInputStream(bain);
    } else {
        in = bain;
    }

    byte[] buf = new byte[1024];

    ByteArrayOutputStream out = new ByteArrayOutputStream(buffer.remaining());

    while (true) {
        int len = in.read(buf);

        if (len <= 0) {
            break;
        }
        out.write(buf, 0, len);
    }

    GTSDecoder decoder = new GTSDecoder(base, key, ByteBuffer.wrap(out.toByteArray()));

    return decoder;
}

From source file:org.apache.jackrabbit.oak.plugins.segment.file.TarReader.java

/**
 * Tries to read an existing index from the given tar file. The index is
 * returned if it is found and looks valid (correct checksum, passes
 * sanity checks).//from  w  w w. j ava 2 s .  c o  m
 *
 * @param file tar file
 * @param name name of the tar file, for logging purposes
 * @return tar index, or {@code null} if not found or not valid
 * @throws IOException if the tar file could not be read
 */
private static ByteBuffer loadAndValidateIndex(RandomAccessFile file, String name) throws IOException {
    long length = file.length();
    if (length % BLOCK_SIZE != 0 || length < 6 * BLOCK_SIZE || length > Integer.MAX_VALUE) {
        log.warn("Unexpected size {} of tar file {}", length, name);
        return null; // unexpected file size
    }

    // read the index metadata just before the two final zero blocks
    ByteBuffer meta = ByteBuffer.allocate(16);
    file.seek(length - 2 * BLOCK_SIZE - 16);
    file.readFully(meta.array());
    int crc32 = meta.getInt();
    int count = meta.getInt();
    int bytes = meta.getInt();
    int magic = meta.getInt();

    if (magic != INDEX_MAGIC) {
        return null; // magic byte mismatch
    }

    if (count < 1 || bytes < count * 24 + 16 || bytes % BLOCK_SIZE != 0) {
        log.warn("Invalid index metadata in tar file {}", name);
        return null; // impossible entry and/or byte counts
    }

    // this involves seeking backwards in the file, which might not
    // perform well, but that's OK since we only do this once per file
    ByteBuffer index = ByteBuffer.allocate(count * 24);
    file.seek(length - 2 * BLOCK_SIZE - 16 - count * 24);
    file.readFully(index.array());
    index.mark();

    CRC32 checksum = new CRC32();
    long limit = length - 2 * BLOCK_SIZE - bytes - BLOCK_SIZE;
    long lastmsb = Long.MIN_VALUE;
    long lastlsb = Long.MIN_VALUE;
    byte[] entry = new byte[24];
    for (int i = 0; i < count; i++) {
        index.get(entry);
        checksum.update(entry);

        ByteBuffer buffer = ByteBuffer.wrap(entry);
        long msb = buffer.getLong();
        long lsb = buffer.getLong();
        int offset = buffer.getInt();
        int size = buffer.getInt();

        if (lastmsb > msb || (lastmsb == msb && lastlsb > lsb)) {
            log.warn("Incorrect index ordering in tar file {}", name);
            return null;
        } else if (lastmsb == msb && lastlsb == lsb && i > 0) {
            log.warn("Duplicate index entry in tar file {}", name);
            return null;
        } else if (offset < 0 || offset % BLOCK_SIZE != 0) {
            log.warn("Invalid index entry offset in tar file {}", name);
            return null;
        } else if (size < 1 || offset + size > limit) {
            log.warn("Invalid index entry size in tar file {}", name);
            return null;
        }

        lastmsb = msb;
        lastlsb = lsb;
    }

    if (crc32 != (int) checksum.getValue()) {
        log.warn("Invalid index checksum in tar file {}", name);
        return null; // checksum mismatch
    }

    index.reset();
    return index;
}

From source file:com.dreamworks.dsp.server.EmbeddedSftpServer.java

private BigInteger decodeBigInt(ByteBuffer bb) {
    int len = bb.getInt();
    byte[] bytes = new byte[len];
    bb.get(bytes);//w  w  w  . j av a  2  s.  com
    return new BigInteger(bytes);
}

From source file:org.cosmo.common.file.VariableFilePartition.java

public int readSize(long position) throws IOException {
    ByteBuffer redBuf = read(4, position);
    return redBuf.getInt();
}

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

/**
 * Tries to read an existing index from the given tar file. The index is
 * returned if it is found and looks valid (correct checksum, passes
 * sanity checks)./* w  w w .j a  va 2  s  .  c om*/
 *
 * @param file tar file
 * @param name name of the tar file, for logging purposes
 * @return tar index, or {@code null} if not found or not valid
 * @throws IOException if the tar file could not be read
 */
private static ByteBuffer loadAndValidateIndex(RandomAccessFile file, String name) throws IOException {
    long length = file.length();
    if (length % BLOCK_SIZE != 0 || length < 6 * BLOCK_SIZE || length > Integer.MAX_VALUE) {
        log.warn("Unexpected size {} of tar file {}", length, name);
        return null; // unexpected file size
    }

    // read the index metadata just before the two final zero blocks
    ByteBuffer meta = ByteBuffer.allocate(16);
    file.seek(length - 2 * BLOCK_SIZE - 16);
    file.readFully(meta.array());
    int crc32 = meta.getInt();
    int count = meta.getInt();
    int bytes = meta.getInt();
    int magic = meta.getInt();

    if (magic != INDEX_MAGIC) {
        return null; // magic byte mismatch
    }

    if (count < 1 || bytes < count * TarEntry.SIZE + 16 || bytes % BLOCK_SIZE != 0) {
        log.warn("Invalid index metadata in tar file {}", name);
        return null; // impossible entry and/or byte counts
    }

    // this involves seeking backwards in the file, which might not
    // perform well, but that's OK since we only do this once per file
    ByteBuffer index = ByteBuffer.allocate(count * TarEntry.SIZE);
    file.seek(length - 2 * BLOCK_SIZE - 16 - count * TarEntry.SIZE);
    file.readFully(index.array());
    index.mark();

    CRC32 checksum = new CRC32();
    long limit = length - 2 * BLOCK_SIZE - bytes - BLOCK_SIZE;
    long lastmsb = Long.MIN_VALUE;
    long lastlsb = Long.MIN_VALUE;
    byte[] entry = new byte[TarEntry.SIZE];
    for (int i = 0; i < count; i++) {
        index.get(entry);
        checksum.update(entry);

        ByteBuffer buffer = wrap(entry);
        long msb = buffer.getLong();
        long lsb = buffer.getLong();
        int offset = buffer.getInt();
        int size = buffer.getInt();

        if (lastmsb > msb || (lastmsb == msb && lastlsb > lsb)) {
            log.warn("Incorrect index ordering in tar file {}", name);
            return null;
        } else if (lastmsb == msb && lastlsb == lsb && i > 0) {
            log.warn("Duplicate index entry in tar file {}", name);
            return null;
        } else if (offset < 0 || offset % BLOCK_SIZE != 0) {
            log.warn("Invalid index entry offset in tar file {}", name);
            return null;
        } else if (size < 1 || offset + size > limit) {
            log.warn("Invalid index entry size in tar file {}", name);
            return null;
        }

        lastmsb = msb;
        lastlsb = lsb;
    }

    if (crc32 != (int) checksum.getValue()) {
        log.warn("Invalid index checksum in tar file {}", name);
        return null; // checksum mismatch
    }

    index.reset();
    return index;
}