在java 6 webapp中,我試圖從執行的命令中檢索大量的輸出。我在javaworld article上「借/偷」了它。我面臨的問題是,由於輸出被截斷,長度似乎超過了尺寸限制。我已經將數據輸出到一個文件,所以我可以看到返回的大小,這正好是32K(32768)。我已經嘗試過更改緩衝區的默認大小(請參閱BufferedReader構造函數),但是我沒有觀察到任何返回數據長度的變化,無論緩衝區大小(從小到大) 。超過了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;
}
}
更改BufferedReader上的緩衝區大小隻會改變它在內部使用的臨時存儲量。 BufferedReader沒有大小限制,因此您應該能夠使用此代碼讀取整個輸出。你有沒有檢查condor_history - long本身產生完整的輸出,並不停在32K? – Zarkonnen 2010-11-15 23:09:05
謝謝澄清。運行該命令將返回完整的輸出。 – samiam 2010-11-16 14:45:27