深入淺出NIO Channel和Buffer

前言

Java NIO 由以下幾個核心部分組成:

  1. Buffer
  2. Channel
  3. Selector

傳統的IO操作面向數據流,意味着每次從流中讀一個或多個字節,直至完成,數據沒有被緩存在任何地方。NIO操作面向緩衝區,數據從Channel讀取到Buffer緩衝區,隨後在Buffer中處理數據。本文着重介紹Channel和Buffer的概念以及在文件讀寫方面的應用和內部實現原理。

Buffer

A buffer is a linear, finite sequence of elements of a specific
primitive type.

一塊緩存區,內部使用字節數組存儲數據,並維護幾個特殊變量,實現數據的反覆利用。

  • mark:初始值爲-1,用於備份當前的position
  • position:初始值爲0。position表示當前可以寫入或讀取數據的位置。當寫入或讀取一個數據後, position向前移動到下一個位置。
  • limit:
    寫模式下,limit表示最多能往Buffer裏寫多少數據,等於capacity值。
    讀模式下,limit表示最多可以讀取多少數據。
  • capacity:緩存數組大小

Buffer.png

  • mark():把當前的position賦值給mark
public final Buffer mark() {
    mark = position;
    return this;
}
  • reset():把mark值還原給position
public final Buffer reset() {
    int m = mark;
    if (m < 0)
        throw new InvalidMarkException();
    position = m;
    return this;
}
  • clear():一旦讀完Buffer中的數據,需要讓Buffer準備好再次被寫入,clear會恢復狀態值,但不會擦除數據。
public final Buffer clear() {
    position = 0;
    limit = capacity;
    mark = -1;
    return this;
}
  • flip():Buffer有兩種模式,寫模式和讀模式,flip後Buffer從寫模式變成讀模式。
public final Buffer flip() {
    limit = position;
    position = 0;
    mark = -1;
    return this;
}
  • rewind():重置position爲0,從頭讀寫數據。
public final Buffer rewind() {
    position = 0;
    mark = -1;
    return this;
}

目前Buffer的實現類有以下幾種:

  • ByteBuffer
  • CharBuffer
  • DoubleBuffer
  • FloatBuffer
  • IntBuffer
  • LongBuffer
  • ShortBuffer
  • MappedByteBuffer

其中MappedByteBuffer實現比較特殊,感興趣的可以看看 深入淺出MappedByteBuffer

Paste_Image.png

ByteBuffer

A byte buffer,extend from Buffer

ByteBuffer的實現類包括HeapByteBuffer和DirectByteBuffer兩種。

  • HeapByteBuffer
public static ByteBuffer allocate(int capacity) {
  if (capacity < 0)
      throw new IllegalArgumentException();
  return new HeapByteBuffer(capacity, capacity);
}
HeapByteBuffer(int cap, int lim) {  
  super(-1, 0, lim, cap, new byte[cap], 0);
}

HeapByteBuffer通過初始化字節數組hd,在虛擬機堆上申請內存空間。

  • DirectByteBuffer
public static ByteBuffer allocateDirect(int capacity) {
  return new DirectByteBuffer(capacity);
}
DirectByteBuffer(int cap) {
  super(-1, 0, cap, cap);
  boolean pa = VM.isDirectMemoryPageAligned();
  int ps = Bits.pageSize();
  long size = Math.max(1L, (long)cap + (pa ? ps : 0));
  Bits.reserveMemory(size, cap);

  long base = 0;
  try {
      base = unsafe.allocateMemory(size);
  } catch (OutOfMemoryError x) {
      Bits.unreserveMemory(size, cap);
      throw x;
  }
  unsafe.setMemory(base, size, (byte) 0);
  if (pa && (base % ps != 0)) {
      // Round up to page boundary
      address = base + ps - (base & (ps - 1));
  } else {
      address = base;
  }
  cleaner = Cleaner.create(this, new Deallocator(base, size, cap));
  att = null;
}

DirectByteBuffer通過unsafe.allocateMemory在物理內存中申請地址空間(非jvm堆內存),並在ByteBuffer的address變量中維護指向該內存的地址。
unsafe.setMemory(base, size, (byte) 0)方法把新申請的內存數據清零。

Channel

A channel represents an open connection to an entity such as a
hardware device, a file, a network socket, or a program component that
is capable of performing one or more distinct I/O operations, for
example reading or writing.

又稱“通道”,NIO把它支持的I/O對象抽象爲Channel,類似於原I/O中的流(Stream),但有所區別:

  • 流是單向的,通道是雙向的,可讀可寫。
  • 流讀寫是阻塞的,通道可以異步讀寫。
  • 流中的數據可以選擇性的先讀到緩存中,通道的數據總是要先讀到一個緩存中,或從緩存中寫入,如下所示:

Channel.png
目前已知Channel的實現類有:

  • FileChannel
  • DatagramChannel
  • SocketChannel
  • ServerSocketChannel

FileChannel

A channel for reading, writing, mapping, and manipulating a file.

一個用來寫、讀、映射和操作文件的通道。
FileChannel的read、write和map通過其實現類FileChannelImpl實現。

  • read實現
