InputStream.available()
在 Java 中做了什么?我阅读了文档,但仍然无法理解。
文档说:
Returns the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream. The next caller might be the same thread or or another thread.
The available method for class InputStream always returns 0.
阻塞是什么意思?它只是意味着同步调用吗?
最重要的是,available()
方法的目的是什么?
最佳答案
在 InputStreams 中,read()
调用被称为“阻塞”方法调用。这意味着如果在方法调用时没有数据可用,该方法将等待数据可用。
available()
方法告诉您在 read()
调用会阻塞程序的执行流程之前可以读取多少字节。在大多数输入流中,所有对 read()
的调用都是阻塞的,这就是为什么 available 默认返回 0。
但是,在某些流(例如 BufferedInputStream
,具有内部缓冲区)上,会读取一些字节并将其保存在内存中,因此您可以在不阻塞程序流的情况下读取它们。在这种情况下,available()
方法会告诉您缓冲区中保留了多少字节。
关于java - InputStream.available() 在 Java 中做了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3695372/