List of usage examples for java.nio ByteBuffer allocate
public static ByteBuffer allocate(int capacity)
From source file:Main.java
public static void main(String args[]) throws IOException { FileInputStream fis = new FileInputStream("FileChannelExample.java"); FileChannel fc = fis.getChannel(); ByteBuffer bb = ByteBuffer.allocate((int) fc.size()); fc.read(bb);/*from w w w . j a va 2 s . c om*/ bb.flip(); String fileContent = new String(bb.array()); fc.close(); fc = null; System.out.println("fileContent = " + fileContent); }
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
public static void main(String[] argv) throws Exception { // Create a ByteBuffer using a byte array byte[] bytes = new byte[10]; ByteBuffer buffer = ByteBuffer.wrap(bytes); // Create a non-direct ByteBuffer with a 10 byte capacity // The underlying storage is a byte array. buffer = ByteBuffer.allocate(10); // Create a memory-mapped ByteBuffer with a 10 byte capacity. buffer = ByteBuffer.allocateDirect(10); }
From source file:Test.java
public static void main(String[] args) throws Exception { Path file = Paths.get("/usr/a.txt"); AsynchronousFileChannel channel = AsynchronousFileChannel.open(file); ByteBuffer buffer = ByteBuffer.allocate(100_000); Future<Integer> result = channel.read(buffer, 0); while (!result.isDone()) { ProfitCalculator.calculateTax(); }//from w w w. jav a 2s . c o m Integer bytesRead = result.get(); System.out.println("Bytes read [" + bytesRead + "]"); }
From source file:IntBufferDemo.java
public static void main(String[] args) { ByteBuffer bb = ByteBuffer.allocate(BSIZE); IntBuffer ib = bb.asIntBuffer(); // Store an array of int: ib.put(new int[] { 11, 42, 47, 99, 143, 811, 1016 }); // Absolute location read and write: System.out.println(ib.get(3)); ib.put(3, 1811);/*from w ww. j a v a 2 s .co m*/ ib.rewind(); while (ib.hasRemaining()) { int i = ib.get(); if (i == 0) break; // Else we'll get the entire buffer System.out.println(i); } }
From source file:Main.java
public static void main(String[] args) throws Exception { File aFile = new File("main.java"); FileInputStream inFile = new FileInputStream(aFile); FileChannel inChannel = inFile.getChannel(); ByteBuffer lengthBuf = ByteBuffer.allocate(8); while (true) { if (inChannel.read(lengthBuf) == -1) { break; }// w w w .j a v a2 s. com lengthBuf.flip(); int strLength = (int) lengthBuf.getDouble(); ByteBuffer buf = ByteBuffer.allocate(2 * strLength + 8); if (inChannel.read(buf) == -1) { break; } buf.flip(); byte[] strChars = new byte[2 * strLength]; buf.get(strChars); System.out.println(strLength); System.out.println(ByteBuffer.wrap(strChars).asCharBuffer()); System.out.println(buf.getLong()); lengthBuf.clear(); } inFile.close(); }
From source file:GetData.java
public static void main(String[] args) { ByteBuffer bb = ByteBuffer.allocate(BSIZE); // Allocation automatically zeroes the ByteBuffer: int i = 0;/* w w w. j a v a 2s . com*/ while (i++ < bb.limit()) if (bb.get() != 0) System.out.println("nonzero"); System.out.println("i = " + i); bb.rewind(); // Store and read a char array: bb.asCharBuffer().put("Howdy!"); char c; while ((c = bb.getChar()) != 0) System.out.print(c + " "); System.out.println(); bb.rewind(); // Store and read a short: bb.asShortBuffer().put((short) 471142); System.out.println(bb.getShort()); bb.rewind(); // Store and read an int: bb.asIntBuffer().put(99471142); System.out.println(bb.getInt()); bb.rewind(); // Store and read a long: bb.asLongBuffer().put(99471142); System.out.println(bb.getLong()); bb.rewind(); // Store and read a float: bb.asFloatBuffer().put(99471142); System.out.println(bb.getFloat()); bb.rewind(); // Store and read a double: bb.asDoubleBuffer().put(99471142); System.out.println(bb.getDouble()); bb.rewind(); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { File aFile = new File("charData.xml"); FileInputStream inFile = null; inFile = new FileInputStream(aFile); FileChannel inChannel = inFile.getChannel(); ByteBuffer buf = ByteBuffer.allocate(48); while (inChannel.read(buf) != -1) { System.out.println("String read: " + ((ByteBuffer) (buf.flip())).asCharBuffer().get(0)); buf.clear();/*from w w w. j a va 2 s . c o m*/ } inFile.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { FileChannel in = new FileInputStream("source.txt").getChannel(), out = new FileOutputStream("target.txt").getChannel(); ByteBuffer buffer = ByteBuffer.allocate(BSIZE); while (in.read(buffer) != -1) { buffer.flip();/*from w w w .j av a 2s . c om*/ out.write(buffer); buffer.clear(); } }
From source file:Main.java
public static void main(String[] args) throws Exception { File aFile = new File("C:/test.bin"); FileInputStream inFile = new FileInputStream(aFile); FileChannel inChannel = inFile.getChannel(); final int PRIMECOUNT = 6; ByteBuffer buf = ByteBuffer.allocate(8 * PRIMECOUNT); long[] primes = new long[PRIMECOUNT]; while (inChannel.read(buf) != -1) { ((ByteBuffer) (buf.flip())).asLongBuffer().get(primes); for (long prime : primes) { System.out.printf("%10d", prime); }/*from ww w .j a v a 2 s . c o m*/ buf.clear(); } inFile.close(); }