Example usage for java.nio ByteBuffer putInt

List of usage examples for java.nio ByteBuffer putInt

Introduction

In this page you can find the example usage for java.nio ByteBuffer putInt.

Prototype

public abstract ByteBuffer putInt(int value);

Source Link

Document

Writes the given int to the current position and increases the position by 4.

Usage

From source file:voldemort.store.cachestore.impl.ChannelStore.java

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

From source file:com.tongbanjie.tarzan.rpc.protocol.RpcCommand.java

public ByteBuffer encode() throws RpcCommandException {
    /******* ? *******/
    // 1> protocol type size
    int length = Protocol.PROTOCOL_TYPE_SIZE;

    // 2> header length size
    length += Protocol.HEADER_LENGTH_SIZE;

    // 3> header data length
    byte[] headerData = this.headerEncode();
    length += headerData.length;//from   w  w w  .ja v  a 2s . co  m

    // 4> body data length
    if (this.body != null) {
        length += body.length;
    }

    /******* ByteBuffer *******/
    //?
    ByteBuffer result = ByteBuffer.allocate(Protocol.TOTAL_LENGTH_SIZE + length);

    // 0?length
    result.putInt(length);

    // 1?protocol type
    result.put(markProtocolType(serializeType));

    // 2?header length
    result.putInt(headerData.length);

    // 3?header data
    result.put(headerData);

    // 4?body data;
    if (this.body != null) {
        result.put(this.body);
    }

    result.flip();

    return result;
}

From source file:de.hpi.fgis.hdrs.Triple.java

public static void writeTriple(ByteBuffer buffer, Triple t) {
    buffer.putShort(t.getSubjectLength());
    buffer.putShort(t.getPredicateLength());
    buffer.putInt(t.getObjectLength());
    buffer.putInt(t.getMultiplicity());//from w  w  w. java 2  s . c  o  m
    if (0 < t.getSubjectLength())
        buffer.put(t.getBuffer(), t.getOffset(), t.getSubjectLength());
    if (0 < t.getPredicateLength())
        buffer.put(t.getBuffer(), t.getPredicateOffset(), t.getPredicateLength());
    if (0 < t.getObjectLength())
        buffer.put(t.getBuffer(), t.getObjectOffset(), t.getObjectLength());
}

From source file:bamboo.openhash.fileshare.FileShare.java

public void write() {
    logger.debug("write");

    if (is != null) {
        while (ready.size() < MAX_BUFFER) {
            ByteBuffer bb = ByteBuffer.wrap(new byte[1024]);
            bb.putInt(0);
            int len = 0;
            try {
                len = is.read(bb.array(), 4, bb.limit() - 4);
            } catch (IOException e) {
                is = null;//  w w w . j a v  a 2  s  . c om
                break;
            }
            if (len == -1) {
                is = null;
                break;
            }
            logger.debug("position=" + bb.position() + " read " + len + " bytes");
            // We're going to flip this later, so set the position
            // where we want the limit to end up.
            bb.position(len + 4);
            wblocks.elementAt(0).addLast(bb);
            logger.debug("read a block");
            if (wblocks.elementAt(0).size() == BRANCHING)
                make_parents(false);
        }
        if (is == null) {
            make_parents(true);
            // There should now be only one non-empty level, at it
            // should have exactly one block in it.
            for (int l = 0; l < wblocks.size(); ++l) {
                if (!wblocks.elementAt(l).isEmpty()) {
                    ByteBuffer bb = wblocks.elementAt(l).removeFirst();
                    bb.flip();
                    md.update(secret);
                    md.update(bb.array(), 0, bb.limit());
                    byte[] dig = md.digest();
                    StringBuffer sb = new StringBuffer(100);
                    bytes_to_sbuf(dig, 0, dig.length, false, sb);
                    logger.info("root digest is 0x" + sb.toString());
                    ready.addLast(new Pair<byte[], ByteBuffer>(dig, bb));
                    break;
                }
            }
        }
    }

    // Do put.

    if (ready.isEmpty()) {
        if (outstanding == 0) {
            logger.info("all puts finished successfully");
            System.exit(0);
        }
    } else {
        Pair<byte[], ByteBuffer> head = ready.removeFirst();
        outstanding++;

        bamboo_put_args put = new bamboo_put_args();
        put.application = APPLICATION;
        // GatewayClient will fill in put.client_library
        put.value = new bamboo_value();
        if (head.second.limit() == head.second.array().length)
            put.value.value = head.second.array();
        else {
            put.value.value = new byte[head.second.limit()];
            head.second.get(put.value.value);
        }
        put.key = new bamboo_key();
        put.key.value = head.first;
        put.ttl_sec = 3600; // TODO

        StringBuffer sb = new StringBuffer(100);
        bytes_to_sbuf(head.first, 0, head.first.length, false, sb);
        logger.debug("putting block size=" + put.value.value.length + " key=0x" + sb.toString());
        client.put(put, curry(put_done_cb, put));
    }
}

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

