介绍
InputStream是Java中一个抽象类,用于从不同类型的数据源中读取字节数据流,如文件,网络连接,内存缓冲区等等。它包含了一系列方法来读取不同类型的数据,例如read()方法用于读取下一个字节,read(byte[] b)方法用于读取一定数量的字节到指定的字节数组中等等。
InputStream类是Java IO输入流体系的基类,在Java中读取任何数据的基础就是InputStream。由于InputStream是一个抽象类,因此不能直接实例化,而是通过继承InputStream类的具体子类来实现具体的读取操作。常用的子类包括FileInputStream,ByteArrayInputStream,PipedInputStream等等。
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;
常用方法
read
/** * Reads the next byte of data from the input stream. The value byte is * returned as an
int
in the range0
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. * *A subclass must provide an implementation of this method. * * @return the next byte of data, or
public abstract int read() throws IOException; //下面两个read()方法都调用了这个方法子类的实现来完成功能的 /** * Reads some number of bytes from the input stream and stores them into * the buffer array-1
if the end of the * stream is reached. * @exception IOException if an I/O error occurs. * 从输入流中读取数据的 下一个字节。返回 0 到 255 范围内的 int 字节值。如果到达流末尾,则返回值-1。 * 在输入数据可用前或者已到达流的末尾前或抛出异常之前,此方法一直阻塞。 * 抽象方法,没有具体实现。因为子类必须实现此方法的一个实现。 * InputStream正是通过read()向外提供接口,供他们来读取字节数据。 */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. * *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 intob
. * *The first byte read is stored into element
b[0]
, the * next one intob[1]
, and so on. The number of bytes read is, * at most, equal to the length ofb
. Let k be the * number of bytes actually read; these bytes will be stored in elements *b[0]
throughb[
k-1]
, * leaving elementsb[
k]
through *b[b.length-1]
unaffected. * *The
read(b)
method for classInputStream
* has the same effect as: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.
* @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.
* @exception NullPointerException ifb
isnull
.
* @see java.io.InputStream#read(byte[], int, int)
* 从输入流中读取b长度的字节,并存储在缓存字节数组b中,返回的是实际读取的字节数。
*/
public int read(byte b[]) throws IOException {
return read(b, 0, b.length);
}/**
* Reads up tolen
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.
*
*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 intob
.
*
*The first byte read is stored into element
b[off]
, the
* next one intob[off+1]
, and so on. The number of bytes read
* is, at most, equal tolen
. Let k be the number of
* bytes actually read; these bytes will be stored in elements
*b[off]
throughb[off+
k-1]
,
* leaving elementsb[off+
k]
through
*b[off+len-1]
unaffected.
*
*In every case, elements
b[0]
through
*b[off]
and elementsb[off+len]
through
*b[b.length-1]
are unaffected.
*
*The
read(b,
off,
len)
method
* for classInputStream
simply calls the method
*read()
repeatedly. If the first such call results in an
*IOException
, that exception is returned from the call to
* theread(b,
off,
len)
method. If
* any subsequent call toread()
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 datalen
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.
*
* @param b the buffer into which the data is read.
* @param off the start offset in arrayb
* at which the data is written.
* @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.
* @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.
* @exception NullPointerException Ifb
isnull
.
* @exception IndexOutOfBoundsException Ifoff
is negative,
*len
is negative, orlen
is greater than
*b.length - off
* @see java.io.InputStream#read()
* 从输入流中读取数据最多len个字节,并存储在缓存数组b,返回实际读取的字节数。
* 这个方法会被阻断直到输入流可用,已经到了末尾,或者抛出异常。
* 如果读的长度为 0,则不读取任何字节并返回 0;否则,尝试读取至少 1 字节。
*/
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;
}
//如果流位于文件末尾而没有可用的字节(读到最后读完了),则返回值 -1表示文件结束;
int c = read();
if (c == -1) {
return -1;
}
//off是数组b存储的开始位置, 将c转化为byte类型,然后赋值给b[off]
b[off] = (byte)c;int i = 1;
try {
for (; i len ; i++) {
c = read();
//先进行校验,校验下个字节是否为空
if (c == -1) {
break;
}
//如果校验通过后,将读取的第一个字节存储在元素 b[off] 中,下一个存储在 b[off+1] 中,依次类推,读取的字节数最多等于 len。
b[off + i] = (byte)c;
}
//举个例子:设 k (len-off)为实际读取的字节数;这些字节将存储在 b[off] 到 b[off+k-1] 的元素中,不影响 其到数组尾部 的元素。
//如果接下来对read()的调用报IOException异常,异常会被获取并当成是文件的结尾,前面读取的字节会被保存到b中。
} catch (IOException ee) {
}
//返回读取的字节数
return i;
}
read()
此方法从输入流中读取下一个字节数据。如果到达流的末尾,则返回-1。否则,返回一个0到255之间的整数值,表示读取的字节数据。该方法会一直阻塞,直到有数据可读或者抛出I/O异常。
read(byte[] b)
此方法从输入流中读取数据到给定的字节数组中,并返回读取的字节数。该方法会尽可能多地读取字节数据,但不会读取比数组长度更多的数据。如果到达流的末尾,则返回-1。该方法会一直阻塞,直到有数据可读或者抛出I/O异常
read(byte b[], int off, int len)
从输入流中读取字节到缓冲区中,并返回读取的字节数。如果已经到达流的末尾,则返回-1。如果在读取过程中发生了错误,则抛出IOException异常。
byte b[]
:表示缓冲区,读取的数据将存储在这个数组中。int off
:表示缓冲区偏移量,即从缓冲区的哪个位置开始存储读取的数据。int len
:表示要读取的最大字节数。skip
从输入流中跳过指定数量的字节,并返回实际跳过的字节数。
注意,该方法可能会阻塞等待输入,直到n个字节被跳过或发生异常。当流被关闭时,该方法总是返回0。
/** * Skips over and discards
n
bytes of data from this input * stream. Theskip
method may, for a variety of reasons, end * up skipping over some smaller number of bytes, possibly0
. * This may result from any of a number of conditions; reaching end of file * beforen
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. * *The
public long skip(long n) throws IOException { long remaining = n; int nr; if (n 0) { return 0; } // 创建一个字节数组并不断地读取数据放到里面,直至读取到n个字节或者到达流末尾。子类可以提供更高效的方法实现。 //MAX_SKIP_BUFFER_SIZE用于确定跳过时要使用的最大缓冲区大小 int size = (int)Math.min(MAX_SKIP_BUFFER_SIZE, remaining); byte[] skipBuffer = new byte[size]; while (remaining > 0) { //返回跳过数组的字节数 nr = read(skipBuffer, 0, (int)Math.min(size, remaining)); if (nr 0) { break; } remaining -= nr; } return n - remaining; }skip
method of this class creates a * byte array and then repeatedly reads into it untiln
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. * * @param n the number of bytes to be skipped. * @return the actual number of bytes skipped. * @exception IOException if the stream does not support seek, * or if some other I/O error occurs. * 从输入流中跳过并丢弃n个字节的数据,返回实际跳过的字节数。如果n为负值,不跳过任何字节。 */available
子类需要重写该方法。
返回输入流中可被读取的估计字节数。这个估计值可能会比实际可读取的字节数要小,也可能比实际可读取的字节数要大。
这个方法可以用来检查输入流是否还有数据可供读取,但是不能保证读取这些数据不会被阻塞。因此,如果需要确保读取操作不会被阻塞,应该在读取数据之前调用InputStream的available()方法来检查是否有足够的数据可供读取。
如果输入流已经到达流的末尾,则该方法返回0。如果无法确定可读取的字节数,则该方法返回一个小于等于零的值。
/** * 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. * *
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. * *
The {@code available} method for class {@code InputStream} always * returns {@code 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. * @exception IOException if an I/O error occurs. * 返回输入流中可用的估计字节数 * 子类需要重写该方法。 */
public int available() throws IOException { return 0; }close
子类需要重写该方法。
用来关闭当前输入流并释放与之关联的所有系统资源。一旦关闭输入流,就不能再从该输入流中读取任何数据。在Java中,流对象都应该在使用完毕后进行关闭操作,以免资源泄漏和占用系统资源过多。
/** * Closes this input stream and releases any system resources associated * with the stream. * *
The
public void close() throws IOException {}close
method ofInputStream
does * nothing. * * @exception IOException if an I/O error occurs. * 关闭此输入流,并释放与其关联的所有资源 */mark
子类需要重写该方法。
当我们在流中调用mark方法时,流会记住当前的位置,并且我们可以通过调用reset方法来回到该位置。
/** * 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. * *The
readlimit
arguments tells this input stream to * allow that many bytes to be read before the mark position gets * invalidated. * *The general contract of
mark
is that, if the method *markSupported
returnstrue
, the stream somehow * remembers all the bytes read after the call tomark
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 thanreadlimit
bytes are * read from the stream beforereset
is called. * *Marking a closed stream should not have any effect on the stream. * *
The
public synchronized void mark(int readlimit) {}mark
method ofInputStream
does * nothing. * * @param readlimit the maximum limit of bytes that can be read before * the mark position becomes invalid. * @see java.io.InputStream#reset() * 在此输出流中标记当前位置 * 在流中标记当前位置。以后再调用reset时就可以再回到这个mark过的地方,这样再次读取到同样的字节。 readlimit参数的意思是,告诉系统在读出这么多个字符之前,这个mark保持有效。 * mark通常的规范是:如果markSupported返回true,在调用mark方法后,流以某种方式记录所有读取的字节,当调用reset方法后,能够再次提供同样的字节。 */reset
子类需要重写该方法。
将流的当前位置重置为最近的标记位置。如果没有标记,则会抛出IOException。如果标记处于流被关闭后的位置,则也会抛出IOException。如果成功重置,则可以重新读取流中之前已读取过的数据。
/** * Repositions this stream to the position at the time the *
mark
method was last called on this input stream. * *The general contract of
reset
is: * **
* *- If the method
markSupported
returns *true
, then: * ** *
- If the method
mark
has not been called since * the stream was created, or the number of bytes read from the stream * sincemark
was last called is larger than the argument * tomark
at that last call, then an *IOException
might be thrown. * *- 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 tomark
(or since the start of the * file, ifmark
has not been called) will be resupplied * to subsequent callers of theread
method, followed by * any bytes that otherwise would have been the next input data as of * the time of the call toreset
.- If the method
markSupported
returns *false
, then: * *
- The call to
reset
may throw an *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 theread
method depend on the * particular type of the input stream.The method
public synchronized void reset() throws IOException { throw new IOException("mark/reset not supported"); }reset
for classInputStream
* does nothing except throw anIOException
. * * @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 * 将此输入流重新定位到上一次调用mark方法时的标记位置。 * 如果markSupported返回true: * 1.如果从流创建后,mark还未被调用过,或者调用mark后,读取的字节数大于mark的参数readlimit,可能会抛出IOException异常。 * 2.如果异常没有抛出,上次调用mark方法读取的所有字节能够再次提供给以后的read方法,然后调用 reset 时起将mark标记位置作为下一输入数据的字节。 * 如果 markSupported返回false: * 1.对 reset 的调用可能抛出 IOException */markSupported
子类需要重写该方法。
用于判断输入流是否支持标记操作。当输入流支持标记操作时,即可通过调用mark()方法在当前位置添加一个标记,然后通过调用reset()方法返回到标记位置。如果输入流不支持标记操作,则调用mark()方法时会抛出IOException异常。
/** * Tests if this input stream supports the
mark
and *reset
methods. Whether or notmark
and *reset
are supported is an invariant property of a * particular input stream instance. ThemarkSupported
method * ofInputStream
returnsfalse
. * * @returntrue
if this stream instance supports the mark * and reset methods;false
otherwise. * @see java.io.InputStream#mark(int) * @see java.io.InputStream#reset() * 测试此输入流是否支持 mark 和 reset 方法 */ public boolean markSupported() { return false; }服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
一、说明 本文分享基于 Fate 使用 横向联邦 神经网络算法 对 多分类 的数据进行 模型训练,并使用该模型对数据进行 多分类预测。 二分类算法:是指待预测的 label 标签的取值只有两种;直白来讲就是每个实例的可能类别只有两种 (0 或者 1),例如…