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:au.org.ala.delta.intkey.model.IntkeyDatasetFileReader.java

/**
 * Read a list of integers from the current pointer location in the supplied
 * binary file//ww  w .  j  a  v  a 2s  . c o  m
 * 
 * @param bFile
 *            the binary (characters or taxa) file
 * @param numInts
 *            the number of integers to read
 * @return the list of integers
 */
private static List<Integer> readIntegerList(BinFile bFile, int numInts) {
    ByteBuffer bb = bFile.readByteBuffer(numInts * Constants.SIZE_INT_IN_BYTES);

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

From source file:com.sonymobile.android.media.internal.VUParser.java

protected boolean parseODSMData(IsoTrack odsmTrack) {
    int kObjectSize = 11;
    SampleTable sampleTable = odsmTrack.getSampleTable();
    if (sampleTable.getSampleCount() > 1) {
        // TODO: Should multiple entries be supported?
        return false;
    }/*  www .j  av a2  s.c  o  m*/
    mSinfList = new ArrayList<SinfData>(2);

    ByteBuffer stszData = sampleTable.getStszData();
    stszData.rewind();
    stszData.getInt(); // version and flags

    int dataSize = stszData.getInt();
    if (dataSize == 0) {
        stszData.getInt(); // sample_count
        dataSize = stszData.getInt();
    }

    byte[] data = new byte[dataSize];
    try {
        ByteBuffer stcoData = sampleTable.getStcoData();
        stcoData.rewind();

        stcoData.getInt(); // version and flags
        stcoData.getInt(); // entry_count

        long sampleOffset = 0;
        if (sampleTable.isUsingLongChunkOffsets()) {
            sampleOffset = stcoData.getLong();
        } else {
            sampleOffset = 0xFFFFFFFFL & stcoData.getInt();
        }

        mDataSource.readAt(sampleOffset, data, dataSize);
        ByteBuffer dataBuffer = ByteBuffer.wrap(data);
        byte updateTag = dataBuffer.get();
        if (updateTag != 1) {
            return false;
        }
        int size = 0;
        int sizePart = 0;
        do {
            sizePart = (dataBuffer.get() & 0xFF);
            size = ((size << 7) & 0xFFFFFF80) | (sizePart & 0x7F);
        } while (sizePart > 128);
        while (size >= kObjectSize) {
            byte descriptorTag = dataBuffer.get();
            if (descriptorTag != 17) {
                // not mp4 descriptor
                return false;
            }
            dataBuffer.get(); // ODLength
            dataBuffer.getShort(); // 10 bit ObjectDescriptorID, 1 bit
                                   // URL_FLAG and 5 bit reserved

            byte esTag = dataBuffer.get();
            if (esTag != 0x0F) {
                return false;
            }
            dataBuffer.get(); // ES Length
            short esTrackReferenceIndex = dataBuffer.getShort();
            byte ipmpDescriptorPointer = dataBuffer.get();
            if (ipmpDescriptorPointer != 0x0A) {
                // unexpected pointer
                return false;
            }
            dataBuffer.get(); // ipmpLength
            byte ipmpDescriptorId = dataBuffer.get();
            SinfData sinfData = new SinfData();
            sinfData.esIdReference = esTrackReferenceIndex;
            sinfData.ipmpDescriptorId = ipmpDescriptorId;
            mSinfList.add(sinfData);
            size -= kObjectSize;
        }
        dataBuffer.get(); // IPMP Descriptor Update Tag
        int sinfCount = mSinfList.size();
        size = 0;
        sizePart = 0;
        do {
            sizePart = (dataBuffer.get() & 0xFF);
            size = ((size << 7) & 0xFFFFFF80) | (sizePart & 0x7F);
        } while (sizePart > 128);
        while (size > 0) {
            dataBuffer.get(); // IPMP Descriptor Tag
            int ipmpByteCount = 1;
            int ipmpLength = 0;
            sizePart = 0;
            do {
                sizePart = (dataBuffer.get() & 0xFF);
                ipmpByteCount++;
                ipmpLength = ((ipmpLength << 7) & 0xFFFFFF80) | (sizePart & 0x7F);
            } while (sizePart > 128);
            ipmpByteCount += ipmpLength;
            byte ipmpDescriptorId = dataBuffer.get();
            dataBuffer.getShort(); // IPMPS Type
            byte[] ipmpData = new byte[ipmpLength - 3];
            dataBuffer.get(ipmpData);
            SinfData sinfData = null;
            for (int i = 0; i < sinfCount; i++) {
                sinfData = mSinfList.get(i);
                if (sinfData.ipmpDescriptorId == ipmpDescriptorId) {
                    sinfData.ipmpData = new byte[ipmpData.length];
                    for (int j = 0; j < ipmpData.length; j++) {
                        sinfData.ipmpData[j] = ipmpData[j];
                    }
                    break;
                }
            }
            size -= ipmpByteCount;
        }
        int ipmpDataLength = 0;
        for (int i = 0; i < sinfCount; i++) {
            SinfData sinfData = mSinfList.get(i);
            ipmpDataLength += sinfData.ipmpData.length;
        }

        int ipmpMetaDataLength = 16 // MARLIN_SYSTEM_ID
                + 4 // size of all SINF data
                + 4 // size of SINF box id
                + 4 * sinfCount // trackIndex * sinfCount
                + 4 * sinfCount // ipmpLength * sinfCount
                + ipmpDataLength; // size of ipmpData
        byte[] ipmpMetaData = new byte[ipmpMetaDataLength];
        int offset = 16;

        for (int i = 0; i < offset; i++) {
            int hexVal = Integer.parseInt(Util.MARLIN_SYSTEM_ID.substring(i * 2, i * 2 + 2), 16);
            ipmpMetaData[i] = (byte) hexVal;
        }
        ipmpMetaData[offset++] = (byte) ((ipmpDataLength >> 24) & 0xFF);
        ipmpMetaData[offset++] = (byte) ((ipmpDataLength >> 16) & 0xFF);
        ipmpMetaData[offset++] = (byte) ((ipmpDataLength >> 8) & 0xFF);
        ipmpMetaData[offset++] = (byte) (ipmpDataLength & 0xFF);
        ipmpMetaData[offset++] = 0x73; // S
        ipmpMetaData[offset++] = 0x69; // I
        ipmpMetaData[offset++] = 0x6E; // N
        ipmpMetaData[offset++] = 0x66; // F

        int numTracks = mTracks.size();
        for (int i = 0; i < numTracks; i++) {
            IsoTrack track = (IsoTrack) mTracks.get(i);
            for (int j = 0; j < sinfCount; j++) {
                SinfData sinfData = mSinfList.get(j);
                if (sinfData.esIdReference == track.getTrackId()) {
                    track.getMetaData().addValue(MetaData.KEY_IPMP_DATA, sinfData.ipmpData);
                    // track index
                    ipmpMetaData[offset++] = (byte) ((i >> 24) & 0xFF);
                    ipmpMetaData[offset++] = (byte) ((i >> 16) & 0xFF);
                    ipmpMetaData[offset++] = (byte) ((i >> 8) & 0xFF);
                    ipmpMetaData[offset++] = (byte) (i & 0xFF);

                    // sinf data length
                    ipmpMetaData[offset++] = (byte) ((sinfData.ipmpData.length >> 24) & 0xFF);
                    ipmpMetaData[offset++] = (byte) ((sinfData.ipmpData.length >> 16) & 0xFF);
                    ipmpMetaData[offset++] = (byte) ((sinfData.ipmpData.length >> 8) & 0xFF);
                    ipmpMetaData[offset++] = (byte) (sinfData.ipmpData.length & 0xFF);

                    System.arraycopy(sinfData.ipmpData, 0, ipmpMetaData, offset, sinfData.ipmpData.length);

                    byte[] tempData = new byte[4 + sinfData.ipmpData.length];
                    tempData[0] = (byte) ((sinfData.ipmpData.length >> 24) & 0xFF);
                    tempData[1] = (byte) ((sinfData.ipmpData.length >> 16) & 0xFF);
                    tempData[2] = (byte) ((sinfData.ipmpData.length >> 8) & 0xFF);
                    tempData[3] = (byte) (sinfData.ipmpData.length & 0xFF);
                    System.arraycopy(sinfData.ipmpData, 0, tempData, 4, sinfData.ipmpData.length);

                    // Create JSON for this track
                    String jsonData = null;
                    try {
                        jsonData = Util.getJSONIPMPData(tempData);
                    } catch (JSONException e) {
                        if (LOGS_ENABLED)
                            Log.e(TAG, "Exception when creating JSON object" + e);
                        return false;
                    }
                    track.getMediaFormat().setString(KEY_DRM_UUID, Util.MARLIN_SYSTEM_ID);
                    track.getMediaFormat().setString(KEY_MARLIN_JSON, jsonData);

                    offset += sinfData.ipmpData.length;
                    break;
                }
            }
        }

        mIpmpMetaData = ipmpMetaData;

        addMetaDataValue(KEY_IPMP_DATA, mIpmpMetaData);

        mCurrentTrack.getMetaData().addValue(KEY_MIME_TYPE, MimeType.OCTET_STREAM);

    } catch (IOException e) {
        if (LOGS_ENABLED)
            Log.e(TAG, "IOException when parsing ODSM data");
    }
    return true;
}

From source file:org.apache.hadoop.mapred.nativetask.handlers.BufferPuller.java

@Override
public boolean receiveData() throws IOException {
    if (closed) {
        return false;
    }//from  ww w  .  ja  v  a2  s .c om

    final ByteBuffer input = inputBuffer.getByteBuffer();

    if (null != asideBuffer && asideBuffer.length() > 0) {
        if (asideBuffer.remaining() > 0) {
            final byte[] output = asideBuffer.getByteBuffer().array();
            final int write = Math.min(asideBuffer.remaining(), input.remaining());
            input.get(output, asideBuffer.position(), write);
            asideBuffer.position(asideBuffer.position() + write);
        }

        if (asideBuffer.remaining() == 0) {
            asideBuffer.position(0);
        }
    }

    if (input.remaining() == 0) {
        return true;
    }

    if (input.remaining() < KV_HEADER_LENGTH) {
        throw new IOException("incomplete data, input length is: " + input.remaining());
    }
    final int position = input.position();
    final int keyLength = input.getInt();
    final int valueLength = input.getInt();
    input.position(position);
    final int kvLength = keyLength + valueLength + KV_HEADER_LENGTH;
    final int remaining = input.remaining();

    if (kvLength > remaining) {
        if (null == asideBuffer || asideBuffer.capacity() < kvLength) {
            asideBuffer = new InputBuffer(BufferType.HEAP_BUFFER, kvLength);
        }
        asideBuffer.rewind(0, kvLength);

        input.get(asideBuffer.array(), 0, remaining);
        asideBuffer.position(remaining);
    }
    return true;
}

From source file:org.carbondata.core.util.CarbonUtil.java

public static short[] getUnCompressColumnIndex(int totalLength, byte[] columnIndexData) {
    ByteBuffer buffer = ByteBuffer.wrap(columnIndexData);
    buffer.rewind();//  ww w.j  av  a2s  .  c o  m
    int indexDataLength = buffer.getInt();
    short[] indexData = new short[indexDataLength / 2];
    short[] indexMap = new short[(totalLength - indexDataLength - CarbonCommonConstants.INT_SIZE_IN_BYTE) / 2];
    int counter = 0;
    while (counter < indexData.length) {
        indexData[counter] = buffer.getShort();
        counter++;
    }
    counter = 0;
    while (buffer.hasRemaining()) {
        indexMap[counter++] = buffer.getShort();
    }
    return UnBlockIndexer.uncompressIndex(indexData, indexMap);
}

From source file:x10.x10rt.yarn.ApplicationMaster.java

private void processMessage(ByteBuffer msg, SocketChannel sc) {
    assert (msg.capacity() >= headerLength);

    msg.rewind(); // reset the buffer for reading from the beginning
    CTRL_MSG_TYPE type = CTRL_MSG_TYPE.values()[msg.getInt()];
    int destination = msg.getInt();
    final int source = msg.getInt();
    int datalen = msg.getInt();
    assert (datalen == msg.remaining());

    LOG.info("Processing message of type " + type + ", size " + (headerLength + datalen) + " from place "
            + source);//from  ww w.j  a va2s.  co m
    /*      System.err.print("Message contents:");
          for (int i=0; i<msg.capacity(); i++)
             System.err.print(" "+Integer.toHexString(msg.get(i)).toUpperCase());
          System.err.println();
    */
    switch (type) {
    case HELLO: {
        // read the port information, and update the record for this place
        assert (datalen == 4 && source < numRequestedContainers.get() && source >= 0);
        final CommunicationLink linkInfo;
        LOG.info("Getting link for place " + source);
        synchronized (links) {
            linkInfo = links.get(source);
        }
        linkInfo.port = ((msg.get() & 0xFF) << 8) | (msg.get() & 0xFF); // unsigned short in network order
        linkInfo.sc = sc;

        // check if there are pending port requests for this place
        if (linkInfo.pendingPortRequests != null) {
            // prepare response message
            String linkString = linkInfo.node.getHost() + ":" + linkInfo.port;
            byte[] linkBytes = linkString.getBytes(); // matches existing code.  TODO: switch to UTF8
            ByteBuffer response = ByteBuffer.allocateDirect(headerLength + linkBytes.length)
                    .order(ByteOrder.nativeOrder());
            response.putInt(CTRL_MSG_TYPE.PORT_RESPONSE.ordinal());
            response.putInt(-1);
            response.putInt(source);
            response.putInt(linkBytes.length);
            response.put(linkBytes);
            // send response to each waiting place
            for (int place : linkInfo.pendingPortRequests) {
                response.putInt(4, place); // set the destination to the requesting place
                response.rewind();
                // TODO: this code may get stuck here if the reciever isn't reading properly
                try {
                    while (response.hasRemaining())
                        links.get(place).sc.write(response);
                } catch (IOException e) {
                    LOG.warn("Unable to send out port response for place " + place + " to place " + source, e);
                }
            }
            linkInfo.pendingPortRequests = null;
        }
        LOG.info("HELLO from place " + source + " at port " + linkInfo.port);

        if (pendingKills != null && pendingKills.containsKey(source)) {
            int delay = pendingKills.remove(source);
            LOG.info("Scheduling a takedown of place " + source + " in " + delay + " seconds");
            placeKiller.schedule(new Runnable() {
                @Override
                public void run() {
                    LOG.info("KILLING CONTAINER FOR PLACE " + source);
                    nodeManager.stopContainerAsync(linkInfo.container, linkInfo.node);
                }
            }, delay, TimeUnit.SECONDS);
        }
    }
        break;
    case GOODBYE: {
        try {
            CommunicationLink link = links.get(source);
            assert (link.pendingPortRequests == null);
            sc.close();
            link.port = PORT_DEAD;
        } catch (IOException e) {
            LOG.warn("Error closing socket channel", e);
        }
        LOG.info("GOODBYE to place " + source);
    }
        break;
    case PORT_REQUEST: {
        LOG.info("Got PORT_REQUEST from place " + source + " for place " + destination);
        // check to see if we know the requested information
        CommunicationLink linkInfo = links.get(destination);
        if (linkInfo.port != PORT_UNKNOWN) {
            String linkString;
            if (linkInfo.port == PORT_DEAD)
                linkString = DEAD;
            else
                linkString = linkInfo.node.getHost() + ":" + linkInfo.port;
            LOG.info("Telling place " + source + " that place " + destination + " is at " + linkString);
            byte[] linkBytes = linkString.getBytes(); // matches existing code.  TODO: switch to UTF8
            ByteBuffer response = ByteBuffer.allocateDirect(headerLength + linkBytes.length)
                    .order(ByteOrder.nativeOrder());
            response.putInt(CTRL_MSG_TYPE.PORT_RESPONSE.ordinal());
            response.putInt(source);
            response.putInt(destination);
            response.putInt(linkBytes.length);
            response.put(linkBytes);
            response.rewind();
            // TODO: this code may get stuck here if the reciever isn't reading properly
            try {
                while (response.hasRemaining())
                    sc.write(response);
            } catch (IOException e) {
                LOG.warn("Unable to send out port response for place " + destination + " to place " + source,
                        e);
            }
        } else { // port is not known.  remember we have a place asking for it when it becomes available
            if (linkInfo.pendingPortRequests == null)
                linkInfo.pendingPortRequests = new ArrayList<Integer>(2);
            linkInfo.pendingPortRequests.add(source);
            LOG.info("Stashing PORT_REQUEST from place " + source + " for place " + destination
                    + " until the answer is known");
        }
    }
        break;
    case LAUNCH_REQUEST: {
        assert (datalen == 8);
        int numPlacesRequested = (int) msg.getLong();

        int oldvalue = numRequestedContainers.getAndAdd((int) numPlacesRequested);

        // Send request for containers to RM
        for (int i = 0; i < numPlacesRequested; i++) {
            Resource capability = Resource.newInstance(memoryPerPlaceInMb, coresPerPlace);
            ContainerRequest request = new ContainerRequest(capability, null, null, Priority.newInstance(0));
            LOG.info("Adding a new container request " + request.toString());
            resourceManager.addContainerRequest(request);
            pendingRequests.add(request);
        }

        LOG.info("Requested an increase of " + numPlacesRequested + " places on top of the previous " + oldvalue
                + " places");
        msg.rewind();
        msg.putInt(CTRL_MSG_TYPE.LAUNCH_RESPONSE.ordinal());
        msg.rewind();
        try {
            while (msg.hasRemaining())
                sc.write(msg);
        } catch (IOException e) {
            LOG.warn("Unable to send out launch response to place " + source, e);
        }
    }
        break;
    default:
        LOG.warn("unknown message type " + type);
    }
    LOG.info("Finished processing message of size " + (headerLength + datalen) + " from place " + source);
}

From source file:org.carbondata.core.util.CarbonUtil.java

public static int[] getUnCompressColumnIndex(int totalLength, byte[] columnIndexData,
        NumberCompressor numberCompressor) {
    byte[] indexData = null;
    byte[] indexMap = null;
    try {/*from www .j  a va 2  s  .c om*/
        ByteBuffer buffer = ByteBuffer.wrap(columnIndexData);
        buffer.rewind();
        int indexDataLength = buffer.getInt();
        indexData = new byte[indexDataLength];
        indexMap = new byte[totalLength - indexDataLength - CarbonCommonConstants.INT_SIZE_IN_BYTE];
        buffer.get(indexData);
        buffer.get(indexMap);
    } catch (Exception e) {
        LOGGER.error("Error while compressColumn Index ");
    }
    return UnBlockIndexer.uncompressIndex(numberCompressor.unCompress(indexData),
            numberCompressor.unCompress(indexMap));
}

From source file:org.apache.hadoop.mapred.nativetask.handlers.BufferPushee.java

public boolean collect(InputBuffer buffer) throws IOException {
    if (closed) {
        return false;
    }//  ww w.  j a v  a2  s  .  c  o  m

    final ByteBuffer input = buffer.getByteBuffer();
    if (null != asideBuffer && asideBuffer.length() > 0) {
        if (asideBuffer.remaining() > 0) {
            final byte[] output = asideBuffer.getByteBuffer().array();
            final int write = Math.min(asideBuffer.remaining(), input.remaining());
            input.get(output, asideBuffer.position(), write);
            asideBuffer.position(asideBuffer.position() + write);
        }

        if (asideBuffer.remaining() == 0 && asideBuffer.position() > 0) {
            asideBuffer.position(0);
            write(asideBuffer);
            asideBuffer.rewind(0, 0);
        }
    }

    if (input.remaining() == 0) {
        return true;
    }

    if (input.remaining() < KV_HEADER_LENGTH) {
        throw new IOException("incomplete data, input length is: " + input.remaining());
    }
    final int position = input.position();
    final int keyLength = input.getInt();
    final int valueLength = input.getInt();
    input.position(position);
    final int kvLength = keyLength + valueLength + KV_HEADER_LENGTH;
    final int remaining = input.remaining();

    if (kvLength > remaining) {
        if (null == asideBuffer || asideBuffer.capacity() < kvLength) {
            asideBuffer = new InputBuffer(BufferType.HEAP_BUFFER, kvLength);
        }
        asideBuffer.rewind(0, kvLength);

        input.get(asideBuffer.array(), 0, remaining);
        asideBuffer.position(remaining);
    } else {
        write(buffer);
    }
    return true;
}

From source file:de.rwhq.btree.InnerNode.java

@Override
public void load() throws IOException {
    final ByteBuffer buf = rawPage.bufferForReading(0);
    if (NodeType.deserialize(buf.getChar()) != NODE_TYPE)
        throw new IOException(
                "You are trying to load a InnerNode from a byte array, that does not contain an InnerNode");

    buf.position(Header.NUMBER_OF_KEYS.getOffset());
    numberOfKeys = buf.getInt();
    valid = true;//from w  w  w . j  ava  2 s .c o  m
}

From source file:com.offbynull.portmapper.pcp.PcpResponse.java

/**
 * Constructs a {@link PcpResponse} object by parsing a buffer.
 * @param buffer buffer containing PCP response data
 * @throws NullPointerException if any argument is {@code null}
 * @throws BufferUnderflowException if not enough data is available in {@code buffer}
 * @throws IllegalArgumentException if the version doesn't match the expected version (must always be {@code 2}), or if the r-flag isn't
 * set, or if there's an unsuccessful/unrecognized result code
 *///from   w ww  .  jav  a2 s  .c  om
PcpResponse(ByteBuffer buffer) {
    Validate.notNull(buffer);

    if (buffer.remaining() < 4 || buffer.remaining() > 1100 || buffer.remaining() % 4 != 0) {
        throw new IllegalArgumentException("Bad packet size: " + buffer.remaining());
    }

    int version = buffer.get() & 0xFF;
    Validate.isTrue(version == 2, "Unknown version: %d", version);

    int temp = buffer.get() & 0xFF;
    Validate.isTrue((temp & 128) == 128, "Bad R-flag: %d", temp);
    op = temp & 0x7F; // discard first bit, it was used for rflag

    buffer.get(); // skip reserved field

    int resultCodeNum = buffer.get() & 0xFF;
    PcpResultCode[] resultCodes = PcpResultCode.values();

    Validate.isTrue(resultCodeNum < resultCodes.length, "Unknown result code encountered: %d", resultCodeNum);
    Validate.isTrue(resultCodeNum == PcpResultCode.SUCCESS.ordinal(), "Unsuccessful result code: %s [%s]",
            resultCodes[resultCodeNum].toString(), resultCodes[resultCodeNum].getMessage());

    lifetime = buffer.getInt() & 0xFFFFFFFFL;

    epochTime = buffer.getInt() & 0xFFFFFFFFL;

    for (int i = 0; i < 12; i++) {
        buffer.get();
        //Validate.isTrue(buffer.get() == 0, "Reserved space indicates unsuccessful response");
    }

    options = Collections.emptyList();
}

From source file:org.trafodion.sql.HBaseAccess.HBulkLoadClient.java

public boolean addToHFile(short rowIDLen, Object rowIDs, Object rows) throws IOException, URISyntaxException {
    if (logger.isDebugEnabled())
        logger.debug("Enter addToHFile() ");
    Put put;//from w ww. java  2 s . c  o m
    if (isNewFileNeeded()) {
        doCreateHFile();
    }
    ByteBuffer bbRows, bbRowIDs;
    short numCols, numRows;
    short colNameLen;
    int colValueLen;
    byte[] colName, colValue, rowID;

    bbRowIDs = (ByteBuffer) rowIDs;
    bbRows = (ByteBuffer) rows;
    numRows = bbRowIDs.getShort();
    HTableClient htc = new HTableClient();
    long now = System.currentTimeMillis();
    for (short rowNum = 0; rowNum < numRows; rowNum++) {
        rowID = new byte[rowIDLen];
        bbRowIDs.get(rowID, 0, rowIDLen);
        numCols = bbRows.getShort();
        for (short colIndex = 0; colIndex < numCols; colIndex++) {
            colNameLen = bbRows.getShort();
            colName = new byte[colNameLen];
            bbRows.get(colName, 0, colNameLen);
            colValueLen = bbRows.getInt();
            colValue = new byte[colValueLen];
            bbRows.get(colValue, 0, colValueLen);
            KeyValue kv = new KeyValue(rowID, htc.getFamily(colName), htc.getName(colName), now, colValue);
            writer.append(kv);
        }
    }
    if (logger.isDebugEnabled())
        logger.debug("End addToHFile() ");
    return true;
}