public void writeInts(int[] values) {
    ByteBuffer b = ByteBuffer.allocate(values.length * 4);
    b.order(ByteOrder.LITTLE_ENDIAN);
    for (int value : values) {
        b.putInt(value);
    }/*w w  w.  java 2 s .  c  o  m*/
    writeBytes(b);
}

From source file:net.dv8tion.jda.audio.AudioWebSocket.java

private InetSocketAddress handleUdpDiscovery(InetSocketAddress address, int ssrc) {
    //We will now send a packet to discord to punch a port hole in the NAT wall.
    //This is called UDP hole punching.
    try {//from   www.j  a  v a2s. co  m
        udpSocket = new DatagramSocket(); //Use UDP, not TCP.

        //Create a byte array of length 70 containing our ssrc.
        ByteBuffer buffer = ByteBuffer.allocate(70); //70 taken from https://github.com/Rapptz/discord.py/blob/async/discord/voice_client.py#L208
        buffer.putInt(ssrc); //Put the ssrc that we were given into the packet to send back to discord.

        //Construct our packet to be sent loaded with the byte buffer we store the ssrc in.
        DatagramPacket discoveryPacket = new DatagramPacket(buffer.array(), buffer.array().length, address);
        udpSocket.send(discoveryPacket);

        //Discord responds to our packet, returning a packet containing our external ip and the port we connected through.
        DatagramPacket receivedPacket = new DatagramPacket(new byte[70], 70); //Give a buffer the same size as the one we sent.
        udpSocket.setSoTimeout(1000);
        udpSocket.receive(receivedPacket);

        //The byte array returned by discord containing our external ip and the port that we used
        //to connect to discord with.
        byte[] received = receivedPacket.getData();

        //Example string:"   121.83.253.66                                                   "
        //You'll notice that there are 4 leading nulls and a large amount of nulls between the the ip and
        // the last 2 bytes. Not sure why these exist.  The last 2 bytes are the port. More info below.
        String ourIP = new String(receivedPacket.getData());//Puts the entire byte array in. nulls are converted to spaces.
        ourIP = ourIP.substring(0, ourIP.length() - 2); //Removes the port that is stuck on the end of this string. (last 2 bytes are the port)
        ourIP = ourIP.trim(); //Removes the extra whitespace(nulls) attached to both sides of the IP

        //The port exists as the last 2 bytes in the packet data, and is encoded as an UNSIGNED short.
        //Furthermore, it is stored in Little Endian instead of normal Big Endian.
        //We will first need to convert the byte order from Little Endian to Big Endian (reverse the order)
        //Then we will need to deal with the fact that the bytes represent an unsigned short.
        //Java cannot deal with unsigned types, so we will have to promote the short to a higher type.
        //Options:  char or int.  I will be doing int because it is just easier to work with.
        byte[] portBytes = new byte[2]; //The port is exactly 2 bytes in size.
        portBytes[0] = received[received.length - 1]; //Get the second byte and store as the first
        portBytes[1] = received[received.length - 2]; //Get the first byte and store as the second.
        //We have now effectively converted from Little Endian -> Big Endian by reversing the order.

        //For more information on how this is converting from an unsigned short to an int refer to:
        //http://www.darksleep.com/player/JavaAndUnsignedTypes.html
        int firstByte = (0x000000FF & ((int) portBytes[0])); //Promotes to int and handles the fact that it was unsigned.
        int secondByte = (0x000000FF & ((int) portBytes[1])); //

        //Combines the 2 bytes back together.
        int ourPort = (firstByte << 8) | secondByte;

        this.address = address;

        return new InetSocketAddress(ourIP, ourPort);
    } catch (SocketException e) {
        return null;
    } catch (IOException e) {
        LOG.log(e);
        return null;
    }
}

