【HDFS-Client系列】FSOutputSummer#write1方法的细节

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

本文说一下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;
  }
还有 25% 的精彩内容
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
支付 ¥1.00 继续阅读