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.apache.hadoop.hbase.io.hfile.TestHFileWriterV3.java

private void writeDataAndReadFromHFile(Path hfilePath, Algorithm compressAlgo, int entryCount,
        boolean findMidKey, boolean useTags) throws IOException {
    HFileContext context = new HFileContextBuilder().withBlockSize(4096).withIncludesTags(useTags)
            .withCompression(compressAlgo).build();
    HFileWriterV3 writer = (HFileWriterV3) new HFileWriterV3.WriterFactoryV3(conf, new CacheConfig(conf))
            .withPath(fs, hfilePath).withFileContext(context).withComparator(KeyValue.COMPARATOR).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 = TestHFileWriterV2.randomOrderedKey(rand, i);

        // A random-length random value.
        byte[] valueBytes = TestHFileWriterV2.randomValue(rand);
        KeyValue keyValue = null;
        if (useTags) {
            ArrayList<Tag> tags = new ArrayList<Tag>();
            for (int j = 0; j < 1 + rand.nextInt(4); j++) {
                byte[] tagBytes = new byte[16];
                rand.nextBytes(tagBytes);
                tags.add(new Tag((byte) 1, tagBytes));
            }/*from   w w w  . ja  v  a 2 s  .  com*/
            keyValue = new KeyValue(keyBytes, null, null, HConstants.LATEST_TIMESTAMP, valueBytes, tags);
        } else {
            keyValue = new KeyValue(keyBytes, null, null, HConstants.LATEST_TIMESTAMP, valueBytes);
        }
        writer.append(keyValue);
        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);

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

    assertEquals(3, trailer.getMajorVersion());
    assertEquals(entryCount, trailer.getEntryCount());
    HFileContext meta = new HFileContextBuilder().withCompression(compressAlgo).withIncludesMvcc(false)
            .withIncludesTags(useTags).withHBaseCheckSum(true).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(HFileWriterV3.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();
        int keyLen = -1;
        while (buf.hasRemaining()) {

            keyLen = buf.getInt();

            int valueLen = buf.getInt();

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

            byte[] value = new byte[valueLen];
            buf.get(value);
            byte[] tagValue = null;
            if (useTags) {
                int tagLen = buf.getShort();
                tagValue = new byte[tagLen];
                buf.get(tagValue);
            }

            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);
            if (useTags) {
                assertNotNull(tagValue);
                KeyValue tkv = keyValues.get(entriesRead);
                assertEquals(tagValue.length, tkv.getTagsLength());
                assertTrue(Bytes.compareTo(tagValue, 0, tagValue.length, tkv.getTagsArray(),
                        tkv.getTagsOffset(), tkv.getTagsLength()) == 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:org.brekka.phalanx.core.services.impl.AbstractCryptoService.java

private InternalPrivateKeyToken decodePrivateKey(byte[] encoded, UUID idOfData, CryptoProfile cryptoProfile) {
    ByteBuffer buffer = ByteBuffer.wrap(encoded);
    byte[] marker = new byte[PK_MAGIC_MARKER.length];
    buffer.get(marker);//from w  w  w.j a v  a  2  s . c  o  m
    if (!Arrays.equals(PK_MAGIC_MARKER, marker)) {
        throw new PhalanxException(PhalanxErrorCode.CP208,
                "CryptoData item '%s' does not appear to contain a private key", idOfData);
    }
    int profileId = buffer.getInt();
    CryptoProfile profile = cryptoProfileService.retrieveProfile(profileId);
    byte[] data = new byte[encoded.length - (SK_MAGIC_MARKER.length + 4)];
    buffer.get(data);

    PrivateKey privateKey = phoenixAsymmetric.toPrivateKey(data, profile);
    return new InternalPrivateKeyToken(privateKey);
}

From source file:org.brekka.phalanx.core.services.impl.AbstractCryptoService.java

private InternalSecretKeyToken decodeSecretKey(byte[] encoded, UUID idOfData) {
    ByteBuffer buffer = ByteBuffer.wrap(encoded);
    byte[] marker = new byte[SK_MAGIC_MARKER.length];
    buffer.get(marker);//from  w ww .  j  a  v a  2 s  .c o m
    if (!Arrays.equals(SK_MAGIC_MARKER, marker)) {
        throw new PhalanxException(PhalanxErrorCode.CP213,
                "CryptoData item '%s' does not appear to contain a secret key", idOfData);
    }
    int profileId = buffer.getInt();
    byte[] keyId = new byte[16];
    buffer.get(keyId);
    UUID symCryptoDataId = toUUID(keyId);
    CryptoProfile cryptoProfile = cryptoProfileService.retrieveProfile(profileId);
    byte[] data = new byte[encoded.length - (SK_MAGIC_MARKER.length + 20)];
    buffer.get(data);

    SecretKey secretKey = phoenixSymmetric.toSecretKey(data, cryptoProfile);
    SymedCryptoData symedCryptoData = new SymedCryptoData();
    symedCryptoData.setId(symCryptoDataId);
    symedCryptoData.setProfile(profileId);
    return new InternalSecretKeyToken(secretKey, symedCryptoData);
}

From source file:it.anyplace.sync.discovery.protocol.ld.LocalDiscorveryHandler.java

public void startListener() {
    listeningExecutorService.submit(new Runnable() {

        private final int incomingBufferSize = 1024; //TODO check this

        @Override/*from w w w . j a v  a 2 s  .co  m*/
        public void run() {
            try {
                if (datagramSocket == null || datagramSocket.isClosed()) {
                    datagramSocket = new DatagramSocket(UDP_PORT, InetAddress.getByName("0.0.0.0"));
                    logger.info("opening upd socket {}", datagramSocket.getLocalSocketAddress());
                }
                final DatagramPacket datagramPacket = new DatagramPacket(new byte[incomingBufferSize],
                        incomingBufferSize);
                logger.trace("waiting for message on socket addr = {}", datagramSocket.getLocalSocketAddress());
                isListening = true;
                datagramSocket.receive(datagramPacket);
                processingExecutorService.submit(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            final String sourceAddress = datagramPacket.getAddress().getHostAddress();
                            ByteBuffer byteBuffer = ByteBuffer.wrap(datagramPacket.getData(),
                                    datagramPacket.getOffset(), datagramPacket.getLength());
                            int magic = byteBuffer.getInt(); //4 bytes
                            checkArgument(magic == MAGIC, "magic mismatch, expected %s, got %s", MAGIC, magic);
                            final LocalDiscoveryProtos.Announce announce = LocalDiscoveryProtos.Announce
                                    .parseFrom(ByteString.copyFrom(byteBuffer));
                            final String deviceId = hashDataToDeviceIdString(announce.getId().toByteArray());
                            if (!equal(deviceId, configuration.getDeviceId())) {
                                //                                logger.debug("received local announce from device id = {}", deviceId);
                                final List<DeviceAddress> deviceAddresses = Lists
                                        .newArrayList(Iterables.transform(
                                                firstNonNull(announce.getAddressesList(),
                                                        Collections.<String>emptyList()),
                                                new Function<String, DeviceAddress>() {
                                                    @Override
                                                    public DeviceAddress apply(String address) {
                                                        //                                /*
                                                        //                                When interpreting addresses with an unspecified address, e.g., tcp://0.0.0.0:22000 or tcp://:42424, the source address of the discovery announcement is to be used.
                                                        //                                 */
                                                        return DeviceAddress.newBuilder()
                                                                .setAddress(address.replaceFirst(
                                                                        "tcp://(0.0.0.0|):",
                                                                        "tcp://" + sourceAddress + ":"))
                                                                .setDeviceId(deviceId)
                                                                .setInstanceId(announce.getInstanceId())
                                                                .setProducer(
                                                                        DeviceAddress.AddressProducer.LOCAL_DISCOVERY)
                                                                .build();
                                                    }
                                                }));
                                boolean isNew = false;
                                synchronized (localDiscoveryRecords) {
                                    isNew = !localDiscoveryRecords.removeAll(deviceId).isEmpty();
                                    localDiscoveryRecords.putAll(deviceId, deviceAddresses);
                                }
                                eventBus.post(new MessageReceivedEvent() {
                                    @Override
                                    public List<DeviceAddress> getDeviceAddresses() {
                                        return Collections.unmodifiableList(deviceAddresses);
                                    }

                                    @Override
                                    public String getDeviceId() {
                                        return deviceId;
                                    }
                                });
                                if (isNew) {
                                    eventBus.post(new NewLocalPeerEvent() {
                                        @Override
                                        public String getDeviceId() {
                                            return deviceId;
                                        }
                                    });
                                }
                            }
                        } catch (Exception ex) {
                            logger.warn("error processing datagram", ex);
                        }
                    }
                });
                listeningExecutorService.submit(this);
            } catch (Exception ex) {
                isListening = false;
                if (listeningExecutorService.isShutdown()) {
                    return;
                }
                if (datagramSocket != null) {
                    logger.warn("error receiving datagram", ex);
                    listeningExecutorService.submit(this);
                } else if (ex instanceof java.net.BindException) {
                    logger.warn("error opening udp server socket : {}", ex.toString());
                } else {
                    logger.warn("error opening udp server socket", ex);
                }
            }
        }
    });
}

