< prev index next >
src/java.base/share/classes/java/net/Socket.java
Print this page
*** 70,84 ****
* The implementation of this Socket.
*/
SocketImpl impl;
/**
- * Are we using an older SocketImpl?
- */
- private boolean oldImpl = false;
-
- /**
* Socket input/output streams
*/
private volatile InputStream in;
private volatile OutputStream out;
private static final VarHandle IN, OUT;
--- 70,79 ----
*** 156,177 ****
}
// create a SOCKS or HTTP SocketImpl that delegates to a platform SocketImpl
SocketImpl delegate = SocketImpl.createPlatformSocketImpl(false);
impl = (type == Proxy.Type.SOCKS) ? new SocksSocketImpl(p, delegate)
! : new HttpConnectSocketImpl(p, delegate);
! impl.setSocket(this);
} else {
if (p == Proxy.NO_PROXY) {
// create a platform or custom SocketImpl for the DIRECT case
SocketImplFactory factory = Socket.factory;
if (factory == null) {
impl = SocketImpl.createPlatformSocketImpl(false);
} else {
impl = factory.createSocketImpl();
}
- impl.setSocket(this);
} else
throw new IllegalArgumentException("Invalid Proxy");
}
}
--- 151,170 ----
}
// create a SOCKS or HTTP SocketImpl that delegates to a platform SocketImpl
SocketImpl delegate = SocketImpl.createPlatformSocketImpl(false);
impl = (type == Proxy.Type.SOCKS) ? new SocksSocketImpl(p, delegate)
! : new HttpConnectSocketImpl(p, delegate, this);
} else {
if (p == Proxy.NO_PROXY) {
// create a platform or custom SocketImpl for the DIRECT case
SocketImplFactory factory = Socket.factory;
if (factory == null) {
impl = SocketImpl.createPlatformSocketImpl(false);
} else {
impl = factory.createSocketImpl();
}
} else
throw new IllegalArgumentException("Invalid Proxy");
}
}
*** 186,199 ****
* such as a TCP error.
* @since 1.1
*/
protected Socket(SocketImpl impl) throws SocketException {
this.impl = impl;
- if (impl != null) {
- checkOldImpl();
- this.impl.setSocket(this);
- }
}
/**
* Creates a stream socket and connects it to the specified port
* number on the named host.
--- 179,188 ----
*** 484,542 ****
} catch (IOException e) {
throw new SocketException(e.getMessage());
}
}
- private void checkOldImpl() {
- if (impl == null)
- return;
- // SocketImpl.connect() is a protected method, therefore we need to use
- // getDeclaredMethod, therefore we need permission to access the member
-
- oldImpl = AccessController.doPrivileged
- (new PrivilegedAction<>() {
- public Boolean run() {
- Class<?> clazz = impl.getClass();
- while (true) {
- try {
- clazz.getDeclaredMethod("connect", SocketAddress.class, int.class);
- return Boolean.FALSE;
- } catch (NoSuchMethodException e) {
- clazz = clazz.getSuperclass();
- // java.net.SocketImpl class will always have this abstract method.
- // If we have not found it by now in the hierarchy then it does not
- // exist, we are an old style impl.
- if (clazz.equals(java.net.SocketImpl.class)) {
- return Boolean.TRUE;
- }
- }
- }
- }
- });
- }
-
void setImpl(SocketImpl si) {
impl = si;
- impl.setSocket(this);
}
/**
* Sets impl to the system-default type of SocketImpl.
* @since 1.4
*/
void setImpl() {
SocketImplFactory factory = Socket.factory;
if (factory != null) {
impl = factory.createSocketImpl();
- checkOldImpl();
} else {
// create a SOCKS SocketImpl that delegates to a platform SocketImpl
SocketImpl delegate = SocketImpl.createPlatformSocketImpl(false);
impl = new SocksSocketImpl(delegate);
}
- if (impl != null)
- impl.setSocket(this);
}
/**
* Get the {@code SocketImpl} attached to this socket, creating
* it if necessary.
--- 473,499 ----
*** 593,603 ****
throw new IllegalArgumentException("connect: timeout can't be negative");
if (isClosed())
throw new SocketException("Socket is closed");
! if (!oldImpl && isConnected())
throw new SocketException("already connected");
if (!(endpoint instanceof InetSocketAddress))
throw new IllegalArgumentException("Unsupported address type");
--- 550,560 ----
throw new IllegalArgumentException("connect: timeout can't be negative");
if (isClosed())
throw new SocketException("Socket is closed");
! if (isConnected())
throw new SocketException("already connected");
if (!(endpoint instanceof InetSocketAddress))
throw new IllegalArgumentException("Unsupported address type");
*** 613,631 ****
else
security.checkConnect(addr.getHostAddress(), port);
}
if (!created)
createImpl(true);
! if (!oldImpl)
impl.connect(epoint, timeout);
- else if (timeout == 0) {
- if (epoint.isUnresolved())
- impl.connect(addr.getHostName(), port);
- else
- impl.connect(addr, port);
- } else
- throw new UnsupportedOperationException("SocketImpl.connect(addr, timeout)");
connected = true;
/*
* If the socket was not bound before the connect, it is now because
* the kernel will have picked an ephemeral port & a local address
*/
--- 570,581 ----
else
security.checkConnect(addr.getHostAddress(), port);
}
if (!created)
createImpl(true);
!
impl.connect(epoint, timeout);
connected = true;
/*
* If the socket was not bound before the connect, it is now because
* the kernel will have picked an ephemeral port & a local address
*/
*** 651,661 ****
* @see #isBound
*/
public void bind(SocketAddress bindpoint) throws IOException {
if (isClosed())
throw new SocketException("Socket is closed");
! if (!oldImpl && isBound())
throw new SocketException("Already bound");
if (bindpoint != null && (!(bindpoint instanceof InetSocketAddress)))
throw new IllegalArgumentException("Unsupported address type");
InetSocketAddress epoint = (InetSocketAddress) bindpoint;
--- 601,611 ----
* @see #isBound
*/
public void bind(SocketAddress bindpoint) throws IOException {
if (isClosed())
throw new SocketException("Socket is closed");
! if (isBound())
throw new SocketException("Already bound");
if (bindpoint != null && (!(bindpoint instanceof InetSocketAddress)))
throw new IllegalArgumentException("Unsupported address type");
InetSocketAddress epoint = (InetSocketAddress) bindpoint;
*** 689,710 ****
*/
final void postAccept() {
connected = true;
created = true;
bound = true;
! }
!
! void setCreated() {
! created = true;
! }
!
! void setBound() {
! bound = true;
! }
!
! void setConnected() {
! connected = true;
}
/**
* Returns the address to which the socket is connected.
* <p>
--- 639,649 ----
*/
final void postAccept() {
connected = true;
created = true;
bound = true;
! // TODO impl.postAccept(); set bound / connected
}
/**
* Returns the address to which the socket is connected.
* <p>
*** 1668,1679 ****
*
* @return true if the socket was successfully connected to a server
* @since 1.4
*/
public boolean isConnected() {
! // Before 1.3 Sockets were always connected during creation
! return connected || oldImpl;
}
/**
* Returns the binding state of the socket.
* <p>
--- 1607,1617 ----
*
* @return true if the socket was successfully connected to a server
* @since 1.4
*/
public boolean isConnected() {
! return connected;
}
/**
* Returns the binding state of the socket.
* <p>
*** 1685,1696 ****
* @return true if the socket was successfully bound to an address
* @since 1.4
* @see #bind
*/
public boolean isBound() {
! // Before 1.3 Sockets were always bound during creation
! return bound || oldImpl;
}
/**
* Returns the closed state of the socket.
*
--- 1623,1633 ----
* @return true if the socket was successfully bound to an address
* @since 1.4
* @see #bind
*/
public boolean isBound() {
! return bound;
}
/**
* Returns the closed state of the socket.
*
< prev index next >