Nio and bio differences of
bio:
- Stream-oriented.
- one way.
- Obstruction, which is the origin of this b.
nio:
- Block-oriented. (Buffer)
- Bi-directional.
- Non-blocking. Synchronous programming. Select a model
Conventional programming step nio
Any data obtained from the channel, will be subject to buffer to store.
- Get to the channel. If this is taken from the stream channel, then the channel, only supports one-way.
- Construction of Buffer
- Data stuffed Buffer, if the write-back operation is executed flip
- Read or write back
Buffer usage
The three core buffer variables, and not a common variable (mark):
- mark flag set, note the position of a position, a reset method, which can return
- posotion read position, the next position to be read and written.
- limit the maximum value of read or write, subscripts next unreadable or non-writable element.
- Maximum capacity buffer, and will not become after creation.
、
Direct memory mapping, zero-copy:
Conventional java flow operation:
- Jvm data from the copy to the outside of the heap memory, the jvm, only save the address.
- The modified data blocks outside the stack, the writing, reading.
- The external heap memory, changes, and then copy back jvm memory.
When the buffer has a direct memory-mapped buffer, called DirectByteBuffer, by DirectByteBuffer database blocks can be placed directly on the outer jvm heap memory, so that the operation data, the data memory is not necessary to copy from the outer to the jvm heap memory.
Can map the buffer method call, get an in-memory buffer. Direct operating buffer, equivalent to the original file operations.