741 |
10 Oct 06 |
olle |
1 |
/* |
1652 |
22 May 07 |
gregory |
$Id$ |
741 |
10 Oct 06 |
olle |
3 |
|
1916 |
31 Aug 07 |
jari |
Copyright (C) 2006 Olle Mansson |
1916 |
31 Aug 07 |
jari |
Copyright (C) 2007 Gregory Vincic |
741 |
10 Oct 06 |
olle |
6 |
|
1652 |
22 May 07 |
gregory |
This file is part of Proteios. |
1652 |
22 May 07 |
gregory |
Available at http://www.proteios.org/ |
741 |
10 Oct 06 |
olle |
9 |
|
1652 |
22 May 07 |
gregory |
Proteios is free software; you can redistribute it and/or modify it |
1652 |
22 May 07 |
gregory |
under the terms of the GNU General Public License as published by |
1652 |
22 May 07 |
gregory |
the Free Software Foundation; either version 2 of the License, or |
1652 |
22 May 07 |
gregory |
(at your option) any later version. |
741 |
10 Oct 06 |
olle |
14 |
|
1652 |
22 May 07 |
gregory |
Proteios is distributed in the hope that it will be useful, but |
1652 |
22 May 07 |
gregory |
WITHOUT ANY WARRANTY; without even the implied warranty of |
1652 |
22 May 07 |
gregory |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
1652 |
22 May 07 |
gregory |
General Public License for more details. |
741 |
10 Oct 06 |
olle |
19 |
|
1652 |
22 May 07 |
gregory |
You should have received a copy of the GNU General Public License |
1652 |
22 May 07 |
gregory |
along with this program; if not, write to the Free Software |
1652 |
22 May 07 |
gregory |
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
1652 |
22 May 07 |
gregory |
02111-1307, USA. |
1652 |
22 May 07 |
gregory |
24 |
*/ |
741 |
10 Oct 06 |
olle |
25 |
|
741 |
10 Oct 06 |
olle |
// Xerver Free Web Server |
741 |
10 Oct 06 |
olle |
// Copyright (C) 2002-2005 Omid Rouhani |
741 |
10 Oct 06 |
olle |
28 |
// |
741 |
10 Oct 06 |
olle |
29 |
// |
741 |
10 Oct 06 |
olle |
// This program is free software; you can redistribute it and/or |
741 |
10 Oct 06 |
olle |
// modify it under the terms of the GNU General Public License |
741 |
10 Oct 06 |
olle |
// as published by the Free Software Foundation; either version 2 |
741 |
10 Oct 06 |
olle |
// of the License, or (at your option) any later version. |
741 |
10 Oct 06 |
olle |
34 |
// |
741 |
10 Oct 06 |
olle |
// This program is distributed in the hope that it will be useful, |
741 |
10 Oct 06 |
olle |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
741 |
10 Oct 06 |
olle |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
741 |
10 Oct 06 |
olle |
// GNU General Public License for more details. |
741 |
10 Oct 06 |
olle |
39 |
// |
741 |
10 Oct 06 |
olle |
// You should have received a copy of the GNU General Public License |
741 |
10 Oct 06 |
olle |
// along with this program; if not, write to the Free Software |
741 |
10 Oct 06 |
olle |
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
741 |
10 Oct 06 |
olle |
43 |
// |
741 |
10 Oct 06 |
olle |
44 |
// |
741 |
10 Oct 06 |
olle |
45 |
// ############################################################# |
741 |
10 Oct 06 |
olle |
// ## YOU CAN CONTACT THE AUTHOR (OMID ROUHANI) AT: ## |
741 |
10 Oct 06 |
olle |
// ## HTTP://WWW.JAVASCRIPT.NU/XERVER/ ## |
741 |
10 Oct 06 |
olle |
48 |
// ## ## |
741 |
10 Oct 06 |
olle |
// ## IF YOUR SOFTWARE IS NOT RELEASED UNDER THE ## |
741 |
10 Oct 06 |
olle |
// ## GNU GENERAL PUBLIC LICENSE (GPL), ## |
741 |
10 Oct 06 |
olle |
// ## PLEASE DO NOT COPY ANYTHING FROM THIS SOURCE CODE!!! ## |
741 |
10 Oct 06 |
olle |
52 |
// ## ## |
741 |
10 Oct 06 |
olle |
// ## FOR FULL LICENSE, PLEASE READ "XERVER LICENSE". ## |
741 |
10 Oct 06 |
olle |
54 |
// ############################################################# |
741 |
10 Oct 06 |
olle |
55 |
|
1652 |
22 May 07 |
gregory |
56 |
package se.lu.thep.coreftpd.webserver; |
741 |
10 Oct 06 |
olle |
57 |
|
741 |
10 Oct 06 |
olle |
58 |
import java.io.InputStream; |
741 |
10 Oct 06 |
olle |
59 |
import java.io.OutputStream; |
741 |
10 Oct 06 |
olle |
60 |
|
741 |
10 Oct 06 |
olle |
61 |
/** |
1652 |
22 May 07 |
gregory |
62 |
* |
1652 |
22 May 07 |
gregory |
* <B>How to use:</B> <BR> |
741 |
10 Oct 06 |
olle |
* <CODE>(new ReadInputStream(InputStream argIs, OutputStream argOs)).start();</CODE> |
741 |
10 Oct 06 |
olle |
* <BR> |
1652 |
22 May 07 |
gregory |
* <CODE>(new ReadInputStream(String argStringToOutput, OutputStream |
1652 |
22 May 07 |
gregory |
* argOs)).start();</CODE> <BR> |
1652 |
22 May 07 |
gregory |
* <CODE>(new ReadInputStream(char [] argCharToOutput, OutputStream |
1652 |
22 May 07 |
gregory |
* argOs)).start();</CODE> <BR> |
741 |
10 Oct 06 |
olle |
* <BR> |
1652 |
22 May 07 |
gregory |
71 |
* |
1652 |
22 May 07 |
gregory |
* <B>More info:</B> <BR> |
1652 |
22 May 07 |
gregory |
* Takes an <CODE>InputStream</CODE> (<CODE>argIs</CODE>) (or a <CODE>String</CODE> |
1652 |
22 May 07 |
gregory |
* or an array of <CODE>char</CODE>) <BR> |
741 |
10 Oct 06 |
olle |
* and writes everything coming from this stream to the <CODE>OutputStream</CODE> (<CODE>argOs</CODE>). |
741 |
10 Oct 06 |
olle |
* <BR> |
1652 |
22 May 07 |
gregory |
* If you don't give an <CODE>InputStream</CODE>, but a <CODE>String</CODE> |
1652 |
22 May 07 |
gregory |
* or an array of <CODE>char</CODE>, <BR> |
1652 |
22 May 07 |
gregory |
* the <CODE>String</CODE>/the <CODE>char</CODE>s will be written to the |
1652 |
22 May 07 |
gregory |
* <CODE>OutputStream</CODE> (<CODE>argOs</CODE>). <BR> |
741 |
10 Oct 06 |
olle |
* <BR> |
1652 |
22 May 07 |
gregory |
82 |
* |
1652 |
22 May 07 |
gregory |
* <B>IMPORTANT:</B> <CODE>argOs</CODE> is closed ("<CODE>argOs.close()</CODE>") |
1652 |
22 May 07 |
gregory |
* by <CODE>ReadInputStream</CODE>. |
1652 |
22 May 07 |
gregory |
85 |
* |
741 |
10 Oct 06 |
olle |
* @author <a href="http://www.JavaScript.nu/xerver/" TARGET="_top">Omid Rouhani</a> |
741 |
10 Oct 06 |
olle |
* @version 1.0 |
741 |
10 Oct 06 |
olle |
88 |
*/ |
741 |
10 Oct 06 |
olle |
89 |
|
1652 |
22 May 07 |
gregory |
90 |
public class ReadInputStream extends Thread { |
1652 |
22 May 07 |
gregory |
91 |
protected final static int INPUT_MODE = 0; |
1652 |
22 May 07 |
gregory |
92 |
|
1652 |
22 May 07 |
gregory |
93 |
protected final static int CHAR_MODE = 1; |
1652 |
22 May 07 |
gregory |
94 |
|
1652 |
22 May 07 |
gregory |
95 |
protected final static int RESPOND_UNDEFINED = -1; |
1652 |
22 May 07 |
gregory |
96 |
|
1652 |
22 May 07 |
gregory |
97 |
protected final static int RESPOND_STDERR = 0; |
1652 |
22 May 07 |
gregory |
98 |
|
1652 |
22 May 07 |
gregory |
99 |
protected final static int RESPOND_STDOUT = 1; |
1652 |
22 May 07 |
gregory |
100 |
|
741 |
10 Oct 06 |
olle |
101 |
protected InputStream is; |
1652 |
22 May 07 |
gregory |
102 |
|
741 |
10 Oct 06 |
olle |
103 |
protected OutputStream os; |
1652 |
22 May 07 |
gregory |
104 |
|
1652 |
22 May 07 |
gregory |
105 |
private String s_StringToOutput = null; |
1652 |
22 May 07 |
gregory |
106 |
|
1652 |
22 May 07 |
gregory |
107 |
private char[] c_charToOutput; |
1652 |
22 May 07 |
gregory |
108 |
|
741 |
10 Oct 06 |
olle |
109 |
protected int currentMode; |
741 |
10 Oct 06 |
olle |
110 |
|
1652 |
22 May 07 |
gregory |
111 |
protected final static boolean b_showErrors = false; |
1652 |
22 May 07 |
gregory |
112 |
|
1652 |
22 May 07 |
gregory |
113 |
public ReadInputStream() // now we can Extend ReadInputStream without |
1652 |
22 May 07 |
gregory |
// problem |
741 |
10 Oct 06 |
olle |
115 |
{ |
741 |
10 Oct 06 |
olle |
116 |
} |
741 |
10 Oct 06 |
olle |
117 |
|
1652 |
22 May 07 |
gregory |
118 |
public ReadInputStream(InputStream argIs, OutputStream argOs) { |
1652 |
22 May 07 |
gregory |
119 |
currentMode = INPUT_MODE; |
1652 |
22 May 07 |
gregory |
120 |
is = argIs; |
1652 |
22 May 07 |
gregory |
121 |
os = argOs; |
741 |
10 Oct 06 |
olle |
122 |
} |
741 |
10 Oct 06 |
olle |
123 |
|
1652 |
22 May 07 |
gregory |
124 |
public ReadInputStream(String argStringToOutput, OutputStream argOs) { |
741 |
10 Oct 06 |
olle |
125 |
this(argStringToOutput.toCharArray(), argOs); |
741 |
10 Oct 06 |
olle |
126 |
} |
741 |
10 Oct 06 |
olle |
127 |
|
1652 |
22 May 07 |
gregory |
128 |
public ReadInputStream(char[] argCharToOutput, OutputStream argOs) { |
1652 |
22 May 07 |
gregory |
129 |
currentMode = CHAR_MODE; |
1652 |
22 May 07 |
gregory |
130 |
c_charToOutput = argCharToOutput; |
1652 |
22 May 07 |
gregory |
131 |
os = argOs; |
741 |
10 Oct 06 |
olle |
132 |
} |
741 |
10 Oct 06 |
olle |
133 |
|
2386 |
14 Nov 07 |
gregory |
134 |
@Override |
1652 |
22 May 07 |
gregory |
135 |
public void run() { |
1652 |
22 May 07 |
gregory |
136 |
if (currentMode == INPUT_MODE) |
741 |
10 Oct 06 |
olle |
137 |
sendDataInputMode(); |
1652 |
22 May 07 |
gregory |
138 |
else if (currentMode == CHAR_MODE) |
741 |
10 Oct 06 |
olle |
139 |
sendDataCharMode(); |
741 |
10 Oct 06 |
olle |
140 |
} |
741 |
10 Oct 06 |
olle |
141 |
|
1652 |
22 May 07 |
gregory |
142 |
public void sendDataCharMode() { |
1652 |
22 May 07 |
gregory |
143 |
try { |
1652 |
22 May 07 |
gregory |
144 |
char[] b = c_charToOutput; |
1652 |
22 May 07 |
gregory |
145 |
for (int i = 0, bLength = b.length; i < bLength; i++) { |
741 |
10 Oct 06 |
olle |
146 |
os.write(b[i]); |
741 |
10 Oct 06 |
olle |
147 |
} |
741 |
10 Oct 06 |
olle |
148 |
|
741 |
10 Oct 06 |
olle |
149 |
os.flush(); |
741 |
10 Oct 06 |
olle |
150 |
os.close(); |
1652 |
22 May 07 |
gregory |
151 |
} catch (Exception e) { |
1652 |
22 May 07 |
gregory |
152 |
if (b_showErrors) |
1652 |
22 May 07 |
gregory |
153 |
System.out.println("An error has occured @ sendDataCharMode\n" |
1652 |
22 May 07 |
gregory |
154 |
+ e); |
1652 |
22 May 07 |
gregory |
155 |
} |
741 |
10 Oct 06 |
olle |
156 |
} |
741 |
10 Oct 06 |
olle |
157 |
|
1652 |
22 May 07 |
gregory |
158 |
public void sendDataInputMode() { |
1652 |
22 May 07 |
gregory |
159 |
try { |
741 |
10 Oct 06 |
olle |
160 |
int n; |
1652 |
22 May 07 |
gregory |
161 |
byte[] buffer = new byte[8192]; |
1652 |
22 May 07 |
gregory |
162 |
while ((n = is.read(buffer)) != -1) { |
1652 |
22 May 07 |
gregory |
163 |
os.write(buffer, 0, n); |
741 |
10 Oct 06 |
olle |
164 |
} |
741 |
10 Oct 06 |
olle |
165 |
|
741 |
10 Oct 06 |
olle |
166 |
os.flush(); |
1652 |
22 May 07 |
gregory |
167 |
os.close(); // os MUST be closed, or the browser will wait for more |
1652 |
22 May 07 |
gregory |
// data. (with netscape: the stop-button will be |
1652 |
22 May 07 |
gregory |
// "clickable" as the browser waits for the server to |
1652 |
22 May 07 |
gregory |
// write more data or to close the connection (we close |
1652 |
22 May 07 |
gregory |
// the connection with this line). |
1652 |
22 May 07 |
gregory |
172 |
} catch (Exception e) { |
1652 |
22 May 07 |
gregory |
173 |
if (b_showErrors) |
1652 |
22 May 07 |
gregory |
174 |
System.out |
1652 |
22 May 07 |
gregory |
175 |
.println("An error has occured @ sendDataInputMode:\n" |
1652 |
22 May 07 |
gregory |
176 |
+ e.getMessage()); |
1652 |
22 May 07 |
gregory |
177 |
} |
741 |
10 Oct 06 |
olle |
178 |
} |
741 |
10 Oct 06 |
olle |
179 |
} |