2 * OutputStreamMultiplexor
3 * Takes one output stream and sends it to multiple output streams.
5 * @version 1.0 - 20010418.
7 * Copyright (C) 2001 Ian Norton.
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.
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.
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.
23 * Contacting the author :
25 * i.norton@lancaster.ac.uk
26 * http://www.lancs.ac.uk/~norton/
30 import java.util.Vector ;
31 import java.util.Enumeration ;
32 import java.util.Calendar ;
34 class PipedOutputMUX implements Runnable
36 public static final boolean DEBUG = false ;
37 public static final String encoding = "latin1"; // "ISO8859_1";
39 private PipedInputStream pin ;
40 private Vector streams ;
45 * PipedOutputMUX initialiser
46 * @param PipedOutputStream i - Source stream
48 public PipedOutputMUX(PipedInputStream i)
52 // Streams Vector holds all the OutputStreams we know about.
53 streams = new Vector() ;
55 // Initialise and start the thread.
56 t = new Thread(this, "OutputMultiplexor") ;
62 * @param PipedOutputStream po - add a stream to send output to.
64 public void addOutputStream(PipedOutputStream po)
66 // Add the supplied stream to the vector of streams.
67 streams.addElement(po) ;
71 * run - Thread run method.
75 // Loop continually reading the input stream.
80 byte[] b = new byte[16];
82 // Read a line and see if it has any data in it.
86 while(pin.available() > 0)
88 int rdb = pin.available() ;
89 if(rdb > 16) rdb = 16 ;
90 n = pin.read(b, 0, rdb);
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) ;
101 catch(IOException ex)
103 System.out.println("PipedOutputMUX: IOException trying to read.") ;
111 * @param String s - string to send to all streams.
113 private void send(String s)
115 // Calendar cal = Calendar.getInstance() ;
116 // if(DEBUG) System.out.println("PipedOutputMUX: " + cal.getTime() + " Send called with :" + s) ;
118 // If we have no streams, then we can't do anything.
119 if(streams.size() == 0) return ;
121 // Create Enumeration object to enumerate with :-)
122 Enumeration e = streams.elements() ;
124 // Go through the enumeration and send the string to each stream.
125 while(e.hasMoreElements())
127 PipedOutputStream os = (PipedOutputStream)e.nextElement() ;
131 // Write the data to the stream.
132 for(int i=0;i<s.length();i++)
134 os.write(s.charAt(i)) ;
138 catch(IOException ex)
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.") ;