From source file:com.tongbanjie.tarzan.rpc.protocol.RpcCommand.java

public ByteBuffer encodeHeader(final int bodyLength) throws RpcCommandException {
    /******* ? *******/
    // 1> protocol type size
    int length = Protocol.PROTOCOL_TYPE_SIZE;

    // 2> header length size
    length += Protocol.HEADER_LENGTH_SIZE;

    // 3> header data length
    byte[] headerData;
    headerData = this.headerEncode();
    length += headerData.length;//from www  . jav a  2s  .co m

    // 4> body data length
    length += bodyLength;

    /******* ByteBuffer *******/
    //??body
    ByteBuffer result = ByteBuffer.allocate(Protocol.TOTAL_LENGTH_SIZE + length - bodyLength);

    // 0?length
    result.putInt(length);

    // 1?protocol type
    result.put(markProtocolType(serializeType));

    // 2?header length
    result.putInt(headerData.length);

    // 3?header data
    result.put(headerData);

    result.flip();

    return result;
}

From source file:edu.cmu.cylab.starslinger.transaction.WebEngine.java

/**
 * get a file, based on the retrieval id from the server
 * //from  w w  w .  ja  v a  2 s  . co  m
 * @throws MessageNotFoundException
 */
public byte[] getFile(byte[] msgHashBytes) throws ExchangeException, MessageNotFoundException {

    ByteBuffer msg = ByteBuffer.allocate(mVersionLen //
            + 4 + msgHashBytes.length //
    );
    msg.putInt(mVersion);
    msg.putInt(msgHashBytes.length);
    msg.put(msgHashBytes);

    byte[] resp = doPost(mUrlPrefix + mHost + "/getFile" + mUrlSuffix, msg.array());

    resp = handleResponseExceptions(resp, 0);

    return resp;
}

From source file:edu.cmu.cylab.starslinger.transaction.WebEngine.java

/**
 * get a message meta-data, based on the retrieval id from the server
 * // w ww  .j a v  a 2  s .  co  m
 * @throws MessageNotFoundException
 */
public byte[] getMessage(byte[] msgHashBytes) throws ExchangeException, MessageNotFoundException {

    ByteBuffer msg = ByteBuffer.allocate(mVersionLen //
            + 4 + msgHashBytes.length //
    );
    msg.putInt(mVersion);
    msg.putInt(msgHashBytes.length);
    msg.put(msgHashBytes);

    byte[] resp = doPost(mUrlPrefix + mHost + "/getMessage" + mUrlSuffix, msg.array());

    resp = handleResponseExceptions(resp, 0);

    return resp;
}

From source file:org.commoncrawl.service.listcrawler.HDFSFileIndex.java

