List of usage examples for java.nio ByteBuffer flip
public final Buffer flip()
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);/*w w w . j a v a 2 s . co m*/ buff.flip(); while (buff.hasRemaining()) System.out.print((char) buff.get()); }
From source file:Main.java
public static void main(String[] argv) throws Exception { SocketChannel sChannel = SocketChannel.open(); sChannel.configureBlocking(false);/* ww w. java 2 s . co m*/ sChannel.connect(new InetSocketAddress("hostName", 12345)); ByteBuffer buf = ByteBuffer.allocateDirect(1024); buf.put((byte) 0xFF); buf.flip(); int numBytesWritten = sChannel.write(buf); }
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);// w w w . jav a 2 s .c o m bb.flip(); String fileContent = new String(bb.array()); fc.close(); fc = null; System.out.println("fileContent = " + fileContent); }
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())); fc.close();//from w w w. ja v a2 s. co m fc = new FileInputStream("data2.txt").getChannel(); ByteBuffer buff = ByteBuffer.allocate(BSIZE); fc.read(buff); buff.flip(); System.out.println(buff.asCharBuffer()); // Decode using this system's default Charset: buff.rewind(); String encoding = System.getProperty("file.encoding"); System.out.println("Decoded using " + encoding + ": " + Charset.forName(encoding).decode(buff)); // Or, we could encode with something that will print: fc = new FileOutputStream("data2.txt").getChannel(); fc.write(ByteBuffer.wrap("Some text".getBytes("UTF-16BE"))); fc.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(); out.write(buffer);//from w ww. java 2s. com buffer.clear(); } }
From source file:GetChannel.java
public static void main(String[] args) throws Exception { // Write a file: FileChannel fc = new FileOutputStream("data.txt").getChannel(); fc.write(ByteBuffer.wrap("Some text ".getBytes())); fc.close();/*from w w w.java2s.c om*/ // Add to the end of the file: fc = new RandomAccessFile("data.txt", "rw").getChannel(); fc.position(fc.size()); // Move to the end fc.write(ByteBuffer.wrap("Some more".getBytes())); fc.close(); // Read the file: fc = new FileInputStream("data.txt").getChannel(); ByteBuffer buff = ByteBuffer.allocate(BSIZE); fc.read(buff); buff.flip(); while (buff.hasRemaining()) System.out.print((char) buff.get()); }
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(); out.write(buffer);//from w ww. jav a 2s .c o m buffer.clear(); } }
From source file:MainClass.java
public static void main(String[] args) throws Exception { String fromFileName = args[0]; String toFileName = args[1];//w ww. j ava2 s .c o m 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(); out.write(buff);//from w ww . j a v a2 s. c om buff.clear(); } in.close(); out.close(); }
From source file:Test.java
public static void main(String[] args) throws Exception { Path path = Paths.get("C:/home/docs/users.txt"); UserDefinedFileAttributeView view = Files.getFileAttributeView(path, UserDefinedFileAttributeView.class); view.write("publishable", Charset.defaultCharset().encode("true")); System.out.println("Publishable set"); String name = "publishable"; ByteBuffer buffer = ByteBuffer.allocate(view.size(name)); view.read(name, buffer);//from w ww . jav a 2 s . co m buffer.flip(); String value = Charset.defaultCharset().decode(buffer).toString(); System.out.println(value); }