From source file:org.apache.hadoop.hbase.io.encoding.EncodedDataBlock.java

/**
 * Do the encoding, but do not cache the encoded data.
 * @return encoded data block with header and checksum
 *///from  www . j  a va 2 s  .  c  om
public byte[] encodeData() {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        baos.write(HConstants.HFILEBLOCK_DUMMY_HEADER);
        DataOutputStream out = new DataOutputStream(baos);
        this.dataBlockEncoder.startBlockEncoding(encodingCtx, out);
        ByteBuffer in = getUncompressedBuffer();
        in.rewind();
        int klength, vlength;
        short tagsLength = 0;
        long memstoreTS = 0L;
        KeyValue kv = null;
        while (in.hasRemaining()) {
            int kvOffset = in.position();
            klength = in.getInt();
            vlength = in.getInt();
            ByteBufferUtils.skip(in, klength + vlength);
            if (this.meta.isIncludesTags()) {
                tagsLength = in.getShort();
                ByteBufferUtils.skip(in, tagsLength);
            }
            if (this.meta.isIncludesMvcc()) {
                memstoreTS = ByteBufferUtils.readVLong(in);
            }
            kv = new KeyValue(in.array(), kvOffset,
                    (int) KeyValue.getKeyValueDataStructureSize(klength, vlength, tagsLength));
            kv.setMvccVersion(memstoreTS);
            this.dataBlockEncoder.encode(kv, encodingCtx, out);
        }
        BufferGrabbingByteArrayOutputStream stream = new BufferGrabbingByteArrayOutputStream();
        baos.writeTo(stream);
        this.dataBlockEncoder.endBlockEncoding(encodingCtx, out, stream.buf);
    } catch (IOException e) {
        throw new RuntimeException(String.format("Bug in encoding part of algorithm %s. "
                + "Probably it requested more bytes than are available.", toString()), e);
    }
    return baos.toByteArray();
}

