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.hadoop.util.NativeStair.java

public static boolean encodeBulk(byte[][] inputs, byte[][] outputs) {
    ByteBuffer[] inputBuffers = new ByteBuffer[inputs.length];
    ByteBuffer[] outputBuffers = new ByteBuffer[outputs.length];
    int bufferLen = inputs[0].length;

    for (int i = 0; i < inputs.length; i++) {
        inputBuffers[i] = directify(inputs[i], 0, bufferLen);
    }//from  w w  w.java 2 s. c o  m
    for (int i = 0; i < outputs.length; i++) {
        outputBuffers[i] = ByteBuffer.allocateDirect(bufferLen);
    }

    //LOG.info("inputLength:" + inputs.length +
    //    " outputLength:" + outputs.length +
    //    " bufferLength:" + bufferLen);
    if (!nativeEncodeBulk(inputBuffers, inputs.length, outputBuffers, outputs.length, bufferLen)) {
        LOG.info("nativeEncodeBulk returns false");
        return false;
    }
    //LOG.info("nativeEncodeBulk returns true");

    for (int i = 0; i < outputs.length; i++) {
        outputBuffers[i].get(outputs[i]);
    }

    return true;
}

From source file:com.l2jfree.loginserver.L2LoginIdentifier.java

private synchronized void load() {
    if (isLoaded())
        return;/*  w  w  w.j a  va  2  s.c o m*/

    File f = new File(System.getProperty("user.home", null), FILENAME);
    ByteBuffer bb = ByteBuffer.allocateDirect(8);

    if (!f.exists() || f.length() != 8) {
        _uid = getRandomUID();
        _loaded = true;
        _log.info("A new UID has been generated for this login server.");

        FileOutputStream fos = null;
        try {
            f.createNewFile();
            fos = new FileOutputStream(f);
            FileChannel fc = fos.getChannel();
            bb.putLong(getUID());
            bb.flip();
            fc.write(bb);
            fos.flush();
        } catch (IOException e) {
            _log.warn("Could not store login server's UID!", e);
        } finally {
            IOUtils.closeQuietly(fos);
            f.setReadOnly();
        }
    } else {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(f);
            FileChannel fc = fis.getChannel();
            fc.read(bb);
        } catch (IOException e) {
            _log.warn("Could not read stored login server's UID!", e);
        } finally {
            IOUtils.closeQuietly(fis);
        }

        if (bb.position() > 0) {
            bb.flip();
            _uid = bb.getLong();
        } else
            _uid = getRandomUID();
        _loaded = true;
    }
}

From source file:org.apache.tajo.tuple.BaseTupleBuilder.java

public BaseTupleBuilder(Schema schema) {
    super(SchemaUtil.toDataTypes(schema));
    buffer = ByteBuffer.allocateDirect(64 * StorageUnit.KB).order(ByteOrder.nativeOrder());
    address = UnsafeUtil.getAddress(buffer);
}

From source file:org.apache.hadoop.raid.JRSOEncoder.java

public JRSOEncoder(Configuration conf, int stripeSize, int paritySize) {
    super(conf, stripeSize, paritySize);
    bufSize = 64 * 1024;/*  www .  ja  va  2 s  .  c  o m*/
    dataBufs = ByteBuffer.allocateDirect(bufSize * stripeSize);
    parityBufs = ByteBuffer.allocateDirect(bufSize * paritySize);
    reserve = ByteBuffer.allocateDirect(1024);
}

From source file:org.apache.tajo.tuple.offheap.OffHeapMemory.java

public OffHeapMemory(ResizableLimitSpec limitSpec) {
    this(ByteBuffer.allocateDirect((int) limitSpec.initialSize()).order(ByteOrder.nativeOrder()), limitSpec);
}

From source file:org.apache.hadoop.raid.IAOEncoder.java

public IAOEncoder(Configuration conf, int stripeSize, int paritySize) {
    super(conf, stripeSize, paritySize);
    LOG.info("bufsize: " + bufSize);
    dataBufs = ByteBuffer.allocateDirect(bufSize * stripeSize);
    parityBufs = ByteBuffer.allocateDirect(bufSize * paritySize);
    reserve = ByteBuffer.allocateDirect(1024);
}

From source file:org.apache.hadoop.raid.PMOEncoder.java

public PMOEncoder(Configuration conf, int stripeSize, int paritySize) {
    super(conf, stripeSize, paritySize);
    LOG.info("bufsize: " + bufSize);
    dataBufs = ByteBuffer.allocateDirect(bufSize * stripeSize);
    parityBufs = ByteBuffer.allocateDirect(bufSize * paritySize);
    reserve = ByteBuffer.allocateDirect(1024);
}

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

/**
* 
*///ww w . j a va2  s  .  c om
public AbstractPort(IConnectionPolicy policy) {
    this.policy = policy;
    sendQueue = new LinkedList<OutPacket>();
    sendBuf = ByteBuffer.allocateDirect(QQ.QQ_MAX_PACKET_SIZE);
    receiveBuf = ByteBuffer.allocateDirect(QQ.QQ_MAX_PACKET_SIZE);
}

From source file:org.apache.hadoop.mapred.nativetask.buffer.DirectBufferPool.java

public synchronized ByteBuffer borrowBuffer(int capacity) throws IOException {
    Queue<WeakReference<ByteBuffer>> list = bufferMap.get(capacity);
    if (null == list) {
        return ByteBuffer.allocateDirect(capacity);
    }/* w ww .  j  a va 2  s.c  o  m*/
    WeakReference<ByteBuffer> ref;
    while ((ref = list.poll()) != null) {
        ByteBuffer buf = ref.get();
        if (buf != null) {
            return buf;
        }
    }
    return ByteBuffer.allocateDirect(capacity);
}

From source file:com.msr.dnsdemo.network.DownloadFile.java

public static void fastChannelCopy(final ReadableByteChannel src, final WritableByteChannel dest)
        throws IOException, NullPointerException {
    if (src != null && dest != null) {
        final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
        while (src.read(buffer) != -1) {
            // prepare the buffer to be drained
            buffer.flip();//from   w  w  w .jav  a2  s.c  om
            // 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);
        }
    }
}