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.impl;
18  
19  import java.io.BufferedReader;
20  import java.io.BufferedWriter;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.InputStreamReader;
24  import java.io.OutputStream;
25  import java.io.OutputStreamWriter;
26  import java.net.InetAddress;
27  import java.net.InetSocketAddress;
28  import java.net.Socket;
29  
30  import net.sf.asterisk.io.SocketConnectionFacade;
31  
32  /***
33   * Default implementation of the SocketConnectionFacade interface using java.io.
34   * 
35   * @author srt
36   * @version $Id: SocketConnectionFacadeImpl.java,v 1.1 2005/10/25 22:43:25 srt Exp $
37   */
38  public class SocketConnectionFacadeImpl implements SocketConnectionFacade
39  {
40      private final Socket socket;
41      private final BufferedReader reader;
42      private final BufferedWriter writer;
43  
44      public SocketConnectionFacadeImpl(String host, int port) throws IOException
45      {
46      	this(host, port, 0);
47      }    
48      
49      public SocketConnectionFacadeImpl(String host, int port, int timeout) throws IOException
50      {
51      	this.socket = new Socket();
52      	this.socket.connect(new InetSocketAddress(host, port), timeout);
53  
54          InputStream inputStream = socket.getInputStream();
55          OutputStream outputStream = socket.getOutputStream();
56  
57          this.reader = new BufferedReader(new InputStreamReader(inputStream));
58          this.writer = new BufferedWriter(new OutputStreamWriter(outputStream));
59      }
60  
61      SocketConnectionFacadeImpl(Socket socket) throws IOException
62      {
63          this.socket = socket;
64  
65          InputStream inputStream = socket.getInputStream();
66          OutputStream outputStream = socket.getOutputStream();
67  
68          this.reader = new BufferedReader(new InputStreamReader(inputStream));
69          this.writer = new BufferedWriter(new OutputStreamWriter(outputStream));
70      }
71  
72      public String readLine() throws IOException
73      {
74          return reader.readLine();
75      }
76  
77      public void write(String s) throws IOException
78      {
79          writer.write(s);
80      }
81  
82      public void flush() throws IOException
83      {
84          writer.flush();
85      }
86  
87      public void close() throws IOException
88      {
89          this.socket.close();
90      }
91  
92      public boolean isConnected()
93      {
94          return socket.isConnected();
95      }
96  
97      public InetAddress getLocalAddress()
98      {
99          return socket.getLocalAddress();
100     }
101 
102     public int getLocalPort()
103     {
104         return socket.getLocalPort();
105     }
106 
107     public InetAddress getRemoteAddress()
108     {
109         return socket.getInetAddress();
110     }
111 
112     public int getRemotePort()
113     {
114         return socket.getPort();
115     }
116 }