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 { File aFile = new File("C:/test.bin"); FileInputStream inFile = new FileInputStream(aFile); FileChannel inChannel = inFile.getChannel(); final int PRIMECOUNT = 6; ByteBuffer buf = ByteBuffer.allocate(8 * PRIMECOUNT); long[] primes = new long[PRIMECOUNT]; while (inChannel.read(buf) != -1) { ((ByteBuffer) (buf.flip())).asLongBuffer().get(primes); for (long prime : primes) { System.out.printf("%10d", prime); }// www .ja va 2 s .c om buf.clear(); } inFile.close(); }
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(); outc.write(bb);/* ww w . j ava 2s.c om*/ bb.clear(); } }
From source file:Main.java
public static void main(String[] args) throws Exception { String source = "s.txt"; String destination = "d.txt"; FileInputStream fis = new FileInputStream(source); FileOutputStream fos = new FileOutputStream(destination); FileChannel fci = fis.getChannel(); FileChannel fco = fos.getChannel(); ByteBuffer buffer = ByteBuffer.allocate(1024); while (true) { int read = fci.read(buffer); if (read == -1) break; buffer.flip(); fco.write(buffer);/*from w ww . ja v a2s.c om*/ buffer.clear(); } }
From source file:Main.java
public static void main(String[] args) throws Exception { File aFile = new File("main.java"); FileInputStream inFile = new FileInputStream(aFile); FileChannel inChannel = inFile.getChannel(); ByteBuffer lengthBuf = ByteBuffer.allocate(8); while (true) { if (inChannel.read(lengthBuf) == -1) { break; }//from ww w. j a va 2 s. c o m lengthBuf.flip(); int strLength = (int) lengthBuf.getDouble(); ByteBuffer buf = ByteBuffer.allocate(2 * strLength + 8); if (inChannel.read(buf) == -1) { break; } buf.flip(); byte[] strChars = new byte[2 * strLength]; buf.get(strChars); System.out.println(strLength); System.out.println(ByteBuffer.wrap(strChars).asCharBuffer()); System.out.println(buf.getLong()); lengthBuf.clear(); } inFile.close(); }
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();/*w w w . j a v a2s . c o m*/ 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:MainClass.java
public static void main(String[] args) { File aFile = new File("file.dat"); FileInputStream inFile = null; try {//from ww w . j a va 2s . c o m inFile = new FileInputStream(aFile); } catch (FileNotFoundException e) { e.printStackTrace(System.err); System.exit(0); } FileChannel inChannel = inFile.getChannel(); final int COUNT = 6; ByteBuffer buf = ByteBuffer.allocate(8 * COUNT); long[] data = new long[COUNT]; try { while (inChannel.read(buf) != -1) { ((ByteBuffer) (buf.flip())).asLongBuffer().get(data); System.out.println(); for (long prime : data) System.out.printf("%10d", prime); buf.clear(); } System.out.println("\nEOF reached."); inFile.close(); } catch (IOException e) { e.printStackTrace(System.err); System.exit(1); } }
From source file:MainClass.java
public static void main(String[] args) throws IOException { DatagramChannel channel = DatagramChannel.open(); SocketAddress address = new InetSocketAddress(0); DatagramSocket socket = channel.socket(); socket.bind(address);//from www. j a va 2s. c o m SocketAddress server = new InetSocketAddress("time-a.nist.gov", 37); channel.connect(server); ByteBuffer buffer = ByteBuffer.allocate(8); buffer.order(ByteOrder.BIG_ENDIAN); // send a byte of data to the server buffer.put((byte) 0); buffer.flip(); channel.write(buffer); // get the buffer ready to receive data buffer.clear(); // fill the first four bytes with zeros buffer.putInt(0); channel.read(buffer); buffer.flip(); // convert seconds since 1900 to a java.util.Date long secondsSince1900 = buffer.getLong(); long differenceBetweenEpochs = 2208988800L; long secondsSince1970 = secondsSince1900 - differenceBetweenEpochs; long msSince1970 = secondsSince1970 * 1000; Date time = new Date(msSince1970); System.out.println(time); }
From source file:MainClass.java
public static void main(String[] args) { File aFile = new File("C:/test.bin"); FileInputStream inFile = null; try {/*from w w w. j a va 2s . c om*/ inFile = new FileInputStream(aFile); } catch (FileNotFoundException e) { e.printStackTrace(System.err); } FileChannel inChannel = inFile.getChannel(); final int PRIMECOUNT = 6; ByteBuffer buf = ByteBuffer.allocate(8 * PRIMECOUNT); long[] primes = new long[PRIMECOUNT]; try { while (inChannel.read(buf) != -1) { ((ByteBuffer) (buf.flip())).asLongBuffer().get(primes); for (long prime : primes) { System.out.printf("%10d", prime); } buf.clear(); } inFile.close(); } catch (IOException e) { e.printStackTrace(System.err); } }
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.putShort(2, (short) 123); System.out.println(Arrays.toString(bbuf.array())); bbuf.flip(); }
From source file:MainClass.java
public static void main(String[] args) { File aFile = new File("primes.txt"); FileInputStream inFile = null; try {/*from w ww. j av a 2s.com*/ inFile = new FileInputStream(aFile); } catch (FileNotFoundException e) { e.printStackTrace(System.err); } FileChannel inChannel = inFile.getChannel(); try { ByteBuffer lengthBuf = ByteBuffer.allocate(8); while (true) { if (inChannel.read(lengthBuf) == -1) { break; } lengthBuf.flip(); int strLength = (int) lengthBuf.getDouble(); ByteBuffer buf = ByteBuffer.allocate(2 * strLength + 8); if (inChannel.read(buf) == -1) { break; } buf.flip(); byte[] strChars = new byte[2 * strLength]; buf.get(strChars); System.out.println(strLength); System.out.println(ByteBuffer.wrap(strChars).asCharBuffer()); System.out.println(buf.getLong()); lengthBuf.clear(); } inFile.close(); } catch (IOException e) { e.printStackTrace(System.err); } }