From source file:org.opendaylight.controller.protocol_plugin.openflow.vendorextension.v6extension.V6StatsReply.java

@Override
public void readFrom(ByteBuffer data) {
    short i;/*from   w w w  .  ja  v  a  2s  .  com*/
    this.length = data.getShort();
    if (length < MINIMUM_LENGTH)
        return; //TBD - Spurious Packet?
    this.tableId = data.get();
    data.get(); // pad
    this.durationSeconds = data.getInt();
    this.durationNanoseconds = data.getInt();
    this.priority = data.getShort();
    this.idleTimeout = data.getShort();
    this.hardTimeout = data.getShort();
    this.match_len = data.getShort();
    this.idleAge = data.getShort();
    this.hardAge = data.getShort();
    this.cookie = data.getLong();
    this.packetCount = data.getLong();
    this.byteCount = data.getLong();
    if (this.length == MINIMUM_LENGTH) {
        return; //TBD - can this happen??
    }
    if (this.match == null)
        this.match = new V6Match();
    ByteBuffer mbuf = ByteBuffer.allocate(match_len);
    for (i = 0; i < match_len; i++) {
        mbuf.put(data.get());
    }
    mbuf.rewind();
    this.match.readFrom(mbuf);
    if (this.actionFactory == null)
        throw new RuntimeException("OFActionFactory not set");
    /*
     * action list may be preceded by a padding of 0 to 7 bytes based upon this:
     */
    short pad_size = (short) (((match_len + 7) / 8) * 8 - match_len);
    for (i = 0; i < pad_size; i++)
        data.get();
    int action_len = this.length - MINIMUM_LENGTH - (match_len + pad_size);
    if (action_len > 0)
        this.actions = this.actionFactory.parseActions(data, action_len);
}

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

protected IndexImpl(ByteBuffer tableBuffer, List<IndexData> indexDatas, JetFormat format) throws IOException {

    ByteUtil.forward(tableBuffer, format.SKIP_BEFORE_INDEX_SLOT); //Forward past Unknown
    _indexNumber = tableBuffer.getInt();
    int indexDataNumber = tableBuffer.getInt();

    // read foreign key reference info
    byte relIndexType = tableBuffer.get();
    int relIndexNumber = tableBuffer.getInt();
    int relTablePageNumber = tableBuffer.getInt();
    byte cascadeUpdatesFlag = tableBuffer.get();
    byte cascadeDeletesFlag = tableBuffer.get();

    _indexType = tableBuffer.get();/*from  w ww.j  av a  2  s.c  o  m*/

    if ((_indexType == FOREIGN_KEY_INDEX_TYPE) && (relIndexNumber != INVALID_INDEX_NUMBER)) {
        _reference = new ForeignKeyReference(relIndexType, relIndexNumber, relTablePageNumber,
                (cascadeUpdatesFlag == CASCADE_UPDATES_FLAG), (cascadeDeletesFlag == CASCADE_DELETES_FLAG));
    } else {
        _reference = null;
    }

    ByteUtil.forward(tableBuffer, format.SKIP_AFTER_INDEX_SLOT); //Skip past Unknown

    _data = indexDatas.get(indexDataNumber);

    _data.addIndex(this);
}

