1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 }