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:edu.umn.cs.spatialHadoop.nasa.HDFRasterLayer.java

@Override
public void readFields(DataInput in) throws IOException {
    super.readFields(in);
    this.timestamp = in.readLong();
    int length = in.readInt();
    byte[] serializedData = new byte[length];
    in.readFully(serializedData);/*from  ww  w  .j av  a  2 s  . c o m*/
    ByteArrayInputStream bais = new ByteArrayInputStream(serializedData);
    GZIPInputStream gzis = new GZIPInputStream(bais);

    byte[] buffer = new byte[8];
    gzis.read(buffer);
    ByteBuffer bbuffer = ByteBuffer.wrap(buffer);
    int width = bbuffer.getInt();
    int height = bbuffer.getInt();
    // Reallocate memory only if needed
    if (width != this.getWidth() || height != this.getHeight()) {
        sum = new long[width][height];
        count = new long[width][height];
    }
    buffer = new byte[getHeight() * 2 * 8];
    for (int x = 0; x < getWidth(); x++) {
        int size = 0;
        while (size < buffer.length) {
            size += gzis.read(buffer, size, buffer.length - size);
        }
        bbuffer = ByteBuffer.wrap(buffer);
        for (int y = 0; y < getHeight(); y++) {
            sum[x][y] = bbuffer.getLong();
            count[x][y] = bbuffer.getLong();
        }
    }
}

From source file:AutoDJ.metaReader.OggIndexer.java

/**
 * Extracts the Image from a FLAC picture structure
 * http://flac.sourceforge.net/format.html#metadata_block_picture
 * Expects a base64 encoded string/*ww w .j  av  a 2  s . c  o m*/
 * 
 * @param String pictureBlock (base64)
 * @return BufferedImage
 */
BufferedImage readAlbumImage(String pictureBlock) {
    if (pictureBlock == null || pictureBlock.isEmpty())
        return null;

    byte[] pictureBytes = Base64.decodeBase64(pictureBlock);
    BufferedImage img = null;

    String mimeString = "", description = "";
    ByteBuffer picBuff = ByteBuffer.allocate(pictureBytes.length);
    picBuff.put(pictureBytes);
    picBuff.rewind();

    /*int picType = */picBuff.getInt(); // not interesting, discard

    // read the mime type string
    int mimeStrLength = picBuff.getInt();
    byte[] mimeBytes = new byte[mimeStrLength];
    picBuff.get(mimeBytes);
    mimeString = new String(mimeBytes);

    // read the string describing the image 
    int descStrLength = picBuff.getInt();
    byte[] descBytes = new byte[descStrLength];
    picBuff.get(descBytes);
    try {
        description = new String(descBytes, "UTF-8");
    } catch (Exception e) {
        e.printStackTrace();
    }

    // skip over some unnecessary information
    /*int picWidth  = */picBuff.getInt();
    /*int picHeight = */picBuff.getInt();
    /*int colDepth  = */picBuff.getInt();
    /*int idxColors = */picBuff.getInt();

    // read the image data
    int picDataLength = picBuff.getInt();
    byte[] picBytes = new byte[picDataLength];
    picBuff.get(picBytes);
    try {
        img = ImageIO.read(new ByteArrayInputStream(picBytes));
    } catch (Exception e) {
        e.printStackTrace();
    }

    return img;
}

From source file:shardableobjectids.ShardableObjectId.java

public ShardableObjectId(String s, boolean babble) {

    if (!isValid(s))
        throw new IllegalArgumentException("invalid ShardableObjectId [" + s + "]");

    if (babble)/*from  w  w  w .  j  a v  a  2s  .co  m*/
        s = babbleToMongod(s);

    byte b[];
    if (s.length() == 16) {
        b = Base64Mod.decode(s);
    } else {
        b = new byte[12];
        for (int i = 0; i < b.length; i++) {
            b[i] = (byte) Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16);
        }
    }

    ByteBuffer bb = ByteBuffer.wrap(b);
    _machine = bb.getInt();
    _time = bb.getInt();
    _inc = bb.getInt();
    _new = false;
}

From source file:org.jmangos.sniffer.network.model.impl.WOWNetworkChannel.java

