2 * InputStreamMultiplexor
3 * This takes multiple input streams and sends them to one input stream.
5 * @version 1.00 - 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 PipedInputMUX implements Runnable
36 public static final boolean DEBUG = false ;
37 public static final String encoding = "latin1"; // "ISO8859_1";
39 private PipedOutputStream pos ;
40 private Vector streams ;
45 * PipedInputMUX initialiser.
46 * @param PipedOutputStream - target stream.
48 public PipedInputMUX(PipedOutputStream o)
52 // Streams Vector holds all the InputStreams we know about.
53 streams = new Vector() ;
55 // Initialise and start the thread.
56 t = new Thread(this, "InputMultiplexor") ;
62 * @param PipedInputStream pi - add a stream get input from.
64 public void addInputStream(PipedInputStream pi)
66 // Add the supplied stream to the vector of streams.
67 streams.addElement(pi) ;
71 * run - Thread run method.
75 // Loop continually reading from the input streams
78 // Enumeration thing here.
79 Enumeration e = streams.elements() ;
81 byte[] b = new byte[16];
83 while(e.hasMoreElements())
85 PipedInputStream is = (PipedInputStream)e.nextElement() ;
89 // Read a line and see if it has any data in it.
92 // While there is non-blocking data available to read
93 while(is.available() > 0)
95 // find out how many bytes we can read without blocking
96 int rdb = is.available() ;
97 if(rdb > 16) rdb = 16 ;
99 // Read that many bytes and return.
100 n = is.read(b, 0, rdb);
103 String output = new String(b, 0, n, encoding) ;
108 if(DEBUG) System.out.println("After reading a line.") ;
110 catch(IOException ex)
112 // If we get an IO exception, then the other end of the pipe
113 // has been closed. We need to remove this stream.
114 streams.removeElement(is) ;
115 System.out.println("IOException - stream removed.") ;
118 } // End of while(e.hasMoreElements())
119 } // End of while(true)
124 * @param String s - string to send to destination stream.
126 private void send(String s)
128 // Calendar cal = Calendar.getInstance() ;
129 // if(DEBUG) System.out.println("PipedInputMUX: " + cal.getTime() + " Send called with : " + s) ;
133 // Write the data to the stream.
134 for(int i=0;i<s.length();i++)
136 pos.write(s.charAt(i)) ;
140 catch(IOException ex)
142 System.out.println("PipedInputMUX: IOException on destination stream.") ;