【HDFS-Client系列】FSOutputSummer#wr

2023-12-19  本文已影响0人  小北觅

有些代码初次阅读并不能看出其中的处理细节,但是当过一段时间对这个部分熟悉了之后再来阅读,就能发现并体会其中的细节。

本文说一下FSOutputSummer#write1的细节。

细节①: 当local buffer为empty并且此次write1要写的数据长度len > buf.length时,直接计算buf.length个数个字节的checksum并把这些数据发送到底层流。不用再做System.arraycopy到buf里了。返回值就是buf.length,即这次write1真实处理了多少数据字节。

  /**
   * Write a portion of an array, flushing to the underlying
   * stream at most once if necessary.
   */
  private int write1(byte b[], int off, int len) throws IOException {
    // 细节①的判断条件
    if(count==0 && len>=buf.length) {
      // local buffer is empty and user buffer size >= local buffer size, so
      // simply checksum the user buffer and send it directly to the underlying
      // stream
      final int length = buf.length;
      writeChecksumChunks(b, off, length);
      return length;
    }
    
    // copy user data to local buffer
    int bytesToCopy = buf.length-count;
    bytesToCopy = (len<bytesToCopy) ? len : bytesToCopy;
    System.arraycopy(b, off, buf, count, bytesToCopy);
    count += bytesToCopy;
    if (count == buf.length) {
      // local buffer is full
      flushBuffer();
    } 
    return bytesToCopy;
  }
上一篇 下一篇

猜你喜欢

热点阅读