Java non-blocking IO (NIO)
- Support form JDK 1.4
- java.nio.buffer
capacity
limit
position
mark Allocate buffer
12ByteBuffer buf = ByteBuffer.allocate(20); // allocate in heap.ByteBuffer buf = ByteBuffer.allocateDirect(20;) // allocate in off-heap.ByteBuffer common operations
rewind()
: pos=0, mark discardclear()
: pos=0, lim=cap, mark discardflip()
: lim=pos, pos=0, mark discardreset()
: pos=markslice()
: split a buffer at remaining position
Garbage Collection (GC)
System.gc()
Channel
FileChannel
Copy file by Channel.12345678910111213141516171819public void copyFile() {try {FileInputStream fis = new FileInputStream("TestChannelSrc.txt");FileChannel fcin = fis.getChannel();FileOutputStream fos = new FileOutputStream("TestChannelDst.txt");FileChannel fcout = fos.getChannel();ByteBuffer buf = ByteBuffer.allocate(1024 * 50);while(fcin.read(buf) != -1) {buf.flip();fcout.write(buf);buf.clear();}fis.close();fos.close();} catch (Exception e) {e.printStackTrace();}}SocketChannel
Full code can be found on github.MappedByteBuffer
Map File on device to memory synchronizely.
Full code can be found on github.