超过BufferedReader的大小限制?

时间:2010-11-15 22:58:21

标签: java bufferedreader

在java 6 webapp中,我试图从执行的命令中检索大量输出。我在javaworld article上“借用/偷窃/基于”它。我面临的问题是,由于输出被削掉,长度似乎超过了大小限制。我已经将数据输出到一个文件,所以我可以看到返回的大小,这正好是32K(32768)。我已经尝试过改变缓冲区的默认大小(参见BufferedReader构造函数),但是我没有观察到返回数据长度的任何变化,无论我对缓冲大小(非常小到非常大)有什么价值

非常感谢任何建议!

public class StreamGobbler extends Thread {

private InputStream is;
private String type;
private List<String> output;

public StreamGobbler(InputStream is, String type) {
    this.is = is;
    this.type = type;
}

@Override
public void run() {
    try {
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line = null;
        this.output = new ArrayList<String>();
        while ((line = br.readLine()) != null) {
            this.getOutput().add(line + "\n");
            System.out.println(type + ">" + line);
        }
        br.close();
    } catch (IOException ioe) {
        System.err.println("ERROR: " + ioe.getMessage());
    }
}

/**
 * @return the output
 */
public List<String> getOutput() {
    return output;
}

}

public class JobClassAds {

private String CONDOR_HISTORY = "condor_history";
private String CONDOR_HISTORY_XML = CONDOR_HISTORY + " -xml";
private String CONDOR_HISTORY_LONG = CONDOR_HISTORY + " -long";

public String getHistory() {
    try {
        Runtime runtime = Runtime.getRuntime();
        String exec = CONDOR_HISTORY_LONG;
        Process process = runtime.exec(exec);
        System.out.println("Running " + exec + " ...");

        // Error message
        StreamGobbler errGobbler = new StreamGobbler(process.getErrorStream(), "ERROR");

        // Output
        StreamGobbler outGobbler = new StreamGobbler(process.getInputStream(), "OUTPUT");

        Thread outThread = new Thread(outGobbler);
        Thread errThread = new Thread(errGobbler);

        outThread.start();
        errThread.start();

        outThread.join();
        errThread.join();

        /*
        String line = null;

        while ((line = input.readLine()) != null) {
            System.out.println(line);
            content.append(line);
        }

         *
         */

        int exitVal = process.waitFor();

        List<String> output = outGobbler.getOutput();
        String inputString = "";
        for (String o : output) {
            inputString += o;
        }

        System.out.println(exec + " Exited with error code " + exitVal);

        BufferedWriter out = new BufferedWriter(new FileWriter("/tmp/history_result.xml"));
        out.write(inputString);
        out.close();
        return inputString;

    } catch (Exception e) {
        System.err.println(e.getMessage());
        return null;
    }
}

1 个答案:

答案 0 :(得分:0)

问题不在于BufferedReader的缓冲区大小。

我认为真正的原因是外部命令正在做的事情。我怀疑它在没有冲洗它的标准流的情况下正在拯救它。请注意,您正在“gobbling”但不输出命令的stderr流。在那里你可以找到指出问题真正原因的证据。


顺便说一句,您正在以次优的方式使用StreamGobbler类。它扩展了Thread,因此预期的使用方式是:

    SteamGobbler sg = new StreamGobbler(...);
    sg.start();
    sg.join();

但你实际上是这样做的:

    SteamGobbler sg = new StreamGobbler(...);
    Thread th = new Thread(sg);
    th.start();
    th.join();

它有效...但只是因为Thread 是-a Runnable