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 void main(String[] argv) throws Exception {

    byte[] bytes = new byte[10];
    ByteBuffer buf = ByteBuffer.allocateDirect(10);
    buf = ByteBuffer.wrap(bytes);

    System.out.println(Arrays.toString(buf.array()));
    System.out.println(buf.toString());
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ReadableByteChannel channel = new FileInputStream("infile").getChannel();

    ByteBuffer buf = ByteBuffer.allocateDirect(10);

    int numRead = 0;
    while (numRead >= 0) {
        buf.rewind();/*from w ww. j a va  2s  .  c  o m*/

        numRead = channel.read(buf);

        buf.rewind();

        for (int i = 0; i < numRead; i++) {
            byte b = buf.get();
        }
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ReadableByteChannel channel = new FileInputStream("infile").getChannel();

    ByteBuffer buf = ByteBuffer.allocateDirect(10);

    int numRead = 0;
    while (numRead >= 0) {
        buf.rewind();//from ww w  .j av  a 2  s  .  c om

        numRead = channel.read(buf);

        buf.rewind();

        // Read bytes from ByteBuffer; see also
        for (int i = 0; i < numRead; i++) {
            byte b = buf.get();
        }
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ReadableByteChannel channel = new FileInputStream("infile.dat").getChannel();

    ByteBuffer buf = ByteBuffer.allocateDirect(10);

    int numRead = 0;
    while (numRead >= 0) {
        buf.rewind();//ww w . j a  v a2s.  c  o  m

        numRead = channel.read(buf);

        buf.rewind();

        for (int i = 0; i < numRead; i++) {
            byte b = buf.get();
        }
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    SocketChannel sChannel = SocketChannel.open();
    sChannel.configureBlocking(false);/*from   w w w .  ja v  a 2 s  .  c  o m*/
    sChannel.connect(new InetSocketAddress("hostName", 12345));

    ByteBuffer buf = ByteBuffer.allocateDirect(1024);
    buf.put((byte) 0xFF);

    buf.flip();

    int numBytesWritten = sChannel.write(buf);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    File aFile = new File("primes.txt");
    FileInputStream inFile = new FileInputStream(aFile);
    FileChannel inChannel = inFile.getChannel();
    ByteBuffer buf = ByteBuffer.allocateDirect(1024);
    buf.position(buf.limit());/*ww w .  ja v  a2s.co m*/
    while (true) {
        if (buf.remaining() < 8) {
            if (inChannel.read(buf.compact()) == -1) {
                break;
            }
            buf.flip();
        }
        int strLength = (int) buf.getDouble();
        if (buf.remaining() < 2 * strLength) {
            if (inChannel.read(buf.compact()) == -1) {
                break;
            }
            buf.flip();
        }
        byte[] strChars = new byte[2 * strLength];
        buf.get(strChars);
        if (buf.remaining() < 8) {
            if (inChannel.read(buf.compact()) == -1) {
                break;
            }
            buf.flip();
        }
        System.out.println(strLength);
        System.out.println(ByteBuffer.wrap(strChars).asCharBuffer());
        System.out.println(buf.getLong());
    }
    inFile.close();
}

From source file:ExplicitChannelWrite.java

public static void main(String args[]) {
    FileOutputStream fOut;//from w ww . j a va  2 s . co  m
    FileChannel fChan;
    ByteBuffer mBuf;

    try {
        fOut = new FileOutputStream("test.txt");
        fChan = fOut.getChannel();
        mBuf = ByteBuffer.allocateDirect(26);
        for (int i = 0; i < 26; i++)
            mBuf.put((byte) ('A' + i));
        mBuf.rewind();
        fChan.write(mBuf);
        fChan.close();
        fOut.close();
    } catch (IOException exc) {
        System.out.println(exc);
        System.exit(1);
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Charset charset = Charset.forName("ISO-8859-1");
    CharsetDecoder decoder = charset.newDecoder();
    CharsetEncoder encoder = charset.newEncoder();
    ByteBuffer bbuf = ByteBuffer.allocateDirect(1024);

    CharBuffer cbuf = CharBuffer.allocate(1024);

    encoder.encode(cbuf, bbuf, false);/*from   ww w.  ja va 2  s  . co m*/

    bbuf.flip();

    decoder.decode(bbuf, cbuf, false);

    cbuf.flip();

}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    ByteBuffer bbuf = ByteBuffer.wrap(new byte[10]);
    boolean isDirect = bbuf.isDirect(); // false

    bbuf = ByteBuffer.allocate(10);
    isDirect = bbuf.isDirect(); // false

    bbuf = ByteBuffer.allocateDirect(10);
    isDirect = bbuf.isDirect(); // true

}

From source file:Main.java

static public void main(String args[]) throws Exception {
    FileInputStream fin = new FileInputStream("infile.txt");
    FileOutputStream fout = new FileOutputStream("outfile.txt");

    FileChannel inc = fin.getChannel();
    FileChannel outc = fout.getChannel();

    ByteBuffer bb = ByteBuffer.allocateDirect(1024);

    while (true) {
        int ret = inc.read(bb);
        if (ret == -1)
            break;
        bb.flip();/*from  w w w.  ja  v a  2  s. c o m*/
        outc.write(bb);
        bb.clear();
    }
}