add the Java Client from M0AZM
[spider.git] / SpiderConsole / src / PipedInputMUX.java
1 /**
2  * InputStreamMultiplexor 
3  * This takes multiple input streams and sends them to one input stream.
4  * @author Ian Norton
5  * @version 1.00 - 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 PipedInputMUX implements Runnable
35     {
36     public static final boolean DEBUG = false ;
37     public static final String encoding = "latin1"; // "ISO8859_1";
38
39     private PipedOutputStream pos ;
40     private Vector streams ;
41
42     private Thread t ;
43     
44     /**
45      * PipedInputMUX initialiser.
46      * @param PipedOutputStream - target stream.
47      **/
48     public PipedInputMUX(PipedOutputStream o)
49         {
50         pos = o ;
51
52         // Streams Vector holds all the InputStreams we know about.
53         streams = new Vector() ;
54
55         // Initialise and start the thread.
56         t = new Thread(this, "InputMultiplexor") ;
57         t.start() ;
58         }
59
60     /**
61      * addInputStream
62      * @param PipedInputStream pi - add a stream get input from.
63      **/
64     public void addInputStream(PipedInputStream pi)
65         {
66         // Add the supplied stream to the vector of streams.
67         streams.addElement(pi) ;
68         }
69
70     /**
71      * run - Thread run method.
72      **/
73     public void run()
74         {
75         // Loop continually reading from the input streams
76         while(true)
77             {
78             // Enumeration thing here.
79             Enumeration e = streams.elements() ;
80  
81             byte[] b = new byte[16];
82
83             while(e.hasMoreElements())
84                 {
85                 PipedInputStream is = (PipedInputStream)e.nextElement() ;
86                 
87                 try
88                     {
89                     // Read a line and see if it has any data in it.
90                     int n = 0;
91
92                     // While there is non-blocking data available to read
93                     while(is.available() > 0)
94                         {
95                         // find out how many bytes we can read without blocking
96                         int rdb = is.available() ;
97                         if(rdb > 16) rdb = 16 ;
98                         
99                         // Read that many bytes and return.
100                         n = is.read(b, 0, rdb);
101                         if(n > 0)
102                             {
103                             String output = new String(b, 0, n, encoding) ;
104                             send(output) ;
105                             }
106                         }
107      
108                     if(DEBUG) System.out.println("After reading a line.") ;
109                     }
110                 catch(IOException ex)
111                     {
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.") ;
116                     }
117                                                                                 
118                 } // End of while(e.hasMoreElements())
119             } // End of while(true)
120         } // End of run()
121
122     /**
123      * send
124      * @param String s - string to send to destination stream.
125      **/
126     private void send(String s)
127         {
128         // Calendar cal = Calendar.getInstance() ;
129         // if(DEBUG) System.out.println("PipedInputMUX: " + cal.getTime() + " Send called with : " + s) ;
130
131         try
132             {
133             // Write the data to the stream.
134             for(int i=0;i<s.length();i++)
135                 {
136                 pos.write(s.charAt(i)) ;
137                 pos.flush() ;
138                 }
139             }
140         catch(IOException ex)
141             {
142             System.out.println("PipedInputMUX: IOException on destination stream.") ;
143             }
144         }
145     } // End of class.