Example usage for java.nio ByteBuffer allocateDirect

List of usage examples for java.nio ByteBuffer allocateDirect

Introduction

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

Prototype

public static ByteBuffer allocateDirect(int capacity) 

Source Link

Document

Creates a direct byte buffer based on a newly allocated memory block.

Usage

From source file:com.stainlesscode.mediapipeline.audioout.ExperimentalAudioPlayerWithAudioWriter.java

private void allocateBuffers() {
    if (bytesPerFrame > 0) {
        if (LogUtil.isDebugEnabled()) {
            LogUtil.debug("bytesPerFrame is " + bytesPerFrame);
            LogUtil.debug("Allocating audio buffers " + bufferSizeInFrames + "*" + bytesPerFrame);
        }/*  w  w w .ja  v a 2 s . c o m*/
        bufferSizeInBytes = bufferSizeInFrames * bytesPerFrame;
        smoothingBuffer = ByteBuffer.allocateDirect(bufferSizeInBytes);
        playBuffer = ByteBuffer.allocateDirect(bufferSizeInBytes);
        actualWriterThreadRunnable.setPlayBuffer(playBuffer);
    }
}

From source file:com.stainlesscode.mediapipeline.audioout.TestAudioPlayer.java

private void allocateBuffers() {
    if (bytesPerFrame > 0) {
        if (LogUtil.isDebugEnabled()) {
            LogUtil.debug("bytesPerFrame is " + bytesPerFrame);
            LogUtil.debug("Allocating audio buffers " + bufferSizeInFrames + "*" + bytesPerFrame);
        }/*from w w w . j a  v a2 s  .co m*/
        bufferSizeInBytes = bufferSizeInFrames * bytesPerFrame;
        smoothingBuffer = ByteBuffer.allocateDirect(bufferSizeInBytes);
        // playBuffer = ByteBuffer.allocateDirect(bufferSizeInBytes);
        playBuffer = ByteBuffer.allocateDirect(1000000);
    }
}

From source file:com.streamsets.pipeline.lib.generator.wholefile.WholeFileDataGenerator.java

@Override
public void write(Record record) throws IOException, DataGeneratorException {
    validateRecord(record);//from   www.  j av  a2 s. co  m
    FileRef fileRef = record.get(FileRefUtil.FILE_REF_FIELD_PATH).getValueAsFileRef();
    int bufferSize = fileRef.getBufferSize();
    boolean canUseDirectByteBuffer = fileRef.getSupportedStreamClasses().contains(ReadableByteChannel.class);
    if (canUseDirectByteBuffer) {
        //Don't have to close this here, because generate.close will call output stream close
        WritableByteChannel writableByteChannel = Channels.newChannel(outputStream); //NOSONAR
        try (ReadableByteChannel readableByteChannel = getReadableStream(fileRef, ReadableByteChannel.class)) {
            ByteBuffer buffer = ByteBuffer.allocateDirect(bufferSize);
            while ((readableByteChannel.read(buffer)) > 0) {
                //Flip to use the buffer from 0 to position.
                buffer.flip();
                while (buffer.hasRemaining()) {
                    writableByteChannel.write(buffer);
                }
                //Compact the buffer for reuse.
                buffer.clear();
            }
        }
    } else {
        byte[] b = new byte[bufferSize];
        try (InputStream stream = getReadableStream(fileRef, InputStream.class)) {
            IOUtils.copyLarge(stream, outputStream, b);
        }
    }
}

From source file:com.bytelightning.opensource.pokerface.BufferIOController.java

/**
 * Returns the <code>ByteBuffer</code>, lazily obtaining one from the pool if need be.
 *///  w w  w .  ja va  2s  .  co  m
public ByteBuffer getByteBuffer() {
    if (buffer == null) {
        try {
            buffer = bufferPool.borrowObject();
            buffer.reset(); // Yes it's redundant with our return code, but it *is* a community pool :-)
            borrowedBuffer = true;
        } catch (Exception e) {
            buffer = ByteBuffer.allocateDirect(1024 * 1024);
            borrowedBuffer = false;
        }
    }
    return buffer;
}

From source file:com.l2jfree.network.mmocore.ReadWriteThread.java

