InputStream.available() 在 Java 中做什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3695372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 03:35:17  来源:igfitidea点击:

What does InputStream.available() do in Java?

javablockinginputstream

提问by Albus Dumbledore

What does InputStream.available()do in Java? I read the documentation, but I still cannot make it out.

InputStream.available()Java中做什么?我阅读了文档,但我仍然无法理解。

The doc says:

医生说:

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.

返回可以从此输入流读取(或跳过)的字节数,而不会被此输入流的方法的下一个调用者阻塞。下一个调用者可能是同一个线程或另一个线程。

InputStream 类的可用方法始终返回 0。

What do they mean by blocking? Does it just mean a synchronized call?

他们所说的阻塞是什么意思?它只是意味着同步呼叫吗?

And most of all, what is the purpose of the available()method?

最重要的是,该available()方法的目的是什么?

采纳答案by Brian Agnew

Blocking doesn't relate to threading or synchronisation here. Instead it relates to blocking IO (see thisfor more info). If you issue a request to read, and the channel has none available, a blocking call will wait (or block) until data is available (or the channel is closed, throws an exception etc.)

阻塞与这里的线程或同步无关。相反,它涉及到阻塞的IO(见获取更多信息)。如果您发出读取请求,而通道没有可用的,阻塞调用将等待(或阻塞)直到数据可用(或通道关闭,抛出异常等)

So why use available()? So you can determine how many bytes to read, or determine whether you're going to block.

那么为什么要使用available()?因此,您可以确定要读取的字节数,或者确定是否要阻塞。

Note that Java has non-blocking IO capabilities as well. See herefor more details

请注意,Java 也具有非阻塞 IO 功能。请参阅此处了解更多详情

回答by Vivien Barousse

In InputStreams, read()calls are said to be "blocking" method calls. That means that if no data is available at the time of the method call, the method will wait for data to be made available.

在 InputStreams 中,read()调用被称为“阻塞”方法调用。这意味着如果在方法调用时没有数据可用,该方法将等待数据可用。

The available()method tells you how many bytes can be read until the read()call will block the execution flow of your program. On most of the input streams, all call to read()are blocking, that's why available returns 0 by default.

available()方法告诉您可以读取多少字节,直到read()调用阻塞程序的执行流程。在大多数输入流上,所有调用read()都是阻塞的,这就是默认情况下可用返回 0 的原因。

However, on some streams (such as BufferedInputStream, that have an internal buffer), some bytes are read and kept in memory, so you can read them without blocking the program flow. In this case, the available()method tells you how many bytes are kept in the buffer.

但是,在某些流(例如BufferedInputStream,具有内部缓冲区的 )上,会读取一些字节并将其保存在内存中,因此您可以在不阻塞程序流的情况下读取它们。在这种情况下,该available()方法会告诉您缓冲区中保留了多少字节。

回答by bob

Consider if you write software VERY BADLY.. and you write an operating system.

考虑一下您是否编写了非常糟糕的软件……并且您编写了一个操作系统。

This operating system takes keyboard input amongst other things.

该操作系统除其他外还接受键盘输入。

So you ask your OS to go and get some keyboard input, but there are no keys pressed and none in the buffer. your Whole OS will then HANG DEAD until it gets a keyboard input.

所以你让你的操作系统去获取一些键盘输入,但没有按下任何键,缓冲区中也没有。然后,您的整个操作系统将挂起,直到它获得键盘输入。

Contrast this with 'look ahead', you ask if the KB has any characters BEFORE making the call. You get the answer NO, so your OS then goes and does something else.

将此与“向前看”进行对比,您在拨打电话之前询问 KB 是否有任何字符。您得到的答案是“否”,因此您的操作系统会执行其他操作。

That is WHY you should care, now if you then multiply that by every other potentially blocking task, you can see why 'look ahead' is critical.

这就是您应该关心的原因,现在如果您将其乘以所有其他潜在的阻塞任务,您就会明白为什么“向前看”至关重要。

Because It also applies to OUTPUT: A memory to a disk-drive interface can also flood data to the disk-drive faster than it can process it. if you do not know the drive buffer is flooded with data, then the task will block until the buffer can accept more data.

因为它也适用于 OUTPUT:磁盘驱动器接口的内存也可以比处理数据的速度更快地将数据淹没到磁盘驱动器。如果您不知道驱动器缓冲区已被数据淹没,则任务将阻塞,直到缓冲区可以接受更多数据。

This also highlights the nonsense of 'there are very few useful uses.'

这也凸显了“有用的用途很少”的胡说八道。