Okio之RealBufferedSource

先看一段简单的写文件代码:

 private void read() {
        Source source = null;
        BufferedSource buffer = null;
        try {
            testFile = new File(Environment.getExternalStorageDirectory(), "test.txt");
            source = Okio.source(testFile);
            buffer = Okio.buffer(source);
            System.out.println("String = " + buffer.readString(Charset.forName("UTF-8")));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (buffer != null ) {
                    buffer.close();
                }
                if (source != null) {
                    source.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

Okio的source(xxx)方法返回了Source对象(即相当于java IO中的InputStream),看看其重载方法:

    public static Source source(InputStream in) {
        return source(in, new Timeout());
    }

    public static Source source(File file) throws FileNotFoundException {
        if (file == null) throw new IllegalArgumentException("file == null");
        return source(new FileInputStream(file));
    }

    @IgnoreJRERequirement // Should only be invoked on Java 7+.
    public static Source source(Path path, OpenOption... options) throws IOException {
        if (path == null) throw new IllegalArgumentException("path == null");
        return source(Files.newInputStream(path, options));
    }

    public static Source source(Socket socket) throws IOException {
        if (socket == null) throw new IllegalArgumentException("socket == null");
        AsyncTimeout timeout = timeout(socket);
        Source source = source(socket.getInputStream(), timeout);
        return timeout.source(source);
    }

由此可见,source()方法可接受File、Socket、Path等作为参数,构建InputStream,最终由其适配成一个Source对象(适配器模式)。最后,所有的source(xxx)都调用
Source source(InputStream in, Timeout timeout)方法,具体实现:

private static Source source(final InputStream in, final Timeout timeout) {
        if (in == null) throw new IllegalArgumentException("in == null");
        if (timeout == null) throw new IllegalArgumentException("timeout == null");
        return new Source() {
            @Override
            public long read(Buffer sink, long byteCount) throws IOException {
                if (byteCount < 0)
                    throw new IllegalArgumentException("byteCount < 0: " + byteCount);
                if (byteCount == 0) return 0;
                try {
                    timeout.throwIfReached();
                    Segment tail = sink.writableSegment(1);
                    int maxToCopy = (int) Math.min(byteCount, Segment.SIZE - tail.limit);
                    int bytesRead = in.read(tail.data, tail.limit, maxToCopy);
                    if (bytesRead == -1) return -1;
                    tail.limit += bytesRead;
                    sink.size += bytesRead;
                    return bytesRead;
                } catch (AssertionError e) {
                    if (isAndroidGetsocknameError(e)) throw new IOException(e);
                    throw e;
                }
            }
            @Override
            public void close() throws IOException {
                in.close();
            }
            @Override
            public Timeout timeout() {
                return timeout;
            }
            @Override
            public String toString() {
                return "source(" + in + ")";
            }
        };
    }

在该方法中,我们可见Timeout 参数,这是Okio中的一个超时机制。方法内部,直接new Source(){...}返回Source对象,其中可见,真正的读操作就是通过上面根据不同“源”构建出来的InputStream进行操作的。
  在重写的read()方法中有参数sink(Buffer类型),其作用就是用来缓存读到的数据。在Okio整个体系中,有Source和Sink(相当于InputStream和OutputStream)输入输出流,这里的参数命名为sink,大概就是表达为:所有读进来的数据就是为了取出来使用,故而去名为sink。
timeout.throwIfReached();用来检查是否超时。接下来看方法 writableSegment(1):

/**
   * 返回一个Segment对象,我们至少可以向该对象写入minimumCapacity个字节
   */
  Segment writableSegment(int minimumCapacity) {
    //判断参数合法性
    if (minimumCapacity < 1 || minimumCapacity > Segment.SIZE) throw new IllegalArgumentException();
    //若head为空,则调用SegmentPool的take()方法去获取一个可用的Segment
    if (head == null) {
      head = SegmentPool.take(); // Acquire a first segment.
      return head.next = head.prev = head;
    }
    //当前双向链表的最后一个节点tail
    Segment tail = head.prev;
    //若最后一个Segment的剩余空间不足以容纳将要存入的数据或者该Segment不存在自己的data字节数组
    //或者其byte数组不能进行追加数据的操作,则调用push方法添加一个新的Segment到双向链表的尾部
    if (tail.limit + minimumCapacity > Segment.SIZE || !tail.owner) {
      tail = tail.push(SegmentPool.take()); // Append a new empty segment to fill up.
    }
    return tail;
  }

最后,调用InputStream的read(byte b[], int off, int len)进行读操作,读取合适数量的字节数。
注:在这句代码:int bytesRead = in.read(tail.data, tail.limit, maxToCopy);中,若在输入流in中读不到数据,则返回-1,会产生EOFException

  在RealBufferedSource这个类中,还有很多通过特定类型读取数据的操作。例如:readUtf8():String

public String readUtf8() throws IOException {
    buffer.writeAll(source);
    return buffer.readUtf8();
  }

  其中,buffer.writeAll(source)就是讲source中的所有数据写入到buffer中,正应了前面所说,写入就是为了读取:

public long writeAll(Source source) throws IOException {
    if (source == null) throw new IllegalArgumentException("source == null");
    long totalBytesRead = 0;
    //this就是buffer.writeAll(source)这句代码的buffer对象
    for (long readCount; (readCount = source.read(this, Segment.SIZE)) != -1; ) {
      totalBytesRead += readCount;
    }
    return totalBytesRead;
  }

再看看buffer.readUtf8()这句代码的源码:

public String readUtf8() {
    try {
      return readString(size, Util.UTF_8);
    } catch (EOFException e) {
      throw new AssertionError(e);
    }
  }

public String readString(long byteCount, Charset charset) throws EOFException {
    checkOffsetAndCount(size, 0, byteCount);
    if (charset == null) throw new IllegalArgumentException("charset == null");
    if (byteCount > Integer.MAX_VALUE) {
      throw new IllegalArgumentException("byteCount > Integer.MAX_VALUE: " + byteCount);
    }
    if (byteCount == 0) return "";

    Segment s = head;
    if (s.pos + byteCount > s.limit) {
      // If the string spans multiple segments, delegate to readBytes().
      return new String(readByteArray(byteCount), charset);
    }

    String result = new String(s.data, s.pos, (int) byteCount, charset);
    s.pos += byteCount;
    size -= byteCount;

    if (s.pos == s.limit) {
      head = s.pop();
      SegmentPool.recycle(s);
    }

    return result;
  }

  由此可见,是通过new String(byte[],Charset)进行使用特定格式编码的。其中,readByteArray(long)就是将segment中的byteCount个字节读取出来缓存在一个byte[]中,然后构成特定编码的字符串。
  另外,该RealBufferedSource类中还存在readInt(),readIntLe(),readShort(),readShortLe()等方法,其实差别就是前者大端格式读取,后者小端格式读取。关于大端小端格式,见# 小端格式和大端格式(Little-Endian&Big-Endian)。简单来说:

  • 大端格式就是字节的低位保存在高地址
  • 小端格式就是字节的高位保存在高地址
    比如,我们常见的16进制数0x10203040,在内存中的起始地址是0x00000001。那么,10就是数据的高位,04就是最低位;
内存地址 0x00000001 0x00000002 0x00000003 0x00000004
小端格式 40 30 20 10
大端格式 10 20 30 40

你可能感兴趣的:(Okio之RealBufferedSource)