List of usage examples for java.nio ByteBuffer capacity
public final int capacity()
From source file:Main.java
public static void main(String[] argv) throws Exception { ByteBuffer bbuf = ByteBuffer.allocate(10); int capacity = bbuf.capacity(); // 10 System.out.println(capacity); bbuf.order(ByteOrder.nativeOrder()); bbuf.put("java2s.com".getBytes()); System.out.println(Arrays.toString(bbuf.array())); }
From source file:Main.java
public static void main(String[] argv) throws Exception { byte[] bytes = new byte[10]; ByteBuffer buf = ByteBuffer.wrap(bytes); buf.clear();/* w w w.j ava 2 s . c o m*/ bytes = new byte[buf.capacity()]; buf.get(bytes, 0, bytes.length); }
From source file:Main.java
public static void main(String[] args) { ByteBuffer bb = ByteBuffer.allocate(8); System.out.println("Capacity: " + bb.capacity()); System.out.println("Limit: " + bb.limit()); System.out.println("Position: " + bb.position()); // The mark is not set for a new buffer. Calling the // reset() method throws a runtime exception if the mark is not set. try {/* w w w .ja v a 2 s .co m*/ bb.reset(); System.out.println("Mark: " + bb.position()); } catch (InvalidMarkException e) { System.out.println("Mark is not set"); } }
From source file:Main.java
public static void main(String[] args) throws Exception { String phrase = new String("www.java2s.com\n"); File aFile = new File("test.txt"); FileOutputStream outputFile = null; outputFile = new FileOutputStream(aFile, true); System.out.println("File stream created successfully."); FileChannel outChannel = outputFile.getChannel(); ByteBuffer buf = ByteBuffer.allocate(1024); System.out.println("New buffer: position = " + buf.position() + "\tLimit = " + buf.limit() + "\tcapacity = " + buf.capacity()); // Load the data into the buffer for (char ch : phrase.toCharArray()) { buf.putChar(ch);/* w w w . j a v a 2 s .c om*/ } System.out.println("Buffer after loading: position = " + buf.position() + "\tLimit = " + buf.limit() + "\tcapacity = " + buf.capacity()); buf.flip(); System.out.println("Buffer after flip: position = " + buf.position() + "\tLimit = " + buf.limit() + "\tcapacity = " + buf.capacity()); outChannel.write(buf); outputFile.close(); System.out.println("Buffer contents written to file."); }
From source file:MainClass.java
public static void main(String[] args) { String phrase = new String("text \n"); String dirname = "C:/test"; String filename = "charData.txt"; File dir = new File(dirname); File aFile = new File(dir, filename); FileOutputStream outputFile = null; try {// w w w . j a v a 2 s . com outputFile = new FileOutputStream(aFile, true); System.out.println("File stream created successfully."); } catch (FileNotFoundException e) { e.printStackTrace(System.err); } FileChannel outChannel = outputFile.getChannel(); ByteBuffer buf = ByteBuffer.allocate(1024); System.out.println("New buffer: position = " + buf.position() + "\tLimit = " + buf.limit() + "\tcapacity = " + buf.capacity()); for (char ch : phrase.toCharArray()) { buf.putChar(ch); } System.out.println("Buffer after loading: position = " + buf.position() + "\tLimit = " + buf.limit() + "\tcapacity = " + buf.capacity()); buf.flip(); System.out.println("Buffer after flip: position = " + buf.position() + "\tLimit = " + buf.limit() + "\tcapacity = " + buf.capacity()); try { outChannel.write(buf); outputFile.close(); System.out.println("Buffer contents written to file."); } catch (IOException e) { e.printStackTrace(System.err); } }
From source file:MainClass.java
public static void main(String[] args) { String[] phrases = { "A", "B 1", "C 1.3" }; String dirname = "C:/test"; String filename = "Phrases.txt"; File dir = new File(dirname); File aFile = new File(dir, filename); FileOutputStream outputFile = null; try {// ww w . jav a 2 s . c o m outputFile = new FileOutputStream(aFile, true); } catch (FileNotFoundException e) { e.printStackTrace(System.err); } FileChannel outChannel = outputFile.getChannel(); ByteBuffer buf = ByteBuffer.allocate(1024); System.out.println(buf.position()); System.out.println(buf.limit()); System.out.println(buf.capacity()); CharBuffer charBuf = buf.asCharBuffer(); System.out.println(charBuf.position()); System.out.println(charBuf.limit()); System.out.println(charBuf.capacity()); Formatter formatter = new Formatter(charBuf); int number = 0; for (String phrase : phrases) { formatter.format("%n %s", ++number, phrase); System.out.println(charBuf.position()); System.out.println(charBuf.limit()); System.out.println(charBuf.capacity()); charBuf.flip(); System.out.println(charBuf.position()); System.out.println(charBuf.limit()); System.out.println(charBuf.length()); buf.limit(2 * charBuf.length()); // Set byte buffer limit System.out.println(buf.position()); System.out.println(buf.limit()); System.out.println(buf.remaining()); try { outChannel.write(buf); buf.clear(); charBuf.clear(); } catch (IOException e) { e.printStackTrace(System.err); } } try { outputFile.close(); } catch (IOException e) { e.printStackTrace(System.err); } }
From source file:MainClass.java
public static void main(String[] args) { File aFile = new File("afile.txt"); FileOutputStream outputFile = null; try {/* ww w .j a va 2 s . co m*/ outputFile = new FileOutputStream(aFile, true); System.out.println("File stream created successfully."); } catch (FileNotFoundException e) { e.printStackTrace(System.err); } ByteBuffer buf = ByteBuffer.allocate(1024); System.out.println("\nByte buffer:"); System.out.printf("position = %2d Limit = %4d capacity = %4d%n", buf.position(), buf.limit(), buf.capacity()); // Create a view buffer CharBuffer charBuf = buf.asCharBuffer(); System.out.println("Char view buffer:"); System.out.printf("position = %2d Limit = %4d capacity = %4d%n", charBuf.position(), charBuf.limit(), charBuf.capacity()); try { outputFile.close(); // Close the O/P stream & the channel } catch (IOException e) { e.printStackTrace(System.err); } }
From source file:MainClass.java
public static void main(String[] args) { String phrase = new String("www.java2s.com\n"); File aFile = new File("test.txt"); FileOutputStream outputFile = null; try {/*from www. j av a 2 s.c om*/ outputFile = new FileOutputStream(aFile, true); System.out.println("File stream created successfully."); } catch (FileNotFoundException e) { e.printStackTrace(System.err); } FileChannel outChannel = outputFile.getChannel(); ByteBuffer buf = ByteBuffer.allocate(1024); System.out.println("New buffer: position = " + buf.position() + "\tLimit = " + buf.limit() + "\tcapacity = " + buf.capacity()); // Load the data into the buffer for (char ch : phrase.toCharArray()) { buf.putChar(ch); } System.out.println("Buffer after loading: position = " + buf.position() + "\tLimit = " + buf.limit() + "\tcapacity = " + buf.capacity()); buf.flip(); System.out.println("Buffer after flip: position = " + buf.position() + "\tLimit = " + buf.limit() + "\tcapacity = " + buf.capacity()); try { outChannel.write(buf); outputFile.close(); System.out.println("Buffer contents written to file."); } catch (IOException e) { e.printStackTrace(System.err); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { // Create a ByteBuffer from a byte array byte[] bytes = new byte[10]; ByteBuffer buffer = ByteBuffer.wrap(bytes); // Retrieve bytes between the position and limit bytes = new byte[buffer.remaining()]; buffer.get(bytes, 0, bytes.length);/* w w w . j av a 2 s . c om*/ // Retrieve all bytes in the buffer buffer.clear(); bytes = new byte[buffer.capacity()]; buffer.get(bytes, 0, bytes.length); }
From source file:Main.java
protected static ByteBuffer renewBuffer(ByteBuffer buf) { buf.position(0); buf.limit(buf.capacity()); return buf; }