List of usage examples for java.nio ByteBuffer allocate
public static ByteBuffer allocate(int capacity)
From source file:MainClass.java
public static void main(String[] argv) throws Exception { ByteBuffer bb = ByteBuffer.allocate(20); bb.put((byte) 0x07); bb.put((byte) 0x08); bb.put((byte) 0x09); bb.put((byte) 0x10); bb.put((byte) 0x11); bb.put((byte) 0x12); bb.put((byte) 0x13); bb.put((byte) 0x14); bb.position(1).limit(5);// www .ja v a2 s . co m bb.mark(); System.out.println("Expect an exception here"); System.out.println("" + bb.order().toString() + ": " + bb.getLong()); }
From source file:Main.java
public static void main(String[] argv) throws Exception { ByteBuffer buf = ByteBuffer.allocate(10); OutputStream os = new ByteBufferBackedOutputStream(buf); InputStream is = new ByteBufferBackedInputStream(buf); }
From source file:Buffers.java
public static void main(String[] args) { try {/* w ww .j a v a 2s. c o m*/ float[] floats = { 6.61E-39F, 9.918385E-39F }; ByteBuffer bb = ByteBuffer.allocate(floats.length * 4); FloatBuffer fb = bb.asFloatBuffer(); fb.put(floats); CharBuffer cb = bb.asCharBuffer(); System.out.println(cb.toString()); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { ByteBuffer byteBuffer = ByteBuffer.allocate(7).order(ByteOrder.BIG_ENDIAN); CharBuffer charBuffer = byteBuffer.asCharBuffer(); byteBuffer.put(0, (byte) 0); byteBuffer.put(1, (byte) 'H'); byteBuffer.put(2, (byte) 0); byteBuffer.put(3, (byte) 'i'); byteBuffer.put(4, (byte) 0); byteBuffer.put(5, (byte) '!'); byteBuffer.put(6, (byte) 0); println(byteBuffer);/*from w w w . j ava 2s . c o m*/ println(charBuffer); // now slice it differently byteBuffer.position(4); charBuffer = byteBuffer.asCharBuffer(); println(byteBuffer); println(charBuffer); }
From source file:Main.java
public static void main(String[] args) throws Exception { FileChannel fc = new FileInputStream("data.txt").getChannel(); ByteBuffer buff = ByteBuffer.allocate(BSIZE); fc.read(buff);/*from www .ja v a 2s . c o m*/ buff.flip(); while (buff.hasRemaining()) System.out.print((char) buff.get()); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { FileChannel fc = new FileOutputStream("data2.txt").getChannel(); ByteBuffer buff = ByteBuffer.allocate(24); // More than needed buff.asCharBuffer().put("Some text"); fc.write(buff);//from ww w .ja v a 2 s . c o m fc.close(); fc = new FileInputStream("data2.txt").getChannel(); buff.clear(); fc.read(buff); buff.flip(); System.out.println(buff.asCharBuffer()); }
From source file:Main.java
public static void main(String[] args) throws Exception { CharsetEncoder encoder = Charset.forName("US-ASCII").newEncoder(); String response = "java2s.com"; ByteBuffer bb = ByteBuffer.allocate(10); System.out.println(encoder.encode(CharBuffer.wrap(response), bb, true)); }
From source file:Main.java
public static void main(String[] args) throws Exception { CharsetEncoder encoder = Charset.forName("US-ASCII").newEncoder(); String response = "java2s.com"; ByteBuffer bb = ByteBuffer.allocate(10); System.out.println(encoder.flush(bb)); }
From source file:Main.java
public static void main(String args[]) throws Exception { FileInputStream fIn = new FileInputStream("test.txt"); FileChannel fChan = fIn.getChannel(); long fSize = fChan.size(); ByteBuffer mBuf = ByteBuffer.allocate((int) fSize); fChan.read(mBuf, 10);//from www . ja v a 2 s .c o m mBuf.rewind(); for (int i = 0; i < fSize; i++) { System.out.print((char) mBuf.get()); } fChan.close(); fIn.close(); }
From source file:Main.java
public static void main(String args[]) throws Exception { FileInputStream fIn = new FileInputStream("test.txt"); FileChannel fChan = fIn.getChannel(); long fSize = fChan.size(); ByteBuffer mBuf = ByteBuffer.allocate((int) fSize); fChan.read(mBuf);// w w w .j a va2 s .c o m mBuf.rewind(); for (int i = 0; i < fSize; i++) { System.out.print((char) mBuf.get()); } fChan.close(); fIn.close(); }