List of usage examples for java.nio ByteBuffer rewind
public final Buffer rewind()
From source file:Main.java
public static void main(String[] args) throws Exception { DatagramChannel server = null; server = DatagramChannel.open(); InetSocketAddress sAddr = new InetSocketAddress("localhost", 8989); server.bind(sAddr);// w ww . j a va 2 s . c o m ByteBuffer buffer = ByteBuffer.allocate(1024); while (true) { System.out.println("Waiting for a message from" + " a remote host at " + sAddr); SocketAddress remoteAddr = server.receive(buffer); buffer.flip(); int limits = buffer.limit(); byte bytes[] = new byte[limits]; buffer.get(bytes, 0, limits); String msg = new String(bytes); System.out.println("Client at " + remoteAddr + " says: " + msg); buffer.rewind(); server.send(buffer, remoteAddr); buffer.clear(); } //server.close(); }
From source file:MainClass.java
public static void main(String[] argv) throws Exception { int port = 1234; // default ByteBuffer buffer = ByteBuffer.wrap(GREETING.getBytes()); ServerSocketChannel ssc = ServerSocketChannel.open(); ssc.socket().bind(new InetSocketAddress(port)); ssc.configureBlocking(false);//from w ww. j av a 2 s .c o m while (true) { System.out.println("Waiting for connections"); SocketChannel sc = ssc.accept(); if (sc == null) { Thread.sleep(2000); } else { System.out.println("Incoming connection from: " + sc.socket().getRemoteSocketAddress()); buffer.rewind(); sc.write(buffer); sc.close(); } } }
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 ww.j a va 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:Main.java
public static void print(ByteBuffer bb) { while (bb.hasRemaining()) System.out.print(bb.get() + " "); System.out.println();/*from w w w.j av a 2s . c om*/ bb.rewind(); }
From source file:Main.java
public static ByteBuffer clone(ByteBuffer original) { ByteBuffer clone = ByteBuffer.allocate(original.capacity()); original.rewind();//copy from the beginning clone.put(original);//w ww . ja va 2 s .com original.rewind(); clone.flip(); return clone; }
From source file:Main.java
public static byte[] array(ByteBuffer byteBuffer) { ByteBuffer byteBuffer2 = byteBuffer.duplicate(); byte[] arrby = new byte[byteBuffer2.limit()]; byteBuffer2.rewind(); byteBuffer2.get(arrby);//from w w w . j a v a 2 s. c o m return arrby; }
From source file:Main.java
public static byte[] getBytes(Bitmap bitmap) { int byteCount = bitmap.getByteCount(); ByteBuffer buffer = ByteBuffer.allocate(byteCount); bitmap.copyPixelsToBuffer(buffer);/* ww w .j av a2 s. c om*/ buffer.rewind(); byte[] data = new byte[byteCount]; buffer.get(data); return data; }
From source file:Main.java
public static ByteBuffer createByteBufferOnHeap(ByteBuffer buf, final int size) { if (buf != null && buf.limit() == size) { buf.rewind(); return buf; }//from w w w . j a v a2s. c o m buf = createByteBufferOnHeap(size); return buf; }
From source file:Main.java
/** * Converts bitmap to the byte array without compression * @param bitmap source bitmap/*w w w. j a v a 2 s.c om*/ * @return result byte array */ public static byte[] convertBitmapToByteArrayUncompressed(Bitmap bitmap) { ByteBuffer byteBuffer = ByteBuffer.allocate(bitmap.getByteCount()); bitmap.copyPixelsToBuffer(byteBuffer); byteBuffer.rewind(); return byteBuffer.array(); }
From source file:Main.java
public static ByteBuffer createByteBuffer(ByteBuffer buf, final int size) { if (buf != null && buf.limit() == size) { buf.rewind(); return buf; }//ww w .j a va2 s.c o m buf = createByteBuffer(size); return buf; }