Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/nio/ch/ServerSocketAdaptor.java
38918 views
/*1* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.nio.ch;2627import java.io.IOException;28import java.net.InetAddress;29import java.net.InetSocketAddress;30import java.net.ServerSocket;31import java.net.Socket;32import java.net.SocketAddress;33import java.net.SocketException;34import java.net.SocketTimeoutException;35import java.net.StandardSocketOptions;36import java.nio.channels.ClosedChannelException;37import java.nio.channels.IllegalBlockingModeException;38import java.nio.channels.NotYetBoundException;39import java.nio.channels.ServerSocketChannel;40import java.nio.channels.SocketChannel;414243// Make a server-socket channel look like a server socket.44//45// The methods in this class are defined in exactly the same order as in46// java.net.ServerSocket so as to simplify tracking future changes to that47// class.48//4950class ServerSocketAdaptor // package-private51extends ServerSocket52{5354// The channel being adapted55private final ServerSocketChannelImpl ssc;5657// Timeout "option" value for accepts58private volatile int timeout = 0;5960public static ServerSocket create(ServerSocketChannelImpl ssc) {61try {62return new ServerSocketAdaptor(ssc);63} catch (IOException x) {64throw new Error(x);65}66}6768// ## super will create a useless impl69private ServerSocketAdaptor(ServerSocketChannelImpl ssc)70throws IOException71{72this.ssc = ssc;73}747576public void bind(SocketAddress local) throws IOException {77bind(local, 50);78}7980public void bind(SocketAddress local, int backlog) throws IOException {81if (local == null)82local = new InetSocketAddress(0);83try {84ssc.bind(local, backlog);85} catch (Exception x) {86Net.translateException(x);87}88}8990public InetAddress getInetAddress() {91if (!ssc.isBound())92return null;93return Net.getRevealedLocalAddress(ssc.localAddress()).getAddress();9495}9697public int getLocalPort() {98if (!ssc.isBound())99return -1;100return Net.asInetSocketAddress(ssc.localAddress()).getPort();101}102103104public Socket accept() throws IOException {105synchronized (ssc.blockingLock()) {106if (!ssc.isBound())107throw new IllegalBlockingModeException();108try {109if (timeout == 0) {110// for compatibility reasons: accept connection if available111// when configured non-blocking112SocketChannel sc = ssc.accept();113if (sc == null && !ssc.isBlocking())114throw new IllegalBlockingModeException();115return sc.socket();116}117118if (!ssc.isBlocking())119throw new IllegalBlockingModeException();120ssc.configureBlocking(false);121try {122SocketChannel sc;123if ((sc = ssc.accept()) != null)124return sc.socket();125long to = timeout;126for (;;) {127if (!ssc.isOpen())128throw new ClosedChannelException();129long st = System.currentTimeMillis();130int result = ssc.poll(Net.POLLIN, to);131if (result > 0 && ((sc = ssc.accept()) != null))132return sc.socket();133to -= System.currentTimeMillis() - st;134if (to <= 0)135throw new SocketTimeoutException();136}137} finally {138try {139ssc.configureBlocking(true);140} catch (ClosedChannelException e) { }141}142} catch (Exception x) {143Net.translateException(x);144assert false;145return null; // Never happens146}147}148}149150public void close() throws IOException {151ssc.close();152}153154public ServerSocketChannel getChannel() {155return ssc;156}157158public boolean isBound() {159return ssc.isBound();160}161162public boolean isClosed() {163return !ssc.isOpen();164}165166public void setSoTimeout(int timeout) throws SocketException {167this.timeout = timeout;168}169170public int getSoTimeout() throws SocketException {171return timeout;172}173174public void setReuseAddress(boolean on) throws SocketException {175try {176ssc.setOption(StandardSocketOptions.SO_REUSEADDR, on);177} catch (IOException x) {178Net.translateToSocketException(x);179}180}181182public boolean getReuseAddress() throws SocketException {183try {184return ssc.getOption(StandardSocketOptions.SO_REUSEADDR).booleanValue();185} catch (IOException x) {186Net.translateToSocketException(x);187return false; // Never happens188}189}190191public String toString() {192if (!isBound())193return "ServerSocket[unbound]";194return "ServerSocket[addr=" + getInetAddress() +195",localport=" + getLocalPort() + "]";196}197198public void setReceiveBufferSize(int size) throws SocketException {199// size 0 valid for ServerSocketChannel, invalid for ServerSocket200if (size <= 0)201throw new IllegalArgumentException("size cannot be 0 or negative");202try {203ssc.setOption(StandardSocketOptions.SO_RCVBUF, size);204} catch (IOException x) {205Net.translateToSocketException(x);206}207}208209public int getReceiveBufferSize() throws SocketException {210try {211return ssc.getOption(StandardSocketOptions.SO_RCVBUF).intValue();212} catch (IOException x) {213Net.translateToSocketException(x);214return -1; // Never happens215}216}217218}219220221