Example usage for java.nio ByteBuffer capacity

List of usage examples for java.nio ByteBuffer capacity

Introduction

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

Prototype

public final int capacity() 

Source Link

Document

Returns the capacity of this buffer.

Usage

From source file:org.codehaus.preon.buffer.DefaultBitBuffer.java

/** Read byte buffer containing binary stream and set the bit pointer position to 0. */
public DefaultBitBuffer(String fileName) {

    File file = new File(fileName);

    // Open the file and then get a org.codehaus.preon.channel.channel from the stream
    FileInputStream fis;//from   w  w  w. j  a v a2  s. c  o  m

    try {
        fis = new FileInputStream(file);

        FileChannel fc = fis.getChannel();

        // Get the file's size and then map it into memory
        int fileSize = (int) fc.size();
        ByteBuffer inputByteBuffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fileSize);

        // Close the org.codehaus.preon.channel.channel and the stream
        fc.close();

        this.byteBuffer = inputByteBuffer;
        bitBufBitSize = ((long) (inputByteBuffer.capacity())) << 3;
        bitPos = 0;

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

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

private void init(boolean reset) throws IOException {
    if (reset) {/*from   w w  w .  j av a2s. c  om*/
        indexChannel.truncate(OFFSET);
        dataChannel.truncate(OFFSET);
        keyChannel.truncate(OFFSET);
        totalRecord = 0;
    } else {
        long length = indexChannel.size() - OFFSET;
        totalRecord = (int) (length / RECORD_SIZE);
        ByteBuffer buf = ByteBuffer.allocate(RECORD_SIZE);
        logger.info("Building key map and read index file for " + filename + " total record " + totalRecord);
        long per = 0;
        int j = 0;
        if (totalRecord >= 1000000)
            per = totalRecord / 10;

        for (int i = 0; i < totalRecord; i++) {
            indexChannel.read(buf);
            assert (buf.capacity() == RECORD_SIZE);
            buf.rewind();
            byte status = buf.get();
            if (isDeleted(status))
                this.deleted++;
            else {
                long key = buf.getLong();
                byte[] keys;
                try {
                    keys = readChannel(key, keyChannel);
                    long data = buf.getLong();
                    long block2version = buf.getLong();
                    CacheBlock block = new CacheBlock(i, data, block2version, status);
                    map.put(toKey(keys), block);
                } catch (Exception ex) {
                    logger.warn("Not able to read record no " + i + " , skip reason " + ex.getMessage());
                    buf.clear();
                    error++;
                    continue;
                }
            }
            buf.clear();
            if (per > 0 && (i + 1) % per == 0) {
                logger.info((++j * 10) + "% complete");
            }
        }
    }
    dataOffset = dataChannel.size();
    keyOffset = keyChannel.size();
    //logOffset = logChannel.size();
    logger.info("Total record " + totalRecord + " deleted " + deleted + " error " + error + " active "
            + (totalRecord - deleted - error));
}

From source file:com.kitware.tangoproject.paraviewtangorecorder.PointCloudActivity.java

private void writePointCloudToFile(TangoXyzIjData xyzIj, byte[] buffer,
        ArrayList<TangoCoordinateFramePair> framePairs) {

    ByteBuffer myBuffer = ByteBuffer.allocate(xyzIj.xyzCount * 3 * 4);
    myBuffer.order(ByteOrder.LITTLE_ENDIAN);
    myBuffer.put(buffer, xyzIj.xyzParcelFileDescriptorOffset, myBuffer.capacity());

    File mainDir = new File(mMainDirPath);
    if (!mainDir.exists()) {
        boolean created = mainDir.mkdir();
        if (created) {
            Log.i(TAG, "Folder: \"" + mMainDirPath + "\" created\n");
        }//from ww w . j  a v a  2 s.c o m
    }

    File dir = new File(mSaveDirAbsPath);
    if (!dir.exists()) {
        boolean created = dir.mkdir();
        if (created) {
            Log.i(TAG, "Folder: \"" + mSaveDirAbsPath + "\" created\n");
        }
    }

    mFilename = "pc_" + mNowTimeString + "_" + String.format("%03d", mNumberOfFilesWritten) + ".vtk";
    mFilenameBuffer.add(mSaveDirAbsPath + mFilename);
    File file = new File(dir, mFilename);

    try {

        DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));

        out.write(("# vtk DataFile Version 3.0\n" + "vtk output\n" + "BINARY\n" + "DATASET POLYDATA\n"
                + "POINTS " + xyzIj.xyzCount + " float\n").getBytes());

        for (int i = 0; i < xyzIj.xyzCount; i++) {

            out.writeFloat(myBuffer.getFloat(3 * i * 4));
            out.writeFloat(myBuffer.getFloat((3 * i + 1) * 4));
            out.writeFloat(myBuffer.getFloat((3 * i + 2) * 4));
        }

        out.write(("\nVERTICES 1 " + String.valueOf(xyzIj.xyzCount + 1) + "\n").getBytes());
        out.writeInt(xyzIj.xyzCount);
        for (int i = 0; i < xyzIj.xyzCount; i++) {
            out.writeInt(i);
        }

        out.write(("\nFIELD FieldData 1\n" + "timestamp 1 1 float\n").getBytes());
        out.writeFloat((float) xyzIj.timestamp);

        out.close();
        mNumberOfFilesWritten++;
        mTimeToTakeSnap = false;

    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:edu.mbl.jif.imaging.mmtiff.MultipageTiffWriter.java

private void writeImageDescription(String value, long imageDescriptionTagOffset) throws IOException {
    //write first image IFD
    ByteBuffer ifdCountAndValueBuffer = ByteBuffer.allocate(8).order(BYTE_ORDER);
    ifdCountAndValueBuffer.putInt(0, value.length());
    ifdCountAndValueBuffer.putInt(4, (int) filePosition_);
    fileChannel_.write(ifdCountAndValueBuffer, imageDescriptionTagOffset + 4);

    //write String
    ByteBuffer buffer = ByteBuffer.wrap(getBytesFromString(value));
    fileChannel_.write(buffer, filePosition_);
    filePosition_ += buffer.capacity();
}

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  w  ww . j  av  a2s .  c  om
    /*      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:de.rwhq.btree.LeafNode.java

@Override
public int remove(final K key) {

    final int pos = offsetOfKey(key);
    if (pos == NOT_FOUND)
        return 0;

    final int numberOfValues = get(key).size();
    final int sizeOfValues = numberOfValues
            * (valueSerializer.getSerializedLength() + keySerializer.getSerializedLength());

    //TODO: free key and value pages

    // shift the pointers after key
    final ByteBuffer buffer = rawPage().bufferForWriting(0);
    System.arraycopy(buffer.array(), pos + sizeOfValues, buffer.array(), pos,
            buffer.capacity() - pos - sizeOfValues);
    setNumberOfEntries(getNumberOfEntries() - numberOfValues);

    rawPage().sync();//from w  w  w. j  a  v a 2 s.co  m

    return numberOfValues;
}

From source file:com.google.cloud.hadoop.gcsio.GoogleCloudStorageIntegrationHelper.java

/**
 * Writes a file with the given buffer repeated numWrites times.
 *
 * @param bucketName name of the bucket to create object in
 * @param objectName name of the object to create
 * @param buffer Data to write//  w  w  w  .  j  ava2 s  .com
 * @param numWrites number of times to repeat the data
 * @return number of bytes written
 */
protected int writeFile(String bucketName, String objectName, ByteBuffer buffer, int numWrites)
        throws IOException {
    int numBytesWritten = -1;
    int totalBytesWritten = 0;
    WritableByteChannel writeChannel = null;

    try {
        writeChannel = create(bucketName, objectName, new CreateFileOptions(false /* overwrite existing */));
        for (int i = 0; i < numWrites; i++) {
            buffer.clear();
            numBytesWritten = writeChannel.write(buffer);
            Assert.assertEquals("could not write the entire buffer", buffer.capacity(), numBytesWritten);
            totalBytesWritten += numBytesWritten;
        }

    } finally {
        if (writeChannel != null) {
            writeChannel.close();
        }
    }

    return totalBytesWritten;
}

From source file:org.apache.hadoop.ipc.ServerRpcSSLEngineImpl.java

@Override
public int read(ReadableByteChannel channel, ByteBuffer buffer, Server.Connection connection)
        throws IOException {
    int netRead = channel.read(clientNetBuffer);
    if (netRead == -1) {
        return -1;
    }//from   ww  w  .j  a v  a 2  s .c om

    int read = 0;
    SSLEngineResult unwrapResult;
    do {
        clientNetBuffer.flip();
        unwrapResult = sslEngine.unwrap(clientNetBuffer, clientAppBuffer);
        clientNetBuffer.compact();

        if (unwrapResult.getStatus().equals(SSLEngineResult.Status.OK)) {
            read += unwrapResult.bytesProduced();
            clientAppBuffer.flip();

            while (clientAppBuffer.hasRemaining()) {
                byte currentByte = clientAppBuffer.get();
                try {
                    buffer.put(currentByte);
                } catch (BufferOverflowException ex) {
                    if (buffer.capacity() < maxUnWrappedDataLength) {
                        buffer = enlargeUnwrappedBuffer(buffer, currentByte);
                        connection.setSslUnwrappedBuffer(buffer);
                    } else {
                        LOG.error("Buffer overflow clientAppBuffer position: " + clientAppBuffer.position()
                                + " but buffer capacity " + buffer.capacity(), ex);
                        throw ex;
                    }
                }
            }
            clientAppBuffer.compact();
        } else if (unwrapResult.getStatus().equals(SSLEngineResult.Status.BUFFER_UNDERFLOW)) {
            read += unwrapResult.bytesProduced();
            break;
        } else if (unwrapResult.getStatus().equals(SSLEngineResult.Status.BUFFER_OVERFLOW)) {
            clientAppBuffer = enlargeApplicationBuffer(clientAppBuffer);
        } else if (unwrapResult.getStatus().equals(SSLEngineResult.Status.CLOSED)) {
            sslEngine.closeOutbound();
            doHandshake();
            read = -1;
            break;
        } else {
            throw new IOException("SSLEngine UNWRAP invalid status: " + unwrapResult.getStatus());
        }
    } while (clientNetBuffer.position() != 0);

    return read;
}

From source file:io.github.dsheirer.source.tuner.hackrf.HackRFTunerController.java

public void write(Request request, int value, int index, ByteBuffer buffer) throws UsbException {
    if (mDeviceHandle != null) {
        int transferred = LibUsb.controlTransfer(mDeviceHandle, REQUEST_TYPE_OUT, request.getRequestNumber(),
                (short) value, (short) index, buffer, USB_TIMEOUT_US);

        if (transferred < 0) {
            throw new LibUsbException("error writing byte buffer", transferred);
        } else if (transferred != buffer.capacity()) {
            throw new LibUsbException("transferred bytes [" + transferred + "] is not what was expected ["
                    + buffer.capacity() + "]", transferred);
        }//from   w  w  w. ja v a  2 s .  c o m
    } else {
        throw new LibUsbException("device handle is null", LibUsb.ERROR_NO_DEVICE);
    }
}

From source file:com.alvermont.terraj.fracplanet.geom.VertexBufferArray.java

/**
 * Resize the buffer. This is done by reallocating a new one and copying
 * data from the old buffer to the new one. This is necessary as buffers
 * cannot be dynamically resized.//w  ww  .ja  v  a 2 s. co  m
 */
protected void resizeBuffer() {
    // we can't resize it so we have to allocate a new one and copy the data
    final int slots = (buffer.capacity() / ELEMENTSIZE);
    final int newCapacity = buffer.capacity()
            + (((slots * CAPACITY_PCT_INCREASE) / HUNDRED_PERCENT) * ELEMENTSIZE);

    final ByteBuffer newBuffer = ByteBuffer.allocateDirect(newCapacity).order(ByteOrder.nativeOrder());

    if (log.isDebugEnabled()) {
        log.debug("Resizing vertex buffer capacity to: " + newBuffer.capacity());
    }

    final FloatBuffer oldVertexBuffer = positionBuffer;
    final FloatBuffer oldNormalBuffer = normalBuffer;
    final ByteBuffer oldColourBuffer = colourBuffer;
    final ByteBuffer oldEmissiveBuffer = emissiveBuffer;

    this.buffer = newBuffer;

    sliceAndDice(newCapacity / ELEMENTSIZE);

    oldVertexBuffer.rewind();
    positionBuffer.rewind();
    positionBuffer.limit(oldVertexBuffer.limit());
    positionBuffer.put(oldVertexBuffer);

    oldNormalBuffer.rewind();
    normalBuffer.rewind();
    normalBuffer.limit(oldNormalBuffer.limit());
    normalBuffer.put(oldNormalBuffer);

    oldColourBuffer.rewind();
    colourBuffer.rewind();
    colourBuffer.limit(oldColourBuffer.limit());
    colourBuffer.put(oldColourBuffer);

    oldEmissiveBuffer.rewind();
    emissiveBuffer.rewind();
    emissiveBuffer.limit(oldEmissiveBuffer.limit());
    emissiveBuffer.put(oldEmissiveBuffer);
}