add the Java Client from M0AZM
[spider.git] / SpiderConsole / src / ConnectionInput.java
1 /**
2  * ConnectionInput - reads from the socket and writes data to the pipe.
3  * @author Ian Norton
4  * @version 1.00 - 20010418.
5  *
6  * Copyright (C) 2001 Ian Norton.
7  *
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.
12  *
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.
17  *
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.
21  *
22  * Contacting the author :
23  * Ian Norton
24  * i.norton@lancaster.ac.uk
25  * http://www.lancs.ac.uk/~norton/
26  **/
27
28 import java.io.* ;
29 import java.net.* ;
30
31 class ConnectionInput implements Runnable
32     {
33     // Debug me bugs
34     public static final boolean DEBUG = false ;
35     
36     // Data streams.
37     private InputStream is ;
38     private PipedOutputStream pos ;
39
40     // Connection object that created us.
41     private Connection connection ;
42
43     // Connection status.
44     private boolean disconnected ;
45     
46     // Thread to run the read code in.
47     private Thread t ;
48
49     // Encoding string.
50     public static final String encoding = "latin1"; // "ISO8859_1";   
51
52     /**
53      * ConnectionInput
54      * @param InputStream - InputStream from the socket to read from
55      * @param PipedOutputStream - Write the data out to here
56      * @param Connection - the object that created us
57      **/
58     public ConnectionInput(PipedOutputStream p, Connection c)
59         {
60         // Initialise the streams & connection
61         pos = p ;
62         connection = c ;
63
64         disconnected = true ;
65         }
66
67     /**
68      * disconnect - disconnect the current connection.
69      **/
70     public void disconnect()
71         {
72         if(!disconnected)
73             {
74             if(DEBUG) System.out.println("ConnectionInput: disconnect()") ;
75
76             try { pos.flush() ; }
77             catch(IOException ex) { }
78
79             disconnected = true ;
80             connection.disconnect() ;
81             }
82         }
83
84     /**
85      * start - begin reading.  Called when a connect has been achieved.
86      **/
87     public void start(InputStream i)
88         {
89         is = i ;
90
91         disconnected = false ;
92
93         // Initialise the thread to read data & start it.
94         t = new Thread(this, "ConnectionInput") ;
95         t.start() ;
96         }
97
98     /**
99      * Thread run method.
100      **/
101     public void run()
102         {
103         byte[] b = new byte[16];   
104
105         // Loop reading data.
106         while(!disconnected)
107             {
108             try
109                 {
110                 // Read from InputStream and write to PipedOutputStream
111                 int n = 0;
112
113                 n = is.read(b) ;
114                 if(n > 0)
115                     {
116                     String output = new String(b, 0, n, encoding) ;
117                     send(output) ;
118                     }                                                   
119                 else if(n == -1)
120                     {
121                     this.disconnect() ;
122                     }
123                 }
124             catch(IOException ex)
125                 {
126                 if(disconnected)
127                     return ;
128
129                 System.out.println("ConnectionInput: IOException reading data.") ;
130                 this.disconnect() ;
131                 }
132             } // End while(true)
133         } // End run()
134
135     /**
136      * send
137      * @param String s - string to send to destination stream.
138      **/
139     private void send(String s)
140         {
141         try
142             {
143             // Write the data to the stream.
144             for(int i=0;i<s.length();i++)
145                 {
146                 pos.write(s.charAt(i)) ;
147                 pos.flush() ;
148                 }
149             }
150         catch(IOException ex)
151             {
152             System.out.println("ConnectionInput:  IOException writing to multiplexor.") ;
153             System.exit(1) ;
154             }
155         } // End of send(String s)
156     } // End class
157