当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Java Java.io.SequenceInputStream.available()用法及代码示例


描述

这个java.io.SequenceInputStream.available()方法返回可以从当前底层输入流读取(或跳过)的字节数的估计值,而不会被当前底层输入流的方法的下一次调用阻塞。下一次调用可能是同一个线程或另一个线程。单次读取或跳过这么多字节不会阻塞,但可能读取或跳过更少的字节。

声明

以下是声明java.io.SequenceInputStream.available()方法。

public int available()

参数

NA

返回值

如果此输入流已通过调用其 close() 方法关闭,则此方法返回可以在不阻塞的情况下从当前底层输入流读取(或跳过)的字节数的估计值或 0。

异常

IOException- 如果发生 I/O 错误。

示例

下面的例子展示了使用java.io.SequenceInputStream.available()方法。

package com.tutorialspoint;

import java.io.*;

public class SequenceInputStreamDemo {
   public static void main(String[] args) {

      // create two  new strings with 5 characters each
      String s1 = "Hello";
      String s2 = "World";

      // create 2 input streams
      byte[] b1 = s1.getBytes();
      byte[] b2 = s2.getBytes();
      ByteArrayInputStream is1 = new ByteArrayInputStream(b1);
      ByteArrayInputStream is2 = new ByteArrayInputStream(b2);

      // create a new Sequence Input Stream
      SequenceInputStream sis = new SequenceInputStream(is1, is2);
      
      try {
         // determine how many bytes are available in the first stream
         System.out.println("" + sis.available());
         
         // read 10 characters, 5 from each stream
         for (int i = 0; i < 10; i++) {
            char c = (char) sis.read();
            System.out.print("" + c);
         }

         // close the stream
         sis.close();

      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

让我们编译并运行上面的程序,这将产生以下结果——

5
HelloWorld

相关用法


注:本文由纯净天空筛选整理自 Java.io.SequenceInputStream.available() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。