Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/classes/java/net/PlainSocketImpl.java
32287 views
/*1* Copyright (c) 2007, 2013, 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*/24package java.net;2526import java.io.*;27import java.security.PrivilegedAction;2829/*30* This class PlainSocketImpl simply delegates to the appropriate real31* SocketImpl. We do this because PlainSocketImpl is already extended32* by SocksSocketImpl.33* <p>34* There are two possibilities for the real SocketImpl,35* TwoStacksPlainSocketImpl or DualStackPlainSocketImpl. We use36* DualStackPlainSocketImpl on systems that have a dual stack37* TCP implementation. Otherwise we create an instance of38* TwoStacksPlainSocketImpl and delegate to it.39*40* @author Chris Hegarty41*/4243class PlainSocketImpl extends AbstractPlainSocketImpl44{45private AbstractPlainSocketImpl impl;4647/* the windows version. */48private static float version;4950/* java.net.preferIPv4Stack */51private static boolean preferIPv4Stack = false;5253/* If the version supports a dual stack TCP implementation */54private static boolean useDualStackImpl = false;5556/* sun.net.useExclusiveBind */57private static String exclBindProp;5859/* True if exclusive binding is on for Windows */60private static boolean exclusiveBind = true;6162static {63java.security.AccessController.doPrivileged( new PrivilegedAction<Object>() {64public Object run() {65version = 0;66try {67version = Float.parseFloat(System.getProperties().getProperty("os.version"));68preferIPv4Stack = Boolean.parseBoolean(69System.getProperties().getProperty("java.net.preferIPv4Stack"));70exclBindProp = System.getProperty("sun.net.useExclusiveBind");71} catch (NumberFormatException e ) {72assert false : e;73}74return null; // nothing to return75} });7677// (version >= 6.0) implies Vista or greater.78if (version >= 6.0 && !preferIPv4Stack) {79useDualStackImpl = true;80}8182if (exclBindProp != null) {83// sun.net.useExclusiveBind is true84exclusiveBind = exclBindProp.length() == 0 ? true85: Boolean.parseBoolean(exclBindProp);86} else if (version < 6.0) {87exclusiveBind = false;88}89}9091/**92* Constructs an empty instance.93*/94PlainSocketImpl() {95if (useDualStackImpl) {96impl = new DualStackPlainSocketImpl(exclusiveBind);97} else {98impl = new TwoStacksPlainSocketImpl(exclusiveBind);99}100}101102/**103* Constructs an instance with the given file descriptor.104*/105PlainSocketImpl(FileDescriptor fd) {106if (useDualStackImpl) {107impl = new DualStackPlainSocketImpl(fd, exclusiveBind);108} else {109impl = new TwoStacksPlainSocketImpl(fd, exclusiveBind);110}111}112113// Override methods in SocketImpl that access impl's fields.114115protected FileDescriptor getFileDescriptor() {116return impl.getFileDescriptor();117}118119protected InetAddress getInetAddress() {120return impl.getInetAddress();121}122123protected int getPort() {124return impl.getPort();125}126127protected int getLocalPort() {128return impl.getLocalPort();129}130131void setSocket(Socket soc) {132impl.setSocket(soc);133}134135Socket getSocket() {136return impl.getSocket();137}138139void setServerSocket(ServerSocket soc) {140impl.setServerSocket(soc);141}142143ServerSocket getServerSocket() {144return impl.getServerSocket();145}146147public String toString() {148return impl.toString();149}150151// Override methods in AbstractPlainSocketImpl that access impl's fields.152153protected synchronized void create(boolean stream) throws IOException {154impl.create(stream);155156// set fd to delegate's fd to be compatible with older releases157this.fd = impl.fd;158}159160protected void connect(String host, int port)161throws UnknownHostException, IOException162{163impl.connect(host, port);164}165166protected void connect(InetAddress address, int port) throws IOException {167impl.connect(address, port);168}169170protected void connect(SocketAddress address, int timeout) throws IOException {171impl.connect(address, timeout);172}173174public void setOption(int opt, Object val) throws SocketException {175impl.setOption(opt, val);176}177178public Object getOption(int opt) throws SocketException {179return impl.getOption(opt);180}181182synchronized void doConnect(InetAddress address, int port, int timeout) throws IOException {183impl.doConnect(address, port, timeout);184}185186protected synchronized void bind(InetAddress address, int lport)187throws IOException188{189impl.bind(address, lport);190}191192protected synchronized void accept(SocketImpl s) throws IOException {193if (s instanceof PlainSocketImpl) {194// pass in the real impl not the wrapper.195SocketImpl delegate = ((PlainSocketImpl)s).impl;196delegate.address = new InetAddress();197delegate.fd = new FileDescriptor();198impl.accept(delegate);199// set fd to delegate's fd to be compatible with older releases200s.fd = delegate.fd;201} else {202impl.accept(s);203}204}205206void setFileDescriptor(FileDescriptor fd) {207impl.setFileDescriptor(fd);208}209210void setAddress(InetAddress address) {211impl.setAddress(address);212}213214void setPort(int port) {215impl.setPort(port);216}217218void setLocalPort(int localPort) {219impl.setLocalPort(localPort);220}221222protected synchronized InputStream getInputStream() throws IOException {223return impl.getInputStream();224}225226void setInputStream(SocketInputStream in) {227impl.setInputStream(in);228}229230protected synchronized OutputStream getOutputStream() throws IOException {231return impl.getOutputStream();232}233234protected void close() throws IOException {235try {236impl.close();237} finally {238// set fd to delegate's fd to be compatible with older releases239this.fd = null;240}241}242243void reset() throws IOException {244try {245impl.reset();246} finally {247// set fd to delegate's fd to be compatible with older releases248this.fd = null;249}250}251252protected void shutdownInput() throws IOException {253impl.shutdownInput();254}255256protected void shutdownOutput() throws IOException {257impl.shutdownOutput();258}259260protected void sendUrgentData(int data) throws IOException {261impl.sendUrgentData(data);262}263264FileDescriptor acquireFD() {265return impl.acquireFD();266}267268void releaseFD() {269impl.releaseFD();270}271272public boolean isConnectionReset() {273return impl.isConnectionReset();274}275276public boolean isConnectionResetPending() {277return impl.isConnectionResetPending();278}279280public void setConnectionReset() {281impl.setConnectionReset();282}283284public void setConnectionResetPending() {285impl.setConnectionResetPending();286}287288public boolean isClosedOrPending() {289return impl.isClosedOrPending();290}291292public int getTimeout() {293return impl.getTimeout();294}295296// Override methods in AbstractPlainSocketImpl that need to be implemented.297298void socketCreate(boolean isServer) throws IOException {299impl.socketCreate(isServer);300}301302void socketConnect(InetAddress address, int port, int timeout)303throws IOException {304impl.socketConnect(address, port, timeout);305}306307void socketBind(InetAddress address, int port)308throws IOException {309impl.socketBind(address, port);310}311312void socketListen(int count) throws IOException {313impl.socketListen(count);314}315316void socketAccept(SocketImpl s) throws IOException {317impl.socketAccept(s);318}319320int socketAvailable() throws IOException {321return impl.socketAvailable();322}323324void socketClose0(boolean useDeferredClose) throws IOException {325impl.socketClose0(useDeferredClose);326}327328void socketShutdown(int howto) throws IOException {329impl.socketShutdown(howto);330}331332void socketSetOption(int cmd, boolean on, Object value)333throws SocketException {334impl.socketSetOption(cmd, on, value);335}336337int socketGetOption(int opt, Object iaContainerObj) throws SocketException {338return impl.socketGetOption(opt, iaContainerObj);339}340341void socketSendUrgentData(int data) throws IOException {342impl.socketSendUrgentData(data);343}344}345346347