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.hadoop.compression.fourmc.zstd.ZstdStreamCompressor.java

private void init() {
    cStream = createCStream();/*from   w w  w .ja v a  2s.co m*/

    /*
     * Notice for developers:
     * Is a direct buffer pool needed here?
     * The iBuffSize and oBuffSize is about ~128K, which is way smaller than 4MB. Take the community Lz4Compressor
     * for reference, allocateDirect shall not trigger a java.lang.OutOfMemoryError: Direct Buffer Memory exception.
     *
     * We can always use a direct buffer pool if needed.
     */
    iBuff = ByteBuffer.allocateDirect(iBuffSize);
    oBuff = ByteBuffer.allocateDirect(oBuffSize);

    reset();
}

From source file:HttpDownloadManager.java

public HttpDownloadManager(Logger log) throws IOException {
    if (log == null)
        log = Logger.getLogger(this.getClass().getName());
    this.log = log;
    selector = Selector.open(); // create Selector
    buffer = ByteBuffer.allocateDirect(64 * 1024); // allocate buffer
    pendingDownloads = Collections.synchronizedList(new ArrayList());
    this.start(); // start thread
}

From source file:org.alfresco.cacheserver.dropwizard.resources.CacheServerResource.java

private void fastChannelCopy(final ReadableByteChannel src, final WritableByteChannel dest) throws IOException {
    final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
    while (src.read(buffer) != -1) {
        // prepare the buffer to be drained
        buffer.flip();//from  w  w w . j a va  2 s  .com
        // write to the channel, may block
        dest.write(buffer);
        // If partial transfer, shift remainder down
        // If buffer is empty, same as doing clear()
        buffer.compact();
    }
    // EOF will leave buffer in fill state
    buffer.flip();
    // make sure the buffer is fully drained.
    while (buffer.hasRemaining()) {
        dest.write(buffer);
    }
}

From source file:com.slytechs.utils.memory.Malloc.java

/**
 * 
 */
public Malloc() {
    heap = ByteBuffer.allocateDirect(HEAP_CAPACITY);
    Entry e = new Entry();
    e.setStart(0);
    e.setLength(HEAP_CAPACITY);
    freeList.add(e);
}

From source file:com.ibm.crail.tools.CrailBenchmark.java

void write(String filename, int size, int loop, int storageClass, int locationClass, boolean buffered)
        throws Exception {
    System.out.println("write, filename " + filename + ", size " + size + ", loop " + loop + ", storageClass "
            + storageClass + ", locationClass " + locationClass + ", buffered " + buffered);

    CrailBuffer buf = null;//www  . j  a  va2  s.  com
    if (size == CrailConstants.BUFFER_SIZE) {
        buf = fs.allocateBuffer();
    } else if (size < CrailConstants.BUFFER_SIZE) {
        CrailBuffer _buf = fs.allocateBuffer();
        _buf.clear().limit(size);
        buf = _buf.slice();
    } else {
        buf = OffHeapBuffer.wrap(ByteBuffer.allocateDirect(size));
    }

    //warmup
    ConcurrentLinkedQueue<CrailBuffer> bufferQueue = new ConcurrentLinkedQueue<CrailBuffer>();
    bufferQueue.add(buf);
    warmUp(filename, warmup, bufferQueue);

    //benchmark
    System.out.println("starting benchmark...");
    fs.getStatistics().reset();
    long _loop = (long) loop;
    long _bufsize = (long) CrailConstants.BUFFER_SIZE;
    long _capacity = _loop * _bufsize;
    double sumbytes = 0;
    double ops = 0;
    CrailFile file = fs.create(filename, CrailNodeType.DATAFILE, CrailStorageClass.get(storageClass),
            CrailLocationClass.get(locationClass)).get().asFile();
    CrailBufferedOutputStream bufferedStream = buffered ? file.getBufferedOutputStream(_capacity) : null;
    CrailOutputStream directStream = !buffered ? file.getDirectOutputStream(_capacity) : null;
    long start = System.currentTimeMillis();
    while (ops < loop) {
        buf.clear();
        if (buffered) {
            bufferedStream.write(buf.getByteBuffer());
        } else {
            directStream.write(buf).get();
        }
        sumbytes = sumbytes + buf.capacity();
        ops = ops + 1.0;
    }
    if (buffered) {
        bufferedStream.close();
    }
    long end = System.currentTimeMillis();
    double executionTime = ((double) (end - start)) / 1000.0;
    double throughput = 0.0;
    double latency = 0.0;
    double sumbits = sumbytes * 8.0;
    if (executionTime > 0) {
        throughput = sumbits / executionTime / 1000.0 / 1000.0;
        latency = 1000000.0 * executionTime / ops;
    }

    System.out.println("execution time " + executionTime);
    System.out.println("ops " + ops);
    System.out.println("sumbytes " + sumbytes);
    System.out.println("throughput " + throughput);
    System.out.println("latency " + latency);

    fs.getStatistics().print("close");
}

