BlockReader接口实现类的read操作----BlockReaderLocalLegacy类中read(ByteBuffer buf)函数

之前我们分析了四个构造BlockReader类对象函数

1、getLegacyBlockReaderLocal(),该函数返回一个BlockReaderLocalLegacy类对象

2、getBlockReaderLocal(),该函数返回一个BlockReaderLocal类对象

3、getRemoteBlockReaderFromDomain(),该函数返回一个RemoteBlockReader2类对象

4、getRemoteBlockReaderFromTcp(),该函数返回一个RemoteBlockReader2类对象

这三个类的关系如下:

BlockReader类

接下来我们开始分析这三个类的read函数。

我们回顾一下之前分析过的read函数,主要分析的是从FSDataInputStream类中的read函数到DFSInputStream类read函数,再到BlockReaderLocal类read函数的过程,但我们没有深入到相应的BlockReaderLocal类的read函数中,这里我们主要开始分析这个类中的read函数,来看看客户端是怎么读取到相应的文件数据的。

总结下来,最终调用BlockReaderLocal类中的函数有两个:

第一个:int read(ByteBuffer buf)

第二个:int read(byte[] buf, int off, int len)

接下来我们将分别分析上面三个类中的这两个函数。

BlockReaderLocalLegacy类中的read函数

BlockReaderLocalLegacy类是老版本的短路读类,基本上已经废弃了,不过我们这里还是进行分析一下,了解它的思路。我们开始分析函数read(ByteBuffer buf),该函数代码如下:

@Override
  public synchronized int read(ByteBuffer buf) throws IOException {
    int nRead = 0;
    //如果是校验和读取
    if (verifyChecksum) {
      // A 'direct' read actually has three phases. The first drains any
      // remaining bytes from the slow read buffer. After this the read is
      // guaranteed to be on a checksum chunk boundary. If there are still bytes
      // to read, the fast direct path is used for as many remaining bytes as
      // possible, up to a multiple of the checksum chunk size. Finally, any
      // 'odd' bytes remaining at the end of the read cause another slow read to
      // be issued, which involves an extra copy.

      // Every 'slow' read tries to
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值