java - Socket.getInputStream().read(byte[]) 是否保证在至少读取一些数据后不会阻塞?

标签 java io inputstream

类 InputStream 的 JavaDoc 说明如下:

Reads up to len bytes of data from the input stream into an array of bytes. An attempt is made to read as many as len bytes, but a smaller number may be read. 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.

这也符合我的经验。例如,请参见下面的示例代码:

Client:
Socket socket = new Socket("localhost", PORT);
OutputStream out = socket.getOutputStream();
byte[] b = { 0, 0 };
Thread.sleep(5000);
out.write(b);
Thread.sleep(5000);
out.write(b);

Server:
ServerSocket server = new ServerSocket(PORT);
Socket socket = server.accept();
InputStream in = socket.getInputStream();
byte[] buffer = new byte[4];
System.out.println(in.read(buffer));
System.out.println(in.read(buffer));

Output:
2   // Two bytes read five seconds after Client is started.
2   // Two bytes read ten seconds after Client is started.

第一次调用 read(buffer) 阻塞,直到输入数据可用。然而,该方法在读取两个字节后返回,即使字节缓冲区中仍有空间,这对应于 JavaDoc 声明“尝试读取多达 len 个字节,但较小的数字可能是阅读'。但是,当输入流来自套接字时,是否保证该方法不会在读取至少一个字节的数据后阻塞?

我问的原因是我在小型Java web服务器中看到了以下代码NanoHTTPD ,而且我想知道小于 8k 字节(大多数请求是)的 HTTP 请求是否可能使线程无限期阻塞,除非保证它不会在读取某些数据后阻塞。

InputStream is = mySocket.getInputStream();
// Read the first 8192 bytes. The full header should fit in here.
byte[] buf = new byte[8192];
int rlen = is.read(buf, 0, bufsize);

编辑:

让我尝试用一​​个相对相似的代码示例再次说明。 EJP 表示,该方法会阻塞,直到 EOS 发出信号或至少有一个字节的数据到达,在这种情况下,无论有多少字节的数据到达,它都会读取,而不会再次阻塞,并返回该数字,对应于 JavaDoc用于 InputStream 类中的方法 read(byte[], int, int)。但是,如果真正查看源代码,就会清楚该方法确实会阻塞,直到缓冲区已满。在我的服务器示例中,我使用与上面相同的客户端并将 InputStream 代码复制到静态方法来测试它。

public static void main(String[] args) throws Exception {
    ServerSocket server = new ServerSocket(PORT);
    Socket socket = server.accept();
    InputStream in = socket.getInputStream();
    byte[] buffer = new byte[4];
    System.out.println(read(in, buffer, 0, buffer.length));
}

public static int read(InputStream in, byte b[], int off, int len) throws IOException {
    if (b == null) {
        throw new NullPointerException();
    }
    else if (off < 0 || len < 0 || len > b.length - off) {
        throw new IndexOutOfBoundsException();
    }
    else if (len == 0) {
        return 0;
    }

    int c = in.read();
    if (c == -1) {
        return -1;
    }
    b[off] = (byte)c;

    int i = 1;
    try {
        for (; i < len; i++) {
            c = in.read();
            if (c == -1) {
                break;
            }
            b[off + i] = (byte)c;
        }
    }
    catch (IOException ee) {
    }
    return i;
}

这段代码的输出是:

4   // Four bytes read ten seconds after Client is started.

现在显然 5 秒后有数据可用,但是该方法仍然阻止尝试填充整个缓冲区。 Socket.getInputStream() 返回的输入流似乎不是这种情况,但是它是否保证一旦数据可用就永远不会阻塞,如 JavaDoc 所说但不像源代码显示?

最佳答案

However, is it guaranteed that the method will not block once at least one byte of data is read when the input stream comes from a socket?

我不认为这个问题有任何意义。该方法会阻塞,直到 EOS 发出信号或至少一个字节的数据已到达,在这种情况下,它会读取已到达的所有字节数,而不会再次阻塞,并返回该数字。

I saw the following code in the small Java web server NanoHTTPD

代码有误。它做出了整个 header 将在第一次读取时交付的无效假设。我希望在这里看到一个循环,循环直到检测到一个空行。

I wondered if a HTTP Request smaller than 8k bytes (which most requests are) potientially could make the thread block indefinitely unless there is a guarantee that it won't block once some data is read.

同样,我认为这没有任何意义。该方法将阻塞,直到至少一个字节到达,或 EOS。期间。

关于java - Socket.getInputStream().read(byte[]) 是否保证在至少读取一些数据后不会阻塞?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13247410/

相关文章:

java - 从 ServletRequest.getInputStream() 哪里读取内容?

java - 调整 Controller 中的方法。 [持久性单元: default] Unable to build Hibernate SessionFactory

c++ - 如何输入某个文件,同时将某些部分分配到不同的变量中

c++ - 如何在fortran中使用write语句调用另一个自定义函数?

java - 从输入流 Java 读取时有没有超时的方法?

Java:如何将 InputStream 转换为 GZIPInputStream?

java - 快速绘制任意点的集合

Java POI API - 在 XLS 中绘制饼图

java - Android 应用程序无法在模拟器中运行

c - 为什么连续的 recvfrom() 调用在 UDP 套接字的情况下被阻塞