Search code examples
javastreamapache-commons-io

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?


Solution

  • 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();