java-流-输入流

  java的基本输入类是java.io.InputStream:

    public abstract class InputStream implements Closeable 

  本类提供了将数据读取为原始字节所需的基本方法。

    public abstract int read() throws IOException;
    public int read(byte b[]) throws IOException;
    public int read(byte b[], int off, int len) throws IOException;
    public long skip(long n) throws IOException;
    public int available() throws IOException;
    public void close() throws IOException;

  InputStream的具体子类使用上述方法从某种媒介中读取数据。例如,FileInputStream从文件中读取数据;TelnetInputStream从网络连接中读取数据;ByteArrayInputStream从字节数组中读取数据。

    InputStream的基本方法是没有参数的read方法。这个方法从输入流的源中读取一个字节数据,作为一个0-255的int返回。流的结束由返回-1表示。read方法会等待并阻塞其后任何代码的执行,直到有一个字节的数据可用,准备读取。

    read方法声明为抽象方法,因为各个子类需要修改这个方法来处理特定的媒介。FileInputStream调用本地方法来实现文件的读取工作,ByteArrayInputStream使用纯java方法来实现,从其数组复制字节。

    下面的代码段从InputStream in中读取10个字节,存储在byte数组input中,如果检测到流结束,循环就会提前终止: 

    byte[] input = new byte[10];

    for(int  i = 0; i < input.length; i++) {
        int b = in.read();
        if (b == -1) break;
        input[i] = (byte)b;
    }
    与一次写一个字节数据一样,一次读取一个字节数据效率也不高。因此有两个重载的read方法,可以用从流中读取多个字节数据来填充指定的数组:read(byte[] b)和read(byte[] b, int offset,int length)。第一个方法尝试填充指定的数组input,第二个尝试填充指定的input中从offset开始连续length个字节的子数组。

    read(byte[] b) javaDoc文档如下:read(byte[] b,int offset, int len)的java doc请参看jdk的api

/**
     * Reads some number of bytes from the input stream and stores them into
     * the buffer array <code>b</code>. 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.
     *
     * <p> If the length of <code>b</code> is zero, then no bytes are read and
     * <code>0</code> 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 <code>-1</code> is returned; otherwise, at
     * least one byte is read and stored into <code>b</code>.
     *
     * <p> The first byte read is stored into element <code>b[0]</code>, the
     * next one into <code>b[1]</code>, and so on. The number of bytes read is,
     * at most, equal to the length of <code>b</code>. Let <i>k</i> be the
     * number of bytes actually read; these bytes will be stored in elements
     * <code>b[0]</code> through <code>b[</code><i>k</i><code>-1]</code>,
     * leaving elements <code>b[</code><i>k</i><code>]</code> through
     * <code>b[b.length-1]</code> unaffected.
     *
     * <p> The <code>read(b)</code> method for class <code>InputStream</code>
     * has the same effect as: <pre><code> read(b, 0, b.length) </code></pre>

    这两个方法都会尝试填充数组,但不是一定会成功。更常见的是,一次尝试不会完全失败,也不会完全成功。比如尝试冲网络中读取1024个字节,但是实际上只有512个字节从服务器接收到,其他的可能仍在传输中。

    3个read方法都用返回-1表示流的结束。如果流已结束,而尚有没有读取的数据,多字节read方法会返回这些数据,直到缓冲区清空。其后任何一个read方法的调用会返回-1,-1不会放在数组中,数组中只会包含实际的数据。

int bytesRead = 0;
int bytesToRead = 1024;
byte[] input = new byte[bytesToRead];
while(bytesRead < bytesToRead) {
    <span style="color:#ff9966;">int result = in.read(input,bytesRead,bytesToRead-bytesRead);</span>
    //测试流是否结束
    <span style="color:#ff9900;">if ( result == -1) break;</span>
    bytesRead += result;
}
    如果不想等待所有所需字节都立即可用,可使用available方法,在不阻塞的情况下确定可以有多少个字节可以读取。它会返回可以读取的最少字节数。

int bytesAvailable = in.available();
byte[] input = new byte[bytesAvailable];
int bytesRead = in.read(input,0,bytesAvailable];
// other thing.
   一般来说,read(byte[] input, int offset, int length)在流结束时返回-1,但如果length为零,那么它不会注意流的结束,而是返回0;

   与输出流一样,一旦结束对输入流的操作,应调用其close方法将其关闭,释放与这个流关联的所有资源。

   还有三个不常用的方法

public synchronized void mark(int readlimit);
public synchronized void reset() throws IOException 
public boolean markSupported()
    为了重新读取数据,要用mark方法来标记当前流所在位置,调用reset方法重置到标记的位置,然后接下来的读取会返回从标记位置开始的数据。从标记处读取数据和重置的字节数据取决于参数readlimit。大于参数的话,会抛出IoException。

   在尝试使用标记和重置方法前,应调用方法markSupported,如果返回true,说明可以使用mark或reset方法;否则抛出IoException。

   java.io中唯一两个始终支持标识的输入类是BufferedInputStream和ByteArrayInputStream。

 

   参考: java network programming

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值