public int read(ByteBuffer dst) throws IOException {
  ensureOpen();
  if (!readable)
      throw new NonReadableChannelException();
  synchronized (positionLock) {
      int n = 0;
      int ti = -1;
      try {
          begin();
          ti = threads.add();
          if (!isOpen())
              return 0;
          do {
              n = IOUtil.read(fd, dst, -1, nd);
          } while ((n == IOStatus.INTERRUPTED) && isOpen());
          return IOStatus.normalize(n);
      } finally {
          threads.remove(ti);
          end(n > 0);
          assert IOStatus.check(n);
      }
  }
}

FileChannelImpl的read方法通過IOUtil的read實現:

static int read(FileDescriptor fd, ByteBuffer dst, long position,
              NativeDispatcher nd) IOException {   if (dst.isReadOnly())
      throw new IllegalArgumentException("Read-only buffer");   if (dst instanceof DirectBuffer)
      return readIntoNativeBuffer(fd, dst, position, nd);

  // Substitute a native buffer   ByteBuffer bb = Util.getTemporaryDirectBuffer(dst.remaining());   try {
      int n = readIntoNativeBuffer(fd, bb, position, nd);
      bb.flip();
      if (n > 0)
          dst.put(bb);
      return n;   } finally {
      Util.offerFirstTemporaryDirectBuffer(bb);   } }

通過上述實現可以看出,基於channel的文件數據讀取步驟如下:

  1. 申請一塊和緩存同大小的DirectByteBuffer bb。

  2. 讀取數據到緩存bb,底層由NativeDispatcher的read實現。

  3. 把bb的數據讀取到dst(用戶定義的緩存,在jvm中分配內存)。

read方法導致數據複製了兩次。

write實現

public int write(ByteBuffer src) throws IOException {
  ensureOpen();
  if (!writable)
      throw new NonWritableChannelException();
  synchronized (positionLock) {
      int n = 0;
      int ti = -1;
      try {
          begin();
          ti = threads.add();
          if (!isOpen())
              return 0;
          do {
              n = IOUtil.write(fd, src, -1, nd);
          } while ((n == IOStatus.INTERRUPTED) && isOpen());
          return IOStatus.normalize(n);
      } finally {
          threads.remove(ti);
          end(n > 0);
          assert IOStatus.check(n);
      }
  }
}

和read實現一樣,FileChannelImpl的write方法通過IOUtil的write實現:

static int write(FileDescriptor fd, ByteBuffer src, long position,
               NativeDispatcher nd) throws IOException {
  if (src instanceof DirectBuffer)
      return writeFromNativeBuffer(fd, src, position, nd);
  // Substitute a native buffer
  int pos = src.position();
  int lim = src.limit();
  assert (pos <= lim);
  int rem = (pos <= lim ? lim - pos : 0);
  ByteBuffer bb = Util.getTemporaryDirectBuffer(rem);
  try {
      bb.put(src);
      bb.flip();
      // Do not update src until we see how many bytes were written
      src.position(pos);
      int n = writeFromNativeBuffer(fd, bb, position, nd);
      if (n > 0) {
          // now update src
          src.position(pos + n);
      }
      return n;
  } finally {
      Util.offerFirstTemporaryDirectBuffer(bb);
  }
}

通過上述實現可以看出,基於channel的文件數據寫入步驟如下:

  1. 申請一塊DirectByteBuffer,bb大小爲byteBuffer中的limit - position。

  2. 複製byteBuffer中的數據到bb中。

  3. 把數據從bb中寫入到文件,底層由NativeDispatcher的write實現,具體如下:

private static int writeFromNativeBuffer(FileDescriptor fd, 
      ByteBuffer bb, long position, NativeDispatcher nd)
  throws IOException {
  int pos = bb.position();
  int lim = bb.limit();
  assert (pos <= lim);
  int rem = (pos <= lim ? lim - pos : 0);

  int written = 0;
  if (rem == 0)
      return 0;
  if (position != -1) {
      written = nd.pwrite(fd,
                          ((DirectBuffer)bb).address() + pos,
                          rem, position);
  } else {
      written = nd.write(fd, ((DirectBuffer)bb).address() + pos, rem);
  }
  if (written > 0)
      bb.position(pos + written);
  return written;
}

write方法也導致了數據複製了兩次

Channel和Buffer示例

File file = new RandomAccessFile("data.txt", "rw");
FileChannel channel = file.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(48);

int bytesRead = channel.read(buffer);
while (bytesRead != -1) {
    System.out.println("Read " + bytesRead);
    buffer.flip();
    while(buffer.hasRemaining()){
        System.out.print((char) buffer.get());
    }
    buffer.clear();
    bytesRead = channel.read(buffer);
}
file.close();

注意:buffer.flip() 的調用,首先將數據寫入到buffer,然後變成讀模式,再從buffer中讀取數據。

總結

通過本文的介紹,希望大家對Channel和Buffer在文件讀寫方面的應用和內部實現有了一定了解,努力做到不被一葉障目。

作者:佔小狼
鏈接:http://www.jianshu.com/p/052035037297
來源:簡書
著作權歸作者所有。商業轉載請聯繫作者獲得授權,非商業轉載請註明出處。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章