1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.asteriskjava.util.internal;
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 javax.net.SocketFactory;
31 import javax.net.ssl.SSLSocketFactory;
32
33 import org.asteriskjava.util.SocketConnectionFacade;
34
35
36 /***
37 * Default implementation of the SocketConnectionFacade interface using java.io.
38 *
39 * @author srt
40 * @version $Id: SocketConnectionFacadeImpl.java 592 2007-01-21 16:44:00Z srt $
41 */
42 public class SocketConnectionFacadeImpl implements SocketConnectionFacade
43 {
44 private final Socket socket;
45 private final BufferedReader reader;
46 private final BufferedWriter writer;
47
48 /***
49 * Creates a new instance.
50 *
51 * @param host the foreign host to connect to.
52 * @param port the foreign port to connect to.
53 * @param ssl <code>true</code> to use SSL, <code>false</code> otherwise.
54 * @param timeout 0 incidcates default
55 * @param readTimeout see {@link Socket#setSoTimeout(int)}
56 * @throws IOException
57 */
58 public SocketConnectionFacadeImpl(String host, int port, boolean ssl, int timeout, int readTimeout) throws IOException
59 {
60 if (ssl)
61 {
62 this.socket = SSLSocketFactory.getDefault().createSocket();
63 }
64 else
65 {
66 this.socket = SocketFactory.getDefault().createSocket();
67 }
68 this.socket.setSoTimeout(readTimeout);
69 this.socket.connect(new InetSocketAddress(host, port), timeout);
70
71 InputStream inputStream = socket.getInputStream();
72 OutputStream outputStream = socket.getOutputStream();
73
74 this.reader = new BufferedReader(new InputStreamReader(inputStream));
75 this.writer = new BufferedWriter(new OutputStreamWriter(outputStream));
76 }
77
78 SocketConnectionFacadeImpl(Socket socket) throws IOException
79 {
80 this.socket = socket;
81
82 InputStream inputStream = socket.getInputStream();
83 OutputStream outputStream = socket.getOutputStream();
84
85 this.reader = new BufferedReader(new InputStreamReader(inputStream));
86 this.writer = new BufferedWriter(new OutputStreamWriter(outputStream));
87 }
88
89 public String readLine() throws IOException
90 {
91 return reader.readLine();
92 }
93
94 public void write(String s) throws IOException
95 {
96 writer.write(s);
97 }
98
99 public void flush() throws IOException
100 {
101 writer.flush();
102 }
103
104 public void close() throws IOException
105 {
106 this.socket.close();
107 }
108
109 public boolean isConnected()
110 {
111 return socket.isConnected();
112 }
113
114 public InetAddress getLocalAddress()
115 {
116 return socket.getLocalAddress();
117 }
118
119 public int getLocalPort()
120 {
121 return socket.getLocalPort();
122 }
123
124 public InetAddress getRemoteAddress()
125 {
126 return socket.getInetAddress();
127 }
128
129 public int getRemotePort()
130 {
131 return socket.getPort();
132 }
133 }