简体   繁体   中英

IOUtils.copy() hangs when copying big stream?

I want to parse content of some file by srcML parser which is an external windows program. I'm doing this in a following way:

String command = "src2srcml.exe --language java";
Process proc = Runtime.getRuntime().exec(command);

InputStream fileInput = Files.newInputStream(file)
OutputStream procOutput = proc.getOutputStream();

IOUtils.copy(fileInput, procOutput);

IOUtils.copy() is from Commons IO 2.4 library.

When my file is small (several KB) everything works fine. However, when I try to copy some relatively big file (~72 KB) my program hangs.

Moreover, when I execute the parser 'manually' in cmd:

src2srcml.exe --language Java < BigFile.java

everything works fine, too.

Any ideas why this is happening?

You should buffer the OutputStream:

OutputStream procOutput = proc.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(procOutput);
IOUtils.copy(fileInput, bos);

Moreover, why don't you simply redirect fileInput as the process InputStream?

 ProcessBuilder pb = new ProcessBuilder(command);
 pb.redirectInput(file);
 Process proc = pb.start();
 proc.waitFor();

The problem is most likely that you are not consuming the output of the external program in a separate thread. you need to start a separate thread to consume the output so that the external program does not get blocked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM