List of usage examples for java.nio ByteBuffer clear
public final Buffer clear()
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(); bytes = new byte[buf.capacity()]; buf.get(bytes, 0, bytes.length);/*from w w w . j a v a2 s .com*/ }
From source file:MainClass.java
public static void main(String[] args) throws Exception { FileChannel fc = new FileOutputStream("data2.txt").getChannel(); fc.write(ByteBuffer.wrap("Some text".getBytes("UTF-16BE"))); fc.close();// www . j a va 2s . com ByteBuffer buff = ByteBuffer.allocate(BSIZE); // Now try reading again: 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 { 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 ww . j av a 2s. co m*/ out.write(buffer); buffer.clear(); } }
From source file:MainClass.java
public static void main(String[] args) throws Exception { String fromFileName = args[0]; String toFileName = args[1];//w w w . j ava2 s. com FileChannel in = new FileInputStream(fromFileName).getChannel(); FileChannel out = new FileOutputStream(toFileName).getChannel(); ByteBuffer buff = ByteBuffer.allocate(32 * 1024); while (in.read(buff) > 0) { buff.flip(); out.write(buff); buff.clear(); } in.close(); out.close(); }
From source file:CopyChannels.java
public static void main(String[] args) throws Exception { String fromFileName = "from.txt"; String toFileName = "to.txt"; FileChannel in = new FileInputStream(fromFileName).getChannel(); FileChannel out = new FileOutputStream(toFileName).getChannel(); ByteBuffer buff = ByteBuffer.allocate(32 * 1024); while (in.read(buff) > 0) { buff.flip();//from www. ja v a 2 s . c o m out.write(buff); buff.clear(); } in.close(); out.close(); }
From source file:Main.java
public static void main(String[] args) throws IOException { FileInputStream fin = new FileInputStream(args[0]); GZIPInputStream gzin = new GZIPInputStream(fin); ReadableByteChannel in = Channels.newChannel(gzin); WritableByteChannel out = Channels.newChannel(System.out); ByteBuffer buffer = ByteBuffer.allocate(65536); while (in.read(buffer) != -1) { buffer.flip();/*w w w .ja va2 s.c om*/ out.write(buffer); buffer.clear(); } }
From source file:Main.java
public static void main(String[] args) throws Exception { String fromFileName = "from.txt"; String toFileName = "to.txt"; FileChannel in = new FileInputStream(fromFileName).getChannel(); FileChannel out = new FileOutputStream(toFileName).getChannel(); ByteBuffer buff = ByteBuffer.allocateDirect(32 * 1024); while (in.read(buff) > 0) { buff.flip();// www .j a v a2s . com out.write(buff); buff.clear(); } in.close(); out.close(); }
From source file:ChannelCopy.java
public static void main(String[] args) throws Exception { if (args.length != 2) { System.out.println("arguments: sourcefile destfile"); System.exit(1);/*from ww w . j av a 2s . com*/ } FileChannel in = new FileInputStream(args[0]).getChannel(), out = new FileOutputStream(args[1]).getChannel(); ByteBuffer buffer = ByteBuffer.allocate(BSIZE); while (in.read(buffer) != -1) { buffer.flip(); // Prepare for writing out.write(buffer); buffer.clear(); // Prepare for reading } }
From source file:Main.java
public static void main(String[] args) { ByteBuffer bb = ByteBuffer.allocate(2); System.out.println("Default Byte Order: " + bb.order()); bb.putShort((short) 300); bb.flip();/* w w w . j a v a2 s. co m*/ showByteOrder(bb); bb.clear(); bb.order(ByteOrder.LITTLE_ENDIAN); bb.putShort((short) 300); bb.flip(); showByteOrder(bb); }
From source file:MainClass.java
public static void main(String[] args) throws IOException { URL u = new URL("http://www.java2s.com"); String host = u.getHost();/* w w w . j a v a2 s.com*/ int port = 80; String file = "/"; SocketAddress remote = new InetSocketAddress(host, port); SocketChannel channel = SocketChannel.open(remote); FileOutputStream out = new FileOutputStream("yourfile.htm"); FileChannel localFile = out.getChannel(); String request = "GET " + file + " HTTP/1.1\r\n" + "User-Agent: HTTPGrab\r\n" + "Accept: text/*\r\n" + "Connection: close\r\n" + "Host: " + host + "\r\n" + "\r\n"; ByteBuffer header = ByteBuffer.wrap(request.getBytes("US-ASCII")); channel.write(header); ByteBuffer buffer = ByteBuffer.allocate(8192); while (channel.read(buffer) != -1) { buffer.flip(); localFile.write(buffer); buffer.clear(); } localFile.close(); channel.close(); }