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
23
24
25
26
27
28 public class SocketEndPoint extends StreamEndPoint
29 {
30 final Socket _socket;
31 final InetSocketAddress _local;
32 final InetSocketAddress _remote;
33
34
35
36
37
38 public SocketEndPoint(Socket socket)
39 throws IOException
40 {
41 super(socket.getInputStream(),socket.getOutputStream());
42 _socket=socket;
43 _local=(InetSocketAddress)_socket.getLocalSocketAddress();
44 _remote=(InetSocketAddress)_socket.getRemoteSocketAddress();
45 super.setMaxIdleTime(_socket.getSoTimeout());
46 }
47
48
49
50
51
52 protected SocketEndPoint(Socket socket, int maxIdleTime)
53 throws IOException
54 {
55 super(socket.getInputStream(),socket.getOutputStream());
56 _socket=socket;
57 _local=(InetSocketAddress)_socket.getLocalSocketAddress();
58 _remote=(InetSocketAddress)_socket.getRemoteSocketAddress();
59 _socket.setSoTimeout(maxIdleTime>0?maxIdleTime:0);
60 super.setMaxIdleTime(maxIdleTime);
61 }
62
63
64
65
66
67 @Override
68 public boolean isOpen()
69 {
70 return super.isOpen() && _socket!=null && !_socket.isClosed() && !_socket.isInputShutdown() && !_socket.isOutputShutdown();
71 }
72
73
74
75
76
77 @Override
78 public void shutdownOutput() throws IOException
79 {
80 if (!_socket.isClosed() && !_socket.isOutputShutdown())
81 _socket.shutdownOutput();
82 }
83
84
85
86
87
88 @Override
89 public void close() throws IOException
90 {
91 _socket.close();
92 _in=null;
93 _out=null;
94 }
95
96
97
98
99
100
101 @Override
102 public String getLocalAddr()
103 {
104 if (_local==null || _local.getAddress()==null || _local.getAddress().isAnyLocalAddress())
105 return StringUtil.ALL_INTERFACES;
106
107 return _local.getAddress().getHostAddress();
108 }
109
110
111
112
113
114 @Override
115 public String getLocalHost()
116 {
117 if (_local==null || _local.getAddress()==null || _local.getAddress().isAnyLocalAddress())
118 return StringUtil.ALL_INTERFACES;
119
120 return _local.getAddress().getCanonicalHostName();
121 }
122
123
124
125
126
127 @Override
128 public int getLocalPort()
129 {
130 if (_local==null)
131 return -1;
132 return _local.getPort();
133 }
134
135
136
137
138
139 @Override
140 public String getRemoteAddr()
141 {
142 if (_remote==null)
143 return null;
144 InetAddress addr = _remote.getAddress();
145 return ( addr == null ? null : addr.getHostAddress() );
146 }
147
148
149
150
151
152 @Override
153 public String getRemoteHost()
154 {
155 if (_remote==null)
156 return null;
157 return _remote.getAddress().getCanonicalHostName();
158 }
159
160
161
162
163
164 @Override
165 public int getRemotePort()
166 {
167 if (_remote==null)
168 return -1;
169 return _remote.getPort();
170 }
171
172
173
174
175
176 @Override
177 public Object getTransport()
178 {
179 return _socket;
180 }
181
182
183
184
185
186 @Override
187 public void setMaxIdleTime(int timeMs) throws IOException
188 {
189 if (timeMs!=getMaxIdleTime())
190 _socket.setSoTimeout(timeMs>0?timeMs:0);
191 super.setMaxIdleTime(timeMs);
192 }
193
194
195 }