| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package eu.simuline.octave.util; |
| 17 | |
|
| 18 | |
import java.io.IOException; |
| 19 | |
import java.io.Reader; |
| 20 | |
import java.io.Writer; |
| 21 | |
|
| 22 | |
import eu.simuline.octave.exception.OctaveIOException; |
| 23 | |
import eu.simuline.octave.exception.OctaveInterruptedException; |
| 24 | |
|
| 25 | |
import org.apache.commons.logging.LogFactory; |
| 26 | |
import org.apache.commons.logging.Log; |
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
public final class ReaderWriterPipeThread extends Thread { |
| 34 | |
|
| 35 | 2 | private static final Log LOG = LogFactory |
| 36 | 2 | .getLog(ReaderWriterPipeThread.class); |
| 37 | |
|
| 38 | |
@SuppressWarnings("checkstyle:magicnumber") |
| 39 | 2 | private static final char[] BUF = new char[4 * 1024]; |
| 40 | |
|
| 41 | |
private final Reader reader; |
| 42 | |
|
| 43 | |
private Writer writer; |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
public static ReaderWriterPipeThread instantiate(final Reader reader, |
| 56 | |
final Writer writer) { |
| 57 | 106 | final ReaderWriterPipeThread readerWriterPipeThread = |
| 58 | |
new ReaderWriterPipeThread(reader, writer); |
| 59 | 212 | readerWriterPipeThread.setName(Thread.currentThread().getName() |
| 60 | |
+ "-javaoctave-" |
| 61 | |
+ ReaderWriterPipeThread.class |
| 62 | 106 | .getSimpleName()); |
| 63 | 106 | readerWriterPipeThread |
| 64 | 106 | .setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { |
| 65 | |
public void uncaughtException(Thread th, Throwable ex) { |
| 66 | 4 | System.out.println("Uncaught : " + ex + |
| 67 | 2 | " of thread " + th.getName()); |
| 68 | 2 | } |
| 69 | |
}); |
| 70 | 106 | readerWriterPipeThread.start(); |
| 71 | 106 | return readerWriterPipeThread; |
| 72 | |
} |
| 73 | |
|
| 74 | 106 | private ReaderWriterPipeThread(final Reader reader, final Writer writer) { |
| 75 | 106 | this.reader = reader; |
| 76 | 106 | this.writer = writer; |
| 77 | 106 | } |
| 78 | |
|
| 79 | |
@Override |
| 80 | |
public void run() { |
| 81 | 112 | while (!interrupted()) { |
| 82 | |
int len; |
| 83 | |
try { |
| 84 | 112 | len = reader.read(BUF); |
| 85 | 2 | } catch (final IOException e) { |
| 86 | 2 | LOG.error("Error when reading from reader", e); |
| 87 | 2 | throw new OctaveIOException(e); |
| 88 | 106 | } |
| 89 | 106 | if (len == -1) { |
| 90 | 100 | break; |
| 91 | |
} |
| 92 | |
try { |
| 93 | 6 | synchronized (this) { |
| 94 | 6 | if (writer != null) { |
| 95 | 6 | writer.write(BUF, 0, len); |
| 96 | 6 | writer.flush(); |
| 97 | |
} |
| 98 | 6 | } |
| 99 | 0 | } catch (final IOException e) { |
| 100 | 0 | LOG.error("Error when writing to writer", e); |
| 101 | 0 | throw new OctaveIOException(e); |
| 102 | 6 | } |
| 103 | 6 | } |
| 104 | 100 | LOG.debug("ReaderWriterPipeThread finished without error"); |
| 105 | 100 | } |
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
public void setWriter(final Writer writer) { |
| 112 | 2 | synchronized (this) { |
| 113 | 2 | this.writer = writer; |
| 114 | 2 | } |
| 115 | 2 | } |
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
public void close() { |
| 121 | 106 | interrupt(); |
| 122 | |
try { |
| 123 | 106 | join(); |
| 124 | 0 | } catch (final InterruptedException e) { |
| 125 | 0 | throw new OctaveInterruptedException(e); |
| 126 | 106 | } |
| 127 | 106 | } |
| 128 | |
|
| 129 | |
} |