comparison CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/opt/bbmap-39.01-1/current/fileIO/PipeThread.java @ 68:5028fdace37b

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 16:23:26 -0400
parents
children
comparison
equal deleted inserted replaced
67:0e9998148a16 68:5028fdace37b
1 package fileIO;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.OutputStream;
6
7 /**
8 * Listens to an output stream and copies it to an input stream.
9 * For example, redirects the error stream of some process to stderr.
10 * @author Brian Bushnell
11 * @date Jan 22, 2013
12 *
13 */
14 public class PipeThread extends Thread {
15
16 // public PipeThread(InputStream is_){this(is_, System.err);}
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 // synchronized(list){list.add(this);}
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 // TODO Auto-generated catch block
35 e.printStackTrace();
36 }
37
38 if(is!=System.in){
39 try {
40 is.close();
41 } catch (IOException e) {
42 // TODO Auto-generated catch block
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 // public static void killList(){
73 // System.err.println("Kill list.");
74 // synchronized(list){
75 // for(PipeThread pt : list){
76 // if(!pt.finished){
77 // pt.terminate();
78 // }
79 // }
80 // }
81 // }
82
83 public final InputStream is;
84 public final OutputStream os;
85 private volatile boolean finished=false;
86
87 // private static ArrayList<PipeThread> list=new ArrayList<PipeThread>(8);
88
89 }