public static void writeIndex(Vector<FingerprintAndOffsetTuple> offsetInfo, DataOutput indexFileOut)
        throws IOException {

    long firstFingerprint = offsetInfo.get(0)._fingerprint;

    BloomFilter bloomFilter = new BloomFilter(offsetInfo.size(), 0.001201);

    // sort the offset list by fingerprint 
    Collections.sort(offsetInfo, new Comparator<FingerprintAndOffsetTuple>() {

        @Override/*from   ww w  .  j  av  a 2  s .  co  m*/
        public int compare(FingerprintAndOffsetTuple o1, FingerprintAndOffsetTuple o2) {
            return (o1._fingerprint < o2._fingerprint) ? -1 : o1._fingerprint > o2._fingerprint ? 1 : 0;
        }

    });
    // now we need to write the index out

    // allocate working set buffers ...
    ByteBuffer indexDataBuffer = ByteBuffer.allocate(offsetInfo.size() * 16);
    ByteBuffer indexHintsBuffer = ByteBuffer
            .allocate(((((offsetInfo.size() + INDEX_HINT_RECORD_INTERVAL) / INDEX_HINT_RECORD_INTERVAL) + 1)
                    * INDEX_HINT_SIZE) + 4);

    // build index hints placeholder 
    Vector<HDFSFileIndex.IndexItem> hints = new Vector<HDFSFileIndex.IndexItem>();
    // 0 100 200 300 400 500
    for (int i = 0; i < offsetInfo.size(); ++i) {

        if (i % INDEX_HINT_RECORD_INTERVAL == 0 || (i == (offsetInfo.size() - 1))) {
            HDFSFileIndex.IndexItem hint = new IndexItem(offsetInfo.get(i)._fingerprint,
                    (int) offsetInfo.get(i)._offset);
            hints.add(hint);
            // add fingerprint to bloom filter 
            bloomFilter.add(hint.fingerprint);
        }
    }
    // start off the index hints buffer with a hint of the index hint buffer size 
    indexHintsBuffer.putInt(hints.size());

    // track total bits used ... 
    int bitsUsedForHints = 0;
    int bitsUsedForFingerprints = 0;
    int bitsUsedForOffsets = 0;

    // now start populating index data ... 
    for (int hintIdx = 0; hintIdx < hints.size(); ++hintIdx) {

        HDFSFileIndex.IndexItem hint = hints.get(hintIdx);

        LOG.info("IndexWriter FP:" + hint.fingerprint);
        indexHintsBuffer.putLong(hint.fingerprint);
        indexHintsBuffer.putInt(hint.dataOffset);
        indexHintsBuffer.putInt(indexDataBuffer.position());

        // update stats 
        bitsUsedForHints += INDEX_HINT_SIZE * 8;

        if (hintIdx < hints.size() - 1) {
            // track cumilative delta and offset values (for average calc later)
            double cumilativeDelta = 0;
            long cumilativeOffset = 0;

            int subIndexItemCount = 0;
            int nonZeroDeltaCount = 0;

            Vector<HDFSFileIndex.IndexItem> subHints = new Vector<HDFSFileIndex.IndexItem>();

            // initialize last fingerprint to indexed value ... 
            long lastFingerprint = hint.fingerprint;

            // first collect values in between index hints
            for (int nonIndexItem = (hintIdx * INDEX_HINT_RECORD_INTERVAL) + 1; nonIndexItem < ((hintIdx + 1)
                    * INDEX_HINT_RECORD_INTERVAL); ++nonIndexItem) {
                if (nonIndexItem >= offsetInfo.size())
                    break;

                // calculdate fingerprint delta ... 
                long fingerprintDelta = offsetInfo.get(nonIndexItem)._fingerprint - lastFingerprint;
                LOG.info("IndexWriter FP:" + offsetInfo.get(nonIndexItem)._fingerprint + " Delta:"
                        + fingerprintDelta);
                // offset delta

                if (fingerprintDelta != 0) {

                    cumilativeDelta += (double) fingerprintDelta;
                    LOG.info("Cumilative Delta is:" + cumilativeDelta);
                    nonZeroDeltaCount++;
                }

                cumilativeOffset += offsetInfo.get(nonIndexItem)._offset;

                ++subIndexItemCount;

                // add to collection vector 
                subHints.add(new IndexItem(fingerprintDelta, (int) offsetInfo.get(nonIndexItem)._offset));

                // remember the last fingerpint ... 
                lastFingerprint = offsetInfo.get(nonIndexItem)._fingerprint;

                // add item to bloom filter
                bloomFilter.add(lastFingerprint);
            }

            // calculate average delta value 
            double averageDeltaValue = (double) cumilativeDelta / (double) nonZeroDeltaCount;
            // calculate m for fingerprint deltas 
            int mForFingerprints = (int) Math.floor(lg(averageDeltaValue));
            LOG.info("Average Delta Value is:" + averageDeltaValue + " m is:" + mForFingerprints);
            // cacluldate average offset value 
            double averageOffsetValue = (double) cumilativeOffset / (double) subIndexItemCount;
            // calculate m for offsets 
            int mForOffsets = (int) Math.floor(lg(averageOffsetValue));

            // calculate rice codes
            RiceCoding riceCodeFP = new RiceCoding(mForFingerprints);
            RiceCoding riceCodeOffsets = new RiceCoding(mForOffsets);

            // populate bits 
            for (HDFSFileIndex.IndexItem subItemHint : subHints) {
                if (subItemHint.fingerprint == 0) {
                    LOG.warn("Zero Delta for Fingerprint Detected.There are two duplicate entires in log!");
                }
                riceCodeFP.addItem(subItemHint.fingerprint + 1);
                riceCodeOffsets.addItem(subItemHint.dataOffset + 1);
            }
            // now track bits used ... 
            bitsUsedForFingerprints += riceCodeFP.getNumBits();
            bitsUsedForOffsets += riceCodeOffsets.getNumBits();

            // write out metadata 

            // save the current position 
            int currentPosition = indexDataBuffer.position();

            // fingerprint data 
            indexDataBuffer.put((byte) mForFingerprints);
            CacheManager.writeVLongToByteBuffer(indexDataBuffer, riceCodeFP.getNumBits());
            indexDataBuffer.put(riceCodeFP.getBits(), 0, (riceCodeFP.getNumBits() + 7) / 8);

            // offset data 
            indexDataBuffer.put((byte) mForOffsets);
            CacheManager.writeVLongToByteBuffer(indexDataBuffer, riceCodeOffsets.getNumBits());
            indexDataBuffer.put(riceCodeOffsets.getBits(), 0, (riceCodeOffsets.getNumBits() + 7) / 8);

            System.out.println("Item Count:" + subIndexItemCount + "FP Bits:" + subIndexItemCount * 64
                    + " Compressed:" + riceCodeFP.getNumBits() + " Offset Bits:" + subIndexItemCount * 32
                    + " Compressed:" + riceCodeOffsets.getNumBits());

            LOG.info("Item Count:" + subIndexItemCount + "FP Bits:" + subIndexItemCount * 64 + " Compressed:"
                    + riceCodeFP.getNumBits() + " Offset Bits:" + subIndexItemCount * 32 + " Compressed:"
                    + riceCodeOffsets.getNumBits());

            if ((subIndexItemCount * 64) < riceCodeFP.getNumBits()) {
                throw new RuntimeException("Compressed Size > UnCompressed Size!!!!");
            }

            validateIndexData(indexDataBuffer.array(), currentPosition, hint.fingerprint, subHints,
                    bloomFilter);
        }

    }

    if (!bloomFilter.isPresent(firstFingerprint)) {
        throw new RuntimeException("Test Failed!");
    }

    // serialize bloomfilter
    ByteStream baos = new ByteStream(1 << 12);
    BloomFilter.serializer().serialize(bloomFilter, new DataOutputStream(baos));

    // spit out final stats 
    System.out.println(" Bloomfilter Size:" + baos.size() + " IndexHintBuffer Size:"
            + indexHintsBuffer.position() + " IndexDataBuffer Size:" + indexDataBuffer.position());

    // now write out the final index file ... 

    // bloom filter data ... 
    indexFileOut.write(baos.getBuffer(), 0, baos.size());
    // write hint data  
    indexFileOut.write(indexHintsBuffer.array(), 0, indexHintsBuffer.position());
    // write out rice code data size 
    indexFileOut.writeInt(indexDataBuffer.position());
    // finally rice coded sub-index data
    indexFileOut.write(indexDataBuffer.array(), 0, indexDataBuffer.position());
}