1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mortbay.io.bio;
17
18 import java.io.IOException;
19 import java.net.InetAddress;
20 import java.net.InetSocketAddress;
21 import java.net.Socket;
22
23 import org.mortbay.io.Portable;
24 import org.mortbay.log.Log;
25
26
27
28
29
30
31
32 public class SocketEndPoint extends StreamEndPoint
33 {
34 final Socket _socket;
35 final InetSocketAddress _local;
36 final InetSocketAddress _remote;
37
38
39
40
41 public SocketEndPoint(Socket socket)
42 throws IOException
43 {
44 super(socket.getInputStream(),socket.getOutputStream());
45 _socket=socket;
46 _local=(InetSocketAddress)_socket.getLocalSocketAddress();
47 _remote=(InetSocketAddress)_socket.getRemoteSocketAddress();
48 }
49
50
51
52
53 public boolean isOpen()
54 {
55 return super.isOpen() && _socket!=null && !_socket.isClosed() && !_socket.isInputShutdown() && !_socket.isOutputShutdown();
56 }
57
58
59
60
61 public void close() throws IOException
62 {
63 if (!_socket.isClosed() && !_socket.isOutputShutdown())
64 {
65 try
66 {
67 _socket.shutdownOutput();
68 }
69 catch(IOException e)
70 {
71 Log.ignore(e);
72 }
73 catch(UnsupportedOperationException e)
74 {
75 Log.ignore(e);
76 }
77 }
78 _socket.close();
79 _in=null;
80 _out=null;
81
82 }
83
84
85
86
87
88
89 public String getLocalAddr()
90 {
91 if (_local==null || _local.getAddress()==null || _local.getAddress().isAnyLocalAddress())
92 return Portable.ALL_INTERFACES;
93
94 return _local.getAddress().getHostAddress();
95 }
96
97
98
99
100
101 public String getLocalHost()
102 {
103 if (_local==null || _local.getAddress()==null || _local.getAddress().isAnyLocalAddress())
104 return Portable.ALL_INTERFACES;
105
106 return _local.getAddress().getCanonicalHostName();
107 }
108
109
110
111
112
113 public int getLocalPort()
114 {
115 if (_local==null)
116 return -1;
117 return _local.getPort();
118 }
119
120
121
122
123
124 public String getRemoteAddr()
125 {
126 if (_remote==null)
127 return null;
128 InetAddress addr = _remote.getAddress();
129 return ( addr == null ? null : addr.getHostAddress() );
130 }
131
132
133
134
135
136 public String getRemoteHost()
137 {
138 if (_remote==null)
139 return null;
140 return _remote.getAddress().getCanonicalHostName();
141 }
142
143
144
145
146
147 public int getRemotePort()
148 {
149 if (_remote==null)
150 return -1;
151 return _remote.getPort();
152 }
153
154
155
156
157
158 public Object getTransport()
159 {
160 return _socket;
161 }
162 }