< prev index next >
src/java.base/share/classes/java/net/SocketImpl.java
Print this page
@@ -44,23 +44,36 @@
* @author unascribed
* @since 1.0
*/
public abstract class SocketImpl implements SocketOptions {
+ // true if this SocketImpl for a ServerSocket. By default, this field
+ // will be set to true. It can only be set to false by platform impls.
+ /*package*/ final boolean server;
+
/**
- * Creates an instance of platform's SocketImpl
+ * Creates a new SocketImpl.
*/
- @SuppressWarnings("unchecked")
- static <S extends SocketImpl & PlatformSocketImpl> S createPlatformSocketImpl(boolean server) {
- return (S) new PlainSocketImpl();
+ public SocketImpl() {
+ this(true);
+ }
+
+ /**
+ * Creates a new SocketImpl.
+ * @param server true if this SocketImpl for a ServerSocket
+ */
+ /*package*/ SocketImpl(boolean server) {
+ this.server = server;
}
/**
- * The actual Socket object.
+ * Creates an instance of platform's SocketImpl
*/
- Socket socket = null;
- ServerSocket serverSocket = null;
+ @SuppressWarnings("unchecked")
+ static <S extends SocketImpl & PlatformSocketImpl> S createPlatformSocketImpl(boolean server) {
+ return (S) new PlainSocketImpl(server);
+ }
/**
* The file descriptor object for this socket.
*/
protected FileDescriptor fd;
@@ -298,26 +311,10 @@
*/
protected int getLocalPort() {
return localport;
}
- void setSocket(Socket soc) {
- this.socket = soc;
- }
-
- Socket getSocket() {
- return socket;
- }
-
- void setServerSocket(ServerSocket soc) {
- this.serverSocket = soc;
- }
-
- ServerSocket getServerSocket() {
- return serverSocket;
- }
-
/**
* Returns the address and port of this socket as a {@code String}.
*
* @return a string representation of this socket.
*/
@@ -393,30 +390,26 @@
* @throws IOException if an I/O error occurs, or if the socket is closed.
*
* @since 9
*/
protected <T> void setOption(SocketOption<T> name, T value) throws IOException {
- if (name == StandardSocketOptions.SO_KEEPALIVE &&
- (getSocket() != null)) {
+ if (name == StandardSocketOptions.SO_KEEPALIVE && !server) {
setOption(SocketOptions.SO_KEEPALIVE, value);
- } else if (name == StandardSocketOptions.SO_SNDBUF &&
- (getSocket() != null)) {
+ } else if (name == StandardSocketOptions.SO_SNDBUF && !server) {
setOption(SocketOptions.SO_SNDBUF, value);
} else if (name == StandardSocketOptions.SO_RCVBUF) {
setOption(SocketOptions.SO_RCVBUF, value);
} else if (name == StandardSocketOptions.SO_REUSEADDR) {
setOption(SocketOptions.SO_REUSEADDR, value);
} else if (name == StandardSocketOptions.SO_REUSEPORT &&
supportedOptions().contains(name)) {
setOption(SocketOptions.SO_REUSEPORT, value);
- } else if (name == StandardSocketOptions.SO_LINGER &&
- (getSocket() != null)) {
+ } else if (name == StandardSocketOptions.SO_LINGER && !server) {
setOption(SocketOptions.SO_LINGER, value);
} else if (name == StandardSocketOptions.IP_TOS) {
setOption(SocketOptions.IP_TOS, value);
- } else if (name == StandardSocketOptions.TCP_NODELAY &&
- (getSocket() != null)) {
+ } else if (name == StandardSocketOptions.TCP_NODELAY && !server) {
setOption(SocketOptions.TCP_NODELAY, value);
} else {
throw new UnsupportedOperationException("unsupported option");
}
}
@@ -436,30 +429,26 @@
*
* @since 9
*/
@SuppressWarnings("unchecked")
protected <T> T getOption(SocketOption<T> name) throws IOException {
- if (name == StandardSocketOptions.SO_KEEPALIVE &&
- (getSocket() != null)) {
+ if (name == StandardSocketOptions.SO_KEEPALIVE && !server) {
return (T)getOption(SocketOptions.SO_KEEPALIVE);
- } else if (name == StandardSocketOptions.SO_SNDBUF &&
- (getSocket() != null)) {
+ } else if (name == StandardSocketOptions.SO_SNDBUF && !server) {
return (T)getOption(SocketOptions.SO_SNDBUF);
} else if (name == StandardSocketOptions.SO_RCVBUF) {
return (T)getOption(SocketOptions.SO_RCVBUF);
} else if (name == StandardSocketOptions.SO_REUSEADDR) {
return (T)getOption(SocketOptions.SO_REUSEADDR);
} else if (name == StandardSocketOptions.SO_REUSEPORT &&
supportedOptions().contains(name)) {
return (T)getOption(SocketOptions.SO_REUSEPORT);
- } else if (name == StandardSocketOptions.SO_LINGER &&
- (getSocket() != null)) {
+ } else if (name == StandardSocketOptions.SO_LINGER && !server) {
return (T)getOption(SocketOptions.SO_LINGER);
} else if (name == StandardSocketOptions.IP_TOS) {
return (T)getOption(SocketOptions.IP_TOS);
- } else if (name == StandardSocketOptions.TCP_NODELAY &&
- (getSocket() != null)) {
+ } else if (name == StandardSocketOptions.TCP_NODELAY && !server) {
return (T)getOption(SocketOptions.TCP_NODELAY);
} else {
throw new UnsupportedOperationException("unsupported option");
}
}
@@ -502,12 +491,12 @@
* @return a Set of SocketOptions
*
* @since 9
*/
protected Set<SocketOption<?>> supportedOptions() {
- if (getSocket() != null) {
- return socketOptions;
- } else {
+ if (server) {
return serverSocketOptions;
+ } else {
+ return socketOptions;
}
}
}
< prev index next >