@Override
public void onResiveClientData(final long frameNumber, final byte[] buffer) {
    final ByteBuffer bytebuf = ByteBuffer.wrap(buffer);
    long opcode = 0;
    long size = 0;
    byte[] data;/*from  www .j  ava2  s. co  m*/
    bytebuf.order(ByteOrder.LITTLE_ENDIAN);
    if (getChannelState().contains(State.AUTHED)) {
        final long header = bytebuf.getInt() & 0xFFFFFFFF;
        size = header >> 13;
        opcode = (header & 0x1FFF);
        data = new byte[(int) size];
        bytebuf.get(data);
    } else {
        size = bytebuf.getShort();
        opcode = bytebuf.getInt();
        data = new byte[(int) size - 4];
        bytebuf.get(data);
        // old (opcode == 0x1a72) | (opcode == 0x3f3)
        if ((opcode == 0x1aa1) | (opcode == 0x9f1)) {
            this.log.debug("Init cryptography system for channel {}", this.getChannelId());
            addChannelState(State.AUTHED);
            this.csPacketBuffer.getCrypt().init(this.woWKeyReader.getKey());
            this.scPacketBuffer.getCrypt().init(this.woWKeyReader.getKey());
        }
    }
    this.log.debug(String.format("Frame: %d; Resive packet %s Opcode: 0x%x\n", frameNumber, "CMSG", opcode));
    for (final PacketLogHandler logger : this.packetLoggers) {
        logger.onDecodePacket(this, Direction.CLIENT, (int) size, (int) opcode, data, (int) frameNumber);
    }
}

From source file:com.sm.store.utils.FileStore.java

private boolean checkSignature(FileChannel channel) throws IOException {
    ByteBuffer intBytes = ByteBuffer.allocate(OFFSET);
    if (channel.size() == 0) {
        intBytes.putInt(MAGIC);/*from   ww  w  .j  a  v a2s  .  c  o m*/
        intBytes.flip();
        channel.write(intBytes);
        return true;
    } else {
        channel.read(intBytes);
        intBytes.rewind();
        if (intBytes.getInt() != MAGIC)
            throw new StoreException("Header mismatch expect " + MAGIC + " read " + intBytes.getInt());
    }
    return true;
}

From source file:org.wso2.carbon.databridge.receiver.binary.internal.BinaryDataReceiver.java

private String processMessage(int messageType, byte[] message, OutputStream outputStream) {
    ByteBuffer byteBuffer = ByteBuffer.wrap(message);
    int sessionIdLength;
    String sessionId;//from ww w  . jav  a  2  s  .co  m

    switch (messageType) {
    case 0: //Login
        int userNameLength = byteBuffer.getInt();
        int passwordLength = byteBuffer.getInt();

        String userName = new String(message, 8, userNameLength);
        String password = new String(message, 8 + userNameLength, passwordLength);

        try {
            sessionId = dataBridgeReceiverService.login(userName, password);

            ByteBuffer buffer = ByteBuffer.allocate(5 + sessionId.length());
            buffer.put((byte) 2);
            buffer.putInt(sessionId.length());
            buffer.put(sessionId.getBytes(BinaryMessageConstants.DEFAULT_CHARSET));

            outputStream.write(buffer.array());
            outputStream.flush();
        } catch (Exception e) {
            try {
                sendError(e, outputStream);
            } catch (IOException e1) {
                log.error("Error while sending response for login message: " + e1.getMessage(), e1);
            }
        }
        break;
    case 1://Logout
        sessionIdLength = byteBuffer.getInt();
        sessionId = new String(message, 4, sessionIdLength);
        try {
            dataBridgeReceiverService.logout(sessionId);

            outputStream.write((byte) 0);
            outputStream.flush();
        } catch (Exception e) {
            try {
                sendError(e, outputStream);
            } catch (IOException e1) {
                log.error("Error while sending response for login message: " + e1.getMessage(), e1);
            }
        }
        break;
    case 2: //Publish
        sessionIdLength = byteBuffer.getInt();
        sessionId = new String(message, 4, sessionIdLength);
        try {
            dataBridgeReceiverService.publish(message, sessionId, BinaryEventConverter.getConverter());

            outputStream.write((byte) 0);
            outputStream.flush();
        } catch (Exception e) {
            try {
                sendError(e, outputStream);
            } catch (IOException e1) {
                log.error("Error while sending response for login message: " + e1.getMessage(), e1);
            }
        }
        break;
    default:
        log.error("Message Type " + messageType + " is not supported!");
    }
    return null;
}

From source file:org.jmangos.sniffer.network.model.impl.WOWNetworkChannel.java

