View Javadoc

1   /*
2    * Copyright  2004-2005 Stefan Reuter
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *
16   */
17  package net.sf.asterisk.io;
18  
19  import java.io.BufferedReader;
20  import java.io.BufferedWriter;
21  import java.io.IOException;
22  import java.io.InputStreamReader;
23  import java.io.OutputStreamWriter;
24  import java.net.Socket;
25  
26  public class SocketConnectionFacadeImpl implements SocketConnectionFacade
27  {
28      private final Socket socket;
29      private final BufferedReader reader;
30      private final BufferedWriter writer;
31      
32      public SocketConnectionFacadeImpl(String host, int port) throws IOException
33      {
34          this.socket = new Socket(host, port);
35          this.reader = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
36          this.writer = new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream()));
37      }
38      
39      SocketConnectionFacadeImpl(Socket socket) throws IOException
40      {
41          this.socket = socket;
42          this.reader = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
43          this.writer = new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream()));
44      }
45      
46      public String readLine() throws IOException
47      {
48          return reader.readLine();
49      }
50  
51      public void write(String s) throws IOException
52      {
53          writer.write(s);
54      }
55  
56      public void flush() throws IOException
57      {
58          writer.flush();
59      }
60  
61      public void close() throws IOException
62      {
63          this.socket.close();
64      }
65  
66      public boolean isConnected()
67      {
68          return socket.isConnected();
69      }
70  }