From source file:com.springrts.springls.ServerThread.java

public ServerThread() {

    this.context = null;
    this.readBuffer = ByteBuffer.allocateDirect(READ_BUFFER_SIZE);
    this.updateables = new ArrayList<Updateable>();
    initDeprecatedCommands();// w ww  .j  av a2s. com
}

From source file:org.apache.hadoop.util.NativeStair.java

public static boolean decodeBulk(byte[][] readBufs, byte[][] writeBufs, int[] erasedLocation, int dataStart,
        int dataLen) {
    //LOG.info("decodeBulk(): checkpoint1 " + System.currentTimeMillis());
    ByteBuffer[] inputBuffers = new ByteBuffer[readBufs.length];
    ByteBuffer[] outputBuffers = new ByteBuffer[writeBufs.length];

    //LOG.info("decodeBulk(): checkpoint1.1 " + System.currentTimeMillis());
    //LOG.info("decodeBulk(): checkpoint3 " + System.currentTimeMillis());
    /* Added by RH Dec 16th 2014 begins
     * We use -1 to mark placeholder */
    int erasedLen = 0;
    int erasedLocationsBitmap[] = new int[20];
    int locationsToReadBitmap[] = new int[20];
    for (int i = 0; i < erasedLocation.length; i++) {
        //LOG.info("erasedLocation[" + i + "]: " + erasedLocation[i]);
        if (erasedLocation[i] != -1) {
            //LOG.info("erasedLocationsBitmap[" + erasedLocation[i] + "]=1");
            erasedLocationsBitmap[erasedLocation[i]] = 1;
            //inputBuffers[i] = ByteBuffer.allocateDirect(dataLen);
            erasedLen++;//from  w ww  .j a  v  a  2 s.  co m
        } else {
            break;
        }
    }
    //LOG.info("decodeBulk(): checkpoint2 " + System.currentTimeMillis());
    //for (int i = 0; i < erasedLocation.length; i++) {
    //for (int i = 0; i < writeBufs.length; i++) {
    for (int i = 0; i < erasedLen; i++) {
        outputBuffers[i] = ByteBuffer.allocateDirect(dataLen);
    }
    /* TODO:!! This is hardcoded!! De-hard code after deadline */
    locationsToReadBitmap = nativeLocationsToReadForDecode(erasedLocationsBitmap, locationsToReadBitmap);
    for (int i = 0; i < readBufs.length; i++) {
        //LOG.info("locationsToReadBitmap[" + i + "]=" + locationsToReadBitmap[i]);
        if (locationsToReadBitmap[i] == 1 || erasedLocationsBitmap[i] == 1) {
            //LOG.info("directify()" + i);
            inputBuffers[i] = directify(readBufs[i], dataStart, dataLen);
        } else {
            /**
             * modified by RH Jul 2nd, 2015 begins
             */
            //inputBuffers[i] = ByteBuffer.allocateDirect(dataLen);
            inputBuffers[i] = null;
            /**
             * modified by RH Jul 2nd, 2015 ends
             */
            //inputBuffers[i] = directify(readBufs[i], dataStart, dataLen);
        }
    }
    //LOG.info("decodeBulk(): checkpoint4 " + System.currentTimeMillis());
    /* Added by RH Dec 16th 2014 ends */

    /* Fixed by RH Dec 16th 2014 begins */
    //if (!nativeDecodeBulk(inputBuffers, readBufs.length, outputBuffers, writeBufs.length, 
    //      erasedLocation, erasedLocation.length, dataLen)) { 
    //  return false;
    //}
    //LOG.info("decodeBulk(): before nativeDecodeBulk()");
    if (!nativeDecodeBulk(inputBuffers, readBufs.length, outputBuffers, writeBufs.length, erasedLocation,
            erasedLen, dataLen)) {
        return false;
    }
    //LOG.info("decodeBulk(): checkpoint5 " + System.currentTimeMillis());
    /* Fixed by RH Dec 16th 2014 ends */

    //for (int i = 0; i < writeBufs.length; i++) {
    //for (int i = 0; i < writeBufs.length; i++) {
    for (int i = 0; i < erasedLen; i++) {
        //LOG.info(i);
        outputBuffers[i].get(writeBufs[i], dataStart, dataLen);
    }
    //LOG.info("decodeBulk(): checkpoint6 " + System.currentTimeMillis());

    return true;
}

