[TOC]html
最近研究JDK源码,发现IO体系中的BufferedInputStream
,颇有意思,平时对这个类有很多误解,因而写下这篇博客,以供学习java
/** * 此类继承FilterInputStream,该类使用了装饰着设计模式,FilterInputStream的源码超级简单 */ public class BufferedInputStream extends FilterInputStream { // 默认的buf[]缓存数组大小 private static int DEFAULT_BUFFER_SIZE = 8192; /** * The maximum size of array to allocate. * Some VMs reserve some header words in an array. * Attempts to allocate larger arrays may result in * OutOfMemoryError: Requested array size exceeds VM limit * * buf[]缓存数组最大值 为何会 减去8呢?由于一些JVM会数组头部存一些数据 */ private static int MAX_BUFFER_SIZE = Integer.MAX_VALUE - 8; /** * The internal buffer array where the data is stored. When necessary, * it may be replaced by another array of * a different size. * * 缓存数组,核心成员变量,全部操做都是围绕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"); /** * The index one greater than the index of the last valid byte in * the buffer. * This value is always * in the range <code>0</code> through <code>buf.length</code>; * elements <code>buf[0]</code> through <code>buf[count-1] * </code>contain buffered input data obtained * from the underlying input stream. * * buf[]数组中,有效数据的总数 */ protected int count; /** * The current position in the buffer. This is the index of the next * character to be read from the <code>buf</code> array. * <p> * This value is always in the range <code>0</code> * through <code>count</code>. If it is less * than <code>count</code>, then <code>buf[pos]</code> * is the next byte to be supplied as input; * if it is equal to <code>count</code>, then * the next <code>read</code> or <code>skip</code> * operation will require more bytes to be * read from the contained input stream. * * @see java.io.BufferedInputStream#buf * * buf[]数组中,当前读取位置 */ protected int pos; /** * The value of the <code>pos</code> field at the time the last * <code>mark</code> method was called. * <p> * This value is always * in the range <code>-1</code> through <code>pos</code>. * If there is no marked position in the input * stream, this field is <code>-1</code>. If * there is a marked position in the input * stream, then <code>buf[markpos]</code> * is the first byte to be supplied as input * after a <code>reset</code> operation. If * <code>markpos</code> is not <code>-1</code>, * then all bytes from positions <code>buf[markpos]</code> * through <code>buf[pos-1]</code> must remain * in the buffer array (though they may be * moved to another place in the buffer array, * with suitable adjustments to the values * of <code>count</code>, <code>pos</code>, * and <code>markpos</code>); they may not * be discarded unless and until the difference * between <code>pos</code> and <code>markpos</code> * exceeds <code>marklimit</code>. * * @see java.io.BufferedInputStream#mark(int) * @see java.io.BufferedInputStream#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() * * 该变量惟一入口就是mark(int readLimit),好比调用方法,mark(1024),那么后面读取的数据若是 * 超过了1024字节,那么这次mark就为无效标记,子类能够选择抛弃该mark标记,从头开始。不过具体实现 * 跟具体的子类有关,在BufferedInputStream中,会抛弃mark标记,从新将markpos赋值为-1 */ protected int marklimit; /** * Check to make sure that underlying input stream has not been * nulled out due to close; if not return it; * * 获取真正的输入流 */ private InputStream getInIfOpen() throws IOException { InputStream input = in; if (input == null) throw new IOException("Stream closed"); return input; } /** * Check to make sure that buffer has not been nulled out due to * close; if not return it; * * 获取缓存数组 */ private byte[] getBufIfOpen() throws IOException { byte[] buffer = buf; if (buffer == null) throw new IOException("Stream closed"); return buffer; } /** * Creates a <code>BufferedInputStream</code> * and saves its argument, the input stream * <code>in</code>, for later use. An internal * buffer array is created and stored in <code>buf</code>. * * @param in the underlying input stream. * * 默认缓存数组大小为8kb */ public BufferedInputStream(InputStream in) { this(in, DEFAULT_BUFFER_SIZE); } /** * Creates a <code>BufferedInputStream</code> * with the specified buffer size, * and saves its argument, the input stream * <code>in</code>, for later use. An internal * buffer array of length <code>size</code> * is created and stored in <code>buf</code>. * * @param in the underlying input stream. * @param size the buffer size. * @exception IllegalArgumentException if {@code size <= 0}. */ 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. * * 该方法做用,经过丢弃buf[]数据、增大buf[]数组,以腾出位置,将输入流中新的数据保存到buf[]缓存数组中 */ private void fill() throws IOException { byte[] buffer = getBufIfOpen(); if (markpos < 0) // 由于没有mark标记,直接丢弃buf[]数据 pos = 0; /* no mark: throw away the buffer */ else if (pos >= buffer.length) /* no room left in buffer */ if (markpos > 0) { /* can throw away early part of the buffer */ int sz = pos - markpos; System.arraycopy(buffer, markpos, buffer, 0, sz); pos = sz; markpos = 0; // !!!往下执行,markpos所有等于0 } else if (buffer.length >= marklimit) { markpos = -1; /* buffer got too big, invalidate mark */ pos = 0; /* drop buffer contents */ } else if (buffer.length >= MAX_BUFFER_SIZE) { throw new OutOfMemoryError("Required array size too large"); } else { /* grow buffer */ int nsz = (pos <= MAX_BUFFER_SIZE - pos) ? pos * 2 : MAX_BUFFER_SIZE; if (nsz > marklimit) // buf[]长度不超过marklimit,这样mark标记始终有效 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; // 将输入流中的数据独到buf[]数组中 int n = getInIfOpen().read(buffer, pos, buffer.length - pos); if (n > 0) count = n + pos; } /** * See * the general contract of the <code>read</code> * method of <code>InputStream</code>. * * @return the next byte of data, or <code>-1</code> if the end of the * stream is reached. * @exception IOException if this input stream has been closed by * invoking its {@link #close()} method, * or an I/O error occurs. * @see java.io.FilterInputStream#in */ public synchronized int read() throws IOException { // 说明当前buf[]数组大小不够了,须要fill() if (pos >= count) { fill(); // 说明没有读取到任何数据 if (pos >= count) 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 the requested length is at least as large as the buffer, and if there is no mark/reset activity, do not bother to copy the bytes into the local buffer. In this way buffered streams will cascade harmlessly. */ // !!!这个位置代码很重要 // !!!这个位置代码很重要 // !!!这个位置代码很重要 /** * 当写入指定数组b的长度大小超过BufferedInputStream中核心缓存数组buf[]的大小而且 * markpos < 0,那么就直接从数据流中读取数据给b数组,而不经过buf[]缓存数组,避免buf[]数组急剧增大 * */ if (len >= getBufIfOpen().length && markpos < 0) { return getInIfOpen().read(b, off, len); } fill(); avail = count - pos; if (avail <= 0) return -1; } int cnt = (avail < len) ? avail : len; System.arraycopy(getBufIfOpen(), pos, b, off, cnt); pos += cnt; return cnt; } /** * Reads bytes from this byte-input stream into the specified byte array, * starting at the given offset. * * <p> This method implements the general contract of the corresponding * <code>{@link InputStream#read(byte[], int, int) read}</code> method of * the <code>{@link InputStream}</code> class. As an additional * convenience, it attempts to read as many bytes as possible by repeatedly * invoking the <code>read</code> method of the underlying stream. This * iterated <code>read</code> continues until one of the following * conditions becomes true: <ul> * * <li> The specified number of bytes have been read, * * <li> The <code>read</code> method of the underlying stream returns * <code>-1</code>, indicating end-of-file, or * * <li> The <code>available</code> method of the underlying stream * returns zero, indicating that further input requests would block. * * </ul> If the first <code>read</code> on the underlying stream returns * <code>-1</code> to indicate end-of-file then this method returns * <code>-1</code>. Otherwise this method returns the number of bytes * actually read. * * <p> Subclasses of this class are encouraged, but not required, to * attempt to read as many bytes as possible in the same fashion. * * @param b destination buffer. * @param off offset at which to start storing bytes. * @param len maximum number of bytes to read. * @return the number of bytes read, or <code>-1</code> if the end of * the stream has been reached. * @exception IOException if this input stream has been closed by * invoking its {@link #close()} method, * or an I/O error occurs. * * 该方法主要调用read1(byte[] b, int off, int 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) { throw new IndexOutOfBoundsException(); } else if (len == 0) { return 0; } int n = 0; for (;;) { int nread = read1(b, off + n, len - n); if (nread <= 0) return (n == 0) ? nread : n; n += nread; if (n >= len) return n; // if not closed but no bytes available, return InputStream input = in; if (input != null && input.available() <= 0) return n; } } /** * See the general contract of the <code>skip</code> * method of <code>InputStream</code>. * * @exception IOException if the stream does not support seek, * or if this input stream has been closed by * invoking its {@link #close()} method, or an * I/O error occurs. * * 跳过流中指定字节数,感受该方法用处不大,至少到目前为止,我本人还历来没有用过skip方法 */ 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) return getInIfOpen().skip(n); // Fill in buffer to save bytes for reset fill(); avail = count - pos; if (avail <= 0) return 0; } long skipped = (avail < n) ? avail : n; pos += skipped; return skipped; } /** * Returns an estimate of the number of bytes that can be read (or * skipped over) from this input stream without blocking by the next * invocation of a method for this input stream. The next invocation might be * the same thread or another thread. A single read or skip of this * many bytes will not block, but may read or skip fewer bytes. * <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. * * buf[]数组剩余字节数+输入流中剩余字节数 */ public synchronized int available() throws IOException { int n = count - pos; int avail = getInIfOpen().available(); return n > (Integer.MAX_VALUE - avail) ? Integer.MAX_VALUE : n + avail; } /** * See the general contract of the <code>mark</code> * method of <code>InputStream</code>. * * @param readlimit the maximum limit of bytes that can be read before * the mark position becomes invalid. * @see java.io.BufferedInputStream#reset() * * 标记位置,marklimit只有在这里才可以被赋值,readlimit表示mark()方法执行后,最多可以从流中 * 读取的数据,若是超过该字节大小,那么在fill()的时候,就会认为此mark()标记无效,从新将 * markpos = -1,pos = 0 */ public synchronized void mark(int readlimit) { marklimit = readlimit; markpos = pos; } /** * See the general contract of the <code>reset</code> * method of <code>InputStream</code>. * <p> * If <code>markpos</code> is <code>-1</code> * (no mark has been set or the mark has been * invalidated), an <code>IOException</code> * is thrown. Otherwise, <code>pos</code> is * set equal to <code>markpos</code>. * * @exception IOException if this stream has not been marked or, * if the mark has been invalidated, or the stream * has been closed by invoking its {@link #close()} * method, or an I/O error occurs. * @see java.io.BufferedInputStream#mark(int) */ public synchronized void reset() throws IOException { getBufIfOpen(); // Cause exception if closed if (markpos < 0) throw new IOException("Resetting to invalid mark"); pos = markpos; } /** * Tests if this input stream supports the <code>mark</code> * and <code>reset</code> methods. The <code>markSupported</code> * method of <code>BufferedInputStream</code> returns * <code>true</code>. * * @return a <code>boolean</code> indicating if this stream type supports * the <code>mark</code> and <code>reset</code> methods. * @see java.io.InputStream#mark(int) * @see java.io.InputStream#reset() */ public boolean markSupported() { return true; } /** * Closes this input stream and releases any system resources * associated with the stream. * Once the stream has been closed, further read(), available(), reset(), * or skip() invocations will throw an IOException. * Closing a previously closed stream has no effect. * * @exception IOException if an I/O error occurs. */ public void close() throws IOException { byte[] buffer; while ( (buffer = buf) != null) { if (bufUpdater.compareAndSet(this, buffer, null)) { InputStream input = in; in = null; if (input != null) input.close(); return; } // Else retry in case a new buf was CASed in fill() } } }
网上不少博客,说BufferedInputStream
颇有用,能够一次性从IO中读入不少数据,而后缓存在buf[]中,这样就减小了IO消耗,不少博主,甚至给出了一些代码实操,证实BufferedInputStream
确实能够提升效率,这自己没有任何问题,可是经我深刻源码研究事后,却发现实际场景中,该类使用频率不多,根本不须要BufferedInputStream
设计模式
我将结合代码,进行更有力的说明:数组
// file文件大小1个G private static String file = "D:\\StudySoftware\\VMware_virtualbox\\Data_vmware\\VMwareMachine\\kafka_single\\kafka-single-103-da5cf665.vmem"; private static void file() throws IOException{ long beginTime = System.currentTimeMillis(); FileInputStream input = new FileInputStream(file); byte[] bytes = new byte[1024 * 1]; int read = 0; while ((read = input.read(bytes, 0, bytes.length)) != -1) { // 不执行任何操做,仅仅读取文件 } long endTime = System.currentTimeMillis(); System.out.println("file: 耗费时间:" + (endTime - beginTime)); } private static void bufferd() throws IOException{ long beginTime = System.currentTimeMillis(); FileInputStream input = new FileInputStream(file); BufferedInputStream bufferedInput = new BufferedInputStream(input); byte[] bytes = new byte[1024 * 1]; int read = 0; while ((read = bufferedInput.read(bytes, 0, bytes.length)) != -1) { //不执行任何操做,仅仅读取文件 } long endTime = System.currentTimeMillis(); System.out.println("buffered: 耗费时间:" + (endTime - beginTime)); }
注意:缓存
代码操做的时候,两个方法不可以对同一个文件进行操做,防止JVM会自动优化,由于第一个方法读完整个文件,第二个方法再读的时候,JVM可能保存了部分信息,从而形成测试数据的不许确。而且为了最大程度保证测试数据的准确性,一次JVM启动,只测试一个方法安全
结果:多线程
①当 byte[] bytes = new byte[1024 * 1]; 数组大小为1024less
buffered: 耗费时间:855 file: 耗费时间:3073async
②当 byte[] bytes = new byte[1024 * 2]; 数组大小为2018ide
buffered: 耗费时间:813 file: 耗费时间:1909
③当 byte[] bytes = new byte[1024 * 3]; 数组大小为3072
buffered: 耗费时间:1304 file: 耗费时间:1476
④当 byte[] bytes = new byte[1024 * 4]; 数组大小为4096
buffered: 耗费时间:844 file: 耗费时间:1287
⑤当 byte[] bytes = new byte[1024 * 5]; 数组大小为5120
buffered: 耗费时间:1343 file: 耗费时间:1061
⑥当 byte[] bytes = new byte[1024 * 6]; 数组大小为6144
buffered: 耗费时间:1280 file: 耗费时间:985
⑦当 byte[] bytes = new byte[1024 * 7]; 数组大小为7168
buffered: 耗费时间:1443 file: 耗费时间:851
⑧当 byte[] bytes = new byte[1024 * 8]; 数组大小为8192
buffered: 耗费时间:774 file: 耗费时间:739
⑨当 byte[] bytes = new byte[1024 * 9]; 数组大小为9216
buffered: 耗费时间:734 file: 耗费时间:749
⑩当 byte[] bytes = new byte[1024 * 10]; 数组大小为10240
buffered: 耗费时间:739 file: 耗费时间:697
... ... ...
咱们能够得出如下重要结论:
<font color="red">当bytes比较小时,使用BufferedInputStream
确实读取文件时要快很多,但是当bytes逐步增大,尤为是达到8kb的时候,咱们会发现 BufferedInputStream
和FileInputStream
读取文件速度差很少了,没有明显差别</font>
咱们深刻源码,便可发现:
所以当咱们把 while ((read = input.read(bytes, 0, bytes.length)) != -1)
中的bytes增大时,BufferedInputStream
没有任何做用(<font color="red">除非有mark、reset需求</font>)
有的小伙伴,确定会说,那我将BufferedInputStream
中的buf[]大小提升不就好了吗?
能够是能够,可是我将 while ((read = input.read(bytes, 0, bytes.length)) != -1)
中的bytes大小增大不就好了? 说到底都是字节数组,一个是在BufferedInputStream
外面,一个是在BufferedInputStream
内部,而如今咱们进行流读取的时候,不少时候是不须要mark、reset操做的,而且咱们设置外部bytes大小一般会比较大,这个时候,彻底能够不使用BufferedInputStream
①第一种使用场景,就是当咱们须要mark、reset特性时。不过要特别注意,mark、reset的使用,里面涉及到不少东西,特别是当BufferedInputStream
执行fill()操做时
public static void main(String[] args) { try { final byte[] src = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}; final ByteArrayInputStream bis = new ByteArrayInputStream(src); final BufferedInputStream bufis = new BufferedInputStream(bis, 5); int data = -1; int i = 0; while((data = bufis.read()) != -1) { if(data == 4) { bufis.mark(2); } if(i++ == 9) { bufis.reset(); } System.out.printf("%d", data); } } catch(IOException ioex) { ioex.printStackTrace(); } } // 原文连接:https://blog.csdn.net/qq_26971305/article/details/79472696
有兴趣的朋友,能够debug上面的代码,debug下面的状况,相应你对BufferedInputStream
有更深的理解
if(i++ == 5)
if(i++ == 6)
if(i++ == 7)
if(i++ == 8)
if(i++ == 9)
if(i++ == 10)
... ... ... 时间多的朋友,能够设置BufferedInputStream
中buf[]的大小长度和if(i++ == xx)判断语句中的值来看看BufferedInputStream
类的执行流程
mark、reset特性不可乱用,否则会抛出异常的
public synchronized void reset() throws IOException { getBufIfOpen(); // Cause exception if closed if (markpos < 0) throw new IOException("Resetting to invalid mark"); pos = markpos; }
②第二种使用场景,当BufferedInputStream
配合DataInputStream
和ObjectOutputStream
使用时
ObjectInputStream input = new ObjectInputStream(new BufferedInputStream(new FileInputStream("E:\\obejct.txt"))); DataInputStream input = new DataInputStream(new BufferedInputStream(new FileInputStream("E:\\obejct.txt")));
当DataXxxStream
从管道读取字节流的时候,是一个一个字节读取的
而ObjectInputStream
底层依赖了DataXxxStream
对象
<br /> 参考连接: <https://blog.csdn.net/qq_26971305/article/details/79472696> <br />
<div style="font-size:18px;background-color: #CCFFFF"> 做者:<font color="#551A8B" size="5px">一杯热咖啡AAA</font> <br /> 出处:<a href="https://www.cnblogs.com/AdaiCoffee/">https://www.cnblogs.com/AdaiCoffee/</a> <br /> 本文以学习、研究和分享为主,欢迎转载。若是文中有不妥或者错误的地方还望指出,以避免误人子弟。若是你有更好的想法和意见,能够留言讨论,谢谢! </div>
原文出处:https://www.cnblogs.com/AdaiCoffee/p/11369699.html