解决方法
A BufferedInputStream adds functionality to another input stream-namely,the ability to buffer the input and to support the mark and reset methods. When the BufferedInputStream is created,an internal buffer array is created. As bytes from the stream are read or skipped,the internal buffer is refilled as necessary from the contained input stream,many bytes at a time. The mark operation remembers a point in the input stream and the reset operation causes all the bytes read since the most recent mark operation to be reread before new bytes are taken from the contained input stream.
@H_502_11@在内部使用缓冲区数组,而不是从底层输入流单独读取字节,读取足够的字节来填充缓冲区.这通常导致更快的性能,因为底层输入流需要较少的读取.
对于BufferedOutputStream则是相反的.
mark()和reset()可以如下使用:
1 BufferedInputStream bis = new BufferedInputStream(is); 2 byte[] b = new byte[4]; 3 bis.read(b); // read 4 bytes into b 4 bis.mark(10); // mark the stream at the current position - we can read 10 bytes before the mark point becomes invalid 5 bis.read(b); // read another 4 bytes into b 6 bis.reset(); // resets the position in the stream back to when mark was called 7 bis.read(b); // re-read the same 4 bytes as line 5 into b解释标记/重置一些更多…
BufferInputStream内部记住缓冲区中的当前位置.当您读取字节时,位置将递增.标记(10)的电话将保存当前位置.随后的读取调用将继续增加当前位置,但是当调用mark时,调用reset将将当前位置设置为其值.
标记的参数指定在标记位置无效之前调用标记后可以读取的字节数.一旦标记位置无效,您就不能再调用重置来返回.
例如,如果在第4行中使用了mark(2),则当在第6行调用reset()时,将会抛出IOException,因为标记位置将被无效,因为我们读取超过2个字节.