List of usage examples for java.nio ByteBuffer clear
public final Buffer clear()
From source file:Test.java
public static void main(String[] args) throws IOException { Path path = Paths.get("/users.txt"); try (SeekableByteChannel sbc = Files.newByteChannel(path)) { ByteBuffer buffer = ByteBuffer.allocate(1024); sbc.position(4);// w w w .ja v a 2 s.c om sbc.read(buffer); for (int i = 0; i < 5; i++) { System.out.print((char) buffer.get(i)); } buffer.clear(); sbc.position(0); sbc.read(buffer); for (int i = 0; i < 4; i++) { System.out.print((char) buffer.get(i)); } } }
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();//from www . j av a 2 s .c o m fco.write(buffer); buffer.clear(); } }
From source file:Test.java
public static void main(String[] args) throws IOException { Path path = Paths.get("/users.txt"); try (SeekableByteChannel sbc = Files.newByteChannel(path)) { ByteBuffer buffer = ByteBuffer.allocate(1024); sbc.position(4);//from www .j a v a 2 s . co m sbc.read(buffer); for (int i = 0; i < 5; i++) { System.out.print((char) buffer.get(i)); } buffer.clear(); sbc.position(0); sbc.read(buffer); for (int i = 0; i < 4; i++) { System.out.print((char) buffer.get(i)); } sbc.position(0); buffer = ByteBuffer.allocate(1024); String encoding = System.getProperty("file.encoding"); int numberOfBytesRead = sbc.read(buffer); System.out.println("Number of bytes read: " + numberOfBytesRead); while (numberOfBytesRead > 0) { buffer.rewind(); System.out.print("[" + Charset.forName(encoding).decode(buffer) + "]"); buffer.flip(); numberOfBytesRead = sbc.read(buffer); System.out.println("\nNumber of bytes read: " + numberOfBytesRead); } } }
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 w ww . ja v a2 s . co 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:Main.java
public static void main(String[] args) { File inputFile = new File("test1.txt"); if (!inputFile.exists()) { System.out.println("The input file " + inputFile.getAbsolutePath() + " does not exist."); System.out.println("Aborted the file reading process."); return;//w ww. j a v a2s.co m } try (FileChannel fileChannel = new FileInputStream(inputFile).getChannel()) { ByteBuffer buffer = ByteBuffer.allocate(1024); while (fileChannel.read(buffer) > 0) { buffer.flip(); while (buffer.hasRemaining()) { byte b = buffer.get(); System.out.print((char) b); } buffer.clear(); } } catch (IOException e) { e.printStackTrace(); } }
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); }// w w w .ja va2 s .com buf.clear(); } inFile.close(); }
From source file:UDPTimeClient.java
public static void main(String[] args) throws Exception { DatagramChannel channel = DatagramChannel.open(); // port 0 selects any available port SocketAddress address = new InetSocketAddress(0); DatagramSocket socket = channel.socket(); socket.setSoTimeout(5000);//from w w w . j a v a2 s . co m socket.bind(address); SocketAddress server = new InetSocketAddress("time.nist.gov", 37); ByteBuffer buffer = ByteBuffer.allocate(8192); // time protocol always uses big-endian order buffer.order(ByteOrder.BIG_ENDIAN); // Must put at least one byte of data in the buffer; // it doesn't matter what it is. buffer.put((byte) 65); buffer.flip(); channel.send(buffer, server); buffer.clear(); buffer.put((byte) 0).put((byte) 0).put((byte) 0).put((byte) 0); channel.receive(buffer); buffer.flip(); long secondsSince1970 = buffer.getLong(); System.out.println(secondsSince1970); channel.close(); }
From source file:MainClass.java
public static void main(String[] args) { File aFile = new File("C:/test.bin"); FileInputStream inFile = null; try {// ww w.j a v a2 s . 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:MainClass.java
public static void main(String[] args) { File aFile = new File("file.dat"); FileInputStream inFile = null; try {/* www .j a v a 2 s. co 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:UDPTimeServer.java
public static void main(String[] args) throws IOException { int port = 37; ByteBuffer in = ByteBuffer.allocate(8192); ByteBuffer out = ByteBuffer.allocate(8); out.order(ByteOrder.BIG_ENDIAN); SocketAddress address = new InetSocketAddress(port); DatagramChannel channel = DatagramChannel.open(); DatagramSocket socket = channel.socket(); socket.bind(address);/*from w ww . j ava 2 s . co m*/ System.err.println("bound to " + address); while (true) { try { in.clear(); SocketAddress client = channel.receive(in); System.err.println(client); long secondsSince1970 = System.currentTimeMillis(); out.clear(); out.putLong(secondsSince1970); out.flip(); out.position(4); channel.send(out, client); } catch (Exception ex) { System.err.println(ex); } } }