NIO summary ----- Buffer

Java NIO and NIO for the Buffer channels for interaction. As you know, the data is read into the buffer from the channels, is written from the buffer to the channel.

The buffer is essentially a data can be written, then the data may be read from memory. This memory is packaged objects NIO Buffer, and provides a set of methods for convenient access to the memory block.

Buffer read and write data using the steps of:

Buffer data is written to 
call Flip () method for 
reading data from the Buffer 
call clear () method or a compact () method

Flip () method Buffer switched from write mode to read mode. In the read mode, all data written to the buffer before it can be read.

clear () method clears the entire buffer.

compact () method will only read data has been cleared. Any unread data is moved to the beginning of the buffer, the new data will be written back into the data buffer unread.

A good example:

RandomAccessFile aFile = new RandomAccessFile("data/nio-data.txt", "rw");
FileChannel inChannel = aFile.getChannel();
//create buffer with capacity of 48 bytes
ByteBuffer buf = ByteBuffer.allocate(48);
int bytesRead = inChannel.read(buf); //read into buffer.
while (bytesRead != -1) {
  buf.flip();  //make buffer ready for read
  while(buf.hasRemaining()){
      System.out.print((char) buf.get()); // read 1 byte at a time
  }
  buf.clear(); //make buffer ready for writing
  bytesRead = inChannel.read(buf);
}
aFile.close();

Buffer of three attributes:

capacity 
  capacity value is a fixed size, capacity can only write inside a byte, long, char and other types, once Buffer is full, it needs to be emptied in order to continue to write data inside write data (via read data or clear the data). position
  when writing data to the Buffer, position represents the current position. After the initial position value to 0. When a byte, long, etc. Buffer data is written, will move forward to the next position may be a data insertion unit Buffer. position of the maximum capacity - 1 ..
  When reading data, is read from a particular location. When switching from write mode to Buffer read mode, position is reset to 0. When the position data is read from the Buffer, position to move forward to the next readable position.
limit
  in write mode, Buffer represent the limit of how much data you can write to most in Buffer. Write mode, limit equal to the capacity Buffer.
  When switching to Buffer read mode, limit how much data you indicate up to read. Thus, when switching to reading mode Buffer, limit position is set to a value in the writing mode. In other words, all the data is written before you can read (limit is set to the amount of data has been written, this value is in write mode position)

 Three relationships:

Buffer type (char, short, int, long, float operated type or double byte buffer.)

ByteBuffer
MappedByteBuffer
CharBuffer
DoubleBuffer
FloatBuffer
IntBuffer
LongBuffer
ShortBuffer

Each class has a Buffer allocate method. Buffer used to allocate resources.

 

 

 

 

Guess you like

Origin www.cnblogs.com/sjxbg/p/11207138.html