@Override
synchronized public void onResiveServerData(final long frameNumber, final byte[] buffer) {
    final ByteBuffer bytebuf = ByteBuffer.wrap(buffer);
    bytebuf.order(ByteOrder.LITTLE_ENDIAN);
    long opcode = 0;
    long size = 0;
    byte[] data = null;
    if (getChannelState().contains(State.AUTHED)) {
        final long header = bytebuf.getInt() & 0xFFFFFFFF;
        size = header >> 13;/*w w w.  j  a v  a2s  .c  o  m*/
        opcode = header & 0x1FFF;
        data = new byte[(int) size];
        bytebuf.get(data);
    } else {
        size = bytebuf.getShort();
        opcode = bytebuf.getInt();
        bytebuf.mark();
        data = new byte[(int) size - 4];
        bytebuf.get(data);
        bytebuf.reset();
        // old 0xc0b
        if ((opcode == 0x221) & !getChannelState().contains(State.NOT_ACCEPT_SEED)) {
            this.log.debug("Get new Seed");
            final byte[] serverSeed = new byte[16];
            final byte[] clientSeed = new byte[16];
            for (int i = 0; i < 16; i++) {
                serverSeed[i] = bytebuf.get();
            }
            for (int i = 0; i < 16; i++) {
                clientSeed[i] = bytebuf.get();
            }
            bytebuf.get();
            this.csPacketBuffer.getCrypt().setEncryptionSeed(clientSeed);
            this.scPacketBuffer.getCrypt().setEncryptionSeed(serverSeed);
        }

    }
    this.log.debug(String.format("Frame: %d; Resive packet %s Opcode: 0x%x OpcodeSize: %d", frameNumber, "SMSG",
            opcode, size));
    for (final PacketLogHandler logger : this.packetLoggers) {
        logger.onDecodePacket(this, Direction.SERVER, (int) size, (int) opcode, data, (int) frameNumber);
    }
}

From source file:au.org.ala.delta.io.BinFile.java

public int readInt() {
    ByteBuffer b = readByteBuffer(4);
    _stats.ReadInt++;
    return b.getInt();
}

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

