List of usage examples for java.nio ByteBuffer clear
public final Buffer clear()
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(); }/* w w w. j a v a 2s . co m*/ inFile.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { DatagramChannel client = null; client = DatagramChannel.open(); client.bind(null);/*from ww w. jav a 2s. com*/ String msg = "Hello"; ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes()); InetSocketAddress serverAddress = new InetSocketAddress("localhost", 8989); client.send(buffer, serverAddress); buffer.clear(); client.receive(buffer); buffer.flip(); int limits = buffer.limit(); byte bytes[] = new byte[limits]; buffer.get(bytes, 0, limits); String response = new String(bytes); System.out.println("Server responded: " + response); client.close(); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { DatagramChannel channel = DatagramChannel.open(); DatagramSocket socket = channel.socket(); SocketAddress address = new InetSocketAddress(9999); socket.bind(address);/*from ww w. ja v a2s.c om*/ ByteBuffer buffer = ByteBuffer.allocateDirect(65507); while (true) { SocketAddress client = channel.receive(buffer); buffer.flip(); channel.send(buffer, client); buffer.clear(); } }
From source file:MainClass.java
public static void main(String[] args) { try {/*ww w. ja va2 s .co m*/ File aFile = new File("test.txt"); FileOutputStream outputFile = null; outputFile = new FileOutputStream(aFile, true); FileChannel outChannel = outputFile.getChannel(); ByteBuffer buf = ByteBuffer.allocate(200); buf.putInt(10).asCharBuffer().put("www.java2s.com"); buf.position(10).flip(); outChannel.write(buf); buf.clear(); outputFile.close(); } catch (Exception e) { e.printStackTrace(); } }
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);/* ww w .j a va2 s.com*/ 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
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.allocate(1024); while (true) { int ret = inc.read(bb); if (ret == -1) break; bb.flip();// w ww .j a v a 2 s .c o m outc.write(bb); bb.clear(); } }
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();/* w ww . ja v a 2s. c o m*/ outc.write(bb); bb.clear(); } }
From source file:MainClass.java
public static void main(String[] args) throws Exception { int port = 19; SocketAddress address = new InetSocketAddress("127.0.0.1", port); SocketChannel client = SocketChannel.open(address); ByteBuffer buffer = ByteBuffer.allocate(74); WritableByteChannel out = Channels.newChannel(System.out); while (client.read(buffer) != -1) { buffer.flip();/*from ww w . j ava 2 s . c om*/ out.write(buffer); buffer.clear(); } }
From source file:MainClass.java
public static void main(String[] args) throws Exception { int port = 1919; SocketAddress address = new InetSocketAddress("127.0.0.1", port); SocketChannel client = SocketChannel.open(address); ByteBuffer buffer = ByteBuffer.allocate(4); IntBuffer view = buffer.asIntBuffer(); for (int expected = 0;; expected++) { client.read(buffer);//from w w w .j a v a2 s . c o m int actual = view.get(); buffer.clear(); view.rewind(); if (actual != expected) { System.err.println("Expected " + expected + "; was " + actual); break; } System.out.println(actual); } }
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);/*from w w w . j av a2 s . c om*/ // Retrieve all bytes in the buffer buffer.clear(); bytes = new byte[buffer.capacity()]; buffer.get(bytes, 0, bytes.length); }