From source file:org.zuinnote.hadoop.office.format.common.parser.msexcel.internal.EncryptedCachedDiskStringsTable.java

/**
 * // w  w w  .  j a  va 2  s  .c  om
 * Gets a String from cache or underlying encrypted/compressed file
 * 
 * @param index
 * @return
 * @throws IOException
 */

private String getString(int index) throws IOException {
    // check if it is in cache?
    if (this.cache.containsKey(index)) {
        return this.cache.get(index);
    }
    // if not we have to read it from the file
    long itemPosition = this.stringPositionInFileList.get(index);
    String result = null;
    if (this.tempRAF == null) {
        this.accessTempFile(itemPosition);
        byte[] readSize = new byte[4];
        this.in.read(readSize);
        int sizeOfString = ByteBuffer.wrap(readSize).getInt();
        byte[] strbytes = new byte[sizeOfString];
        this.in.read(strbytes);
        this.currentPos += readSize.length + strbytes.length;
        result = new String(strbytes, EncryptedCachedDiskStringsTable.encoding);
    } else {
        FileChannel fc = this.tempRAF.getChannel().position(itemPosition);
        ByteBuffer bb = ByteBuffer.allocate(4);
        // read size of String
        fc.read(bb);
        bb.flip();
        int sizeOfStr = bb.getInt();
        // read string
        bb = ByteBuffer.allocate(sizeOfStr);
        fc.read(bb);
        bb.flip();
        result = new String(bb.array(), EncryptedCachedDiskStringsTable.encoding);
    }
    if (this.cacheSize != 0) {
        this.cache.put(index, result);
    }
    return result;
}

From source file:org.energy_home.jemma.osgi.ah.io.flexgateway.FlexGatewayButtons.java

public void run() {
    long tv_sec;/*from w ww. j a  v a 2 s  .com*/
    long tv_usec;
    short type;
    short code;
    int value;
    while (true) {
        buttonFile = new File(flexGatewayButtonsDevice);
        ByteBuffer buffer = ByteBuffer.allocate(100);
        buffer.order(ByteOrder.nativeOrder());
        try {
            fis = new FileInputStream(buttonFile);
            channel = fis.getChannel();
            while (true) {
                buffer.clear();
                int size = channel.read(buffer);
                buffer.rewind();

                while (size > 0) {
                    tv_sec = buffer.getInt();
                    tv_usec = buffer.getInt();
                    long tv = tv_sec * 1000000 + tv_usec;
                    type = buffer.getShort();
                    code = buffer.getShort();
                    value = buffer.getInt();
                    size -= 16;

                    if (type == 0 && code == 0 && value == 0)
                        continue;

                    // Code 3 -> front button
                    // Code 2 -> back button
                    // Value > 0 -> button pressed
                    // Value > 0 -> button released

                    // send event
                    log.debug("Button: ms " + tv + " type " + (type & 0xffff) + " code " + (code & 0xffff)
                            + " value " + (value & 0xffffffff));

                    Hashtable props = new Hashtable();
                    props.put("timestamp", new Long(tv));
                    if (value > 0)
                        this.postEvent("button/" + code + "/UP", props);
                    else
                        this.postEvent("button/" + code + "/DOWN", props);
                }
            }
        } catch (ClosedByInterruptException e) {
            break;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            log.error("exception", e);
            break;
        } finally {
            try {
                if (channel != null)
                    channel.close();
            } catch (IOException e) {
                log.error("exception", e);
                break;
            }
        }
    }
    log.debug("exiting");
}

From source file:com.mellanox.jxio.EventQueueHandler.java

private String readString(ByteBuffer buf) {
    int len = buf.getInt();
    byte b[] = new byte[len];
    buf.get(b, 0, len);/*  w  w w  . ja  v a  2s  .c  o m*/
    String s1 = new String(b, Charset.forName("US-ASCII"));
    return s1;
}