Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 378
Kao Avatar asked Oct 14 '25 09:10

Kao


1 Answers

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();
like image 110
laune Avatar answered Oct 16 '25 23:10

laune



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!