hadoop IO 之InputBuffer和DataInputBuffer

   类似于JAVA.IO 的装饰器模式, InputBuffer输入缓冲和DataInputBuffer数据缓冲的实现封装于内部类Buffer,该类的功能只是提供一个空的缓冲区,用于存储数据。Buffer代码如下:

 private static class Buffer extends ByteArrayInputStream {
    public Buffer() {
      super(new byte[] {});
    }

    public void reset(byte[] input, int start, int length) {
      this.buf = input;
      this.count = start+length;
      this.mark = start;
      this.pos = start;
    }

    public int getPosition() { return pos; }
    public int getLength() { return count; }
  }
  InputBuffer和DataInputBuffer的方法委托于内部类private Buffer buffer,例如InputBuffer部分代码:
 /** Returns the current position in the input. */
  public int getPosition() { return buffer.getPosition(); }

  /** Returns the length of the input. */
  public int getLength() { return buffer.getLength(); }
DataInputBuffer 内置的Buffer代码如下:
 private static class Buffer extends ByteArrayInputStream {
    public Buffer() {
      super(new byte[] {});
    }

    public void reset(byte[] input, int start, int length) {
      this.buf = input;
      this.count = start+length;
      this.mark = start;
      this.pos = start;
    }

    public byte[] getData() { return buf; }
    public int getPosition() { return pos; }
    public int getLength() { return count; }
  }
  两个类封装的Buffer一样,而其方法也都委托依赖于buffer,只是InputBuffer和DataInputBuffer继承于不同的类,如下:

 DataInputBuffer:

public class DataInputBuffer extends DataInputStream {
}

InputBuffer:

public class InputBuffer extends FilterInputStream {
}

DataInputBuffer UML结构图

hadoop IO 之InputBuffer和DataInputBuffer_第1张图片


你可能感兴趣的:(hadoop IO 之InputBuffer和DataInputBuffer)