小编voi*_*tar的帖子

Java - 如果数据太小,刷新进程的 OutputStream 不会立即发送数据

我正在从Java启动一个外部进程,并通过等方式获取它的stdin、stdout和stderr。我的问题是:当我想将数据写入我的输出流(过程的stdin)时,直到我实际调用它process.getInputStream()时,它才会被发送。close()溪流。我明确地打电话flush()

我做了一些实验,发现如果我增加发送的字节数,它最终会通过。在我的系统上,神奇数字是4058字节。

为了测试,我将数据发送到 perl 脚本,其内容如下:

#!/usr/bin/perl
use strict;
use warnings;

print "Perl starting";

while(<STDIN>) {
    print "Perl here, printing this: $_" 
}
Run Code Online (Sandbox Code Playgroud)

现在,这是 java 代码:

import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;

public class StreamsExecTest {


    private static String readInputStream(InputStream is) throws IOException {
        int guessSize = is.available();
        byte[] bytes = new byte[guessSize];
        is.read(bytes); // This call has side effect of filling the array
        String output = new String(bytes);
        return output;
    }

    public …
Run Code Online (Sandbox Code Playgroud)

java outputstream

5
推荐指数
1
解决办法
3959
查看次数

标签 统计

java ×1

outputstream ×1