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:Main.java

public static DoubleBuffer createDoubleBuffer(final int size) {
    final DoubleBuffer buf = ByteBuffer.allocateDirect(8 * size).order(ByteOrder.nativeOrder())
            .asDoubleBuffer();//from w  w w .j  av  a 2  s  .c o  m
    buf.clear();
    return buf;
}

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

/**
 * Creates a new decompressor.//from   w ww. ja v  a  2s .c  o  m
 */
public Bzip2Decompressor(boolean conserveMemory, int directBufferSize) {
    this.conserveMemory = conserveMemory;
    this.directBufferSize = directBufferSize;
    compressedDirectBuf = ByteBuffer.allocateDirect(directBufferSize);
    uncompressedDirectBuf = ByteBuffer.allocateDirect(directBufferSize);
    uncompressedDirectBuf.position(directBufferSize);

    stream = init(conserveMemory ? 1 : 0);
}

From source file:net.lightbody.bmp.proxy.jetty.http.nio.SocketChannelOutputStream.java

/** Constructor.
 * 
 */
public SocketChannelOutputStream(SocketChannel channel, int bufferSize) {
    _channel = channel;
    _buffer = ByteBuffer.allocateDirect(bufferSize);
}

From source file:org.apache.hadoop.mapred.nativetask.NativeBatchProcessor.java

public NativeBatchProcessor(Class<IK> iKClass, Class<IV> iVClass, Class<OK> oKClass, Class<OV> oVClass,
        String nativeHandlerName, int inputBufferCapacity, int outputBufferCapacity) throws IOException {
    if (inputBufferCapacity > 0) {
        this.inputBuffer = ByteBuffer.allocateDirect(inputBufferCapacity);
        this.inputBuffer.order(ByteOrder.BIG_ENDIAN);
    }/*from  w ww .ja v  a2 s. c om*/
    if (outputBufferCapacity > 0) {
        this.outputBuffer = ByteBuffer.allocateDirect(outputBufferCapacity);
        this.outputBuffer.order(ByteOrder.BIG_ENDIAN);
    }
    this.nativeHandlerName = nativeHandlerName;

    this.nativeReader = new NativeInputStream(outputBuffer);
    this.nativeWriter = new NativeOutputStream(inputBuffer) {

        @Override
        public void flush() throws IOException {
            flushInputAndProcess();
        }

        @Override
        public void close() throws IOException {
            finishInput();
        }

    };

    if (null != iKClass && null != iVClass) {
        this.serializer = new KVSerializer<IK, IV>(iKClass, iVClass);
    }
    if (null != oKClass && null != oVClass) {
        this.deserializer = new KVSerializer<OK, OV>(oKClass, oVClass);
    }
}

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

/**
 * Converts the specified float 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  .  j  a v a  2  s. c  o  m
 *
 * @param buffer
 *            The float buffer to convert
 * @return The converted float buffer or the source buffer if no conversion
 *         is needed
 */

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

    if (log.isTraceEnabled())
        log.trace("Converting endianess of " + buffer.capacity() + " floats");

    final ByteBuffer bytes = ByteBuffer.allocateDirect(buffer.capacity());
    bytes.order(ByteOrder.nativeOrder());
    final FloatBuffer floats = bytes.asFloatBuffer();
    floats.put(buffer).rewind();
    return floats;
}

From source file:net.jenet.Packet.java

/**
 * Creates a new Packet./* ww w  .ja  v  a 2  s  .  c  o m*/
 * The constructor allocates a new packet and allocates a
 * buffer of <code>dataLength</code> bytes for it.
 * 
 * @param dataLength
 *                      The size in bytes of this packet.
 * @param flags
 *                      An byte inidicating the how to handle this packet.
 */
public Packet(int dataLength, int flags) {
    data = ByteBuffer.allocateDirect(dataLength);
    this.dataLength = dataLength;
    this.flags = flags;
}

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

public void ensureSize(int size) {
    if (buffer.remaining() - size < 0) { // check the remain size
        // enlarge new buffer and copy writing data
        int newBlockSize = UnsafeUtil.alignedSize(buffer.capacity() * 2);
        ByteBuffer newByteBuf = ByteBuffer.allocateDirect(newBlockSize);
        long newAddress = ((DirectBuffer) newByteBuf).address();
        UNSAFE.copyMemory(this.address, newAddress, buffer.limit());
        LOG.debug("Increase the buffer size to " + FileUtil.humanReadableByteCount(newBlockSize, false));

        // release existing buffer and replace variables
        UnsafeUtil.free(buffer);//from w  w w  .j a  v  a 2  s. c  o m
        buffer = newByteBuf;
        address = newAddress;
    }
}

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

private static ByteBuffer directify(byte[] readBufs, int dataStart, int dataLen) {
    ByteBuffer newBuf = null;//from  w  ww .  j av a  2s . c o m
    newBuf = ByteBuffer.allocateDirect(dataLen);
    newBuf.position(0);
    newBuf.mark();
    newBuf.put(readBufs, dataStart, dataLen);
    newBuf.reset();
    newBuf.limit(dataLen);
    return newBuf;
}

From source file:com.sunchenbin.store.feilong.core.io.IOReaderUtil.java

/**
 * ?.//from   w w w.  ja v a 2  s.co  m
 *
 * @param file
 *            
 * @param charsetName
 *            ?,isNullOrEmpty, {@link CharsetType#UTF8}
 * @return the file content
 * @see org.apache.commons.io.FileUtils#readFileToString(File, Charset)
 */
public static String getFileContent(File file, String charsetName) {
    if (Validator.isNullOrEmpty(file)) {
        throw new NullPointerException("the file is null or empty!");
    }
    // ?
    final int capacity = 186140;
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(capacity);
    StringBuilder sb = new StringBuilder(capacity);

    FileInputStream fileInputStream = null;
    try {
        fileInputStream = new FileInputStream(file);

        // ?????.
        FileChannel fileChannel = fileInputStream.getChannel();
        String useCharsetName = Validator.isNullOrEmpty(charsetName) ? DEFAULT_CHARSET_NAME : charsetName;
        Charset charset = Charset.forName(useCharsetName);
        while (fileChannel.read(byteBuffer) != -1) {
            // ??
            byteBuffer.flip();
            CharBuffer charBuffer = charset.decode(byteBuffer);
            sb.append(charBuffer.toString());
            byteBuffer.clear();
        }
        return sb.toString();

    } catch (FileNotFoundException e) {
        throw new UncheckedIOException(e);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    } finally {
        // ? ,^_^
        IOUtils.closeQuietly(fileInputStream);
    }
}

From source file:org.apache.james.mpt.session.ExternalSession.java

public ExternalSession(SocketChannel socket, Monitor monitor, String shabang, boolean debug) {
    super();//from  ww w . j a  va2  s .  co m
    this.socket = socket;
    this.monitor = monitor;
    readBuffer = ByteBuffer.allocateDirect(2048);
    ascii = Charset.forName("US-ASCII");
    lineEndBuffer = ByteBuffer.wrap(CRLF);
    this.shabang = shabang;
}