2 * ConnectionOutput - reads from the pipe and writes data to the socket.
4 * @version 1.00 - 20010418.
6 * Copyright (C) 2001 Ian Norton.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public Licence as published by
10 * the Free Software Foundation; either version 2 of the Licence, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public Licence for more details.
18 * You should have received a copy of the GNU General Public Licence
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * Contacting the author :
24 * i.norton@lancaster.ac.uk
25 * http://www.lancs.ac.uk/~norton/
31 class ConnectionOutput implements Runnable
34 public static final boolean DEBUG = false ;
37 private OutputStream os ;
38 private PipedInputStream pin ;
40 // Connection object that created us.
41 private Connection connection ;
44 private boolean disconnected ;
46 // Thread to run the read code in.
50 public static final String encoding = "latin1"; // "ISO8859_1";
54 * @param OutputStream - OutputStream to the socket to write to
55 * @param PipedInputStream - Read data from here
56 * @param Connection - the object that created us
58 public ConnectionOutput(PipedInputStream p, Connection c)
60 // Initialise the streams & connection
68 * disconnect - disconnect the current connection.
70 public void disconnect()
74 if(DEBUG) System.out.println("ConnectionOutput: disconnect()") ;
77 connection.disconnect() ;
82 * start - begin reading. Called when a connect has been achieved.
84 public void start(OutputStream o)
88 disconnected = false ;
90 // Test to see if the thread has been inititialised.
93 if(DEBUG) System.out.println("ConnectionOutput: Creating thread.") ;
95 // Initialise the thread to read data & start it.
96 t = new Thread(this, "Connection") ;
106 byte[] b = new byte[16];
108 // Loop reading data.
113 // Read from PipedInputStream and write to OutputStream
116 // Read that many bytes and return.
119 // If disconnected read and disguard data or the MUX dies.
120 if(n > 0 && !disconnected)
122 String output = new String(b, 0, n, encoding) ;
126 catch(IOException ex)
128 System.out.println("ConnectionOutput: IOException reading data from multiplexor.") ;
136 * @param String s - string to send to destination stream.
138 private void send(String s)
140 if(DEBUG) System.out.println("ConnectionOutput: Send called : " + s) ;
143 // Write the data to the stream.
144 for(int i=0;i<s.length();i++)
146 os.write(s.charAt(i)) ;
150 catch(IOException ex)
152 System.out.println("ConnectionOutput: IOException writing to socket.") ;