View Javadoc

1   /*
2    * Copyright 2000 Computer System Services, Inc.
3    *
4    * Permission to use this software for any purpose is granted provided that
5    * this copyright notice is preserved.
6    *
7    * This software is provided as-is and without warranty as to its
8    * fitness for any purpose.  In other words, Computer System Services,
9    * Inc. does not guarantee that this software works.  It is provided
10   * only in the hope that it may be found useful by someone.
11   *
12   * Please e-mail tttaylor@cssassociates.com if you find any errors
13   * or want to request changes/enhancements.
14   */
15  package twcsckernel.projectbase.rmi.socketfactory;
16  
17  import java.io.*;
18  import java.net.*;
19  
20  /***
21   * Adapts two sockets by sending the data from the output
22   * stream of one to the input stream of the other and
23   * vice versa.
24   *
25   * @author Tim Taylor  -- tttaylor@cssassociates.com
26   */
27  public class SocketAdapter {
28      Socket socket1;
29      Socket socket2;
30  
31      private class StreamThread extends Thread {
32          InputStream in;
33          OutputStream out;
34          
35          StreamThread(InputStream in, OutputStream out) {
36              this.in = in;
37              this.out = out;
38          }
39  
40          public void run() {
41              try {
42                  for (;;) {
43                      int val = in.read();
44                      out.write(val);
45                      out.flush();
46                      if (val == -1) {
47                          out.close();
48                          in.close();
49                          return;
50                      }
51                  }
52              }
53              catch (IOException e) {
54              }
55          }
56      }
57      
58      public SocketAdapter(Socket socket1, Socket socket2) throws IOException {
59          new StreamThread(socket1.getInputStream(), socket2.getOutputStream()).
60              start();
61          new StreamThread(socket2.getInputStream(), socket1.getOutputStream()).
62              start();
63      }
64  
65      public void close() {
66      }
67  }