Java.io.PushbackInputStream.available() 方法
描述
java.io.PushbackInputStream.available() 方法返回可从此输入流读取(或跳过)的字节数的估计值,而不会被下一次为此调用的方法阻塞 输入流。 下一次调用可能是同一个线程或另一个线程。 单次读取或跳过这么多字节不会阻塞,但可能会读取或跳过更少的字节。
声明
以下是 java.io.PushbackInputStream.available() 方法的声明。
public int available()
参数
NA
返回值
该方法返回可以从输入流中读取(或跳过)而不阻塞的字节数。
异常
IOException − 如果此输入流已通过调用其 close() 方法关闭,或者发生 I/O 错误。
示例
下面的例子展示了 java.io.PushbackInputStream.available() 方法的使用。
package com.tutorialspoint;
import java.io.*;
public class PushbackInputStreamDemo {
public static void main(String[] args) {
// declare a buffer and initialize its size:
byte[] arrByte = new byte[1024];
// create an array for our message
byte[] byteArray = new byte[]{'H', 'e', 'l', 'l', 'o'};
try {
// create object of PushbackInputStream class for specified stream
InputStream is = new ByteArrayInputStream(byteArray);
PushbackInputStream pis = new PushbackInputStream(is);
// check how many bytes are available
System.out.println("" + pis.available());
// read from the buffer one character at a time
for (int i = 0; i < byteArray.length; i++) {
// read a char into our array
arrByte[i] = (byte) pis.read();
// display the read byte
System.out.print((char) arrByte[i]);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
5 Hello

