Android内核及各种干货开源项目实践与分析Android知识

Android下的IO库-Okio源码解析(五)

2017-03-21  本文已影响233人  非墨Zero

本章将介绍Okio下的各种装饰流:

1.Forwarding流

public abstract class ForwardingSink implements Sink {
  private final Sink delegate;

  public ForwardingSink(Sink delegate) {
    if (delegate == null) throw new IllegalArgumentException("delegate == null");
    this.delegate = delegate;
  }

  /** {@link Sink} to which this instance is delegating. */
  public final Sink delegate() {
    return delegate;
  }

  @Override public void write(Buffer source, long byteCount) throws IOException {
    delegate.write(source, byteCount);
  }

  @Override public void flush() throws IOException {
    delegate.flush();
  }

  @Override public Timeout timeout() {
    return delegate.timeout();
  }

  @Override public void close() throws IOException {
    delegate.close();
  }

  @Override public String toString() {
    return getClass().getSimpleName() + "(" + delegate.toString() + ")";
  }
}

Forwarding流乍看之下似乎没有什么实际用处,但是在实际作用中,常常用来作为匿名的代理对象,由于传入的sink本身我们无法继承和复写,这样我们可以直接采用装饰Forwarding流的方式来监控和拦截一些操作。

2.DeflaterSink和InflaterSource流

这对流主要对应于zip压缩流,类似ZipInputStream和ZipOutputStream,而实际上再ZipInputStream中实际上也用到了相同的API.

//code ZipInputStream.java
public class ZipInputStream {

    public void read(...) {
          try {
            read = inf.inflate(buffer, byteOffset, byteCount);//引用了一个protected Inflater inf;对象
        } catch (DataFormatException e) {
            throw new ZipException(e.getMessage());
        }
    }
}

我们看到在ZipInputStream的流读取处理的时候,用到了一个Inflater类型的对象,这个对象是专门用来处理Zip格式编码的工具类。而在InflaterSource这个源去读取数据的时候,也一样用到了这个类:

//
while (true) {
      boolean sourceExhausted = refill();

      // Decompress the inflater's compressed data into the sink.
      try {
        Segment tail = sink.writableSegment(1);
        int bytesInflated = inflater.inflate(tail.data, tail.limit, Segment.SIZE - tail.limit);
        if (bytesInflated > 0) {
          tail.limit += bytesInflated;
          sink.size += bytesInflated;
          return bytesInflated;
        }
        if (inflater.finished() || inflater.needsDictionary()) {
          releaseInflatedBytes();
          if (tail.pos == tail.limit) {
            // We allocated a tail segment, but didn't end up needing it. Recycle!
            sink.head = tail.pop();
            SegmentPool.recycle(tail);
          }
          return -1;
        }
        if (sourceExhausted) throw new EOFException("source exhausted prematurely");
      } catch (DataFormatException e) {
        throw new IOException(e);
      }
    }

3.GzipSource和GzipSink流

Gzip和zip的主要区别在于平台通用性和压缩率,一般情况下,Gzip的压缩率更高点,Gzip是基于zip算法上的再改造。因此,在Gzip流中必须包装原始流为一个InflaterSource流:

public GzipSink(Sink sink) {
    if (sink == null) throw new IllegalArgumentException("sink == null");
    this.deflater = new Deflater(DEFAULT_COMPRESSION, true /* No wrap */);
    this.sink = Okio.buffer(sink);
    this.deflaterSink = new DeflaterSink(this.sink, deflater);

    writeHeader();
  }

4.Pipe流

Okio中的pipe流类似生产者消费者的模式,例如在管道流PipeSource读取的时候,发现buffer.size,也就是缓冲池数据长度为0的时候,管道PipeSource流陷入等待。一直等到PipeSink流往Buffer中再输出的时候,阻塞消失。并且,管道的流一定是成对出现的。

final class PipeSource implements Source {
    final Timeout timeout = new Timeout();

    @Override public long read(Buffer sink, long byteCount) throws IOException {
      synchronized (buffer) {
        if (sourceClosed) throw new IllegalStateException("closed");

        while (buffer.size() == 0) {// buffer为空
          if (sinkClosed) return -1L;
          timeout.waitUntilNotified(buffer); // Wait until the sink fills the buffer.//陷入等待
        }

        long result = buffer.read(sink, byteCount);
        buffer.notifyAll(); // Notify the sink that it can resume writing.
        return result;
      }
    }

    @Override public void close() throws IOException {
      synchronized (buffer) {
        sourceClosed = true;
        buffer.notifyAll(); // Notify the sink that no more bytes are desired.
      }
    }

    @Override public Timeout timeout() {
      return timeout;
    }
  }
上一篇下一篇

猜你喜欢

热点阅读