Source code: com/act365/net/echo/EchoWorker.java
1 /*
2 * JSocket Wrench
3 *
4 * Copyright (C) act365.com October 2003
5 *
6 * Web site: http://www.act365.com/wrench
7 * E-mail: developers@act365.com
8 *
9 * The JSocket Wrench library adds support for low-level Internet protocols
10 * to the Java programming language.
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 2 of the License, or (at your option)
15 * any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
20 * Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * this program; if not, write to the Free Software Foundation, Inc.,
24 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 */
26
27 package com.act365.net.echo ;
28
29 import java.io.* ;
30
31 /**
32 * An instance of the <code>EchoWorker</code> is created by the <code>EchoServer</code>
33 * class to handle each new client that is accepted. The <code>EchoWorker</code>
34 * objects will simply echo all input it receives back to the output.
35 */
36
37 class EchoWorker extends Thread {
38
39 InputStream in ;
40
41 OutputStreamWriter out ;
42
43 public EchoWorker( InputStream in , OutputStream out ){
44 super();
45 this.in = in ;
46 this.out = new OutputStreamWriter( out );
47 }
48
49 public void run() {
50
51 try {
52
53 StringBuffer buffer = new StringBuffer();
54
55 int ch = in.read();
56
57 while( ch > -1 ){
58 if( ch == 0 ){
59 break;
60 } else if( ch != '\n' ){
61 buffer.append((char) ch );
62 } else {
63 out.write( buffer.toString() );
64 out.write('\n');
65 out.flush();
66
67 buffer = new StringBuffer();
68 }
69 ch = in.read();
70 }
71
72 } catch( Exception e ){
73 System.err.println( e.getClass().getName() );
74 System.err.println( e.getMessage() );
75 }
76 }
77 }
78