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