* writeToChannel is needed later on. From the input might have an
* exception been thrown which - because of the spawned thread -
* cannot be caught here. It must be taken from the object and
* thrown separately.
*/
CopyInputStream writeToChannel = new CopyInputStream(
"Write to Process", in, channelOut);
Thread writeToProcessThread = new Thread(writeToChannel);
Thread readErrorsThread = new Thread(new CopyInputStream(
"Read Errors", channelErrorIn, errorOut));
if (LOGGER.isDebugEnabled())
{
LOGGER.debug("Start seperate io threads.");
}
readErrorsThread.start();
writeToProcessThread.start();
if (LOGGER.isDebugEnabled())
{
LOGGER.debug("Start looping around read process.");
}
int i;
byte[] tmp = new byte[1024];
while (true)
{
i = 0;
while (channelIn.available() > 0)
{
i = channelIn.read(tmp);
if (i < 0)
{
break;
}
out.write(tmp, 0, i);
numBytes += i;
}
if (channel.isClosed())
{
break;
}
}
readErrorsThread.join();
String error = new String(errorOut.toByteArray(), "UTF-8");
if (error != null && error.length() > 0)
{
throw new IOException(error);
}
// Throwing exception from input stream if present.
if (writeToChannel.getException() != null)
{
throw new IOException(writeToChannel.getException());
}
}
catch (InterruptedException e)
{
LOGGER.error(e, e);