下面列出了怎么用java.net.SocketImpl的API类实例代码及写法,或者点击链接到github查看源代码。
public void test_Socket_impl_notNull() throws Exception {
try (SocketChannel sc = SocketChannel.open();
Socket socket = sc.socket()) {
Field f_impl = Socket.class.getDeclaredField("impl");
f_impl.setAccessible(true);
Object implFieldValue = f_impl.get(socket);
assertNotNull(implFieldValue);
assertTrue(implFieldValue instanceof SocketImpl);
}
}
private void testSetSocketImpl(HttpServletResponse response)
throws IOException, AssertionFailedException {
SocketImplFactory mockFactory =
new SocketImplFactory() {
@Override
public SocketImpl createSocketImpl() {
return null;
}
};
SocketException caught = null;
try {
Socket.setSocketImplFactory(mockFactory);
} catch (SocketException e) {
caught = e;
}
assertNotNull("caught", caught, response);
}
private SocketAdaptor(SocketChannelImpl sc) throws SocketException {
super((SocketImpl) null);
this.sc = sc;
}
SdpSocket(SocketImpl impl) throws SocketException {
super(impl);
}
private SocketAdaptor(SocketChannelImpl sc) throws SocketException {
super((SocketImpl) null);
this.sc = sc;
}
SdpSocket(SocketImpl impl) throws SocketException {
super(impl);
}
private SocketAdaptor(SocketChannelImpl sc) throws SocketException {
super((SocketImpl) null);
this.sc = sc;
}
SdpSocket(SocketImpl impl) throws SocketException {
super(impl);
}
@Override
protected void accept(SocketImpl s) {
throw new RuntimeException("accept not implemented");
}
SdpSocket(SocketImpl impl) throws SocketException {
super(impl);
}
public HttpProxyAwareSocket(final SocketImpl impl, final Proxy proxy) throws SocketException {
super(impl);
this.proxy = proxy;
}
static SocketImpl create() {
return new DummySocketImpl();
}
@Override
protected void accept(SocketImpl si) {
shouldNotGetHere();
}
/**
* Accepts a new connection so that the given SocketImpl is connected to
* the peer. The SocketImpl must be a newly created NioSocketImpl.
*/
@Override
protected void accept(SocketImpl si) throws IOException {
NioSocketImpl nsi = (NioSocketImpl) si;
if (nsi.state != ST_NEW)
throw new SocketException("Not a newly created SocketImpl");
FileDescriptor newfd = new FileDescriptor();
InetSocketAddress[] isaa = new InetSocketAddress[1];
// acquire the lock, adjusting the timeout for cases where several
// threads are accepting connections and there is a timeout set
ReentrantLock acceptLock = readLock;
int timeout = this.timeout;
long remainingNanos = 0;
if (timeout > 0) {
remainingNanos = tryLock(acceptLock, timeout, MILLISECONDS);
if (remainingNanos <= 0) {
assert !acceptLock.isHeldByCurrentThread();
throw new SocketTimeoutException("Accept timed out");
}
} else {
acceptLock.lock();
}
// accept a connection
try {
int n = 0;
FileDescriptor fd = beginAccept();
try {
if (remainingNanos > 0) {
// accept with timeout
configureNonBlocking(fd);
n = timedAccept(fd, newfd, isaa, remainingNanos);
} else {
// accept, no timeout
n = Net.accept(fd, newfd, isaa);
while (IOStatus.okayToRetry(n) && isOpen()) {
park(fd, Net.POLLIN);
n = Net.accept(fd, newfd, isaa);
}
}
} finally {
endAccept(n > 0);
assert IOStatus.check(n);
}
} finally {
acceptLock.unlock();
}
// get local address and configure accepted socket to blocking mode
InetSocketAddress localAddress;
try {
localAddress = Net.localAddress(newfd);
IOUtil.configureBlocking(newfd, true);
} catch (IOException ioe) {
nd.close(newfd);
throw ioe;
}
// set the fields
Runnable closer = closerFor(newfd, true);
synchronized (nsi.stateLock) {
nsi.fd = newfd;
nsi.stream = true;
nsi.cleaner = CleanerFactory.cleaner().register(nsi, closer);
nsi.localport = localAddress.getPort();
nsi.address = isaa[0].getAddress();
nsi.port = isaa[0].getPort();
nsi.state = ST_CONNECTED;
}
}
SdpSocket(SocketImpl impl) throws SocketException {
super(impl);
}
private SocketAdaptor(SocketChannelImpl sc) throws SocketException {
super((SocketImpl) null);
this.sc = sc;
}
SdpSocket(SocketImpl impl) throws SocketException {
super(impl);
}
@Override
protected void accept(SocketImpl s) throws IOException {
throw new UnsupportedOperationException();
}
@Override
protected void accept(SocketImpl s) throws IOException {}
/**
* Creates an unconnected and unbound SDP socket. The {@code Socket} is
* associated with a {@link java.net.SocketImpl} of the system-default type.
*
* @return a new Socket
*
* @throws UnsupportedOperationException
* If SDP is not supported
* @throws IOException
* If an I/O error occurs
*/
public static Socket openSocket() throws IOException {
SocketImpl impl = createSocketImpl();
return new SdpSocket(impl);
}
/**
* Creates an unconnected and unbound SDP socket. The {@code Socket} is
* associated with a {@link java.net.SocketImpl} of the system-default type.
*
* @return a new Socket
*
* @throws UnsupportedOperationException
* If SDP is not supported
* @throws IOException
* If an I/O error occurs
*/
public static Socket openSocket() throws IOException {
SocketImpl impl = createSocketImpl();
return new SdpSocket(impl);
}
/**
* Creates an unconnected and unbound SDP socket. The {@code Socket} is
* associated with a {@link java.net.SocketImpl} of the system-default type.
*
* @return a new Socket
*
* @throws UnsupportedOperationException
* If SDP is not supported
* @throws IOException
* If an I/O error occurs
*/
public static Socket openSocket() throws IOException {
SocketImpl impl = createSocketImpl();
return new SdpSocket(impl);
}
/**
* Creates an unconnected and unbound SDP socket. The {@code Socket} is
* associated with a {@link java.net.SocketImpl} of the system-default type.
*
* @return a new Socket
*
* @throws UnsupportedOperationException
* If SDP is not supported
* @throws IOException
* If an I/O error occurs
*/
public static Socket openSocket() throws IOException {
SocketImpl impl = createSocketImpl();
return new SdpSocket(impl);
}
/**
* Creates an unconnected and unbound SDP socket. The {@code Socket} is
* associated with a {@link java.net.SocketImpl} of the system-default type.
*
* @return a new Socket
*
* @throws UnsupportedOperationException
* If SDP is not supported
* @throws IOException
* If an I/O error occurs
*/
public static Socket openSocket() throws IOException {
SocketImpl impl = createSocketImpl();
return new SdpSocket(impl);
}