diff options
author | jfrijters <jfrijters> | 2008-06-11 13:04:56 +0400 |
---|---|---|
committer | jfrijters <jfrijters> | 2008-06-11 13:04:56 +0400 |
commit | 9baab31e8c2ec69b4e098a164fb6ea6d0b8bb56c (patch) | |
tree | cb6ea0550c8653b7614e2d221e848180249e27b7 /openjdk/java/net | |
parent | db4720da166361f480586decec8581648c0b7ab0 (diff) |
Converted tabs to spaces.
Diffstat (limited to 'openjdk/java/net')
-rw-r--r-- | openjdk/java/net/PlainDatagramSocketImpl.java | 884 | ||||
-rw-r--r-- | openjdk/java/net/PlainSocketImpl.java | 1480 | ||||
-rw-r--r-- | openjdk/java/net/SocketInputStream.java | 204 | ||||
-rw-r--r-- | openjdk/java/net/SocketOutputStream.java | 80 |
4 files changed, 1324 insertions, 1324 deletions
diff --git a/openjdk/java/net/PlainDatagramSocketImpl.java b/openjdk/java/net/PlainDatagramSocketImpl.java index 8c7fbbaf..b9f29b36 100644 --- a/openjdk/java/net/PlainDatagramSocketImpl.java +++ b/openjdk/java/net/PlainDatagramSocketImpl.java @@ -93,9 +93,9 @@ class PlainDatagramSocketImpl extends DatagramSocketImpl * Creates a datagram socket */ protected synchronized void create() throws SocketException { - fd = new FileDescriptor(); - fd1 = new FileDescriptor(); - datagramSocketCreate(); + fd = new FileDescriptor(); + fd1 = new FileDescriptor(); + datagramSocketCreate(); } /** @@ -103,30 +103,30 @@ class PlainDatagramSocketImpl extends DatagramSocketImpl */ protected synchronized void bind(int lport, InetAddress laddr) throws SocketException { - - bind0(lport, laddr); - if (laddr.isAnyLocalAddress()) { - anyLocalBoundAddr = laddr; - } + + bind0(lport, laddr); + if (laddr.isAnyLocalAddress()) { + anyLocalBoundAddr = laddr; + } } protected synchronized void bind0(int lport, InetAddress laddr) throws SocketException { - try - { - if (false) throw new cli.System.Net.Sockets.SocketException(); - if (false) throw new cli.System.ObjectDisposedException(""); - netSocket.Bind(new IPEndPoint(PlainSocketImpl.getAddressFromInetAddress(laddr), lport)); - localPort = ((IPEndPoint)netSocket.get_LocalEndPoint()).get_Port(); - } - catch (cli.System.Net.Sockets.SocketException x) - { - throw new BindException(x.getMessage()); - } - catch (cli.System.ObjectDisposedException x1) - { - throw new SocketException("Socket is closed"); - } + try + { + if (false) throw new cli.System.Net.Sockets.SocketException(); + if (false) throw new cli.System.ObjectDisposedException(""); + netSocket.Bind(new IPEndPoint(PlainSocketImpl.getAddressFromInetAddress(laddr), lport)); + localPort = ((IPEndPoint)netSocket.get_LocalEndPoint()).get_Port(); + } + catch (cli.System.Net.Sockets.SocketException x) + { + throw new BindException(x.getMessage()); + } + catch (cli.System.ObjectDisposedException x1) + { + throw new SocketException("Socket is closed"); + } } /** @@ -136,29 +136,29 @@ class PlainDatagramSocketImpl extends DatagramSocketImpl */ protected void send(DatagramPacket p) throws IOException { - try - { - if (false) throw new cli.System.Net.Sockets.SocketException(); - if (false) throw new cli.System.ObjectDisposedException(""); - int len = p.getLength(); - int port = p.getPort(); - if (port < 1 || port > 65535) - { - throw new SocketException("Invalid port"); - } - if (netSocket.SendTo(p.getData(), p.getOffset(), len, SocketFlags.wrap(SocketFlags.None), new IPEndPoint(PlainSocketImpl.getAddressFromInetAddress(p.getAddress()), port)) != len) - { - throw new SocketException("Not all data was sent"); - } - } - catch (cli.System.Net.Sockets.SocketException x) - { - throw PlainSocketImpl.convertSocketExceptionToIOException(x); - } - catch (cli.System.ObjectDisposedException x1) - { - throw new SocketException("Socket is closed"); - } + try + { + if (false) throw new cli.System.Net.Sockets.SocketException(); + if (false) throw new cli.System.ObjectDisposedException(""); + int len = p.getLength(); + int port = p.getPort(); + if (port < 1 || port > 65535) + { + throw new SocketException("Invalid port"); + } + if (netSocket.SendTo(p.getData(), p.getOffset(), len, SocketFlags.wrap(SocketFlags.None), new IPEndPoint(PlainSocketImpl.getAddressFromInetAddress(p.getAddress()), port)) != len) + { + throw new SocketException("Not all data was sent"); + } + } + catch (cli.System.Net.Sockets.SocketException x) + { + throw PlainSocketImpl.convertSocketExceptionToIOException(x); + } + catch (cli.System.ObjectDisposedException x1) + { + throw new SocketException("Socket is closed"); + } } /** @@ -169,10 +169,10 @@ class PlainDatagramSocketImpl extends DatagramSocketImpl * @param port the remote port number */ protected void connect(InetAddress address, int port) throws SocketException { - connect0(address, port); - connectedAddress = address; - connectedPort = port; - connected = true; + connect0(address, port); + connectedAddress = address; + connectedPort = port; + connected = true; } /** @@ -180,10 +180,10 @@ class PlainDatagramSocketImpl extends DatagramSocketImpl * not connected already. */ protected void disconnect() { - disconnect0(connectedAddress.family); - connected = false; - connectedAddress = null; - connectedPort = -1; + disconnect0(connectedAddress.family); + connected = false; + connectedAddress = null; + connectedPort = -1; } /** @@ -192,17 +192,17 @@ class PlainDatagramSocketImpl extends DatagramSocketImpl */ protected synchronized int peek(InetAddress i) throws IOException { - DatagramPacket p = new DatagramPacket(new byte[1], 1); - receiveImpl(p, SocketFlags.Peek); - i.address = p.getAddress().address; - i.family = InetAddress.IPv4; - return p.getPort(); + DatagramPacket p = new DatagramPacket(new byte[1], 1); + receiveImpl(p, SocketFlags.Peek); + i.address = p.getAddress().address; + i.family = InetAddress.IPv4; + return p.getPort(); } protected synchronized int peekData(DatagramPacket p) throws IOException { - receiveImpl(p, SocketFlags.Peek); - return p.getPort(); + receiveImpl(p, SocketFlags.Peek); + return p.getPort(); } /** @@ -211,81 +211,81 @@ class PlainDatagramSocketImpl extends DatagramSocketImpl */ protected synchronized void receive(DatagramPacket p) throws IOException { - try { - receive0(p); - } finally { - fduse = -1; - } + try { + receive0(p); + } finally { + fduse = -1; + } } protected synchronized void receive0(DatagramPacket p) throws IOException { - receiveImpl(p, SocketFlags.None); + receiveImpl(p, SocketFlags.None); } private void receiveImpl(DatagramPacket p, int socketFlags) throws IOException { - cli.System.Net.EndPoint[] remoteEP = new cli.System.Net.EndPoint[] + cli.System.Net.EndPoint[] remoteEP = new cli.System.Net.EndPoint[] { new cli.System.Net.IPEndPoint(0, 0) }; - int length; - for (; ; ) - { - try - { - if (false) throw new cli.System.Net.Sockets.SocketException(); - if (false) throw new cli.System.ObjectDisposedException(""); - if (timeout > 0 && !netSocket.Poll(Math.min(timeout, Integer.MAX_VALUE / 1000) * 1000, - SelectMode.wrap(SelectMode.SelectRead))) - { - throw new SocketTimeoutException(); - } - length = netSocket.ReceiveFrom(p.buf, p.offset, p.bufLength, SocketFlags.wrap(socketFlags), remoteEP); - break; - } - catch (cli.System.Net.Sockets.SocketException x) - { - if (x.get_ErrorCode() == WSAECONNRESET) - { - // A previous send failed (i.e. the remote host responded with a ICMP that the port is closed) and - // the winsock stack helpfully lets us know this, but we don't care so we just retry the receive. - if ((socketFlags & SocketFlags.Peek) != 0) - { - // We did a peek, so we still need to remove the error result. - try - { - if (false) throw new cli.System.Net.Sockets.SocketException(); - if (false) throw new cli.System.ObjectDisposedException(""); - netSocket.ReceiveFrom(p.buf, 0, 0, SocketFlags.wrap(SocketFlags.None), remoteEP); - } - catch (cli.System.Net.Sockets.SocketException _) - { - } - catch (cli.System.ObjectDisposedException _) - { - } - } - continue; - } - if (x.get_ErrorCode() == WSAEMSGSIZE) - { - // The buffer size was too small for the packet, ReceiveFrom receives the part of the packet - // that fits in the buffer and then throws an exception, so we have to ignore the exception in this case. - length = p.bufLength; - break; - } - throw PlainSocketImpl.convertSocketExceptionToIOException(x); - } - catch (cli.System.ObjectDisposedException x1) - { - throw new SocketException("Socket is closed"); - } - } - IPEndPoint endpoint = (IPEndPoint)remoteEP[0]; - p.address = PlainSocketImpl.getInetAddressFromIPEndPoint(endpoint); - p.port = endpoint.get_Port(); - p.length = length; + int length; + for (; ; ) + { + try + { + if (false) throw new cli.System.Net.Sockets.SocketException(); + if (false) throw new cli.System.ObjectDisposedException(""); + if (timeout > 0 && !netSocket.Poll(Math.min(timeout, Integer.MAX_VALUE / 1000) * 1000, + SelectMode.wrap(SelectMode.SelectRead))) + { + throw new SocketTimeoutException(); + } + length = netSocket.ReceiveFrom(p.buf, p.offset, p.bufLength, SocketFlags.wrap(socketFlags), remoteEP); + break; + } + catch (cli.System.Net.Sockets.SocketException x) + { + if (x.get_ErrorCode() == WSAECONNRESET) + { + // A previous send failed (i.e. the remote host responded with a ICMP that the port is closed) and + // the winsock stack helpfully lets us know this, but we don't care so we just retry the receive. + if ((socketFlags & SocketFlags.Peek) != 0) + { + // We did a peek, so we still need to remove the error result. + try + { + if (false) throw new cli.System.Net.Sockets.SocketException(); + if (false) throw new cli.System.ObjectDisposedException(""); + netSocket.ReceiveFrom(p.buf, 0, 0, SocketFlags.wrap(SocketFlags.None), remoteEP); + } + catch (cli.System.Net.Sockets.SocketException _) + { + } + catch (cli.System.ObjectDisposedException _) + { + } + } + continue; + } + if (x.get_ErrorCode() == WSAEMSGSIZE) + { + // The buffer size was too small for the packet, ReceiveFrom receives the part of the packet + // that fits in the buffer and then throws an exception, so we have to ignore the exception in this case. + length = p.bufLength; + break; + } + throw PlainSocketImpl.convertSocketExceptionToIOException(x); + } + catch (cli.System.ObjectDisposedException x1) + { + throw new SocketException("Socket is closed"); + } + } + IPEndPoint endpoint = (IPEndPoint)remoteEP[0]; + p.address = PlainSocketImpl.getInetAddressFromIPEndPoint(endpoint); + p.port = endpoint.get_Port(); + p.length = length; } /** @@ -294,20 +294,20 @@ class PlainDatagramSocketImpl extends DatagramSocketImpl */ protected void setTimeToLive(int ttl) throws IOException { - try - { - if (false) throw new cli.System.Net.Sockets.SocketException(); - if (false) throw new cli.System.ObjectDisposedException(""); - netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.IpTimeToLive), ttl); - } - catch (cli.System.Net.Sockets.SocketException x) - { - throw new SocketException(x.getMessage()); - } - catch (cli.System.ObjectDisposedException x2) - { - throw new SocketException("Socket is closed"); - } + try + { + if (false) throw new cli.System.Net.Sockets.SocketException(); + if (false) throw new cli.System.ObjectDisposedException(""); + netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.IpTimeToLive), ttl); + } + catch (cli.System.Net.Sockets.SocketException x) + { + throw new SocketException(x.getMessage()); + } + catch (cli.System.ObjectDisposedException x2) + { + throw new SocketException("Socket is closed"); + } } /** @@ -315,20 +315,20 @@ class PlainDatagramSocketImpl extends DatagramSocketImpl */ protected int getTimeToLive() throws IOException { - try - { - if (false) throw new cli.System.Net.Sockets.SocketException(); - if (false) throw new cli.System.ObjectDisposedException(""); - return CIL.unbox_int(netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.IpTimeToLive))); - } - catch (cli.System.Net.Sockets.SocketException x) - { - throw new SocketException(x.getMessage()); - } - catch (cli.System.ObjectDisposedException x2) - { - throw new SocketException("Socket is closed"); - } + try + { + if (false) throw new cli.System.Net.Sockets.SocketException(); + if (false) throw new cli.System.ObjectDisposedException(""); + return CIL.unbox_int(netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.IpTimeToLive))); + } + catch (cli.System.Net.Sockets.SocketException x) + { + throw new SocketException(x.getMessage()); + } + catch (cli.System.ObjectDisposedException x2) + { + throw new SocketException("Socket is closed"); + } } /** @@ -337,7 +337,7 @@ class PlainDatagramSocketImpl extends DatagramSocketImpl */ protected void setTTL(byte ttl) throws IOException { - setTimeToLive(ttl & 0xFF); + setTimeToLive(ttl & 0xFF); } /** @@ -345,7 +345,7 @@ class PlainDatagramSocketImpl extends DatagramSocketImpl */ protected byte getTTL() throws IOException { - return (byte)getTimeToLive(); + return (byte)getTimeToLive(); } /** @@ -353,7 +353,7 @@ class PlainDatagramSocketImpl extends DatagramSocketImpl * @param multicast address to join. */ protected void join(InetAddress inetaddr) throws IOException { - join(inetaddr, null); + join(inetaddr, null); } /** @@ -361,7 +361,7 @@ class PlainDatagramSocketImpl extends DatagramSocketImpl * @param multicast address to leave. */ protected void leave(InetAddress inetaddr) throws IOException { - leave(inetaddr, null); + leave(inetaddr, null); } /** * Join the multicast group. @@ -374,47 +374,47 @@ class PlainDatagramSocketImpl extends DatagramSocketImpl */ protected void joinGroup(SocketAddress mcastaddr, NetworkInterface netIf) - throws IOException { - if (mcastaddr == null || !(mcastaddr instanceof InetSocketAddress)) - throw new IllegalArgumentException("Unsupported address type"); - join(((InetSocketAddress)mcastaddr).getAddress(), netIf); + throws IOException { + if (mcastaddr == null || !(mcastaddr instanceof InetSocketAddress)) + throw new IllegalArgumentException("Unsupported address type"); + join(((InetSocketAddress)mcastaddr).getAddress(), netIf); } private void join(InetAddress inetaddr, NetworkInterface netIf) throws IOException { - try - { - if (false) throw new cli.System.Net.Sockets.SocketException(); - if (false) throw new cli.System.ArgumentException(); - if (false) throw new cli.System.ObjectDisposedException(""); - IPAddress mcastAddr = PlainSocketImpl.getAddressFromInetAddress(inetaddr); - if (netIf == null) - { - netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.AddMembership), new MulticastOption(mcastAddr)); - } - else - { - Enumeration e = netIf.getInetAddresses(); - if (e.hasMoreElements()) - { - IPAddress bindAddr = PlainSocketImpl.getAddressFromInetAddress((InetAddress)e.nextElement()); - MulticastOption mcastOption = new MulticastOption(mcastAddr, bindAddr); - netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.AddMembership), mcastOption); - } - } - } - catch (cli.System.Net.Sockets.SocketException x) - { - throw PlainSocketImpl.convertSocketExceptionToIOException(x); - } - catch (cli.System.ArgumentException x1) - { - throw new IOException(x1.getMessage()); - } - catch (cli.System.ObjectDisposedException x2) - { - throw new SocketException("Socket is closed"); - } + try + { + if (false) throw new cli.System.Net.Sockets.SocketException(); + if (false) throw new cli.System.ArgumentException(); + if (false) throw new cli.System.ObjectDisposedException(""); + IPAddress mcastAddr = PlainSocketImpl.getAddressFromInetAddress(inetaddr); + if (netIf == null) + { + netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.AddMembership), new MulticastOption(mcastAddr)); + } + else + { + Enumeration e = netIf.getInetAddresses(); + if (e.hasMoreElements()) + { + IPAddress bindAddr = PlainSocketImpl.getAddressFromInetAddress((InetAddress)e.nextElement()); + MulticastOption mcastOption = new MulticastOption(mcastAddr, bindAddr); + netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.AddMembership), mcastOption); + } + } + } + catch (cli.System.Net.Sockets.SocketException x) + { + throw PlainSocketImpl.convertSocketExceptionToIOException(x); + } + catch (cli.System.ArgumentException x1) + { + throw new IOException(x1.getMessage()); + } + catch (cli.System.ObjectDisposedException x2) + { + throw new SocketException("Socket is closed"); + } } /** @@ -426,58 +426,58 @@ class PlainDatagramSocketImpl extends DatagramSocketImpl * @since 1.4 */ protected void leaveGroup(SocketAddress mcastaddr, NetworkInterface netIf) - throws IOException { - if (mcastaddr == null || !(mcastaddr instanceof InetSocketAddress)) - throw new IllegalArgumentException("Unsupported address type"); - leave(((InetSocketAddress)mcastaddr).getAddress(), netIf); + throws IOException { + if (mcastaddr == null || !(mcastaddr instanceof InetSocketAddress)) + throw new IllegalArgumentException("Unsupported address type"); + leave(((InetSocketAddress)mcastaddr).getAddress(), netIf); } private void leave(InetAddress inetaddr, NetworkInterface netIf) throws IOException { - try - { - if (false) throw new cli.System.Net.Sockets.SocketException(); - if (false) throw new cli.System.ArgumentException(); - if (false) throw new cli.System.ObjectDisposedException(""); - IPAddress mcastAddr = PlainSocketImpl.getAddressFromInetAddress(inetaddr); - if (netIf == null) - { - netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.DropMembership), new MulticastOption(mcastAddr)); - } - else - { - Enumeration e = netIf.getInetAddresses(); - if (e.hasMoreElements()) - { - IPAddress bindAddr = PlainSocketImpl.getAddressFromInetAddress((InetAddress)e.nextElement()); - MulticastOption mcastOption = new MulticastOption(mcastAddr, bindAddr); - netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.DropMembership), mcastOption); - } - } - } - catch (cli.System.Net.Sockets.SocketException x) - { - throw PlainSocketImpl.convertSocketExceptionToIOException(x); - } - catch (cli.System.ArgumentException x1) - { - throw new IOException(x1.getMessage()); - } - catch (cli.System.ObjectDisposedException x2) - { - throw new SocketException("Socket is closed"); - } + try + { + if (false) throw new cli.System.Net.Sockets.SocketException(); + if (false) throw new cli.System.ArgumentException(); + if (false) throw new cli.System.ObjectDisposedException(""); + IPAddress mcastAddr = PlainSocketImpl.getAddressFromInetAddress(inetaddr); + if (netIf == null) + { + netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.DropMembership), new MulticastOption(mcastAddr)); + } + else + { + Enumeration e = netIf.getInetAddresses(); + if (e.hasMoreElements()) + { + IPAddress bindAddr = PlainSocketImpl.getAddressFromInetAddress((InetAddress)e.nextElement()); + MulticastOption mcastOption = new MulticastOption(mcastAddr, bindAddr); + netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.DropMembership), mcastOption); + } + } + } + catch (cli.System.Net.Sockets.SocketException x) + { + throw PlainSocketImpl.convertSocketExceptionToIOException(x); + } + catch (cli.System.ArgumentException x1) + { + throw new IOException(x1.getMessage()); + } + catch (cli.System.ObjectDisposedException x2) + { + throw new SocketException("Socket is closed"); + } } /** * Close the socket. */ protected void close() { - if (fd != null || fd1 != null) { - datagramSocketClose(); - fd = null; - fd1 = null; - } + if (fd != null || fd1 != null) { + datagramSocketClose(); + fd = null; + fd1 = null; + } } /** @@ -489,62 +489,62 @@ class PlainDatagramSocketImpl extends DatagramSocketImpl if (fd == null && fd1 == null) { throw new SocketException("Socket Closed"); } - switch (optID) { - /* check type safety b4 going native. These should never - * fail, since only java.Socket* has access to - * PlainSocketImpl.setOption(). - */ - case SO_TIMEOUT: - if (o == null || !(o instanceof Integer)) { - throw new SocketException("bad argument for SO_TIMEOUT"); - } - int tmp = ((Integer) o).intValue(); - if (tmp < 0) - throw new IllegalArgumentException("timeout < 0"); - timeout = tmp; - return; - case IP_TOS: - if (o == null || !(o instanceof Integer)) { - throw new SocketException("bad argument for IP_TOS"); - } - trafficClass = ((Integer)o).intValue(); - break; - case SO_REUSEADDR: - if (o == null || !(o instanceof Boolean)) { - throw new SocketException("bad argument for SO_REUSEADDR"); - } - break; - case SO_BROADCAST: - if (o == null || !(o instanceof Boolean)) { - throw new SocketException("bad argument for SO_BROADCAST"); - } - break; - case SO_BINDADDR: - throw new SocketException("Cannot re-bind Socket"); - case SO_RCVBUF: - case SO_SNDBUF: - if (o == null || !(o instanceof Integer) || - ((Integer)o).intValue() < 0) { - throw new SocketException("bad argument for SO_SNDBUF or " + - "SO_RCVBUF"); - } - break; - case IP_MULTICAST_IF: - if (o == null || !(o instanceof InetAddress)) - throw new SocketException("bad argument for IP_MULTICAST_IF"); - break; - case IP_MULTICAST_IF2: - if (o == null || !(o instanceof NetworkInterface)) - throw new SocketException("bad argument for IP_MULTICAST_IF2"); - break; - case IP_MULTICAST_LOOP: - if (o == null || !(o instanceof Boolean)) - throw new SocketException("bad argument for IP_MULTICAST_LOOP"); - break; - default: - throw new SocketException("invalid option: " + optID); - } - socketSetOption(optID, o); + switch (optID) { + /* check type safety b4 going native. These should never + * fail, since only java.Socket* has access to + * PlainSocketImpl.setOption(). + */ + case SO_TIMEOUT: + if (o == null || !(o instanceof Integer)) { + throw new SocketException("bad argument for SO_TIMEOUT"); + } + int tmp = ((Integer) o).intValue(); + if (tmp < 0) + throw new IllegalArgumentException("timeout < 0"); + timeout = tmp; + return; + case IP_TOS: + if (o == null || !(o instanceof Integer)) { + throw new SocketException("bad argument for IP_TOS"); + } + trafficClass = ((Integer)o).intValue(); + break; + case SO_REUSEADDR: + if (o == null || !(o instanceof Boolean)) { + throw new SocketException("bad argument for SO_REUSEADDR"); + } + break; + case SO_BROADCAST: + if (o == null || !(o instanceof Boolean)) { + throw new SocketException("bad argument for SO_BROADCAST"); + } + break; + case SO_BINDADDR: + throw new SocketException("Cannot re-bind Socket"); + case SO_RCVBUF: + case SO_SNDBUF: + if (o == null || !(o instanceof Integer) || + ((Integer)o).intValue() < 0) { + throw new SocketException("bad argument for SO_SNDBUF or " + + "SO_RCVBUF"); + } + break; + case IP_MULTICAST_IF: + if (o == null || !(o instanceof InetAddress)) + throw new SocketException("bad argument for IP_MULTICAST_IF"); + break; + case IP_MULTICAST_IF2: + if (o == null || !(o instanceof NetworkInterface)) + throw new SocketException("bad argument for IP_MULTICAST_IF2"); + break; + case IP_MULTICAST_LOOP: + if (o == null || !(o instanceof Boolean)) + throw new SocketException("bad argument for IP_MULTICAST_LOOP"); + break; + default: + throw new SocketException("invalid option: " + optID); + } + socketSetOption(optID, o); } /* @@ -556,179 +556,179 @@ class PlainDatagramSocketImpl extends DatagramSocketImpl throw new SocketException("Socket Closed"); } - Object result; - - switch (optID) { - case SO_TIMEOUT: - result = new Integer(timeout); - break; - - case IP_TOS: - result = socketGetOption(optID); - if ( ((Integer)result).intValue() == -1) { - result = new Integer(trafficClass); - } - break; - - case SO_BINDADDR: - if (fd != null && fd1 != null) { - return anyLocalBoundAddr; - } - /* fall through */ - case IP_MULTICAST_IF: - case IP_MULTICAST_IF2: - case SO_RCVBUF: - case SO_SNDBUF: - case IP_MULTICAST_LOOP: - case SO_REUSEADDR: - case SO_BROADCAST: - result = socketGetOption(optID); - break; - - default: - throw new SocketException("invalid option: " + optID); - } - - return result; + Object result; + + switch (optID) { + case SO_TIMEOUT: + result = new Integer(timeout); + break; + + case IP_TOS: + result = socketGetOption(optID); + if ( ((Integer)result).intValue() == -1) { + result = new Integer(trafficClass); + } + break; + + case SO_BINDADDR: + if (fd != null && fd1 != null) { + return anyLocalBoundAddr; + } + /* fall through */ + case IP_MULTICAST_IF: + case IP_MULTICAST_IF2: + case SO_RCVBUF: + case SO_SNDBUF: + case IP_MULTICAST_LOOP: + case SO_REUSEADDR: + case SO_BROADCAST: + result = socketGetOption(optID); + break; + + default: + throw new SocketException("invalid option: " + optID); + } + + return result; } private void datagramSocketCreate() throws SocketException { - try - { - if (false) throw new cli.System.Net.Sockets.SocketException(); - netSocket = new cli.System.Net.Sockets.Socket( - AddressFamily.wrap(AddressFamily.InterNetwork), - SocketType.wrap(SocketType.Dgram), - ProtocolType.wrap(ProtocolType.Udp)); - netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.Broadcast), 1); - fd1 = null; - } - catch (cli.System.Net.Sockets.SocketException x) - { - throw new SocketException(x.getMessage()); - } + try + { + if (false) throw new cli.System.Net.Sockets.SocketException(); + netSocket = new cli.System.Net.Sockets.Socket( + AddressFamily.wrap(AddressFamily.InterNetwork), + SocketType.wrap(SocketType.Dgram), + ProtocolType.wrap(ProtocolType.Udp)); + netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.Broadcast), 1); + fd1 = null; + } + catch (cli.System.Net.Sockets.SocketException x) + { + throw new SocketException(x.getMessage()); + } } private void datagramSocketClose() { - netSocket.Close(); + netSocket.Close(); } private void socketSetOption(int opt, Object val) throws SocketException { - try - { - if (false) throw new cli.System.Net.Sockets.SocketException(); - if (false) throw new cli.System.ObjectDisposedException(""); - switch (opt) - { - case SocketOptions.SO_BROADCAST: - netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.Broadcast), ((Boolean)val).booleanValue() ? 1 : 0); - break; - case SocketOptions.IP_MULTICAST_IF: - { - InetAddress addr = (InetAddress)val; - netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.MulticastInterface), (int)PlainSocketImpl.getAddressFromInetAddress(addr).get_Address()); - break; - } - case SocketOptions.IP_MULTICAST_IF2: - { - NetworkInterface netIf = (NetworkInterface)val; - Enumeration e = netIf.getInetAddresses(); - while (e.hasMoreElements()) - { - InetAddress addr = (InetAddress)e.nextElement(); - if (addr.getAddress().length == 4) - { - netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.MulticastInterface), (int)PlainSocketImpl.getAddressFromInetAddress(addr).get_Address()); - return; - } - } - throw new SocketException("No IPv4 address found on interface"); - } - case SocketOptions.IP_MULTICAST_LOOP: - netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.MulticastLoopback), ((Boolean)val).booleanValue() ? 1 : 0); - break; - case SocketOptions.SO_REUSEADDR: - PlainSocketImpl.setCommonSocketOption(netSocket, opt, ((Boolean)val).booleanValue(), null); - break; - default: - PlainSocketImpl.setCommonSocketOption(netSocket, opt, false, val); - break; - } - } - catch (cli.System.Net.Sockets.SocketException x) - { - throw new SocketException(x.getMessage()); - } - catch (cli.System.ObjectDisposedException x1) - { - throw new SocketException("Socket is closed"); - } + try + { + if (false) throw new cli.System.Net.Sockets.SocketException(); + if (false) throw new cli.System.ObjectDisposedException(""); + switch (opt) + { + case SocketOptions.SO_BROADCAST: + netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.Broadcast), ((Boolean)val).booleanValue() ? 1 : 0); + break; + case SocketOptions.IP_MULTICAST_IF: + { + InetAddress addr = (InetAddress)val; + netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.MulticastInterface), (int)PlainSocketImpl.getAddressFromInetAddress(addr).get_Address()); + break; + } + case SocketOptions.IP_MULTICAST_IF2: + { + NetworkInterface netIf = (NetworkInterface)val; + Enumeration e = netIf.getInetAddresses(); + while (e.hasMoreElements()) + { + InetAddress addr = (InetAddress)e.nextElement(); + if (addr.getAddress().length == 4) + { + netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.MulticastInterface), (int)PlainSocketImpl.getAddressFromInetAddress(addr).get_Address()); + return; + } + } + throw new SocketException("No IPv4 address found on interface"); + } + case SocketOptions.IP_MULTICAST_LOOP: + netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.MulticastLoopback), ((Boolean)val).booleanValue() ? 1 : 0); + break; + case SocketOptions.SO_REUSEADDR: + PlainSocketImpl.setCommonSocketOption(netSocket, opt, ((Boolean)val).booleanValue(), null); + break; + default: + PlainSocketImpl.setCommonSocketOption(netSocket, opt, false, val); + break; + } + } + catch (cli.System.Net.Sockets.SocketException x) + { + throw new SocketException(x.getMessage()); + } + catch (cli.System.ObjectDisposedException x1) + { + throw new SocketException("Socket is closed"); + } } private static InetAddress getInetAddressFromInt(int addr) throws SocketException { - try - { - return InetAddress.getByAddress(cli.System.BitConverter.GetBytes(addr)); - } - catch (UnknownHostException x) - { - throw new SocketException(x.getMessage()); - } + try + { + return InetAddress.getByAddress(cli.System.BitConverter.GetBytes(addr)); + } + catch (UnknownHostException x) + { + throw new SocketException(x.getMessage()); + } } private Object socketGetOption(int opt) throws SocketException { - try - { - if (false) throw new cli.System.Net.Sockets.SocketException(); - if (false) throw new cli.System.ObjectDisposedException(""); - switch (opt) - { - case SocketOptions.SO_BROADCAST: - return CIL.unbox_int(netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.Broadcast))) != 0; - case SocketOptions.IP_MULTICAST_IF: - return getInetAddressFromInt(CIL.unbox_int(netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.MulticastInterface)))); - case SocketOptions.IP_MULTICAST_IF2: - { - NetworkInterface inf = NetworkInterface.getByInetAddress(getInetAddressFromInt(CIL.unbox_int(netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.MulticastInterface))))); - return inf != null ? inf : new NetworkInterface(null, -1, new InetAddress[] { new Inet4Address() }); - } - case SocketOptions.IP_MULTICAST_LOOP: - return CIL.unbox_int(netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.MulticastLoopback))) != 0; - case SocketOptions.SO_REUSEADDR: - return CIL.unbox_int(netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.ReuseAddress))) != 0; - case SocketOptions.SO_SNDBUF: - return CIL.unbox_int(netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.SendBuffer))); - case SocketOptions.SO_RCVBUF: - return CIL.unbox_int(netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.ReceiveBuffer))); - case SocketOptions.IP_TOS: - // TODO handle IPv6 here - return CIL.unbox_int(netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.TypeOfService))); - case SocketOptions.SO_BINDADDR: - return PlainSocketImpl.getInetAddressFromIPEndPoint((IPEndPoint)netSocket.get_LocalEndPoint()); - default: - throw new SocketException("Invalid socket option: " + opt); - } - } - catch (cli.System.Net.Sockets.SocketException x) - { - throw new SocketException(x.getMessage()); - } - catch (cli.System.ObjectDisposedException x1) - { - throw new SocketException("Socket is closed"); - } + try + { + if (false) throw new cli.System.Net.Sockets.SocketException(); + if (false) throw new cli.System.ObjectDisposedException(""); + switch (opt) + { + case SocketOptions.SO_BROADCAST: + return CIL.unbox_int(netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.Broadcast))) != 0; + case SocketOptions.IP_MULTICAST_IF: + return getInetAddressFromInt(CIL.unbox_int(netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.MulticastInterface)))); + case SocketOptions.IP_MULTICAST_IF2: + { + NetworkInterface inf = NetworkInterface.getByInetAddress(getInetAddressFromInt(CIL.unbox_int(netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.MulticastInterface))))); + return inf != null ? inf : new NetworkInterface(null, -1, new InetAddress[] { new Inet4Address() }); + } + case SocketOptions.IP_MULTICAST_LOOP: + return CIL.unbox_int(netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.MulticastLoopback))) != 0; + case SocketOptions.SO_REUSEADDR: + return CIL.unbox_int(netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.ReuseAddress))) != 0; + case SocketOptions.SO_SNDBUF: + return CIL.unbox_int(netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.SendBuffer))); + case SocketOptions.SO_RCVBUF: + return CIL.unbox_int(netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.ReceiveBuffer))); + case SocketOptions.IP_TOS: + // TODO handle IPv6 here + return CIL.unbox_int(netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.TypeOfService))); + case SocketOptions.SO_BINDADDR: + return PlainSocketImpl.getInetAddressFromIPEndPoint((IPEndPoint)netSocket.get_LocalEndPoint()); + default: + throw new SocketException("Invalid socket option: " + opt); + } + } + catch (cli.System.Net.Sockets.SocketException x) + { + throw new SocketException(x.getMessage()); + } + catch (cli.System.ObjectDisposedException x1) + { + throw new SocketException("Socket is closed"); + } } private void connect0(InetAddress address, int port) throws SocketException { - // If we throw here, DatagramSocket will fake connectedness for us. - // Once we're on .NET 2.0 we can use Socket.Connect/Disconnect. - throw new SocketException(); + // If we throw here, DatagramSocket will fake connectedness for us. + // Once we're on .NET 2.0 we can use Socket.Connect/Disconnect. + throw new SocketException(); } private void disconnect0(int family) @@ -738,6 +738,6 @@ class PlainDatagramSocketImpl extends DatagramSocketImpl // this is a workaround for a bug in java.net.DatagramSocket.receive(), see map.xml for details. static boolean equalsHack(InetAddress addr1, InetAddress addr2) { - return addr1.address == addr2.address; + return addr1.address == addr2.address; } } diff --git a/openjdk/java/net/PlainSocketImpl.java b/openjdk/java/net/PlainSocketImpl.java index 80b7ee7b..2c2c0975 100644 --- a/openjdk/java/net/PlainSocketImpl.java +++ b/openjdk/java/net/PlainSocketImpl.java @@ -69,120 +69,120 @@ public class PlainSocketImpl extends SocketImpl public static IOException convertSocketExceptionToIOException(cli.System.Net.Sockets.SocketException x) throws IOException { - switch (x.get_ErrorCode()) - { - case WSAEADDRINUSE: - return new BindException(x.getMessage()); - case WSAENETUNREACH: - case WSAEHOSTUNREACH: - return new NoRouteToHostException(x.getMessage()); - case WSAETIMEDOUT: - return new SocketTimeoutException(x.getMessage()); - case WSAECONNREFUSED: - return new PortUnreachableException(x.getMessage()); - case WSAHOST_NOT_FOUND: - return new UnknownHostException(x.getMessage()); - default: - return new SocketException(x.getMessage() + "\nError Code: " + x.get_ErrorCode()); - } + switch (x.get_ErrorCode()) + { + case WSAEADDRINUSE: + return new BindException(x.getMessage()); + case WSAENETUNREACH: + case WSAEHOSTUNREACH: + return new NoRouteToHostException(x.getMessage()); + case WSAETIMEDOUT: + return new SocketTimeoutException(x.getMessage()); + case WSAECONNREFUSED: + return new PortUnreachableException(x.getMessage()); + case WSAHOST_NOT_FOUND: + return new UnknownHostException(x.getMessage()); + default: + return new SocketException(x.getMessage() + "\nError Code: " + x.get_ErrorCode()); + } } public static IPAddress getAddressFromInetAddress(InetAddress addr) { - byte[] b = addr.getAddress(); - if (b.length == 16) - { - // FXBUG in .NET 1.1 you can only construct IPv6 addresses (not IPv4) with this constructor - // (according to the documentation this was fixed in .NET 2.0) - return new IPAddress(b); - } - else - { - return new IPAddress((((b[3] & 0xff) << 24) + ((b[2] & 0xff) << 16) + ((b[1] & 0xff) << 8) + (b[0] & 0xff)) & 0xffffffffL); - } + byte[] b = addr.getAddress(); + if (b.length == 16) + { + // FXBUG in .NET 1.1 you can only construct IPv6 addresses (not IPv4) with this constructor + // (according to the documentation this was fixed in .NET 2.0) + return new IPAddress(b); + } + else + { + return new IPAddress((((b[3] & 0xff) << 24) + ((b[2] & 0xff) << 16) + ((b[1] & 0xff) << 8) + (b[0] & 0xff)) & 0xffffffffL); + } } public static InetAddress getInetAddressFromIPEndPoint(IPEndPoint endpoint) { - try - { - return InetAddress.getByAddress(endpoint.get_Address().GetAddressBytes()); - } - catch (UnknownHostException x) - { - // this exception only happens if the address byte array is of invalid length, which cannot happen unless - // the .NET socket returns a bogus address - throw (InternalError)new InternalError().initCause(x); - } + try + { + return InetAddress.getByAddress(endpoint.get_Address().GetAddressBytes()); + } + catch (UnknownHostException x) + { + // this exception only happens if the address byte array is of invalid length, which cannot happen unless + // the .NET socket returns a bogus address + throw (InternalError)new InternalError().initCause(x); + } } static void setCommonSocketOption(cli.System.Net.Sockets.Socket netSocket, int cmd, boolean on, Object value) throws SocketException { - try - { - if (false) throw new cli.System.Net.Sockets.SocketException(); - if (false) throw new cli.System.ObjectDisposedException(""); - switch (cmd) - { - case SocketOptions.SO_REUSEADDR: - netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.ReuseAddress), on ? 1 : 0); - break; - case SocketOptions.SO_SNDBUF: - netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.SendBuffer), ((Integer)value).intValue()); - break; - case SocketOptions.SO_RCVBUF: - netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.ReceiveBuffer), ((Integer)value).intValue()); - break; - case SocketOptions.IP_TOS: - netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.TypeOfService), ((Integer)value).intValue()); - break; - case SocketOptions.SO_BINDADDR: // read-only - default: - throw new SocketException("Invalid socket option: " + cmd); - } - } - catch (cli.System.Net.Sockets.SocketException x) - { - throw new SocketException(x.getMessage()); - } - catch (cli.System.ObjectDisposedException x1) - { - throw new SocketException("Socket is closed"); - } + try + { + if (false) throw new cli.System.Net.Sockets.SocketException(); + if (false) throw new cli.System.ObjectDisposedException(""); + switch (cmd) + { + case SocketOptions.SO_REUSEADDR: + netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.ReuseAddress), on ? 1 : 0); + break; + case SocketOptions.SO_SNDBUF: + netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.SendBuffer), ((Integer)value).intValue()); + break; + case SocketOptions.SO_RCVBUF: + netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.ReceiveBuffer), ((Integer)value).intValue()); + break; + case SocketOptions.IP_TOS: + netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.TypeOfService), ((Integer)value).intValue()); + break; + case SocketOptions.SO_BINDADDR: // read-only + default: + throw new SocketException("Invalid socket option: " + cmd); + } + } + catch (cli.System.Net.Sockets.SocketException x) + { + throw new SocketException(x.getMessage()); + } + catch (cli.System.ObjectDisposedException x1) + { + throw new SocketException("Socket is closed"); + } } static int getCommonSocketOption(cli.System.Net.Sockets.Socket netSocket, int opt, Object iaContainerObj) throws SocketException { - try - { - if (false) throw new cli.System.Net.Sockets.SocketException(); - if (false) throw new cli.System.ObjectDisposedException(""); - switch (opt) - { - case SocketOptions.SO_REUSEADDR: - return CIL.unbox_int(netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.ReuseAddress))) == 0 ? -1 : 1; - case SocketOptions.SO_SNDBUF: - return CIL.unbox_int(netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.SendBuffer))); - case SocketOptions.SO_RCVBUF: - return CIL.unbox_int(netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.ReceiveBuffer))); - case SocketOptions.IP_TOS: - // TODO handle IPv6 here - return CIL.unbox_int(netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.TypeOfService))); - case SocketOptions.SO_BINDADDR: - ((InetAddressContainer)iaContainerObj).addr = getInetAddressFromIPEndPoint((IPEndPoint)netSocket.get_LocalEndPoint()); - return 0; - default: - throw new SocketException("Invalid socket option: " + opt); - } - } - catch (cli.System.Net.Sockets.SocketException x) - { - throw new SocketException(x.getMessage()); - } - catch (cli.System.ObjectDisposedException x1) - { - throw new SocketException("Socket is closed"); - } + try + { + if (false) throw new cli.System.Net.Sockets.SocketException(); + if (false) throw new cli.System.ObjectDisposedException(""); + switch (opt) + { + case SocketOptions.SO_REUSEADDR: + return CIL.unbox_int(netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.ReuseAddress))) == 0 ? -1 : 1; + case SocketOptions.SO_SNDBUF: + return CIL.unbox_int(netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.SendBuffer))); + case SocketOptions.SO_RCVBUF: + return CIL.unbox_int(netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.ReceiveBuffer))); + case SocketOptions.IP_TOS: + // TODO handle IPv6 here + return CIL.unbox_int(netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.IP), SocketOptionName.wrap(SocketOptionName.TypeOfService))); + case SocketOptions.SO_BINDADDR: + ((InetAddressContainer)iaContainerObj).addr = getInetAddressFromIPEndPoint((IPEndPoint)netSocket.get_LocalEndPoint()); + return 0; + default: + throw new SocketException("Invalid socket option: " + opt); + } + } + catch (cli.System.Net.Sockets.SocketException x) + { + throw new SocketException(x.getMessage()); + } + catch (cli.System.ObjectDisposedException x1) + { + throw new SocketException("Socket is closed"); + } } private cli.System.Net.Sockets.Socket netSocket; @@ -245,13 +245,13 @@ public class PlainSocketImpl extends SocketImpl * is a stream socket (true) or an unconnected UDP socket (false). */ protected synchronized void create(boolean stream) throws IOException { - fd = new FileDescriptor(); - fd1 = new FileDescriptor(); - socketCreate(stream); - if (socket != null) - socket.setCreated(); - if (serverSocket != null) - serverSocket.setCreated(); + fd = new FileDescriptor(); + fd1 = new FileDescriptor(); + socketCreate(stream); + if (socket != null) + socket.setCreated(); + if (serverSocket != null) + serverSocket.setCreated(); } /** @@ -263,23 +263,23 @@ public class PlainSocketImpl extends SocketImpl protected void connect(String host, int port) throws UnknownHostException, IOException { - IOException pending = null; - try { - InetAddress address = InetAddress.getByName(host); - - try { - connectToAddress(address, port, timeout); - return; - } catch (IOException e) { - pending = e; - } - } catch (UnknownHostException e) { - pending = e; - } - - // everything failed - close(); - throw pending; + IOException pending = null; + try { + InetAddress address = InetAddress.getByName(host); + + try { + connectToAddress(address, port, timeout); + return; + } catch (IOException e) { + pending = e; + } + } catch (UnknownHostException e) { + pending = e; + } + + // everything failed + close(); + throw pending; } /** @@ -289,17 +289,17 @@ public class PlainSocketImpl extends SocketImpl * @param port the specified port */ protected void connect(InetAddress address, int port) throws IOException { - this.port = port; - this.address = address; - - try { - connectToAddress(address, port, timeout); - return; - } catch (IOException e) { - // everything failed - close(); - throw e; - } + this.port = port; + this.address = address; + + try { + connectToAddress(address, port, timeout); + return; + } catch (IOException e) { + // everything failed + close(); + throw e; + } } /** @@ -313,154 +313,154 @@ public class PlainSocketImpl extends SocketImpl * @since 1.4 */ protected void connect(SocketAddress address, int timeout) throws IOException { - if (address == null || !(address instanceof InetSocketAddress)) - throw new IllegalArgumentException("unsupported address type"); - InetSocketAddress addr = (InetSocketAddress) address; - if (addr.isUnresolved()) - throw new UnknownHostException(addr.getHostName()); - this.port = addr.getPort(); - this.address = addr.getAddress(); - - try { - connectToAddress(this.address, port, timeout); - return; - } catch (IOException e) { - // everything failed - close(); - throw e; - } + if (address == null || !(address instanceof InetSocketAddress)) + throw new IllegalArgumentException("unsupported address type"); + InetSocketAddress addr = (InetSocketAddress) address; + if (addr.isUnresolved()) + throw new UnknownHostException(addr.getHostName()); + this.port = addr.getPort(); + this.address = addr.getAddress(); + + try { + connectToAddress(this.address, port, timeout); + return; + } catch (IOException e) { + // everything failed + close(); + throw e; + } } private void connectToAddress(InetAddress address, int port, int timeout) throws IOException { - if (address.isAnyLocalAddress()) { - doConnect(InetAddress.getLocalHost(), port, timeout); - } else { - doConnect(address, port, timeout); - } + if (address.isAnyLocalAddress()) { + doConnect(InetAddress.getLocalHost(), port, timeout); + } else { + doConnect(address, port, timeout); + } } public void setOption(int opt, Object val) throws SocketException { - if (isClosedOrPending()) { - throw new SocketException("Socket Closed"); - } - boolean on = true; - switch (opt) { - /* check type safety b4 going native. These should never - * fail, since only java.Socket* has access to - * PlainSocketImpl.setOption(). - */ - case SO_LINGER: - if (val == null || (!(val instanceof Integer) && !(val instanceof Boolean))) - throw new SocketException("Bad parameter for option"); - if (val instanceof Boolean) { - /* true only if disabling - enabling should be Integer */ - on = false; - } - break; - case SO_TIMEOUT: - if (val == null || (!(val instanceof Integer))) - throw new SocketException("Bad parameter for SO_TIMEOUT"); - int tmp = ((Integer) val).intValue(); - if (tmp < 0) - throw new IllegalArgumentException("timeout < 0"); - timeout = tmp; - break; - case IP_TOS: - if (val == null || !(val instanceof Integer)) { - throw new SocketException("bad argument for IP_TOS"); - } - trafficClass = ((Integer)val).intValue(); - break; - case SO_BINDADDR: - throw new SocketException("Cannot re-bind socket"); - case TCP_NODELAY: - if (val == null || !(val instanceof Boolean)) - throw new SocketException("bad parameter for TCP_NODELAY"); - on = ((Boolean)val).booleanValue(); - break; - case SO_SNDBUF: - case SO_RCVBUF: - if (val == null || !(val instanceof Integer) || - !(((Integer)val).intValue() > 0)) { - throw new SocketException("bad parameter for SO_SNDBUF " + - "or SO_RCVBUF"); - } - break; - case SO_KEEPALIVE: - if (val == null || !(val instanceof Boolean)) - throw new SocketException("bad parameter for SO_KEEPALIVE"); - on = ((Boolean)val).booleanValue(); - break; - case SO_OOBINLINE: - if (val == null || !(val instanceof Boolean)) - throw new SocketException("bad parameter for SO_OOBINLINE"); - on = ((Boolean)val).booleanValue(); - break; - case SO_REUSEADDR: - if (val == null || !(val instanceof Boolean)) - throw new SocketException("bad parameter for SO_REUSEADDR"); - on = ((Boolean)val).booleanValue(); - break; - default: - throw new SocketException("unrecognized TCP option: " + opt); - } - socketSetOption(opt, on, val); + if (isClosedOrPending()) { + throw new SocketException("Socket Closed"); + } + boolean on = true; + switch (opt) { + /* check type safety b4 going native. These should never + * fail, since only java.Socket* has access to + * PlainSocketImpl.setOption(). + */ + case SO_LINGER: + if (val == null || (!(val instanceof Integer) && !(val instanceof Boolean))) + throw new SocketException("Bad parameter for option"); + if (val instanceof Boolean) { + /* true only if disabling - enabling should be Integer */ + on = false; + } + break; + case SO_TIMEOUT: + if (val == null || (!(val instanceof Integer))) + throw new SocketException("Bad parameter for SO_TIMEOUT"); + int tmp = ((Integer) val).intValue(); + if (tmp < 0) + throw new IllegalArgumentException("timeout < 0"); + timeout = tmp; + break; + case IP_TOS: + if (val == null || !(val instanceof Integer)) { + throw new SocketException("bad argument for IP_TOS"); + } + trafficClass = ((Integer)val).intValue(); + break; + case SO_BINDADDR: + throw new SocketException("Cannot re-bind socket"); + case TCP_NODELAY: + if (val == null || !(val instanceof Boolean)) + throw new SocketException("bad parameter for TCP_NODELAY"); + on = ((Boolean)val).booleanValue(); + break; + case SO_SNDBUF: + case SO_RCVBUF: + if (val == null || !(val instanceof Integer) || + !(((Integer)val).intValue() > 0)) { + throw new SocketException("bad parameter for SO_SNDBUF " + + "or SO_RCVBUF"); + } + break; + case SO_KEEPALIVE: + if (val == null || !(val instanceof Boolean)) + throw new SocketException("bad parameter for SO_KEEPALIVE"); + on = ((Boolean)val).booleanValue(); + break; + case SO_OOBINLINE: + if (val == null || !(val instanceof Boolean)) + throw new SocketException("bad parameter for SO_OOBINLINE"); + on = ((Boolean)val).booleanValue(); + break; + case SO_REUSEADDR: + if (val == null || !(val instanceof Boolean)) + throw new SocketException("bad parameter for SO_REUSEADDR"); + on = ((Boolean)val).booleanValue(); + break; + default: + throw new SocketException("unrecognized TCP option: " + opt); + } + socketSetOption(opt, on, val); } public Object getOption(int opt) throws SocketException { - if (isClosedOrPending()) { - throw new SocketException("Socket Closed"); - } - if (opt == SO_TIMEOUT) { - return new Integer(timeout); - } - int ret = 0; - /* - * The native socketGetOption() knows about 3 options. - * The 32 bit value it returns will be interpreted according - * to what we're asking. A return of -1 means it understands - * the option but its turned off. It will raise a SocketException - * if "opt" isn't one it understands. - */ - - switch (opt) { - case TCP_NODELAY: - ret = socketGetOption(opt, null); - return Boolean.valueOf(ret != -1); - case SO_OOBINLINE: - ret = socketGetOption(opt, null); - return Boolean.valueOf(ret != -1); - case SO_LINGER: - ret = socketGetOption(opt, null); - return (ret == -1) ? Boolean.FALSE: (Object)(new Integer(ret)); - case SO_REUSEADDR: - ret = socketGetOption(opt, null); - return Boolean.valueOf(ret != -1); - case SO_BINDADDR: - if (fd != null && fd1 != null ) { - /* must be unbound or else bound to anyLocal */ - return anyLocalBoundAddr; - } - InetAddressContainer in = new InetAddressContainer(); - ret = socketGetOption(opt, in); - return in.addr; - case SO_SNDBUF: + if (isClosedOrPending()) { + throw new SocketException("Socket Closed"); + } + if (opt == SO_TIMEOUT) { + return new Integer(timeout); + } + int ret = 0; + /* + * The native socketGetOption() knows about 3 options. + * The 32 bit value it returns will be interpreted according + * to what we're asking. A return of -1 means it understands + * the option but its turned off. It will raise a SocketException + * if "opt" isn't one it understands. + */ + + switch (opt) { + case TCP_NODELAY: + ret = socketGetOption(opt, null); + return Boolean.valueOf(ret != -1); + case SO_OOBINLINE: + ret = socketGetOption(opt, null); + return Boolean.valueOf(ret != -1); + case SO_LINGER: + ret = socketGetOption(opt, null); + return (ret == -1) ? Boolean.FALSE: (Object)(new Integer(ret)); + case SO_REUSEADDR: + ret = socketGetOption(opt, null); + return Boolean.valueOf(ret != -1); + case SO_BINDADDR: + if (fd != null && fd1 != null ) { + /* must be unbound or else bound to anyLocal */ + return anyLocalBoundAddr; + } + InetAddressContainer in = new InetAddressContainer(); + ret = socketGetOption(opt, in); + return in.addr; + case SO_SNDBUF: case SO_RCVBUF: - ret = socketGetOption(opt, null); - return new Integer(ret); - case IP_TOS: - ret = socketGetOption(opt, null); - if (ret == -1) { // ipv6 tos - return new Integer(trafficClass); - } else { - return new Integer(ret); - } - case SO_KEEPALIVE: - ret = socketGetOption(opt, null); - return Boolean.valueOf(ret != -1); - // should never get here - default: - return null; - } + ret = socketGetOption(opt, null); + return new Integer(ret); + case IP_TOS: + ret = socketGetOption(opt, null); + if (ret == -1) { // ipv6 tos + return new Integer(trafficClass); + } else { + return new Integer(ret); + } + case SO_KEEPALIVE: + ret = socketGetOption(opt, null); + return Boolean.valueOf(ret != -1); + // should never get here + default: + return null; + } } /** @@ -471,24 +471,24 @@ public class PlainSocketImpl extends SocketImpl private synchronized void doConnect(InetAddress address, int port, int timeout) throws IOException { try { - FileDescriptor fd = acquireFD(); - try { - socketConnect(address, port, timeout); - // If we have a ref. to the Socket, then sets the flags - // created, bound & connected to true. - // This is normally done in Socket.connect() but some - // subclasses of Socket may call impl.connect() directly! - if (socket != null) { - socket.setBound(); - socket.setConnected(); - } - } finally { - releaseFD(); - } - } catch (IOException e) { - close(); - throw e; - } + FileDescriptor fd = acquireFD(); + try { + socketConnect(address, port, timeout); + // If we have a ref. to the Socket, then sets the flags + // created, bound & connected to true. + // This is normally done in Socket.connect() but some + // subclasses of Socket may call impl.connect() directly! + if (socket != null) { + socket.setBound(); + socket.setConnected(); + } + } finally { + releaseFD(); + } + } catch (IOException e) { + close(); + throw e; + } } /** @@ -497,16 +497,16 @@ public class PlainSocketImpl extends SocketImpl * @param port the port */ protected synchronized void bind(InetAddress address, int lport) - throws IOException + throws IOException { - socketBind(address, lport); - if (socket != null) - socket.setBound(); - if (serverSocket != null) - serverSocket.setBound(); - if (address.isAnyLocalAddress()) { - anyLocalBoundAddr = address; - } + socketBind(address, lport); + if (socket != null) + socket.setBound(); + if (serverSocket != null) + serverSocket.setBound(); + if (address.isAnyLocalAddress()) { + anyLocalBoundAddr = address; + } } /** @@ -514,7 +514,7 @@ public class PlainSocketImpl extends SocketImpl * @param count the amount of time to listen for connections */ protected synchronized void listen(int count) throws IOException { - socketListen(count); + socketListen(count); } /** @@ -522,131 +522,131 @@ public class PlainSocketImpl extends SocketImpl * @param s the connection */ protected synchronized void accept(SocketImpl s) throws IOException { - FileDescriptor fd = acquireFD(); - try { - socketAccept(s); - } finally { - releaseFD(); - } + FileDescriptor fd = acquireFD(); + try { + socketAccept(s); + } finally { + releaseFD(); + } } /** * Gets an InputStream for this socket. */ protected synchronized InputStream getInputStream() throws IOException { - if (isClosedOrPending()) { - throw new IOException("Socket Closed"); - } - if (shut_rd) { - throw new IOException("Socket input is shutdown"); - } - if (socketInputStream == null) { - socketInputStream = new SocketInputStream(this); - } - return socketInputStream; + if (isClosedOrPending()) { + throw new IOException("Socket Closed"); + } + if (shut_rd) { + throw new IOException("Socket input is shutdown"); + } + if (socketInputStream == null) { + socketInputStream = new SocketInputStream(this); + } + return socketInputStream; } void setInputStream(SocketInputStream in) { - socketInputStream = in; + socketInputStream = in; } /** * Gets an OutputStream for this socket. */ protected synchronized OutputStream getOutputStream() throws IOException { - if (isClosedOrPending()) { - throw new IOException("Socket Closed"); - } + if (isClosedOrPending()) { + throw new IOException("Socket Closed"); + } if (shut_wr) { - throw new IOException("Socket output is shutdown"); - } - return new SocketOutputStream(this); + throw new IOException("Socket output is shutdown"); + } + return new SocketOutputStream(this); } /** * Returns the number of bytes that can be read without blocking. */ protected synchronized int available() throws IOException { - if (isClosedOrPending()) { + if (isClosedOrPending()) { throw new IOException("Stream closed."); - } - - /* - * If connection has been reset then return 0 to indicate - * there are no buffered bytes. - */ - if (isConnectionReset()) { - return 0; - } - - /* - * If no bytes available and we were previously notified - * of a connection reset then we move to the reset state. - * - * If are notified of a connection reset then check - * again if there are bytes buffered on the socket. - */ - int n = 0; - try { - n = socketAvailable(); - if (n == 0 && isConnectionResetPending()) { - setConnectionReset(); - } - } catch (ConnectionResetException exc1) { - setConnectionResetPending(); - try { - n = socketAvailable(); - if (n == 0) { - setConnectionReset(); - } - } catch (ConnectionResetException exc2) { - } - } - return n; + } + + /* + * If connection has been reset then return 0 to indicate + * there are no buffered bytes. + */ + if (isConnectionReset()) { + return 0; + } + + /* + * If no bytes available and we were previously notified + * of a connection reset then we move to the reset state. + * + * If are notified of a connection reset then check + * again if there are bytes buffered on the socket. + */ + int n = 0; + try { + n = socketAvailable(); + if (n == 0 && isConnectionResetPending()) { + setConnectionReset(); + } + } catch (ConnectionResetException exc1) { + setConnectionResetPending(); + try { + n = socketAvailable(); + if (n == 0) { + setConnectionReset(); + } + } catch (ConnectionResetException exc2) { + } + } + return n; } /** * Closes the socket. */ protected void close() throws IOException { - synchronized(fdLock) { - if (fd != null || fd1 != null) { - if (fdUseCount == 0) { - if (closePending) { - return; - } - closePending = true; - /* - * We close the FileDescriptor in two-steps - first the - * "pre-close" which closes the socket but doesn't - * release the underlying file descriptor. This operation - * may be lengthy due to untransmitted data and a long - * linger interval. Once the pre-close is done we do the - * actual socket to release the fd. - */ - try { - socketPreClose(); - } finally { - socketClose(); - } - fd = null; - fd1 = null; - return; - } else { - /* - * If a thread has acquired the fd and a close - * isn't pending then use a deferred close. - * Also decrement fdUseCount to signal the last - * thread that releases the fd to close it. - */ - if (!closePending) { - closePending = true; - fdUseCount--; - socketPreClose(); - } - } - } - } + synchronized(fdLock) { + if (fd != null || fd1 != null) { + if (fdUseCount == 0) { + if (closePending) { + return; + } + closePending = true; + /* + * We close the FileDescriptor in two-steps - first the + * "pre-close" which closes the socket but doesn't + * release the underlying file descriptor. This operation + * may be lengthy due to untransmitted data and a long + * linger interval. Once the pre-close is done we do the + * actual socket to release the fd. + */ + try { + socketPreClose(); + } finally { + socketClose(); + } + fd = null; + fd1 = null; + return; + } else { + /* + * If a thread has acquired the fd and a close + * isn't pending then use a deferred close. + * Also decrement fdUseCount to signal the last + * thread that releases the fd to close it. + */ + if (!closePending) { + closePending = true; + fdUseCount--; + socketPreClose(); + } + } + } + } } void reset() throws IOException { @@ -664,11 +664,11 @@ public class PlainSocketImpl extends SocketImpl */ protected void shutdownInput() throws IOException { if (fd != null) { - socketShutdown(SHUT_RD); - if (socketInputStream != null) { - socketInputStream.setEOF(true); - } - shut_rd = true; + socketShutdown(SHUT_RD); + if (socketInputStream != null) { + socketInputStream.setEOF(true); + } + shut_rd = true; } } @@ -677,8 +677,8 @@ public class PlainSocketImpl extends SocketImpl */ protected void shutdownOutput() throws IOException { if (fd != null) { - socketShutdown(SHUT_WR); - shut_wr = true; + socketShutdown(SHUT_WR); + shut_wr = true; } } @@ -700,10 +700,10 @@ public class PlainSocketImpl extends SocketImpl * FileDescriptor. */ public final FileDescriptor acquireFD() { - synchronized (fdLock) { - fdUseCount++; - return fd; - } + synchronized (fdLock) { + fdUseCount++; + return fd; + } } /* @@ -712,41 +712,41 @@ public class PlainSocketImpl extends SocketImpl * If the use count goes to -1 then the socket is closed. */ public final void releaseFD() { - synchronized (fdLock) { - fdUseCount--; - if (fdUseCount == -1) { - if (fd != null) { - try { - socketClose(); - } catch (IOException e) { - } finally { - fd = null; - } - } - } - } + synchronized (fdLock) { + fdUseCount--; + if (fdUseCount == -1) { + if (fd != null) { + try { + socketClose(); + } catch (IOException e) { + } finally { + fd = null; + } + } + } + } } public boolean isConnectionReset() { - synchronized (resetLock) { - return (resetState == CONNECTION_RESET); - } + synchronized (resetLock) { + return (resetState == CONNECTION_RESET); + } } public boolean isConnectionResetPending() { - synchronized (resetLock) { + synchronized (resetLock) { return (resetState == CONNECTION_RESET_PENDING); } } public void setConnectionReset() { - synchronized (resetLock) { + synchronized (resetLock) { resetState = CONNECTION_RESET; } } public void setConnectionResetPending() { - synchronized (resetLock) { + synchronized (resetLock) { if (resetState == CONNECTION_NOT_RESET) { resetState = CONNECTION_RESET_PENDING; } @@ -758,24 +758,24 @@ public class PlainSocketImpl extends SocketImpl * Return true if already closed or close is pending */ public boolean isClosedOrPending() { - /* - * Lock on fdLock to ensure that we wait if a - * close is in progress. - */ - synchronized (fdLock) { - if (closePending || (fd == null && fd1 == null)) { - return true; - } else { - return false; - } - } + /* + * Lock on fdLock to ensure that we wait if a + * close is in progress. + */ + synchronized (fdLock) { + if (closePending || (fd == null && fd1 == null)) { + return true; + } else { + return false; + } + } } /* * Return the current value of SO_TIMEOUT */ public int getTimeout() { - return timeout; + return timeout; } /* @@ -783,368 +783,368 @@ public class PlainSocketImpl extends SocketImpl * the socket to be closed without releasing the file descriptor. */ private void socketPreClose() throws IOException { - socketClose0(true); + socketClose0(true); } /* * Close the socket (and release the file descriptor). */ private void socketClose() throws IOException { - socketClose0(false); + socketClose0(false); } private void socketCreate(boolean stream) throws IOException { - try - { - if (false) throw new cli.System.Net.Sockets.SocketException(); - if (false) throw new cli.System.ObjectDisposedException(""); - if (stream) - { - netSocket = new cli.System.Net.Sockets.Socket(AddressFamily.wrap(AddressFamily.InterNetwork), SocketType.wrap(SocketType.Stream), ProtocolType.wrap(ProtocolType.Tcp)); - } - else - { - netSocket = new cli.System.Net.Sockets.Socket(AddressFamily.wrap(AddressFamily.InterNetwork), SocketType.wrap(SocketType.Dgram), ProtocolType.wrap(ProtocolType.Udp)); - } - fd1 = null; - } - catch (cli.System.Net.Sockets.SocketException x) - { - throw convertSocketExceptionToIOException(x); - } - catch (cli.System.ObjectDisposedException x1) - { - throw new SocketException("Socket is closed"); - } + try + { + if (false) throw new cli.System.Net.Sockets.SocketException(); + if (false) throw new cli.System.ObjectDisposedException(""); + if (stream) + { + netSocket = new cli.System.Net.Sockets.Socket(AddressFamily.wrap(AddressFamily.InterNetwork), SocketType.wrap(SocketType.Stream), ProtocolType.wrap(ProtocolType.Tcp)); + } + else + { + netSocket = new cli.System.Net.Sockets.Socket(AddressFamily.wrap(AddressFamily.InterNetwork), SocketType.wrap(SocketType.Dgram), ProtocolType.wrap(ProtocolType.Udp)); + } + fd1 = null; + } + catch (cli.System.Net.Sockets.SocketException x) + { + throw convertSocketExceptionToIOException(x); + } + catch (cli.System.ObjectDisposedException x1) + { + throw new SocketException("Socket is closed"); + } } private void socketConnect(InetAddress address, int port, int timeout) throws IOException { - try - { - if (false) throw new cli.System.Net.Sockets.SocketException(); - if (false) throw new cli.System.ObjectDisposedException(""); - IPEndPoint ep = new IPEndPoint(getAddressFromInetAddress(address), port); - if (timeout <= 0) - { - netSocket.Connect(ep); - } - else - { - cli.System.IAsyncResult result = netSocket.BeginConnect(ep, null, null); - if (!result.get_AsyncWaitHandle().WaitOne(timeout, false)) - { - netSocket.Close(); - throw new SocketTimeoutException(); - } - netSocket.EndConnect(result); - } - this.address = address; - this.port = port; - if (this.localport == 0) - { - this.localport = ((IPEndPoint)netSocket.get_LocalEndPoint()).get_Port(); - } - } - catch (cli.System.Net.Sockets.SocketException x) - { - throw new ConnectException(x.getMessage()); - } - catch (cli.System.ObjectDisposedException x1) - { - throw new SocketException("Socket is closed"); - } + try + { + if (false) throw new cli.System.Net.Sockets.SocketException(); + if (false) throw new cli.System.ObjectDisposedException(""); + IPEndPoint ep = new IPEndPoint(getAddressFromInetAddress(address), port); + if (timeout <= 0) + { + netSocket.Connect(ep); + } + else + { + cli.System.IAsyncResult result = netSocket.BeginConnect(ep, null, null); + if (!result.get_AsyncWaitHandle().WaitOne(timeout, false)) + { + netSocket.Close(); + throw new SocketTimeoutException(); + } + netSocket.EndConnect(result); + } + this.address = address; + this.port = port; + if (this.localport == 0) + { + this.localport = ((IPEndPoint)netSocket.get_LocalEndPoint()).get_Port(); + } + } + catch (cli.System.Net.Sockets.SocketException x) + { + throw new ConnectException(x.getMessage()); + } + catch (cli.System.ObjectDisposedException x1) + { + throw new SocketException("Socket is closed"); + } } private void socketBind(InetAddress address, int localport) throws IOException { - try - { - if (false) throw new cli.System.Net.Sockets.SocketException(); - if (false) throw new cli.System.ObjectDisposedException(""); - netSocket.Bind(new IPEndPoint(getAddressFromInetAddress(address), localport)); - this.address = address; - if (localport == 0) - { - this.localport = ((IPEndPoint)netSocket.get_LocalEndPoint()).get_Port(); - } - else - { - this.localport = localport; - } - } - catch (cli.System.Net.Sockets.SocketException x) - { - throw new BindException(x.getMessage()); - } - catch (cli.System.ObjectDisposedException x1) - { - throw new SocketException("Socket is closed"); - } + try + { + if (false) throw new cli.System.Net.Sockets.SocketException(); + if (false) throw new cli.System.ObjectDisposedException(""); + netSocket.Bind(new IPEndPoint(getAddressFromInetAddress(address), localport)); + this.address = address; + if (localport == 0) + { + this.localport = ((IPEndPoint)netSocket.get_LocalEndPoint()).get_Port(); + } + else + { + this.localport = localport; + } + } + catch (cli.System.Net.Sockets.SocketException x) + { + throw new BindException(x.getMessage()); + } + catch (cli.System.ObjectDisposedException x1) + { + throw new SocketException("Socket is closed"); + } } private void socketListen(int count) throws IOException { - try - { - if (false) throw new cli.System.Net.Sockets.SocketException(); - if (false) throw new cli.System.ObjectDisposedException(""); - netSocket.Listen(count); - } - catch (cli.System.Net.Sockets.SocketException x) - { - throw convertSocketExceptionToIOException(x); - } - catch (cli.System.ObjectDisposedException x1) - { - throw new SocketException("Socket is closed"); - } + try + { + if (false) throw new cli.System.Net.Sockets.SocketException(); + if (false) throw new cli.System.ObjectDisposedException(""); + netSocket.Listen(count); + } + catch (cli.System.Net.Sockets.SocketException x) + { + throw convertSocketExceptionToIOException(x); + } + catch (cli.System.ObjectDisposedException x1) + { + throw new SocketException("Socket is closed"); + } } private void socketAccept(SocketImpl s) throws IOException { - PlainSocketImpl impl = (PlainSocketImpl)s; - try - { - if (false) throw new cli.System.Net.Sockets.SocketException(); - if (false) throw new cli.System.ObjectDisposedException(""); - if (timeout > 0 && !netSocket.Poll(Math.min(timeout, Integer.MAX_VALUE / 1000) * 1000, - SelectMode.wrap(SelectMode.SelectRead))) - { - throw new SocketTimeoutException("Accept timed out"); - } - cli.System.Net.Sockets.Socket accept = netSocket.Accept(); - impl.netSocket = accept; - IPEndPoint remoteEndPoint = ((IPEndPoint)accept.get_RemoteEndPoint()); - impl.address = getInetAddressFromIPEndPoint(remoteEndPoint); - impl.port = remoteEndPoint.get_Port(); - impl.localport = ((IPEndPoint)accept.get_LocalEndPoint()).get_Port(); - } - catch (cli.System.Net.Sockets.SocketException x) - { - throw convertSocketExceptionToIOException(x); - } - catch (cli.System.ObjectDisposedException x1) - { - // TODO we may have to throw java.io.InterruptedIOException here - throw new SocketException("Socket is closed"); - } + PlainSocketImpl impl = (PlainSocketImpl)s; + try + { + if (false) throw new cli.System.Net.Sockets.SocketException(); + if (false) throw new cli.System.ObjectDisposedException(""); + if (timeout > 0 && !netSocket.Poll(Math.min(timeout, Integer.MAX_VALUE / 1000) * 1000, + SelectMode.wrap(SelectMode.SelectRead))) + { + throw new SocketTimeoutException("Accept timed out"); + } + cli.System.Net.Sockets.Socket accept = netSocket.Accept(); + impl.netSocket = accept; + IPEndPoint remoteEndPoint = ((IPEndPoint)accept.get_RemoteEndPoint()); + impl.address = getInetAddressFromIPEndPoint(remoteEndPoint); + impl.port = remoteEndPoint.get_Port(); + impl.localport = ((IPEndPoint)accept.get_LocalEndPoint()).get_Port(); + } + catch (cli.System.Net.Sockets.SocketException x) + { + throw convertSocketExceptionToIOException(x); + } + catch (cli.System.ObjectDisposedException x1) + { + // TODO we may have to throw java.io.InterruptedIOException here + throw new SocketException("Socket is closed"); + } } private int socketAvailable() throws IOException { - try - { - if (false) throw new cli.System.Net.Sockets.SocketException(); - if (false) throw new cli.System.ObjectDisposedException(""); - return netSocket.get_Available(); - } - catch (cli.System.Net.Sockets.SocketException x) - { - throw convertSocketExceptionToIOException(x); - } - catch (cli.System.ObjectDisposedException x1) - { - throw new SocketException("Socket is closed"); - } + try + { + if (false) throw new cli.System.Net.Sockets.SocketException(); + if (false) throw new cli.System.ObjectDisposedException(""); + return netSocket.get_Available(); + } + catch (cli.System.Net.Sockets.SocketException x) + { + throw convertSocketExceptionToIOException(x); + } + catch (cli.System.ObjectDisposedException x1) + { + throw new SocketException("Socket is closed"); + } } private void socketClose0(boolean useDeferredClose) throws IOException { - try - { - if (false) throw new cli.System.Net.Sockets.SocketException(); - if (false) throw new cli.System.ObjectDisposedException(""); - if (netSocket != null) - { - netSocket.Close(); - } - } - catch (cli.System.Net.Sockets.SocketException x) - { - throw convertSocketExceptionToIOException(x); - } - catch (cli.System.ObjectDisposedException x1) - { - throw new SocketException("Socket is closed"); - } + try + { + if (false) throw new cli.System.Net.Sockets.SocketException(); + if (false) throw new cli.System.ObjectDisposedException(""); + if (netSocket != null) + { + netSocket.Close(); + } + } + catch (cli.System.Net.Sockets.SocketException x) + { + throw convertSocketExceptionToIOException(x); + } + catch (cli.System.ObjectDisposedException x1) + { + throw new SocketException("Socket is closed"); + } } private void socketShutdown(int howto) throws IOException { - try - { - if (false) throw new cli.System.Net.Sockets.SocketException(); - if (false) throw new cli.System.ObjectDisposedException(""); - netSocket.Shutdown(SocketShutdown.wrap(howto == SHUT_RD ? SocketShutdown.Receive : SocketShutdown.Send)); - } - catch (cli.System.Net.Sockets.SocketException x) - { - throw convertSocketExceptionToIOException(x); - } - catch (cli.System.ObjectDisposedException x1) - { - throw new SocketException("Socket is closed"); - } + try + { + if (false) throw new cli.System.Net.Sockets.SocketException(); + if (false) throw new cli.System.ObjectDisposedException(""); + netSocket.Shutdown(SocketShutdown.wrap(howto == SHUT_RD ? SocketShutdown.Receive : SocketShutdown.Send)); + } + catch (cli.System.Net.Sockets.SocketException x) + { + throw convertSocketExceptionToIOException(x); + } + catch (cli.System.ObjectDisposedException x1) + { + throw new SocketException("Socket is closed"); + } } private void socketSetOption(int cmd, boolean on, Object value) throws SocketException { - try - { - if (false) throw new cli.System.Net.Sockets.SocketException(); - if (false) throw new cli.System.ObjectDisposedException(""); - switch (cmd) - { - case SocketOptions.SO_LINGER: - if (on) - { - netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.Linger), new LingerOption(true, ((Integer)value).intValue())); - } - else - { - netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.Linger), new LingerOption(false, 0)); - } - break; - case SocketOptions.SO_TIMEOUT: - if (serverSocket == null) - { - netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.ReceiveTimeout), timeout <= 5000 ? 0 : timeout); - } - break; - case SocketOptions.TCP_NODELAY: - netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Tcp), SocketOptionName.wrap(SocketOptionName.NoDelay), on ? 1 : 0); - break; - case SocketOptions.SO_KEEPALIVE: - netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.KeepAlive), on ? 1 : 0); - break; - case SocketOptions.SO_OOBINLINE: - netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.OutOfBandInline), on ? 1 : 0); - break; - default: - setCommonSocketOption(netSocket, cmd, on, value); - break; - } - } - catch (cli.System.Net.Sockets.SocketException x) - { - throw new SocketException(x.getMessage()); - } - catch (cli.System.ObjectDisposedException x1) - { - throw new SocketException("Socket is closed"); - } + try + { + if (false) throw new cli.System.Net.Sockets.SocketException(); + if (false) throw new cli.System.ObjectDisposedException(""); + switch (cmd) + { + case SocketOptions.SO_LINGER: + if (on) + { + netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.Linger), new LingerOption(true, ((Integer)value).intValue())); + } + else + { + netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.Linger), new LingerOption(false, 0)); + } + break; + case SocketOptions.SO_TIMEOUT: + if (serverSocket == null) + { + netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.ReceiveTimeout), timeout <= 5000 ? 0 : timeout); + } + break; + case SocketOptions.TCP_NODELAY: + netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Tcp), SocketOptionName.wrap(SocketOptionName.NoDelay), on ? 1 : 0); + break; + case SocketOptions.SO_KEEPALIVE: + netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.KeepAlive), on ? 1 : 0); + break; + case SocketOptions.SO_OOBINLINE: + netSocket.SetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.OutOfBandInline), on ? 1 : 0); + break; + default: + setCommonSocketOption(netSocket, cmd, on, value); + break; + } + } + catch (cli.System.Net.Sockets.SocketException x) + { + throw new SocketException(x.getMessage()); + } + catch (cli.System.ObjectDisposedException x1) + { + throw new SocketException("Socket is closed"); + } } private int socketGetOption(int opt, Object iaContainerObj) throws SocketException { - try - { - if (false) throw new cli.System.Net.Sockets.SocketException(); - if (false) throw new cli.System.ObjectDisposedException(""); - switch (opt) - { - case SocketOptions.TCP_NODELAY: - return CIL.unbox_int(netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Tcp), SocketOptionName.wrap(SocketOptionName.NoDelay))) == 0 ? -1 : 1; - case SocketOptions.SO_KEEPALIVE: - return CIL.unbox_int(netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.KeepAlive))) == 0 ? -1 : 1; - case SocketOptions.SO_LINGER: - { - LingerOption linger = (LingerOption)netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.Linger)); - if (linger.get_Enabled()) - { - return linger.get_LingerTime(); - } - return -1; - } - case SocketOptions.SO_OOBINLINE: - return CIL.unbox_int(netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.OutOfBandInline))) == 0 ? -1 : 1; - default: - return getCommonSocketOption(netSocket, opt, iaContainerObj); - } - } - catch (cli.System.Net.Sockets.SocketException x) - { - throw new SocketException(x.getMessage()); - } - catch (cli.System.ObjectDisposedException x1) - { - throw new SocketException("Socket is closed"); - } + try + { + if (false) throw new cli.System.Net.Sockets.SocketException(); + if (false) throw new cli.System.ObjectDisposedException(""); + switch (opt) + { + case SocketOptions.TCP_NODELAY: + return CIL.unbox_int(netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Tcp), SocketOptionName.wrap(SocketOptionName.NoDelay))) == 0 ? -1 : 1; + case SocketOptions.SO_KEEPALIVE: + return CIL.unbox_int(netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.KeepAlive))) == 0 ? -1 : 1; + case SocketOptions.SO_LINGER: + { + LingerOption linger = (LingerOption)netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.Linger)); + if (linger.get_Enabled()) + { + return linger.get_LingerTime(); + } + return -1; + } + case SocketOptions.SO_OOBINLINE: + return CIL.unbox_int(netSocket.GetSocketOption(SocketOptionLevel.wrap(SocketOptionLevel.Socket), SocketOptionName.wrap(SocketOptionName.OutOfBandInline))) == 0 ? -1 : 1; + default: + return getCommonSocketOption(netSocket, opt, iaContainerObj); + } + } + catch (cli.System.Net.Sockets.SocketException x) + { + throw new SocketException(x.getMessage()); + } + catch (cli.System.ObjectDisposedException x1) + { + throw new SocketException("Socket is closed"); + } } private void socketSendUrgentData(int data) throws IOException { - try - { - if (false) throw new cli.System.Net.Sockets.SocketException(); - if (false) throw new cli.System.ObjectDisposedException(""); - byte[] oob = { (byte)data }; - netSocket.Send(oob, SocketFlags.wrap(SocketFlags.OutOfBand)); - } - catch (cli.System.Net.Sockets.SocketException x) - { - throw convertSocketExceptionToIOException(x); - } - catch (cli.System.ObjectDisposedException x1) - { - throw new SocketException("Socket is closed"); - } + try + { + if (false) throw new cli.System.Net.Sockets.SocketException(); + if (false) throw new cli.System.ObjectDisposedException(""); + byte[] oob = { (byte)data }; + netSocket.Send(oob, SocketFlags.wrap(SocketFlags.OutOfBand)); + } + catch (cli.System.Net.Sockets.SocketException x) + { + throw convertSocketExceptionToIOException(x); + } + catch (cli.System.ObjectDisposedException x1) + { + throw new SocketException("Socket is closed"); + } } // used by SocketInputStream int read(byte[] buf, int offset, int len, int timeout) throws IOException { - try - { - if (false) throw new cli.System.Net.Sockets.SocketException(); - if (false) throw new cli.System.ObjectDisposedException(""); - if (timeout > 0 && !netSocket.Poll(Math.min(timeout, Integer.MAX_VALUE / 1000) * 1000, - SelectMode.wrap(SelectMode.SelectRead))) - { - throw new SocketTimeoutException(); - } - int read = netSocket.Receive(buf, offset, len, SocketFlags.wrap(SocketFlags.None)); - return read == 0 ? -1 : read; - } - catch (cli.System.Net.Sockets.SocketException x) - { - if (x.get_ErrorCode() == WSAESHUTDOWN) - { - // the socket was shutdown, so we have to return EOF - return -1; - } - else if (x.get_ErrorCode() == WSAEWOULDBLOCK) - { - // nothing to read and would block - return 0; - } - throw convertSocketExceptionToIOException(x); - } - catch (cli.System.ObjectDisposedException x1) - { - throw new SocketException("Socket is closed"); - } + try + { + if (false) throw new cli.System.Net.Sockets.SocketException(); + if (false) throw new cli.System.ObjectDisposedException(""); + if (timeout > 0 && !netSocket.Poll(Math.min(timeout, Integer.MAX_VALUE / 1000) * 1000, + SelectMode.wrap(SelectMode.SelectRead))) + { + throw new SocketTimeoutException(); + } + int read = netSocket.Receive(buf, offset, len, SocketFlags.wrap(SocketFlags.None)); + return read == 0 ? -1 : read; + } + catch (cli.System.Net.Sockets.SocketException x) + { + if (x.get_ErrorCode() == WSAESHUTDOWN) + { + // the socket was shutdown, so we have to return EOF + return -1; + } + else if (x.get_ErrorCode() == WSAEWOULDBLOCK) + { + // nothing to read and would block + return 0; + } + throw convertSocketExceptionToIOException(x); + } + catch (cli.System.ObjectDisposedException x1) + { + throw new SocketException("Socket is closed"); + } } // used by SocketOutputStream int write(byte[] buf, int offset, int len) throws IOException { - try - { - if (false) throw new cli.System.Net.Sockets.SocketException(); - if (false) throw new cli.System.ObjectDisposedException(""); - return netSocket.Send(buf, offset, len, SocketFlags.wrap(SocketFlags.None)); - } - catch (cli.System.Net.Sockets.SocketException x) - { - throw convertSocketExceptionToIOException(x); - } - catch (cli.System.ObjectDisposedException x1) - { - throw new SocketException("Socket is closed"); - } + try + { + if (false) throw new cli.System.Net.Sockets.SocketException(); + if (false) throw new cli.System.ObjectDisposedException(""); + return netSocket.Send(buf, offset, len, SocketFlags.wrap(SocketFlags.None)); + } + catch (cli.System.Net.Sockets.SocketException x) + { + throw convertSocketExceptionToIOException(x); + } + catch (cli.System.ObjectDisposedException x1) + { + throw new SocketException("Socket is closed"); + } } public final static int SHUT_RD = 0; diff --git a/openjdk/java/net/SocketInputStream.java b/openjdk/java/net/SocketInputStream.java index 9dc08ea2..1c13951f 100644 --- a/openjdk/java/net/SocketInputStream.java +++ b/openjdk/java/net/SocketInputStream.java @@ -38,8 +38,8 @@ import sun.net.ConnectionResetException; * public. * * @version 1.41, 05/05/07 - * @author Jonathan Payne - * @author Arthur van Hoff + * @author Jonathan Payne + * @author Arthur van Hoff */ class SocketInputStream extends FileInputStream { @@ -55,9 +55,9 @@ class SocketInputStream extends FileInputStream * @param impl the implemented socket input stream */ SocketInputStream(PlainSocketImpl impl) throws IOException { - super(impl.getFileDescriptor()); - this.impl = impl; - socket = impl.getSocket(); + super(impl.getFileDescriptor()); + this.impl = impl; + socket = impl.getSocket(); } /** @@ -73,7 +73,7 @@ class SocketInputStream extends FileInputStream * @spec JSR-51 */ public final FileChannel getChannel() { - return null; + return null; } /** @@ -90,7 +90,7 @@ class SocketInputStream extends FileInputStream */ private int socketRead0(FileDescriptor fd, byte b[], int off, int len, int timeout) throws IOException { - return impl.read(b, off, len, timeout); + return impl.read(b, off, len, timeout); } /** @@ -101,7 +101,7 @@ class SocketInputStream extends FileInputStream * @exception IOException If an I/O error has occurred. */ public int read(byte b[]) throws IOException { - return read(b, 0, b.length); + return read(b, 0, b.length); } /** @@ -115,112 +115,112 @@ class SocketInputStream extends FileInputStream * @exception IOException If an I/O error has occurred. */ public int read(byte b[], int off, int length) throws IOException { - int n; + int n; - // EOF already encountered - if (eof) { - return -1; - } + // EOF already encountered + if (eof) { + return -1; + } - // connection reset - if (impl.isConnectionReset()) { - throw new SocketException("Connection reset"); - } + // connection reset + if (impl.isConnectionReset()) { + throw new SocketException("Connection reset"); + } - // bounds check - if (length <= 0 || off < 0 || off + length > b.length) { - if (length == 0) { - return 0; - } - throw new ArrayIndexOutOfBoundsException(); - } + // bounds check + if (length <= 0 || off < 0 || off + length > b.length) { + if (length == 0) { + return 0; + } + throw new ArrayIndexOutOfBoundsException(); + } - boolean gotReset = false; + boolean gotReset = false; - // acquire file descriptor and do the read - FileDescriptor fd = impl.acquireFD(); - try { - n = socketRead0(fd, b, off, length, impl.getTimeout()); - if (n > 0) { - return n; - } - } catch (ConnectionResetException rstExc) { - gotReset = true; - } finally { - impl.releaseFD(); - } + // acquire file descriptor and do the read + FileDescriptor fd = impl.acquireFD(); + try { + n = socketRead0(fd, b, off, length, impl.getTimeout()); + if (n > 0) { + return n; + } + } catch (ConnectionResetException rstExc) { + gotReset = true; + } finally { + impl.releaseFD(); + } - /* - * We receive a "connection reset" but there may be bytes still - * buffered on the socket - */ - if (gotReset) { - impl.setConnectionResetPending(); - impl.acquireFD(); - try { - n = socketRead0(fd, b, off, length, impl.getTimeout()); - if (n > 0) { - return n; - } - } catch (ConnectionResetException rstExc) { - } finally { - impl.releaseFD(); - } - } + /* + * We receive a "connection reset" but there may be bytes still + * buffered on the socket + */ + if (gotReset) { + impl.setConnectionResetPending(); + impl.acquireFD(); + try { + n = socketRead0(fd, b, off, length, impl.getTimeout()); + if (n > 0) { + return n; + } + } catch (ConnectionResetException rstExc) { + } finally { + impl.releaseFD(); + } + } - /* - * If we get here we are at EOF, the socket has been closed, - * or the connection has been reset. - */ + /* + * If we get here we are at EOF, the socket has been closed, + * or the connection has been reset. + */ if (impl.isClosedOrPending()) { throw new SocketException("Socket closed"); } - if (impl.isConnectionResetPending()) { - impl.setConnectionReset(); - } - if (impl.isConnectionReset()) { - throw new SocketException("Connection reset"); - } - eof = true; - return -1; + if (impl.isConnectionResetPending()) { + impl.setConnectionReset(); + } + if (impl.isConnectionReset()) { + throw new SocketException("Connection reset"); + } + eof = true; + return -1; } /** * Reads a single byte from the socket. */ public int read() throws IOException { - if (eof) { - return -1; - } - temp = new byte[1]; - int n = read(temp, 0, 1); - if (n <= 0) { - return -1; - } - return temp[0] & 0xff; + if (eof) { + return -1; + } + temp = new byte[1]; + int n = read(temp, 0, 1); + if (n <= 0) { + return -1; + } + return temp[0] & 0xff; } /** * Skips n bytes of input. * @param n the number of bytes to skip - * @return the actual number of bytes skipped. + * @return the actual number of bytes skipped. * @exception IOException If an I/O error has occurred. */ public long skip(long numbytes) throws IOException { - if (numbytes <= 0) { - return 0; - } - long n = numbytes; - int buflen = (int) Math.min(1024, n); - byte data[] = new byte[buflen]; - while (n > 0) { - int r = read(data, 0, (int) Math.min((long) buflen, n)); - if (r < 0) { - break; - } - n -= r; - } - return numbytes - n; + if (numbytes <= 0) { + return 0; + } + long n = numbytes; + int buflen = (int) Math.min(1024, n); + byte data[] = new byte[buflen]; + while (n > 0) { + int r = read(data, 0, (int) Math.min((long) buflen, n)); + if (r < 0) { + break; + } + n -= r; + } + return numbytes - n; } /** @@ -228,7 +228,7 @@ class SocketInputStream extends FileInputStream * @return the number of immediately available bytes */ public int available() throws IOException { - return impl.available(); + return impl.available(); } /** @@ -236,20 +236,20 @@ class SocketInputStream extends FileInputStream */ private boolean closing = false; public void close() throws IOException { - // Prevent recursion. See BugId 4484411 - if (closing) - return; - closing = true; - if (socket != null) { - if (!socket.isClosed()) - socket.close(); - } else - impl.close(); - closing = false; + // Prevent recursion. See BugId 4484411 + if (closing) + return; + closing = true; + if (socket != null) { + if (!socket.isClosed()) + socket.close(); + } else + impl.close(); + closing = false; } void setEOF(boolean eof) { - this.eof = eof; + this.eof = eof; } /** diff --git a/openjdk/java/net/SocketOutputStream.java b/openjdk/java/net/SocketOutputStream.java index a0de1098..531a6f9d 100644 --- a/openjdk/java/net/SocketOutputStream.java +++ b/openjdk/java/net/SocketOutputStream.java @@ -36,8 +36,8 @@ import java.nio.channels.FileChannel; * public. * * @version 1.37, 05/05/07 - * @author Jonathan Payne - * @author Arthur van Hoff + * @author Jonathan Payne + * @author Arthur van Hoff */ class SocketOutputStream extends FileOutputStream { @@ -52,9 +52,9 @@ class SocketOutputStream extends FileOutputStream * @param impl the socket output stream inplemented */ SocketOutputStream(PlainSocketImpl impl) throws IOException { - super(impl.getFileDescriptor()); - this.impl = impl; - socket = impl.getSocket(); + super(impl.getFileDescriptor()); + this.impl = impl; + socket = impl.getSocket(); } /** @@ -83,7 +83,7 @@ class SocketOutputStream extends FileOutputStream */ private void socketWrite0(FileDescriptor fd, byte[] b, int off, int len) throws IOException { - impl.write(b, off, len); + impl.write(b, off, len); } /** @@ -96,29 +96,29 @@ class SocketOutputStream extends FileOutputStream */ private void socketWrite(byte b[], int off, int len) throws IOException { - if (len <= 0 || off < 0 || off + len > b.length) { - if (len == 0) { - return; - } - throw new ArrayIndexOutOfBoundsException(); - } + if (len <= 0 || off < 0 || off + len > b.length) { + if (len == 0) { + return; + } + throw new ArrayIndexOutOfBoundsException(); + } - FileDescriptor fd = impl.acquireFD(); - try { - socketWrite0(fd, b, off, len); - } catch (SocketException se) { - if (se instanceof sun.net.ConnectionResetException) { - impl.setConnectionResetPending(); - se = new SocketException("Connection reset"); - } - if (impl.isClosedOrPending()) { + FileDescriptor fd = impl.acquireFD(); + try { + socketWrite0(fd, b, off, len); + } catch (SocketException se) { + if (se instanceof sun.net.ConnectionResetException) { + impl.setConnectionResetPending(); + se = new SocketException("Connection reset"); + } + if (impl.isClosedOrPending()) { throw new SocketException("Socket closed"); } else { - throw se; - } - } finally { - impl.releaseFD(); - } + throw se; + } + } finally { + impl.releaseFD(); + } } /** @@ -127,8 +127,8 @@ class SocketOutputStream extends FileOutputStream * @exception IOException If an I/O error has occurred. */ public void write(int b) throws IOException { - temp[0] = (byte)b; - socketWrite(temp, 0, 1); + temp[0] = (byte)b; + socketWrite(temp, 0, 1); } /** @@ -137,7 +137,7 @@ class SocketOutputStream extends FileOutputStream * @exception SocketException If an I/O error has occurred. */ public void write(byte b[]) throws IOException { - socketWrite(b, 0, b.length); + socketWrite(b, 0, b.length); } /** @@ -149,7 +149,7 @@ class SocketOutputStream extends FileOutputStream * @exception SocketException If an I/O error has occurred. */ public void write(byte b[], int off, int len) throws IOException { - socketWrite(b, off, len); + socketWrite(b, off, len); } /** @@ -157,16 +157,16 @@ class SocketOutputStream extends FileOutputStream */ private boolean closing = false; public void close() throws IOException { - // Prevent recursion. See BugId 4484411 - if (closing) - return; - closing = true; - if (socket != null) { - if (!socket.isClosed()) - socket.close(); - } else - impl.close(); - closing = false; + // Prevent recursion. See BugId 4484411 + if (closing) + return; + closing = true; + if (socket != null) { + if (!socket.isClosed()) + socket.close(); + } else + impl.close(); + closing = false; } /** |