'input.readLine waiting (blocked) for ever, how to deal with this?

I have a Java server that it is compiling Android APKs by command line and writing the output into a file, this is the source code:

Process p = Runtime.getRuntime().exec("gradlew assembleRelease", null , new File(this.workDir));            
        BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line = null;     
        while ((line = input.readLine()) != null) {
            standardOutput.writeln(line);
        }
        input.close();

It is working perfect with all the Android projects but if the android project has a directory or a file with rare characters (á, ñ...) inside assets folder, then, gets stuck in line = input.readLine()

I opened jconsole to see where is being stuck and i got this:

Name: Thread-3
State: RUNNABLE
Total blocked: 0  Total waited: 6
Stack trace: 
java.io.FileInputStream.readBytes(Native Method)
java.io.FileInputStream.read(FileInputStream.java:255)
java.io.BufferedInputStream.read1(BufferedInputStream.java:284)
java.io.BufferedInputStream.read(BufferedInputStream.java:345)
   - locked java.io.BufferedInputStream@e70e4f8
sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
   - locked java.io.InputStreamReader@4426351b
java.io.InputStreamReader.read(InputStreamReader.java:184)
java.io.BufferedReader.fill(BufferedReader.java:161)
java.io.BufferedReader.readLine(BufferedReader.java:324)
   - locked java.io.InputStreamReader@4426351b
java.io.BufferedReader.readLine(BufferedReader.java:389)
com.mobinGen.jobs.AndroidJob.releaseCompile(AndroidJob.java:329)
com.mobinGen.jobs.AndroidJob.jobProcess(AndroidJob.java:122)
com.mobinGen.jobs.BaseJob.process(BaseJob.java:138)
com.mobinGen.generationAPI.GeneratorAndroid$AndroidJobsAsker.run(GeneratorAndroid.java:230)

As you can see it is blocked in line 329, which is while ((line = input.readLine()) != null)

How can I deal with this? it is a huge problem because the server is being waiting for ever without compiling the next coming Android projects.



Solution 1:[1]

Firstly, are you sure you want to read from p.getInputStream()? The gradle process will be writing to p.getOutputStream() or p.getErrorStream()

Secondly, I think you should use the tooling api to invoke gradle instead of using Runtime (See GradleConnector)

Solution 2:[2]

Perhaps you want to use an explicit charset in the InputStreamReader.

See InputStreamReader(InputStream in, Charset cs)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 lance-java