版本
JDK8(JDK1.8)
InputStream虚拟类源码重点
1.InputStream虚拟类实现 Closeable 接口,自然就具有 Closeable 接口特性
Closeable 源码可以看我这篇文章 Closeable
2.InputStream虚拟类只定义了一个虚拟方法abstract int read(),用于阻塞地读取一个字节,而该虚拟类的其他部分方法都使用该虚拟方法来实现,所以其子类只需要实现一个read()方法即可
3.InputStream虚拟类方法
方法名 | 作用 |
---|---|
abstract int read() | 阻塞地读取一个字节,返回0到255范围内的整数 |
int read(byte b[]) | 有限循环阻塞地读取字节填满字节数组b,返回实际读取的字节数(遇到文件结尾或出错就无法填满,下面也一样) |
int read(byte b[], int off, int len) | 有限循环阻塞地读取字节从字节数组b偏移量off处开始存放,最多读取len个字节,返回实际读取的字节数 |
byte[] readAllBytes() | 读取输入流所有字节存到字节数组中,最多读取2G的文件(由于字节数组长度有限) |
int readNBytes(byte[] b, int off, int len) | 无限循环阻塞地读取字节从字节数组b偏移量off处开始存放,正常情况读取len个字节才返回,返回实际读取的字节数 |
long skip(long n) | 跳过并丢弃此输入流中的n字节数据,使用read()实现,效率较低 |
int available() | 返回可从此输入流读取(或跳过)的字节数的估计值 |
void close() | 关闭此输入流并释放与该流关联的所有系统资源 |
synchronized void mark(int readlimit) | 标记此输入流中的当前位置 |
synchronized void reset() | 将此流重新定位到上次对此输入流调用mark方法时的位置 |
boolean markSupported() | 测试此输入流是否支持标记mark和重置reset方法 |
long transferTo(OutputStream out) | 从该输入流使用read()读取所有字节,并按读取顺序将字节写入给定的输出流 |
long skip(long n) 原生实现的是RandomAccessFile类
RandomAccessFile源码可以看我这篇文章 RandomAccessFile
InputStream虚拟类源码
package java.io;
import java.util.Arrays;
import java.util.Objects;
/**
* This abstract class is the superclass of all classes representing
* an input stream of bytes.
* 这个抽象类是表示字节输入流的所有类的超类。
*
* Applications that need to define a subclass of InputStream
* must always provide a method that returns the next byte of input.
* 需要定义InputStream子类的应用程序必须始终提供返回输入的下一个字节的方法。
*
* @author Arthur van Hoff
* @see java.io.BufferedInputStream
* @see java.io.ByteArrayInputStream
* @see java.io.DataInputStream
* @see java.io.FilterInputStream
* @see java.io.InputStream#read()
* @see java.io.OutputStream
* @see java.io.PushbackInputStream
* @since 1.0
*/
public abstract class InputStream implements Closeable {
// MAX_SKIP_BUFFER_SIZE is used to determine the maximum buffer size to
// use when skipping.
// 最大跳过缓冲区大小用于确定跳过时要使用的最大缓冲区大小。
private static final int MAX_SKIP_BUFFER_SIZE = 2048;
// 默认缓存区大小
private static final int DEFAULT_BUFFER_SIZE = 8192;
/**
* Reads the next byte of data from the input stream. The value byte is
* returned as an int
in the range 0
to
* 255
. If no byte is available because the end of the stream
* has been reached, the value -1
is returned. This method
* blocks until input data is available, the end of the stream is detected,
* or an exception is thrown.
* 从输入流读取数据的下一个字节。值字节作为0到255范围内的整数返回。
* 如果由于到达流的结尾而没有字节可用,则返回值-1。此方法会一直阻塞,
* 直到输入数据可用、检测到流结束或引发异常为止。
*
*
A subclass must provide an implementation of this method.
* 子类必须提供此方法的实现。
*
* @return the next byte of data, or -1
if the end of the
* stream is reached.
* 数据的下一个字节,如果到达流的末尾,则为-1。
* @exception IOException if an I/O error occurs.
*/
public abstract int read() throws IOException;
/**
* Reads some number of bytes from the input stream and stores them into
* the buffer array b
. The number of bytes actually read is
* returned as an integer. This method blocks until input data is
* available, end of file is detected, or an exception is thrown.
* 从输入流中读取一定数量的字节,并将它们存储到缓冲区数组b中。
* 实际读取的字节数作为整数返回。
* 此方法将一直阻塞,直到输入数据可用、检测到文件结尾或引发异常为止。
*
*
*
*
If the length of b
is zero, then no bytes are read and
* 0
is returned; otherwise, there is an attempt to read at
* least one byte. If no byte is available because the stream is at the
* end of the file, the value -1
is returned; otherwise, at
* least one byte is read and stored into b
.
* 如果b的长度为零,则不读取字节,返回0;
* 否则,将尝试读取至少一个字节。
* 如果由于流位于文件末尾而没有可用字节,则返回值-1;
* 否则,至少读取一个字节并将其存储到b中
*
*
*
*
*
The first byte read is stored into element b[0]
, the
* next one into b[1]
, and so on. The number of bytes read is,
* at most, equal to the length of b
. Let k be the
* number of bytes actually read; these bytes will be stored in elements
* b[0]
through b[
k-1]
,
* leaving elements b[
k]
through
* b[b.length-1]
unaffected.
* 读取的第一个字节存储在元素b[0]中,下一个字节存储在元素b[1]中,
* 依此类推。读取的字节数最多等于b的长度。
* 设k为实际读取的字节数;
* 这些字节将存储在元素b[0]到b[k-1]中,使元素b[k]到b[b.length-1]不受影响。
*
*
*
*
The read(b)
method for class InputStream
* has the same effect as:
read(b, 0, b.length)
* 类InputStream的read(b)方法与:read(b, 0, b.length)具有相同的效果
*
*
*
* @param b the buffer into which the data is read. 将数据读入的缓冲区。
* @return the total number of bytes read into the buffer, or
* -1
if there is no more data because the end of
* the stream has been reached.
* 读取到缓冲区的总字节数,如果由于到达流的结尾而没有更多数据,则为-1。
*
* @exception IOException If the first byte cannot be read for any reason
* other than the end of the file, if the input stream has been closed, or
* if some other I/O error occurs.
* 如果由于文件结尾以外的任何原因无法读取第一个字节,如果输入流已关闭,或者如果发生其他I/O错误。
*
* @exception NullPointerException if b
is null
.
* @see java.io.InputStream#read(byte[], int, int)
*/
public int read(byte b[]) throws IOException {
return read(b, 0, b.length);
}
/**
* Reads up to len
bytes of data from the input stream into
* an array of bytes. An attempt is made to read as many as
* len
bytes, but a smaller number may be read.
* The number of bytes actually read is returned as an integer.
* 从输入流读取多达len个字节的数据到一个字节数组中。
* 尝试读取多达len字节的数据,但可以读取较少的数据。实际读取的字节数作为整数返回。
*
* This method blocks until input data is available, end of file is
* detected, or an exception is thrown.
* 此方法将一直阻塞,直到输入数据可用、检测到文件结尾或引发异常为止。
*
*
If len
is zero, then no bytes are read and
* 0
is returned; otherwise, there is an attempt to read at
* least one byte. If no byte is available because the stream is at end of
* file, the value -1
is returned; otherwise, at least one
* byte is read and stored into b
.
* 如果len为零,则不读取字节,返回0;
* 否则,将尝试读取至少一个字节。如果由于流位于文件末尾而没有可用字节,则返回值-1;
* 否则,至少读取一个字节并将其存储到b中。
*
*
*
The first byte read is stored into element b[off]
, the
* next one into b[off+1]
, and so on. The number of bytes read
* is, at most, equal to len
. Let k be the number of
* bytes actually read; these bytes will be stored in elements
* b[off]
through b[off+
k-1]
,
* leaving elements b[off+
k]
through
* b[off+len-1]
unaffected.
* 读取的第一个字节存储在元素b[off]中,下一个字节存储在元素b[off+1]中,依此类推。
* 读取的字节数最多等于len。
* 设k为实际读取的字节数;这些字节将存储在元素b[off]到b[off+k-1]中,
* 使元素b[off+k]到b[off+len-1]不受影响。
*
*
*
*
*
In every case, elements b[0]
through
* b[off]
and elements b[off+len]
through
* b[b.length-1]
are unaffected.
* 在每种情况下,元素b[0]到b[off]以及元素b[off+len]到b[b.length-1]都不受影响。
*
*
*
The read(b,
off,
len)
method
* for class InputStream
simply calls the method
* read()
repeatedly. If the first such call results in an
* IOException
, that exception is returned from the call to
* the read(b,
off,
len)
method. If
* any subsequent call to read()
results in a
* IOException
, the exception is caught and treated as if it
* were end of file; the bytes read up to that point are stored into
* b
and the number of bytes read before the exception
* occurred is returned. The default implementation of this method blocks
* until the requested amount of input data len
has been read,
* end of file is detected, or an exception is thrown. Subclasses are encouraged
* to provide a more efficient implementation of this method.
* InputStream类的read(b,off,len)方法只是重复调用read()方法。
* 如果第一个这样的调用导致IOException,那么从对read(b,off,len)方法的调用返回该异常。
* 如果对read()的任何后续调用导致IOException,则会捕获该异常并将其视为文件结尾;
* 读取到该点的字节存储到b中,并返回异常发生前读取的字节数。
* 此方法的默认实现会一直阻塞,直到读取请求的输入数据量len、检测到文件结尾或引发异常为止。
* 鼓励子类提供此方法更有效的实现。
*
*
*
*
* @param b the buffer into which the data is read. 将数据读入的缓冲区。
* @param off the start offset in array b
* at which the data is written. 数组b中写入数据的起始偏移量。
* @param len the maximum number of bytes to read. 要读取的最大字节数
* @return the total number of bytes read into the buffer, or
* -1
if there is no more data because the end of
* the stream has been reached.
* 读取到缓冲区的总字节数,如果由于到达流的结尾而没有更多数据,则为-1。
*
* @exception IOException If the first byte cannot be read for any reason
* other than end of file, or if the input stream has been closed, or if
* some other I/O error occurs.
* 如果由于文件结尾以外的任何原因无法读取第一个字节,
* 或者如果输入流已关闭,或者如果发生其他I/O错误
*
* @exception NullPointerException If b
is null
.
* @exception IndexOutOfBoundsException If off
is negative,
* len
is negative, or len
is greater than
* b.length - off
* 如果off为负值,len为负值,或len大于 b.length-off
*
*
* @see java.io.InputStream#read()
*/
public int read(byte b[], int off, int len) throws IOException {
if (b == null) {
throw new NullPointerException();
} else if (off < 0 || len < 0 || len > b.length - off) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return 0;
}
// 尝试读取一个字节
int c = read();
// 如果到了文件的结尾
if (c == -1) {
// 直接返回-1
return -1;
}
// 如果读取到该字节,设置到字节数组b中
b[off] = (byte)c;
// 循环len-1次
int i = 1;
try {
for (; i < len ; i++) {
// 每次读取一个字节
c = read();
// 如果遇到文件结尾跳出循环
if (c == -1) {
break;
}
// 如果该字节成功读取,将其设置到字节数组中
b[off + i] = (byte)c;
}
} catch (IOException ee) {
}
return i;
}
/**
* 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
* 要分配的最大数组大小。有些虚拟机在数组中保留一些头字。
* 尝试分配较大的数组可能会导致OutOfMemoryError:请求的数组大小超过VM限制
*/
private static final int MAX_BUFFER_SIZE = Integer.MAX_VALUE - 8;
/**
* Reads all remaining bytes from the input stream. This method blocks until
* all remaining bytes have been read and end of stream is detected, or an
* exception is thrown. This method does not close the input stream.
* 从输入流读取所有剩余字节。
* 此方法会一直阻塞,直到读取完所有剩余字节并检测到流结束,
* 或者引发异常。此方法不会关闭输入流。
*
*
When this stream reaches end of stream, further invocations of this
* method will return an empty byte array.
* 当这个流到达流的末尾时,这个方法的进一步调用将返回一个空字节数组。
*
*
Note that this method is intended for simple cases where it is
* convenient to read all bytes into a byte array. It is not intended for
* reading input streams with large amounts of data.
* 请注意,此方法适用于方便地将所有字节读入字节数组的简单情况。
* 它不用于读取具有大量数据的输入流。
*
*
*
The behavior for the case where the input stream is asynchronously
* closed, or the thread interrupted during the read, is highly input
* stream specific, and therefore not specified.
* 输入流异步关闭或线程在读取期间中断的情况下的行为是高度特定于输入流的,因此未指定。
*
*
If an I/O error occurs reading from the input stream, then it may do
* so after some, but not all, bytes have been read. Consequently the input
* stream may not be at end of stream and may be in an inconsistent state.
* It is strongly recommended that the stream be promptly closed if an I/O
* error occurs.
* 如果从输入流读取时发生I/O错误,则可能会在读取部分(而不是全部)字节后发生。
* 因此,输入流可能不在流的末尾,并且可能处于不一致的状态。
* 强烈建议在发生I/O错误时立即关闭流。
*
* @return a byte array containing the bytes read from this input stream 包含从此输入流读取的字节的字节数组
* @throws IOException if an I/O error occurs 如果发生I/O错误
* @throws OutOfMemoryError if an array of the required size cannot be
* allocated. For example, if an array larger than {@code 2GB} would
* be required to store the bytes.
* 如果无法分配所需大小的数组。例如,如果需要大于2GB的数组来存储字节。
*
* @since 9
*/
// 读取所有字节到字节数组中,除非文件大于2G,不然可以容纳下
public byte[] readAllBytes() throws IOException {
// 创建一个缓冲池字节数组 8KB大小
byte[] buf = new byte[DEFAULT_BUFFER_SIZE];
// 容量为缓冲字节数组长度
int capacity = buf.length;
int nread = 0;
int n;
for (;;) {
// read to EOF which may read more or less than initial buffer size
// 读取到EOF,该EOF可以读取大于或小于初始缓冲区大小
// EOF即结尾
// 循环进行这个操作,直到把缓冲字节数组填满
while ((n = read(buf, nread, capacity - nread)) > 0)
//由于n不确定,每次读取的字节也不确定
//根据读取的字节确定偏移量nread和
//现在要读取的剩余字节长度capacity - nread
nread += n;
// if the last call to read returned -1, then we're done
// 如果最后一个read调用返回-1,那么我们就完成了
if (n < 0)
break;
// need to allocate a larger buffer
// 需要分配更大的缓冲区
// 如果上面缓存区填满了,仍然有数据没有读取完,就进行缓存区扩容操作
// 如果容量小于 最大缓存区容量 - 容量 ,即 capacity <= MAX_BUFFER_SIZE / 2
// 即小于最大容量的二分之一
if (capacity <= MAX_BUFFER_SIZE - capacity) {
// 进行二倍扩容
capacity = capacity << 1;
} else {
// 如果扩容前容量已经等于最大缓冲池大小,则抛出异常
if (capacity == MAX_BUFFER_SIZE)
throw new OutOfMemoryError("Required array size too large");
// 将容量设为最大缓冲池大小
capacity = MAX_BUFFER_SIZE;
}
// 复制数组进行扩容
buf = Arrays.copyOf(buf, capacity);
}
// 如果容量等于偏移量,说明字节数组已经被填满,可以直接返回
// 如果容量大于偏移量,说明字节数组有空闲空间,需要复制缩容
return (capacity == nread) ? buf : Arrays.copyOf(buf, nread);
}
/**
* Reads the requested number of bytes from the input stream into the given
* byte array. This method blocks until {@code len} bytes of input data have
* been read, end of stream is detected, or an exception is thrown. The
* number of bytes actually read, possibly zero, is returned. This method
* does not close the input stream.
* 将请求的字节数从输入流读取到给定的字节数组中。
* 此方法会一直阻塞,直到读取了len字节的输入数据、检测到流结束或引发异常为止。
* 返回实际读取的字节数,可能为零。此方法不会关闭输入流。
*
*
In the case where end of stream is reached before {@code len} bytes
* have been read, then the actual number of bytes read will be returned.
* When this stream reaches end of stream, further invocations of this
* method will return zero.
* 如果在读取len字节之前到达流的末尾,那么将返回实际读取的字节数。
* 当此流到达流的末尾时,此方法的进一步调用将返回零。
*
*
*
If {@code len} is zero, then no bytes are read and {@code 0} is
* returned; otherwise, there is an attempt to read up to {@code len} bytes.
* 如果len为零,则不读取字节,返回0;否则,将尝试读取多达len个字节。
*
*
*
The first byte read is stored into element {@code b[off]}, the next
* one in to {@code b[off+1]}, and so on. The number of bytes read is, at
* most, equal to {@code len}. Let k be the number of bytes actually
* read; these bytes will be stored in elements {@code b[off]} through
* {@code b[off+}k{@code -1]}, leaving elements {@code b[off+}k
* {@code ]} through {@code b[off+len-1]} unaffected.
* 读取的第一个字节存储在元素b[off]中,下一个字节存储在b[off+1]中,依此类推。
* 读取的字节数最多等于len。设k为实际读取的字节数;
* 这些字节将存储在元素b[off]到b[off+k-1]中,而元素b[off+k]到b[off+len-1]不受影响。
*
*
*
*
The behavior for the case where the input stream is asynchronously
* closed, or the thread interrupted during the read, is highly input
* stream specific, and therefore not specified.
* 输入流异步关闭或线程在读取期间中断的情况下的行为是高度特定于输入流的,因此未指定。
*
*
*
*
If an I/O error occurs reading from the input stream, then it may do
* so after some, but not all, bytes of {@code b} have been updated with
* data from the input stream. Consequently the input stream and {@code b}
* may be in an inconsistent state. It is strongly recommended that the
* stream be promptly closed if an I/O error occurs.
* 如果从输入流读取时发生I/O错误,
* 则可能在使用输入流中的数据更新了b的部分(而不是全部)字节后发生。
* 因此,输入流和b可能处于不一致的状态。强烈建议在发生I/O错误时立即关闭流。
*
*
*
* @param b the byte array into which the data is read 读取数据的字节数组
* @param off the start offset in {@code b} at which the data is written 数组b中写入数据的起始偏移量
* @param len the maximum number of bytes to read 要读取的最大字节数
* @return the actual number of bytes read into the buffer 读入缓冲区的实际字节数
* @throws IOException if an I/O error occurs
* @throws NullPointerException if {@code b} is {@code null}
* @throws IndexOutOfBoundsException If {@code off} is negative, {@code len}
* is negative, or {@code len} is greater than {@code b.length - off}
* 如果off为负,len为负,或者len大于b.length-off
*
* @since 9
*/
public int readNBytes(byte[] b, int off, int len) throws IOException {
// 如果b为空,抛出异常
Objects.requireNonNull(b);
// 如果索引越界,抛出异常
if (off < 0 || len < 0 || len > b.length - off)
throw new IndexOutOfBoundsException();
// n为读取的字节总数
int n = 0;
// 如果读取字节数小于要求的读取字节数,则循环不断读取
while (n < len) {
// count返回实际读取字节数
// 每次读取的偏移量 off + n 和要读取的字节数 len - n 都得重新设置
int count = read(b, off + n, len - n);
// 如果遇到文件结尾,则退出循环
if (count < 0)
break;
// 更新读取字节总数
n += count;
}
return n;
}
/**
* Skips over and discards n
bytes of data from this input
* stream. The skip
method may, for a variety of reasons, end
* up skipping over some smaller number of bytes, possibly 0
.
* This may result from any of a number of conditions; reaching end of file
* before n
bytes have been skipped is only one possibility.
* The actual number of bytes skipped is returned. If {@code n} is
* negative, the {@code skip} method for class {@code InputStream} always
* returns 0, and no bytes are skipped. Subclasses may handle the negative
* value differently.
* 跳过并丢弃此输入流中的n字节数据。
* 由于各种原因,skip方法可能最终跳过一些较少的字节数,可能为0。
* 这可能是由许多条件中的任何一种导致的;
* 在跳过n个字节之前到达文件末尾只是一种可能性。返回跳过的实际字节数。
* 如果n为负,则类InputStream的skip方法始终返回0,并且不跳过任何字节。
* 子类可能会以不同的方式处理负值。
*
*
The skip
method implementation of this class creates a
* byte array and then repeatedly reads into it until n
bytes
* have been read or the end of the stream has been reached. Subclasses are
* encouraged to provide a more efficient implementation of this method.
* For instance, the implementation may depend on the ability to seek.
* 此类的skip方法实现创建一个字节数组,然后重复读取该数组,直到读取了n个字节或到达流的末尾。
* 鼓励子类提供此方法更有效的实现。例如,实现可能取决于搜索的能力。
*
* @param n the number of bytes to be skipped. 要跳过的字节数。
* @return the actual number of bytes skipped. 跳过的实际字节数
* @throws IOException if an I/O error occurs.
*/
public long skip(long n) throws IOException {
// 剩余要跳过的字节 remaining
long remaining = n;
int nr;
// 如果要跳过的字节小于n
if (n <= 0) {
return 0;
}
// 如果要跳过的字节数大于2KB,则取2KB
int size = (int)Math.min(MAX_SKIP_BUFFER_SIZE, remaining);
// 跳过的字节缓冲数组
byte[] skipBuffer = new byte[size];
// 当要跳过的剩余字节大于0
while (remaining > 0) {
// 使用read(.) 方法来达到跳过字节目的,效率比较低
// nr为本次read实际读取到的字节数,也就是实际跳过的字节数
nr = read(skipBuffer, 0, (int)Math.min(size, remaining));
if (nr < 0) {
break;
}
// 更新要跳过的剩余字节数
remaining -= nr;
}
// 返回实际跳过的字节数
// 即期望跳过的字节数 - 剩余要跳过的字节数
return n - remaining;
}
/**
* 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.
* 返回可从此输入流读取(或跳过)的字节数的估计值,
* 而无需在下次调用此输入流的方法时阻塞。
* 下一次调用可能是同一个线程或另一个线程。
* 单个读取或跳过这么多字节不会阻塞,但可能读取或跳过更少的字节。
*
*
Note that while some implementations of {@code InputStream} will return
* the total number of bytes in the stream, many will not. It is
* never correct to use the return value of this method to allocate
* a buffer intended to hold all data in this stream.
* 请注意,InputStream的一些实现将返回流中的总字节数,但许多实现不会返回。
* 使用此方法的返回值来分配用于保存此流中所有数据的缓冲区是不正确的。
*
*
A subclass' implementation of this method may choose to throw an
* {@link IOException} if this input stream has been closed by
* invoking the {@link #close()} method.
* 如果此输入流已通过调用close()方法关闭,则此方法的子类实现可以选择抛出IOException
*
*
The {@code available} method for class {@code InputStream} always
* returns {@code 0}.
* 类InputStream的available方法始终返回0
*
*
This method should be overridden by subclasses.
* 此方法应由子类重写。
*
* @return an estimate of the number of bytes that can be read (or skipped
* over) from this input stream without blocking or {@code 0} when
* it reaches the end of the input stream.
* 返回当到达输入流末尾时,可以从该输入流中读取(或跳过)的而不会阻塞字节数的估计值,
* 或者返回0
*
* @exception IOException if an I/O error occurs.
*/
public int available() throws IOException {
return 0;
}
/**
* Closes this input stream and releases any system resources associated
* with the stream.
* 关闭此输入流并释放与该流关联的所有系统资源。
*
*
The close
method of InputStream
does
* nothing.
* InputStream的close方法不起任何作用。
*
* @exception IOException if an I/O error occurs.
*/
public void close() throws IOException {}
/**
* Marks the current position in this input stream. A subsequent call to
* the reset
method repositions this stream at the last marked
* position so that subsequent reads re-read the same bytes.
* 标记此输入流中的当前位置。
* 对reset方法的后续调用将此流重新定位在最后标记的位置,以便后续读取重新读取相同的字节。
*
*
The readlimit
arguments tells this input stream to
* allow that many bytes to be read before the mark position gets
* invalidated.
* readlimit参数告诉此输入流允许在标记位置无效之前读取这么多字节。
*
*
The general contract of mark
is that, if the method
* markSupported
returns true
, the stream somehow
* remembers all the bytes read after the call to mark
and
* stands ready to supply those same bytes again if and whenever the method
* reset
is called. However, the stream is not required to
* remember any data at all if more than readlimit
bytes are
* read from the stream before reset
is called.
* mark的一般约定是,如果方法markSupported返回true,
* 则流会以某种方式记住调用mark后读取的所有字节,
* 并随时准备在调用方法reset时再次提供这些字节。
* 但是,如果在调用reset之前从流中读取的数据超过readlimit字节,
* 则流根本不需要记住任何数据。
*
*
*
*
Marking a closed stream should not have any effect on the stream.
* 标记关闭的流不应对流产生任何影响。
*
*
The mark
method of InputStream
does
* nothing.
* InputStream的mark方法不起任何作用。
*
* @param readlimit the maximum limit of bytes that can be read before
* the mark position becomes invalid.
* 在标记位置无效之前可以读取的最大字节限制。
* @see java.io.InputStream#reset()
*/
public synchronized void mark(int readlimit) {}
/**
* Repositions this stream to the position at the time the
* mark
method was last called on this input stream.
* 将此流重新定位到上次对此输入流调用mark方法时的位置。
*
*
The general contract of reset
is:
* 重置的总约定为:
*
*
* - If the method
markSupported
returns
* true
, then:
* 如果方法markSupported返回true,则:
*
* - If the method
mark
has not been called since
* the stream was created, or the number of bytes read from the stream
* since mark
was last called is larger than the argument
* to mark
at that last call, then an
* IOException
might be thrown.
* 如果自创建流以来未调用方法标记,
* 或者自上次调用标记以来从流读取的字节数大于上次调用时要标记的参数,
* 则可能会引发IOException。
*
*
* - If such an
IOException
is not thrown, then the
* stream is reset to a state such that all the bytes read since the
* most recent call to mark
(or since the start of the
* file, if mark
has not been called) will be resupplied
* to subsequent callers of the read
method, followed by
* any bytes that otherwise would have been the next input data as of
* the time of the call to reset
.
* 如果未引发这样的IOException,则流将重置为这样的状态,
* 即自最近一次调用mark(或自文件开始,如果mark未被调用)
* 以来读取的所有字节将重新提供给read方法的后续调用方,
* 后跟在调用重置时本应是下一个输入数据的任何字节
*
*
*
*
*
* - If the method
markSupported
returns
* false
, then:
* 如果方法markSupported返回false,则
*
* - The call to
reset
may throw an
* IOException
.
* 重置调用可能引发IOException
*
* - If an
IOException
is not thrown, then the stream
* is reset to a fixed state that depends on the particular type of the
* input stream and how it was created. The bytes that will be supplied
* to subsequent callers of the read
method depend on the
* particular type of the input stream.
* 如果未引发IOException,则流将重置为固定状态,
* 该状态取决于输入流的特定类型及其创建方式。
* 将提供给read方法的后续调用方的字节取决于输入流的特定类型。
*
*
* The method reset
for class InputStream
* does nothing except throw an IOException
.
* 类InputStream的方法reset除了抛出IOException之外不做任何事情。
*
* @exception IOException if this stream has not been marked or if the
* mark has been invalidated.
* @see java.io.InputStream#mark(int)
* @see java.io.IOException
*/
public synchronized void reset() throws IOException {
throw new IOException("mark/reset not supported");
}
/**
* Tests if this input stream supports the mark
and
* reset
methods. Whether or not mark
and
* reset
are supported is an invariant property of a
* particular input stream instance. The markSupported
method
* of InputStream
returns false
.
* 测试此输入流是否支持标记和重置方法。
* 是否支持标记和重置是特定输入流实例的不变属性。
* InputStream的markSupported方法返回false。
*
* @return true
if this stream instance supports the mark
* and reset methods; false
otherwise.
* 如果此流实例支持标记和重置方法,则为true;否则返回false
*
* @see java.io.InputStream#mark(int)
* @see java.io.InputStream#reset()
*/
public boolean markSupported() {
return false;
}
/**
* Reads all bytes from this input stream and writes the bytes to the
* given output stream in the order that they are read. On return, this
* input stream will be at end of stream. This method does not close either
* stream.
* 从该输入流读取所有字节,并按读取顺序将字节写入给定的输出流。
* 返回时,此输入流将位于流的末尾。此方法不关闭任何一个流。
*
*
* This method may block indefinitely reading from the input stream, or
* writing to the output stream. The behavior for the case where the input
* and/or output stream is asynchronously closed, or the thread
* interrupted during the transfer, is highly input and output stream
* specific, and therefore not specified.
* 此方法可能会无限期地阻止从输入流读取或写入到输出流。
* 输入和/或输出流异步关闭或线程在传输过程中中断的情况下的行为是高度特定于输入和输出流的,因此未指定。
*
*
*
* If an I/O error occurs reading from the input stream or writing to the
* output stream, then it may do so after some bytes have been read or
* written. Consequently the input stream may not be at end of stream and
* one, or both, streams may be in an inconsistent state. It is strongly
* recommended that both streams be promptly closed if an I/O error occurs.
* 如果从输入流读取或写入输出流时发生I/O错误,则可能在读取或写入某些字节后发生。
* 因此,输入流可能不在流的末尾,并且一个或两个流可能处于不一致的状态。
* 强烈建议在发生I/O错误时立即关闭两个流。
*
* @param out the output stream, non-null
* @return the number of bytes transferred
* @throws IOException if an I/O error occurs when reading or writing
* @throws NullPointerException if {@code out} is {@code null}
*
* @since 9
*/
public long transferTo(OutputStream out) throws IOException {
// 如果out为空,则抛出异常
Objects.requireNonNull(out, "out");
// 总共传输给输出流的字节
long transferred = 0;
// 创建缓冲数组,默认大小为8KB
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
// 每次读取的字节数
int read;
// 如果每次都读取到了字节,并放置在缓冲数组中
while ((read = this.read(buffer, 0, DEFAULT_BUFFER_SIZE)) >= 0) {
// 则将该缓冲数组中有效数据,写入到输出流中
out.write(buffer, 0, read);
// 更新总共传输给输出流的字节数
transferred += read;
}
// 返回总共传输给输出流的字节
return transferred;
}
}