From source file:org.apache.hadoop.hive.ql.io.orc.InStream.java

private static ByteBuffer allocateBuffer(int size, boolean isDirect) {
    // TODO: use the same pool as the ORC readers
    if (isDirect) {
        return ByteBuffer.allocateDirect(size);
    } else {/*  www .  ja  va2  s  .c  o  m*/
        return ByteBuffer.allocate(size);
    }
}

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

/**
 * {@inheritDoc}//  w  w  w.  j ava  2 s.c  o m
 */
@Override
public ByteBuffer createBuffer() {
    ByteBuffer buffer;
    try {
        buffer = bufferPool.borrowObject();
        if (borrowedBuffers == null)
            borrowedBuffers = new ArrayList<ByteBuffer>();
        borrowedBuffers.add(buffer);
    } catch (Exception e) {
        buffer = ByteBuffer.allocateDirect(1024 * 1024);
    }
    return buffer;
}

From source file:com.tinspx.util.io.ChannelSourceTest.java

@Test
public void testByteBufferSource() throws IOException {
    int off = 443, len = 17167;
    ByteBuffer buf, direct;//  w  w w  .j a v  a  2s  .  co m
    direct = ByteBuffer.allocateDirect(INPUT.length);
    assertTrue(direct.isDirect());
    direct.put(INPUT);
    byte[] sub = Arrays.copyOfRange(INPUT, off, off + len);

    //full input
    buf = ByteBuffer.wrap(INPUT);
    ByteSourceTests.testByteSource(ChannelSource.of(buf), INPUT);
    assertEquals(0, buf.position());
    assertEquals(INPUT.length, buf.limit());

    buf = ByteBuffer.wrap(INPUT).asReadOnlyBuffer();
    ByteSourceTests.testByteSource(ChannelSource.of(buf), INPUT);
    assertEquals(0, buf.position());
    assertEquals(INPUT.length, buf.limit());

    direct.clear();
    buf = direct;
    ByteSourceTests.testByteSource(ChannelSource.of(buf), INPUT);
    assertEquals(0, buf.position());
    assertEquals(INPUT.length, buf.limit());

    //sub range of input
    buf = ByteBuffer.wrap(INPUT);
    buf.clear().position(off).limit(off + len);
    ByteSourceTests.testByteSource(ChannelSource.of(buf), sub);
    assertEquals(off, buf.position());
    assertEquals(off + len, buf.limit());

    buf = ByteBuffer.wrap(INPUT).asReadOnlyBuffer();
    buf.clear().position(off).limit(off + len);
    ByteSourceTests.testByteSource(ChannelSource.of(buf), sub);
    assertEquals(off, buf.position());
    assertEquals(off + len, buf.limit());

    direct.clear();
    buf = direct;
    buf.clear().position(off).limit(off + len);
    ByteSourceTests.testByteSource(ChannelSource.of(buf), sub);
    assertEquals(off, buf.position());
    assertEquals(off + len, buf.limit());
}