在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