| 1 |
package fileIO; |
| 2 |
|
| 3 |
import java.io.IOException; |
| 4 |
import java.io.InputStream; |
| 5 |
import java.io.OutputStream; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
public class PipeThread extends Thread { |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
public PipeThread(InputStream is_, OutputStream os_){ |
| 19 |
is=is_; |
| 20 |
os=os_; |
| 21 |
if(is==null){throw new RuntimeException("Null input stream.");} |
| 22 |
if(os==null){throw new RuntimeException("Null output stream.");} |
| 23 |
|
| 24 |
} |
| 25 |
|
| 26 |
@Override |
| 27 |
public void run(){ |
| 28 |
final byte[] buf=new byte[8196]; |
| 29 |
try { |
| 30 |
for(int len=is.read(buf); !finished && len>0; len=is.read(buf)){ |
| 31 |
os.write(buf, 0, len); |
| 32 |
} |
| 33 |
} catch (IOException e) { |
| 34 |
|
| 35 |
e.printStackTrace(); |
| 36 |
} |
| 37 |
|
| 38 |
if(is!=System.in){ |
| 39 |
try { |
| 40 |
is.close(); |
| 41 |
} catch (IOException e) { |
| 42 |
|
| 43 |
e.printStackTrace(); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
if(os!=System.out && os!=System.err){ |
| 48 |
ReadWrite.close(os); |
| 49 |
} |
| 50 |
|
| 51 |
synchronized(this){ |
| 52 |
finished=true; |
| 53 |
this.notify(); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
public boolean finished(){ |
| 58 |
synchronized(this){ |
| 59 |
return finished; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
public void terminate(){ |
| 64 |
synchronized(this){ |
| 65 |
if(!finished){ |
| 66 |
finished=true; |
| 67 |
interrupt(); |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public final InputStream is; |
| 84 |
public final OutputStream os; |
| 85 |
private volatile boolean finished=false; |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
} |