add the Java Client from M0AZM
[spider.git] / SpiderConsole / src / PipedOutputMUX.java
1 /**
2  * OutputStreamMultiplexor 
3  * Takes one output stream and sends it to multiple output streams.
4  * @author Ian Norton
5  * @version 1.0 - 20010418.
6  *
7  * Copyright (C) 2001 Ian Norton.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public Licence as published by
11  * the Free Software Foundation; either version 2 of the Licence, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public Licence for more details.
18  *
19  * You should have received a copy of the GNU General Public Licence
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  * Contacting the author :
24  * Ian Norton
25  * i.norton@lancaster.ac.uk
26  * http://www.lancs.ac.uk/~norton/
27  **/
28
29 import java.io.* ;
30 import java.util.Vector ;
31 import java.util.Enumeration ;
32 import java.util.Calendar ;
33
34 class PipedOutputMUX implements Runnable
35     {
36     public static final boolean DEBUG = false ;
37     public static final String encoding = "latin1"; // "ISO8859_1";
38     
39     private PipedInputStream pin ;
40     private Vector streams ;
41
42     private Thread t ;
43     
44     /**
45      * PipedOutputMUX initialiser
46      * @param PipedOutputStream i - Source stream
47      **/
48     public PipedOutputMUX(PipedInputStream i)
49         {
50         pin = i ;
51          
52         // Streams Vector holds all the OutputStreams we know about.
53         streams = new Vector() ;
54
55         // Initialise and start the thread.
56         t = new Thread(this, "OutputMultiplexor") ;
57         t.start() ;
58         }
59
60     /**
61      * addOutputStream
62      * @param PipedOutputStream po - add a stream to send output to.
63      **/
64     public void addOutputStream(PipedOutputStream po)
65         {
66         // Add the supplied stream to the vector of streams.
67         streams.addElement(po) ;
68         }
69
70     /**
71      * run - Thread run method.
72      **/
73     public void run()
74         {
75         // Loop continually reading the input stream.
76         while(true)
77             {
78             try
79                 {
80                 byte[] b = new byte[16];
81
82                 // Read a line and see if it has any data in it.
83                 int n = 0;
84
85                 // Trying to read
86                 while(pin.available() > 0)
87                     {
88                     int rdb = pin.available() ;
89                     if(rdb > 16) rdb = 16 ;
90                     n = pin.read(b, 0, rdb);
91
92                     if(n > 0)
93                         {
94                         // Convert the output to a string and send it.
95                         String output = new String(b, 0, n, encoding) ;
96                         if(DEBUG) System.out.println(output) ;
97                         send(output) ;
98                         }
99                     }
100                 }
101             catch(IOException ex)
102                 {
103                 System.out.println("PipedOutputMUX: IOException trying to read.") ;
104                 System.exit(1) ;
105                 }
106             } // End of loop
107         } // End of run()
108
109     /**
110      * send
111      * @param String s - string to send to all streams.
112      **/
113     private void send(String s)
114         {
115         // Calendar cal = Calendar.getInstance() ;
116         // if(DEBUG) System.out.println("PipedOutputMUX: " + cal.getTime() + " Send called with :" + s) ;
117
118         // If we have no streams, then we can't do anything.
119         if(streams.size() == 0) return ;
120         
121         // Create Enumeration object to enumerate with :-)
122         Enumeration e = streams.elements() ;
123
124         // Go through the enumeration and send the string to each stream.
125         while(e.hasMoreElements())
126             {
127             PipedOutputStream os = (PipedOutputStream)e.nextElement() ;
128
129             try
130                 {
131                 // Write the data to the stream.
132                 for(int i=0;i<s.length();i++)
133                     {
134                     os.write(s.charAt(i)) ;
135                     os.flush() ;
136                     }
137                 }
138             catch(IOException ex)
139                 {
140                 // If we get an IO exception, then the other end of the pipe
141                 // has been closed.  We need to remove this stream.
142                 streams.removeElement(os) ;
143                 System.out.println("IOException - stream removed.") ;
144                 }
145             }
146         }
147
148     } // End of class.