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:org.apache.hama.bsp.message.io.CombineSpilledDataProcessor.java

@Override
public boolean init(Configuration conf) {
    if (!super.init(conf)) {
        return false;
    }/*from   w  w  w  .j a v a2 s  .  c  o m*/
    String className = conf.get(Constants.COMBINER_CLASS);

    if (className == null)
        return true;
    try {
        combiner = ReflectionUtils.newInstance(className);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
    className = conf.get(Constants.MESSAGE_CLASS);

    if (className != null) {
        try {
            writableObject = ReflectionUtils.newInstance(className);
            iterator = new ReusableByteBuffer<M>(writableObject);
        } catch (ClassNotFoundException e) {
            LOG.error("Error combining the records.", e);
            return false;
        }
    }

    combineOutputBuffer = new DirectByteBufferOutputStream();
    combineBuffer = ByteBuffer.allocateDirect(Constants.BUFFER_DEFAULT_SIZE);
    combineOutputBuffer.setBuffer(combineBuffer);
    return true;
}

From source file:com.linkedin.pinot.perf.ForwardIndexReaderBenchmark.java

public static void singleValuedReadBenchMarkV2(File file, int numDocs, int numBits) throws Exception {
    boolean signed = false;
    boolean isMmap = false;
    long start, end;
    boolean fullScan = true;

    boolean batchRead = true;
    boolean singleRead = true;

    PinotDataBuffer heapBuffer = PinotDataBuffer.fromFile(file, ReadMode.heap, FileChannel.MapMode.READ_ONLY,
            "benchmarking");
    com.linkedin.pinot.core.io.reader.impl.v2.FixedBitSingleValueReader reader = new com.linkedin.pinot.core.io.reader.impl.v2.FixedBitSingleValueReader(
            heapBuffer, numDocs, numBits, signed);

    if (fullScan) {
        DescriptiveStatistics stats = new DescriptiveStatistics();
        ByteBuffer buffer = ByteBuffer.allocateDirect((int) file.length());
        RandomAccessFile raf = new RandomAccessFile(file, "r");
        raf.getChannel().read(buffer);/*from w  w  w . ja  v a2  s. c o m*/
        raf.close();
        int[] input = new int[numBits];
        int[] output = new int[32];
        int numBatches = (numDocs + 31) / 32;
        for (int run = 0; run < MAX_RUNS; run++) {
            start = System.currentTimeMillis();
            for (int i = 0; i < numBatches; i++) {
                for (int j = 0; j < numBits; j++) {
                    input[j] = buffer.getInt(i * numBits * 4 + j * 4);
                }
                BitPacking.fastunpack(input, 0, output, 0, numBits);
            }
            end = System.currentTimeMillis();
            stats.addValue((end - start));
        }
        System.out.println(" v2 full scan stats for " + file.getName());
        System.out.println(
                stats.toString().replaceAll("\n", ", ") + " raw:" + Arrays.toString(stats.getValues()));
    }
    if (singleRead) {
        DescriptiveStatistics stats = new DescriptiveStatistics();
        // sequential read
        for (int run = 0; run < MAX_RUNS; run++) {
            start = System.currentTimeMillis();
            for (int i = 0; i < numDocs; i++) {
                int value = reader.getInt(i);
            }
            end = System.currentTimeMillis();
            stats.addValue((end - start));
        }
        System.out.println(" v2 sequential single read for " + file.getName());
        System.out.println(
                stats.toString().replaceAll("\n", ", ") + " raw:" + Arrays.toString(stats.getValues()));
    }
    if (batchRead) {
        DescriptiveStatistics stats = new DescriptiveStatistics();
        int batchSize = Math.min(5000, numDocs);
        int[] output = new int[batchSize];
        int[] rowIds = new int[batchSize];

        // sequential read
        for (int run = 0; run < MAX_RUNS; run++) {
            start = System.currentTimeMillis();
            int rowId = 0;
            while (rowId < numDocs) {
                int length = Math.min(batchSize, numDocs - rowId);
                for (int i = 0; i < length; i++) {
                    rowIds[i] = rowId + i;
                }
                reader.getIntBatch(rowIds, output, length);
                rowId = rowId + length;
            }
            end = System.currentTimeMillis();
            stats.addValue((end - start));
        }
        System.out.println("v2 sequential batch read stats for " + file.getName());
        System.out.println(
                stats.toString().replaceAll("\n", ", ") + " raw:" + Arrays.toString(stats.getValues()));
    }
    reader.close();

}

From source file:com.google.fpl.liquidfunpaint.ParticleRenderer.java

public ParticleRenderer() {
    mParticlePositionBuffer = ByteBuffer.allocateDirect(2 * 4 * Renderer.MAX_PARTICLE_COUNT)
            .order(ByteOrder.nativeOrder());
    mParticleColorBuffer = ByteBuffer.allocateDirect(4 * Renderer.MAX_PARTICLE_COUNT)
            .order(ByteOrder.nativeOrder());
    mParticleWeightBuffer = ByteBuffer.allocateDirect(4 * Renderer.MAX_PARTICLE_COUNT)
            .order(ByteOrder.nativeOrder());
}

From source file:org.ms123.common.git.FileObject.java

public String getString(String key) throws IOException {
    Path path = getPath();/*from  www .j  a v a  2  s  .  c o m*/
    UserDefinedFileAttributeView view = Files.getFileAttributeView(path, UserDefinedFileAttributeView.class);
    ByteBuffer bb = ByteBuffer.allocateDirect(64);
    int num = view.read(key, bb);
    bb.flip();
    return Charset.defaultCharset().decode(bb).toString();
}

From source file:org.apache.hadoop.io.compress.lzma.LzmaDecompressor.java

/**
 * Creates a new decompressor./*w ww .j a v  a 2  s  .  c  o  m*/
 */
public LzmaDecompressor(int directBufferSize) {
    this.directBufferSize = directBufferSize;
    compressedDirectBuf = ByteBuffer.allocateDirect(directBufferSize);
    uncompressedDirectBuf = ByteBuffer.allocateDirect(directBufferSize);
    uncompressedDirectBuf.position(directBufferSize);

    stream = init();
}

From source file:org.apache.hadoop.hbase.util.ByteBufferArray.java

/**
 * We allocate a number of byte buffers as the capacity. In order not to out
 * of the array bounds for the last byte(see {@link ByteBufferArray#multiple}), 
 * we will allocate one additional buffer with capacity 0;
 * @param capacity total size of the byte buffer array
 * @param directByteBuffer true if we allocate direct buffer
 *//*from   ww w  .j  a va2 s.co m*/
public ByteBufferArray(long capacity, boolean directByteBuffer) {
    this.bufferSize = DEFAULT_BUFFER_SIZE;
    if (this.bufferSize > (capacity / 16))
        this.bufferSize = (int) roundUp(capacity / 16, 32768);
    this.bufferCount = (int) (roundUp(capacity, bufferSize) / bufferSize);
    LOG.info("Allocating buffers total=" + StringUtils.byteDesc(capacity) + ", sizePerBuffer="
            + StringUtils.byteDesc(bufferSize) + ", count=" + bufferCount + ", direct=" + directByteBuffer);
    buffers = new ByteBuffer[bufferCount + 1];
    locks = new Lock[bufferCount + 1];
    for (int i = 0; i <= bufferCount; i++) {
        locks[i] = new ReentrantLock();
        if (i < bufferCount) {
            buffers[i] = directByteBuffer ? ByteBuffer.allocateDirect(bufferSize)
                    : ByteBuffer.allocate(bufferSize);
        } else {
            buffers[i] = ByteBuffer.allocate(0);
        }

    }
}

From source file:edu.tsinghua.lumaqq.qq.net.AbstractProxy.java

/**
 * UDP Socks5?/*from   w ww  . j  ava2s . c  o m*/
 * 
 * @param handler
 * @param channel
 */
public AbstractProxy(IProxyHandler handler, DatagramChannel channel) {
    this.handler = handler;
    buffer = ByteBuffer.allocateDirect(300);
    username = password = "";
    connected = true;
    udp = true;
    datagramChannel = channel;
}

From source file:de.ailis.threedee.utils.BufferUtils.java

/**
 * Converts the specified short buffer to native endian and returns this new
 * buffer. If buffer is already in correct endian format then it is returned
 * right away./*from w  ww.  ja  v  a 2  s  .c o  m*/
 *
 * @param buffer
 *            The short buffer to convert
 * @return The converted short buffer or the source buffer if no conversion
 *         is needed
 */

public static ShortBuffer convertToNativeEndian(final ShortBuffer buffer) {
    if (buffer.order() == ByteOrder.nativeOrder())
        return buffer;

    final ByteBuffer bytes = ByteBuffer.allocateDirect(buffer.capacity());
    bytes.order(ByteOrder.nativeOrder());
    final ShortBuffer shorts = bytes.asShortBuffer();
    shorts.put(buffer).rewind();
    return shorts;
}

From source file:com.chicm.cmraft.rpc.PacketUtils.java

private static int writeRpc(AsynchronousSocketChannel channel, Message header, Message body, int totalSize)
        throws IOException, InterruptedException, ExecutionException {
    // writing total size so that server can read all request data in one read
    //LOG.debug("total size:" + totalSize);
    long t = System.currentTimeMillis();

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    writeIntToStream(totalSize, bos);/*from  w  ww.  ja v a2 s .  c  o m*/

    header.writeDelimitedTo(bos);
    if (body != null)
        body.writeDelimitedTo(bos);

    bos.flush();
    byte[] b = bos.toByteArray();
    ByteBuffer buf = ByteBuffer.allocateDirect(totalSize + 4);
    buf.put(b);

    buf.flip();
    channel.write(buf).get();

    if (LOG.isTraceEnabled()) {
        LOG.trace("Write Rpc message to socket, takes " + (System.currentTimeMillis() - t) + " ms, size "
                + totalSize);
        LOG.trace("message:" + body);
    }
    return totalSize;
}

From source file:eu.stratosphere.runtime.io.network.netty.InboundEnvelopeDecoder.java

public InboundEnvelopeDecoder(BufferProviderBroker bufferProviderBroker) {
    this.bufferProviderBroker = bufferProviderBroker;
    this.headerBuffer = ByteBuffer.allocateDirect(OutboundEnvelopeEncoder.HEADER_SIZE);
}