public ReadWriteThread(MMOController<T, RP, SP> mmoController, MMOConfig config,
        PacketHandler<T, RP, SP> packetHandler) throws IOException {
    super(mmoController, config);

    _bufferSize = config.getBufferSize();
    _helperBufferCount = config.getHelperBufferCount();
    _maxOutgoingPacketsPerPass = config.getMaxOutgoingPacketsPerPass();
    _maxIncomingPacketsPerPass = config.getMaxIncomingPacketsPerPass();
    _maxOutgoingBytesPerPass = config.getMaxOutgoingBytesPerPass();
    _maxIncomingBytesPerPass = config.getMaxIncomingBytesPerPass();
    _byteOrder = config.getByteOrder();/*  w ww .j a va 2s . co m*/

    _directWriteBuffer = ByteBuffer.allocateDirect(getBufferSize()).order(getByteOrder());
    _writeBuffer = ByteBuffer.allocate(getBufferSize()).order(getByteOrder());
    _readBuffer = ByteBuffer.allocate(getBufferSize()).order(getByteOrder());

    _bufferPool = new ArrayDeque<ByteBuffer>(getHelperBufferCount());
    for (int i = 0; i < getHelperBufferCount(); i++)
        getFreeBuffers().addLast(ByteBuffer.allocate(getBufferSize()).order(getByteOrder()));
    _mmoBuffer = new MMOBuffer();
    _dataSizeHolder = new DataSizeHolder();

    _packetHandler = packetHandler;
    _pendingClose = FastList.newInstance();
}

From source file:org.cloudfoundry.caldecott.server.controller.TunnelController.java

@RequestMapping(value = "/{id}/out/{seq}", method = RequestMethod.GET)
@ResponseBody//from w ww  .  j av  a  2 s  .  c  o  m
public ByteBuffer read(@PathVariable TunnelId id, @PathVariable int seq) {
    // FIXME async
    // Tunnel tunnel = this.tunnels.get(id);
    // BlockingCallback<ByteBuffer> callback = BlockingCallback.forClass(ByteBuffer.class);
    // tunnel.read(seq, callback);
    // return callback.get();
    ByteBuffer buffer = ByteBuffer.allocateDirect(100);
    buffer.put("hello".getBytes());
    buffer.flip();
    return buffer;
}

From source file:org.apache.hadoop.io.compress.bzip2.Bzip2Compressor.java

/** 
 * Creates a new compressor using the specified block size.
 * Compressed data will be generated in bzip2 format.
 * /*from w w w. ja v a  2s. c om*/
 * @param blockSize The block size to be used for compression.  This is
 *        an integer from 1 through 9, which is multiplied by 100,000 to 
 *        obtain the actual block size in bytes.
 * @param workFactor This parameter is a threshold that determines when a 
 *        fallback algorithm is used for pathological data.  It ranges from
 *        0 to 250.
 * @param directBufferSize Size of the direct buffer to be used.
 */
public Bzip2Compressor(int blockSize, int workFactor, int directBufferSize) {
    this.blockSize = blockSize;
    this.workFactor = workFactor;
    this.directBufferSize = directBufferSize;
    stream = init(blockSize, workFactor);
    uncompressedDirectBuf = ByteBuffer.allocateDirect(directBufferSize);
    compressedDirectBuf = ByteBuffer.allocateDirect(directBufferSize);
    compressedDirectBuf.position(directBufferSize);
}

From source file:com.stainlesscode.mediapipeline.audioout.DefaultAudioPlayer.java

private void allocateBuffers() {
    if (bytesPerFrame > 0) {
        if (LogUtil.isDebugEnabled()) {
            LogUtil.debug("bytesPerFrame is " + bytesPerFrame);
            LogUtil.debug("Allocating audio buffers " + bufferSizeInFrames + "*" + bytesPerFrame);
        }//from   w  w  w  . j a  va 2s  . com
        bufferSizeInBytes = bufferSizeInFrames * bytesPerFrame;
        smoothingBuffer = ByteBuffer.allocate(bufferSizeInBytes);
        playBuffer = ByteBuffer.allocateDirect(bufferSizeInBytes);
    }
}

From source file:com.github.c77.base_driver.KobukiBaseDevice.java

private void updateReceivedData(final byte[] bytes) {
    int readBytes = bytes.length;
    packetReader.newPacket(ByteBuffer.allocateDirect(readBytes).put(bytes, 0, readBytes));
    baseStatus = packetParser.parseBaseStatus(packetReader.getSensorPacket());
}

From source file:us.ihmc.pubsub.types.ByteBufferPubSubType.java

@Override
public ByteBuffer createData() {
    return ByteBuffer.allocateDirect(maxSize);
}