17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.nio.ch;
27
28 import java.io.*;
29 import java.lang.ref.*;
30 import java.net.*;
31 import java.nio.*;
32 import java.nio.channels.*;
33 import java.security.AccessController;
34 import java.security.PrivilegedExceptionAction;
35 import java.util.*;
36
37
38 // Make a socket channel look like a socket.
39 //
40 // The only aspects of java.net.Socket-hood that we don't attempt to emulate
41 // here are the interrupted-I/O exceptions (which our Solaris implementations
42 // attempt to support) and the sending of urgent data. Otherwise an adapted
43 // socket should look enough like a real java.net.Socket to fool most of the
44 // developers most of the time, right down to the exception message strings.
45 //
46 // The methods in this class are defined in exactly the same order as in
47 // java.net.Socket so as to simplify tracking future changes to that class.
48 //
49
50 public class SocketAdaptor
51 extends Socket
52 {
53
54 // The channel being adapted
55 private final SocketChannelImpl sc;
56
174 }
175 }
176
177 private class SocketInputStream
178 extends ChannelInputStream
179 {
180 private SocketInputStream() {
181 super(sc);
182 }
183
184 protected int read(ByteBuffer bb)
185 throws IOException
186 {
187 synchronized (sc.blockingLock()) {
188 if (!sc.isBlocking())
189 throw new IllegalBlockingModeException();
190 if (timeout == 0)
191 return sc.read(bb);
192 sc.configureBlocking(false);
193
194 try {
195 int n;
196 if ((n = sc.read(bb)) != 0)
197 return n;
198 long to = timeout;
199 for (;;) {
200 if (!sc.isOpen())
201 throw new ClosedChannelException();
202 long st = System.currentTimeMillis();
203 int result = sc.poll(PollArrayWrapper.POLLIN, to);
204 if (result > 0) {
205 if ((n = sc.read(bb)) != 0)
206 return n;
207 }
208 to -= System.currentTimeMillis() - st;
209 if (to <= 0)
210 throw new SocketTimeoutException();
211 }
212 } finally {
213 if (sc.isOpen())
214 sc.configureBlocking(true);
215 }
216
217 }
218 }
219 }
220
221 private InputStream socketInputStream = null;
222
223 public InputStream getInputStream() throws IOException {
224 if (!sc.isOpen())
225 throw new SocketException("Socket is closed");
226 if (!sc.isConnected())
227 throw new SocketException("Socket is not connected");
228 if (!sc.isInputOpen())
229 throw new SocketException("Socket input is shutdown");
230 if (socketInputStream == null) {
231 try {
232 socketInputStream = AccessController.doPrivileged(
|
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.nio.ch;
27
28 import java.io.*;
29 import java.lang.ref.*;
30 import java.net.*;
31 import java.nio.*;
32 import java.nio.channels.*;
33 import java.security.AccessController;
34 import java.security.PrivilegedExceptionAction;
35 import java.util.*;
36
37 import sun.misc.IoTrace;
38 import sun.misc.IoTraceContext;
39
40
41 // Make a socket channel look like a socket.
42 //
43 // The only aspects of java.net.Socket-hood that we don't attempt to emulate
44 // here are the interrupted-I/O exceptions (which our Solaris implementations
45 // attempt to support) and the sending of urgent data. Otherwise an adapted
46 // socket should look enough like a real java.net.Socket to fool most of the
47 // developers most of the time, right down to the exception message strings.
48 //
49 // The methods in this class are defined in exactly the same order as in
50 // java.net.Socket so as to simplify tracking future changes to that class.
51 //
52
53 public class SocketAdaptor
54 extends Socket
55 {
56
57 // The channel being adapted
58 private final SocketChannelImpl sc;
59
177 }
178 }
179
180 private class SocketInputStream
181 extends ChannelInputStream
182 {
183 private SocketInputStream() {
184 super(sc);
185 }
186
187 protected int read(ByteBuffer bb)
188 throws IOException
189 {
190 synchronized (sc.blockingLock()) {
191 if (!sc.isBlocking())
192 throw new IllegalBlockingModeException();
193 if (timeout == 0)
194 return sc.read(bb);
195 sc.configureBlocking(false);
196
197 int n = 0;
198 IoTraceContext traceContext = IoTrace.socketReadBegin(getInetAddress(), getPort(), timeout);
199 try {
200 if ((n = sc.read(bb)) != 0) {
201 return n;
202 }
203 long to = timeout;
204 for (;;) {
205 if (!sc.isOpen())
206 throw new ClosedChannelException();
207 long st = System.currentTimeMillis();
208 int result = sc.poll(PollArrayWrapper.POLLIN, to);
209 if (result > 0) {
210 if ((n = sc.read(bb)) != 0)
211 return n;
212 }
213 to -= System.currentTimeMillis() - st;
214 if (to <= 0)
215 throw new SocketTimeoutException();
216 }
217 } finally {
218 IoTrace.socketReadEnd(traceContext, n > 0 ? n : 0);
219 if (sc.isOpen())
220 sc.configureBlocking(true);
221 }
222
223 }
224 }
225 }
226
227 private InputStream socketInputStream = null;
228
229 public InputStream getInputStream() throws IOException {
230 if (!sc.isOpen())
231 throw new SocketException("Socket is closed");
232 if (!sc.isConnected())
233 throw new SocketException("Socket is not connected");
234 if (!sc.isInputOpen())
235 throw new SocketException("Socket input is shutdown");
236 if (socketInputStream == null) {
237 try {
238 socketInputStream = AccessController.doPrivileged(
|