`

源码剖析之java.io.BufferedInputStream

阅读更多
java 提供了读写缓存api。
byte:java.io.BufferedInputStream 、 java.io.BufferedOutputStream
char: java.io.BufferedReader、java.io.BufferedWriter

好处:
1、可以避免一次性写入大量的数据,这样可能瞬间造成内存占用太多,导致系统不稳定。
2、可以对写入较少的数据进行缓冲,避免写入输入承载终端太过频繁。

缺点:几乎没有。

所以几乎任何时候我们都有必要使用缓存的流进行包装。

记得刚学习java io时,听到缓存,感觉很神秘。其实java提供的缓存实现的数据结构就是数组,java 缓存 api都是建立在byte[] 和 char[]基础之上的,也即是:先write(read) 数据到 byte[](char[])里,然后一旦缓存数据满,再一次性读取(写入)。


源码分析:

package java.io;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

/**
 * BufferedInputStream 继承自 FilterInputStream,所以其本身需要一个 internal InputStream 来承载数据的读取源头
 * 其缓存实现的本质是:byte[] 对数据的缓存。
 */
public class BufferedInputStream extends FilterInputStream {

    private static int defaultBufferSize = 8192;

    /**
     * 缓存数据存储的地方,必要时候可以改变大小(当然也不是原来的buf数组了)
     */
    protected volatile byte buf[];

    /**
     * Atomic updater to provide compareAndSet for buf. This is
     * necessary because closes can be asynchronous. We use nullness
     * of buf[] as primary indicator that this stream is closed. (The
     * "in" field is also nulled out on close.)
     */
    private static final 
        AtomicReferenceFieldUpdater<BufferedInputStream, byte[]> bufUpdater = 
        AtomicReferenceFieldUpdater.newUpdater
        (BufferedInputStream.class,  byte[].class, "buf");

    /**
     * 缓存的有效字节数
     */
    protected int count;

    /**
     * 下一个可读的字节在数组中的位置
     */
    protected int pos;
    
    /**
     * 最近一次mark的位置
     */
    protected int markpos = -1;

    /**
     * The maximum read ahead allowed after a call to the 
     * <code>mark</code> method before subsequent calls to the 
     * <code>reset</code> method fail. 
     * Whenever the difference between <code>pos</code>
     * and <code>markpos</code> exceeds <code>marklimit</code>,
     * then the  mark may be dropped by setting
     * <code>markpos</code> to <code>-1</code>.
     *
     * @see     java.io.BufferedInputStream#mark(int)
     * @see     java.io.BufferedInputStream#reset()
     */
    protected int marklimit;

    /**
     *底层的流是否已关闭
     */
    private InputStream getInIfOpen() throws IOException {
        InputStream input = in;
	if (input == null)
	    throw new IOException("Stream closed");
        return input;
    }

    private byte[] getBufIfOpen() throws IOException {
        byte[] buffer = buf;
	if (buffer == null)
	    throw new IOException("Stream closed");
        return buffer;
    }

  
    public BufferedInputStream(InputStream in) {
	this(in, defaultBufferSize);
    }

    public BufferedInputStream(InputStream in, int size) {
	super(in);
        if (size <= 0) {
            throw new IllegalArgumentException("Buffer size <= 0");
        }
	buf = new byte[size];
    }

    /**
     * Fills the buffer with more data, taking into account
     * shuffling and other tricks for dealing with marks.
     * Assumes that it is being called by a synchronized method.
     * This method also assumes that all data has already been read in,
     * hence pos > count.
     */
    private void fill() throws IOException {
        byte[] buffer = getBufIfOpen();
				if (markpos < 0) //如果没有设置mark,那么pos的位置设置为0。(因为没有mark,算是全部读取新数据)
				    pos = 0;		/* no mark: throw away the buffer */
				else if (pos >= buffer.length)	/* 缓冲里没有空闲空间,并且有mark设置 */
				  if (markpos > 0) {	/* 此种情况必须保留markpos之后的数据 */
					int sz = pos - markpos;
					System.arraycopy(buffer, markpos, buffer, 0, sz);
					pos = sz; //pos 在新的buffer里的位置是sz
					markpos = 0; //markpos 在新的buffer里面为 0,所以必须需要设置为0
				  } else if (buffer.length >= marklimit) { //出错啦,这种情况不该出现的
					markpos = -1;	/* buffer got too big, invalidate mark */
					pos = 0;	/* drop buffer contents */
				  } else {		/* 增长buffer */
					int nsz = pos * 2;
					if (nsz > marklimit)//但是不能超过marklimit
					    nsz = marklimit;
					byte nbuf[] = new byte[nsz];
					System.arraycopy(buffer, 0, nbuf, 0, pos);
			                if (!bufUpdater.compareAndSet(this, buffer, nbuf)) {
			                    // Can't replace buf if there was an async close.
			                    // Note: This would need to be changed if fill()
			                    // is ever made accessible to multiple threads.
			                    // But for now, the only way CAS can fail is via close.
			                    // assert buf == null;
			                    throw new IOException("Stream closed");
			                }
			                buffer = nbuf;
				    }
			        count = pos;
				int n = getInIfOpen().read(buffer, pos, buffer.length - pos);
			        if (n > 0)
			            count = n + pos;
    }

    /**
     * 读取一个字节
     */
    public synchronized int read() throws IOException {
			if (pos >= count) {//如果到了缓冲的最大索引位置,那么去fill数据
			    fill();
			    if (pos >= count) //fill之后如果pos>=count,那么说明 underlying stream 里已经没有数据
				return -1;
			}
			return getBufIfOpen()[pos++] & 0xff; //正常返回
    }

    /**
     * Read characters into a portion of an array, reading from the underlying
     * stream at most once if necessary.
     */
    private int read1(byte[] b, int off, int len) throws IOException {
			int avail = count - pos;
			if (avail <= 0) {
			    if (len >= getBufIfOpen().length && markpos < 0) { //如果没有多余的缓存数据,并且没有设置mark标记,并且流里剩余的数据不足len长度,那么直接从数据流里读取,直接返回。
				    return getInIfOpen().read(b, off, len);
			    }
			    fill(); //其他情况则去填充数据
			    avail = count - pos; //查看缓存中剩下的数据
			    if (avail <= 0) return -1; //如果缓存里没有新数据(即:fill没有添加新数据)。
			}
			int cnt = (avail < len) ? avail : len; //取剩下的数据和len的较小值
			System.arraycopy(getBufIfOpen(), pos, b, off, cnt); //把数据读取 到b数组里。
			pos += cnt;//改变当前字节的位置
			return cnt;
    }

    /**
     * 读取流数据到b数组里,从b[off]开始放数据,长度为len
     */
    public synchronized int read(byte b[], int off, int len)throws IOException
    {
        getBufIfOpen(); // Check for closed stream
        if ((off | len | (off + len) | (b.length - (off + len))) < 0) { //如果 off<0 或者  len <0 或者  (off + len) <0 或者  b.length - (off + len) <0
	         throw new IndexOutOfBoundsException();
	      } else if (len == 0) { //len =0,直接返回
            return 0;
        }

	      int n = 0; //记录已经读到的总byte数
        for (;;) {
            int nread = read1(b, off + n, len - n);
            if (nread <= 0) //如果没有读到数据,那么直接返回
                return (n == 0) ? nread : n;
            n += nread;
            if (n >= len) //如果已经到len 那么返回n(注意:事实上n=len,而不会n>len)
                return n;
            // if not closed but no bytes available, return
            InputStream input = in;
            if (input != null && input.available() <= 0) //每次读过之后判断 input 是否还有多余的可读,如果没有那么直接返回
                return n;
        }
    }

    /**
     * 跳过n字节
     */
    public synchronized long skip(long n) throws IOException {
        getBufIfOpen(); // Check for closed stream
				if (n <= 0) {
				    return 0;
				}
				long avail = count - pos;
     
        if (avail <= 0) { //如果缓存的数据已读完
            // If no mark position set then don't keep in buffer
            if (markpos <0) //如果没有设置mark,那么直接调用 underlying stream  的skip方法
                return getInIfOpen().skip(n);
            
            // 填充数据
            fill();
            avail = count - pos;
            if (avail <= 0) //如果avail<=0,说明underlying stream 中也没有剩余数据
                return 0;
        }
        
        long skipped = (avail < n) ? avail : n; //最avail 和 n 的最小值
        pos += skipped; //调整pos 是skip方法的本质
        return skipped;
    }

    /**
     * 返回可以的字节数。
     * <p>
     * This method returns the sum of the number of bytes remaining to be read in
     * the buffer (<code>count - pos</code>) and the result of calling the
     * {@link java.io.FilterInputStream#in in}.available().
     *
     * @return     an estimate of the number of bytes that can be read (or skipped
     *             over) from this input stream without blocking.
     * @exception  IOException  if this input stream has been closed by
     *                          invoking its {@link #close()} method,
     *                          or an I/O error occurs.
     */
    public synchronized int available() throws IOException {
	     return getInIfOpen().available() + (count - pos);
    }

    /** 
     * 打标记
     * markpos:把当前位置记录为标记
     * readlimit:在mark变的无线之前运行读取的最大字节数
     */
    public synchronized void mark(int readlimit) {
			marklimit = readlimit;
			markpos = pos;
    }

    /**
     * 重置操作。
     * 如果没有mark过,调用reset方法会抛出:IOException
     *
     */
    public synchronized void reset() throws IOException {
        getBufIfOpen(); // Cause exception if closed
			if (markpos < 0)
			    throw new IOException("Resetting to invalid mark");
			pos = markpos; //重置的效果是:下一次read读取继续从上次打标记的地方读取。
    }

    /**
     * 支持回溯操作
     */
    public boolean markSupported() {
	     return true;
    }

    /**
     * 关闭此流,并释放与此流相关的任何系统资源
     
     * 一旦close流后,任何 read(), available(), reset()将抛出异常
     */
    public void close() throws IOException {
        byte[] buffer;
        while ( (buffer = buf) != null) {
            if (bufUpdater.compareAndSet(this, buffer, null)) { //将buf设置为空,如果失败则循环知道buf 为空为止。因为close 不是线程安全的,所以适用原子类bufUpdater 配合操作。
                InputStream input = in;
                in = null;
                if (input != null)
                    input.close();
                return;
            }
            // Else retry in case a new buf was CASed in fill()
        }
    }
}




分享到:
评论

相关推荐

    Android实现下载zip压缩文件并解压的方法(附源码)

    import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import ...

    java7源码-JavaIO:JavaIO

    java.io.InputStream java.io.OutputStream java.io.Reader java.io.Writer 4. FileInputStream和FileOutputStream是什么? 这是在拷贝文件操作的时候,经常用的两个类。在处理小文件的时候,它们的性能还不错,在大...

    java解析txt

    import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.Reader; /** * @author...

    java算法,实现压缩及解压缩

     import java.io.BufferedInputStream;  import java.io.BufferedOutputStream;  import java.io.File;  import java.io.FileInputStream;  import java.io.FileOutputStream;  import java.util.zip.Zip...

    java播放MP3

    import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled....

    Java IO学习之缓冲输入流(BufferedInputStream)

    主要介绍了Java IO学习之缓冲输入流(BufferedInputStream)的相关资料,需要的朋友可以参考下

    java编写的类似文本编辑器的代码

    import java.io.BufferedInputStream; import javax.swing.*; import javax.swing.border.BevelBorder; import javax.swing.event.*; import javax.swing.filechooser.FileFilter; import javax.swing.text....

    图片文件上次,获取图片文件实际类型

    import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream...

    Java基于Socket文件传输示例

    近需要进行网络传输大文件,于是对基于socket的文件传输作了一个初步... import java .io.BufferedInputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java

    彻底解决jspsmartupload中文下载乱码问题

    用几个简单的java语句操作java.io.BufferedInputStream和java.io.BufferedOutputStream,就能彻底解决jspSmartUpload乱码问题。下载的时候无需再用jspsmartupload。 zip包中包含了jspsmartupload.jar和文件上传、...

    Java使用线程池递归压缩文件夹下面的所有子文件

     import java.io.BufferedInputStream;  import java.io.BufferedOutputStream;  import java.io.File;  import java.io.FileInputStream;  import java.io.FileOutputStream;  import java.io.InputS

    一个简单的Java服务器

    import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.IOException; ...

    Android开发之利用jsoup解析HTML页面的方法

    本文实例讲述了Android...分享给大家供大家参考,具体如下: ... 下面是主要的代码,由于使用及其简单,我这里就不再多说了: ... import java.io.BufferedInputStream; import java.io.IOException; import java.io.Input

    JAVA IO流缓冲字节流缓冲字符流等流经典代码示例加注释总结.rar

    2、常用21个IO流:FileWriter、FileReader、...3、JAVA IO流经典代码示例,示例从易到难。代码功能涉及字节、字符、字符串、文本文件、图片、音频、视频。演示错误用法和经典用法。 4、代码的结构可查看README文件。

    Android zip文件下载和解压实例

    import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java....

    BufferedInputStream源码分析图

    BufferedInputStream源码分析图

    Java读存二进制大文件块

    Java读存大块二进制资料文件,java.io 包中提供了BufferedInputStream 和BufferedOutputStream 类来缓存的读写流。但是读写大块的数据文件时,使用这两个类的效果并不是很好。本例演示了如何自定义缓存区,并使用不...

    Android通过SOCKET下载文件的方法

    import java.io.BufferedInputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io....

    JDATASTREAMDEMO_java_

    Java reads and stores large binary data files. The java.io package provides BufferedInputStream and BufferedOutputStream classes to buffer read and write streams

    android通过servlet上传文件到服务器

    import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.

Global site tag (gtag.js) - Google Analytics