private void writeDataAndReadFromHFile(Path hfilePath, Algorithm compressAlgo, int entryCount,
        boolean findMidKey) throws IOException {

    HFileContext context = new HFileContextBuilder().withBlockSize(4096).withCompression(compressAlgo).build();
    HFileWriterV2 writer = (HFileWriterV2) new HFileWriterV2.WriterFactoryV2(conf, new CacheConfig(conf))
            .withPath(fs, hfilePath).withFileContext(context).create();

    Random rand = new Random(9713312); // Just a fixed seed.
    List<KeyValue> keyValues = new ArrayList<KeyValue>(entryCount);

    for (int i = 0; i < entryCount; ++i) {
        byte[] keyBytes = randomOrderedKey(rand, i);

        // A random-length random value.
        byte[] valueBytes = randomValue(rand);
        KeyValue keyValue = new KeyValue(keyBytes, null, null, valueBytes);
        writer.append(keyValue);//  w w  w  . j  a v  a 2 s .  c o  m
        keyValues.add(keyValue);
    }

    // Add in an arbitrary order. They will be sorted lexicographically by
    // the key.
    writer.appendMetaBlock("CAPITAL_OF_USA", new Text("Washington, D.C."));
    writer.appendMetaBlock("CAPITAL_OF_RUSSIA", new Text("Moscow"));
    writer.appendMetaBlock("CAPITAL_OF_FRANCE", new Text("Paris"));

    writer.close();

    FSDataInputStream fsdis = fs.open(hfilePath);

    // A "manual" version of a new-format HFile reader. This unit test was
    // written before the V2 reader was fully implemented.

    long fileSize = fs.getFileStatus(hfilePath).getLen();
    FixedFileTrailer trailer = FixedFileTrailer.readFromStream(fsdis, fileSize);

    assertEquals(2, trailer.getMajorVersion());
    assertEquals(entryCount, trailer.getEntryCount());

    HFileContext meta = new HFileContextBuilder().withHBaseCheckSum(true).withIncludesMvcc(false)
            .withIncludesTags(false).withCompression(compressAlgo).build();

    HFileBlock.FSReader blockReader = new HFileBlock.FSReaderV2(fsdis, fileSize, meta);
    // Comparator class name is stored in the trailer in version 2.
    KVComparator comparator = trailer.createComparator();
    HFileBlockIndex.BlockIndexReader dataBlockIndexReader = new HFileBlockIndex.BlockIndexReader(comparator,
            trailer.getNumDataIndexLevels());
    HFileBlockIndex.BlockIndexReader metaBlockIndexReader = new HFileBlockIndex.BlockIndexReader(
            KeyValue.RAW_COMPARATOR, 1);

    HFileBlock.BlockIterator blockIter = blockReader.blockRange(trailer.getLoadOnOpenDataOffset(),
            fileSize - trailer.getTrailerSize());
    // Data index. We also read statistics about the block index written after
    // the root level.
    dataBlockIndexReader.readMultiLevelIndexRoot(blockIter.nextBlockWithBlockType(BlockType.ROOT_INDEX),
            trailer.getDataIndexCount());

    if (findMidKey) {
        byte[] midkey = dataBlockIndexReader.midkey();
        assertNotNull("Midkey should not be null", midkey);
    }

    // Meta index.
    metaBlockIndexReader.readRootIndex(blockIter.nextBlockWithBlockType(BlockType.ROOT_INDEX).getByteStream(),
            trailer.getMetaIndexCount());
    // File info
    FileInfo fileInfo = new FileInfo();
    fileInfo.read(blockIter.nextBlockWithBlockType(BlockType.FILE_INFO).getByteStream());
    byte[] keyValueFormatVersion = fileInfo.get(HFileWriterV2.KEY_VALUE_VERSION);
    boolean includeMemstoreTS = keyValueFormatVersion != null && Bytes.toInt(keyValueFormatVersion) > 0;

    // Counters for the number of key/value pairs and the number of blocks
    int entriesRead = 0;
    int blocksRead = 0;
    long memstoreTS = 0;

    // Scan blocks the way the reader would scan them
    fsdis.seek(0);
    long curBlockPos = 0;
    while (curBlockPos <= trailer.getLastDataBlockOffset()) {
        HFileBlock block = blockReader.readBlockData(curBlockPos, -1, -1, false);
        assertEquals(BlockType.DATA, block.getBlockType());
        ByteBuffer buf = block.getBufferWithoutHeader();
        while (buf.hasRemaining()) {
            int keyLen = buf.getInt();
            int valueLen = buf.getInt();

            byte[] key = new byte[keyLen];
            buf.get(key);

            byte[] value = new byte[valueLen];
            buf.get(value);

            if (includeMemstoreTS) {
                ByteArrayInputStream byte_input = new ByteArrayInputStream(buf.array(),
                        buf.arrayOffset() + buf.position(), buf.remaining());
                DataInputStream data_input = new DataInputStream(byte_input);

                memstoreTS = WritableUtils.readVLong(data_input);
                buf.position(buf.position() + WritableUtils.getVIntSize(memstoreTS));
            }

            // A brute-force check to see that all keys and values are correct.
            assertTrue(Bytes.compareTo(key, keyValues.get(entriesRead).getKey()) == 0);
            assertTrue(Bytes.compareTo(value, keyValues.get(entriesRead).getValue()) == 0);

            ++entriesRead;
        }
        ++blocksRead;
        curBlockPos += block.getOnDiskSizeWithHeader();
    }
    LOG.info("Finished reading: entries=" + entriesRead + ", blocksRead=" + blocksRead);
    assertEquals(entryCount, entriesRead);

    // Meta blocks. We can scan until the load-on-open data offset (which is
    // the root block index offset in version 2) because we are not testing
    // intermediate-level index blocks here.

    int metaCounter = 0;
    while (fsdis.getPos() < trailer.getLoadOnOpenDataOffset()) {
        LOG.info("Current offset: " + fsdis.getPos() + ", scanning until " + trailer.getLoadOnOpenDataOffset());
        HFileBlock block = blockReader.readBlockData(curBlockPos, -1, -1, false);
        assertEquals(BlockType.META, block.getBlockType());
        Text t = new Text();
        ByteBuffer buf = block.getBufferWithoutHeader();
        if (Writables.getWritable(buf.array(), buf.arrayOffset(), buf.limit(), t) == null) {
            throw new IOException(
                    "Failed to deserialize block " + this + " into a " + t.getClass().getSimpleName());
        }
        Text expectedText = (metaCounter == 0 ? new Text("Paris")
                : metaCounter == 1 ? new Text("Moscow") : new Text("Washington, D.C."));
        assertEquals(expectedText, t);
        LOG.info("Read meta block data: " + t);
        ++metaCounter;
        curBlockPos += block.getOnDiskSizeWithHeader();
    }

    fsdis.close();
}

From source file:au.org.ala.delta.io.BinaryKeyFile.java

public List<Integer> readIntegerList(int recordNum, int numInts) {
    seek(recordOffset(recordNum));//from w  ww  . j  a va  2 s.c o  m
    ByteBuffer bb = readByteBuffer(numInts * SIZE_INT_IN_BYTES);

    List<Integer> retList = new ArrayList<Integer>();
    for (int i = 0; i < numInts; i++) {
        retList.add(bb.getInt());
    }

    return retList;
}