~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/ikvm/openjdk/java/net/PlainSocketImpl.java

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
 
3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 
4
 *
 
5
 * This code is free software; you can redistribute it and/or modify it
 
6
 * under the terms of the GNU General Public License version 2 only, as
 
7
 * published by the Free Software Foundation.  Oracle designates this
 
8
 * particular file as subject to the "Classpath" exception as provided
 
9
 * by Oracle in the LICENSE file that accompanied this code.
 
10
 *
 
11
 * This code is distributed in the hope that it will be useful, but WITHOUT
 
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 
14
 * version 2 for more details (a copy is included in the LICENSE file that
 
15
 * accompanied this code).
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License version
 
18
 * 2 along with this work; if not, write to the Free Software Foundation,
 
19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 
20
 *
 
21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 
22
 * or visit www.oracle.com if you need additional information or have any
 
23
 * questions.
 
24
 */
 
25
package java.net;
 
26
 
 
27
import java.io.*;
 
28
import java.security.PrivilegedAction;
 
29
 
 
30
/*
 
31
 * This class PlainSocketImpl simply delegates to the appropriate real
 
32
 * SocketImpl. We do this because PlainSocketImpl is already extended
 
33
 * by SocksSocketImpl.
 
34
 * <p>
 
35
 * There are two possibilities for the real SocketImpl,
 
36
 * TwoStacksPlainSocketImpl or DualStackPlainSocketImpl. We use
 
37
 * DualStackPlainSocketImpl on systems that have a dual stack
 
38
 * TCP implementation. Otherwise we create an instance of
 
39
 * TwoStacksPlainSocketImpl and delegate to it.
 
40
 *
 
41
 * @author Chris Hegarty
 
42
 */
 
43
 
 
44
class PlainSocketImpl extends AbstractPlainSocketImpl
 
45
{
 
46
    private AbstractPlainSocketImpl impl;
 
47
 
 
48
    /* the windows version. */
 
49
    private static float version;
 
50
 
 
51
    /* java.net.preferIPv4Stack */
 
52
    private static boolean preferIPv4Stack = false;
 
53
 
 
54
    /* If the version supports a dual stack TCP implementation */
 
55
    private static boolean useDualStackImpl = false;
 
56
 
 
57
    static {
 
58
        java.security.AccessController.doPrivileged( new PrivilegedAction<Object>() {
 
59
                public Object run() {
 
60
                    version = 0;
 
61
                    try {
 
62
                        version = Float.parseFloat(System.getProperties().getProperty("os.version"));
 
63
                        preferIPv4Stack = Boolean.parseBoolean(
 
64
                                          System.getProperties().getProperty("java.net.preferIPv4Stack"));
 
65
                    } catch (NumberFormatException e ) {
 
66
                        assert false : e;
 
67
                    }
 
68
                    return null; // nothing to return
 
69
                } });
 
70
 
 
71
        String ipv6 = ikvm.internal.Util.SafeGetEnvironmentVariable("IKVM_IPV6");
 
72
        if (ipv6 != null) {
 
73
            try {
 
74
                if ((Integer.parseInt(ipv6) & 4) == 0) {
 
75
                    preferIPv4Stack = true;
 
76
                } else {
 
77
                    useDualStackImpl = true;
 
78
                }
 
79
            } catch (NumberFormatException _) {
 
80
            }
 
81
        } else if (!InetAddressImplFactory.isIPv6Supported()) {
 
82
            preferIPv4Stack = true;
 
83
        }
 
84
 
 
85
        // (version >= 6.0) implies Vista or greater.
 
86
        if (version >= 6.0 && !preferIPv4Stack) {
 
87
            useDualStackImpl = true;
 
88
        }
 
89
    }
 
90
 
 
91
    /**
 
92
     * Constructs an empty instance.
 
93
     */
 
94
    PlainSocketImpl() {
 
95
        if (useDualStackImpl) {
 
96
            impl = new DualStackPlainSocketImpl();
 
97
        } else {
 
98
            impl = new TwoStacksPlainSocketImpl();
 
99
        }
 
100
    }
 
101
 
 
102
    /**
 
103
     * Constructs an instance with the given file descriptor.
 
104
     */
 
105
    PlainSocketImpl(FileDescriptor fd) {
 
106
        if (useDualStackImpl) {
 
107
            impl = new DualStackPlainSocketImpl(fd);
 
108
        } else {
 
109
            impl = new TwoStacksPlainSocketImpl(fd);
 
110
        }
 
111
    }
 
112
 
 
113
    // Override methods in SocketImpl that access impl's fields.
 
114
 
 
115
    protected FileDescriptor getFileDescriptor() {
 
116
        return impl.getFileDescriptor();
 
117
    }
 
118
 
 
119
    protected InetAddress getInetAddress() {
 
120
        return impl.getInetAddress();
 
121
    }
 
122
 
 
123
    protected int getPort() {
 
124
        return impl.getPort();
 
125
    }
 
126
 
 
127
    protected int getLocalPort() {
 
128
        return impl.getLocalPort();
 
129
    }
 
130
 
 
131
    void setSocket(Socket soc) {
 
132
        impl.setSocket(soc);
 
133
    }
 
134
 
 
135
    Socket getSocket() {
 
136
        return impl.getSocket();
 
137
    }
 
138
 
 
139
    void setServerSocket(ServerSocket soc) {
 
140
        impl.setServerSocket(soc);
 
141
    }
 
142
 
 
143
    ServerSocket getServerSocket() {
 
144
        return impl.getServerSocket();
 
145
    }
 
146
 
 
147
    public String toString() {
 
148
        return impl.toString();
 
149
    }
 
150
 
 
151
    // Override methods in AbstractPlainSocketImpl that access impl's fields.
 
152
 
 
153
    protected synchronized void create(boolean stream) throws IOException {
 
154
        impl.create(stream);
 
155
 
 
156
        // set fd to delegate's fd to be compatible with older releases
 
157
        this.fd = impl.fd;
 
158
    }
 
159
 
 
160
    protected void connect(String host, int port)
 
161
        throws UnknownHostException, IOException
 
162
    {
 
163
        impl.connect(host, port);
 
164
    }
 
165
 
 
166
    protected void connect(InetAddress address, int port) throws IOException {
 
167
        impl.connect(address, port);
 
168
    }
 
169
 
 
170
    protected void connect(SocketAddress address, int timeout) throws IOException {
 
171
        impl.connect(address, timeout);
 
172
    }
 
173
 
 
174
    public void setOption(int opt, Object val) throws SocketException {
 
175
        impl.setOption(opt, val);
 
176
    }
 
177
 
 
178
    public Object getOption(int opt) throws SocketException {
 
179
        return impl.getOption(opt);
 
180
    }
 
181
 
 
182
    synchronized void doConnect(InetAddress address, int port, int timeout) throws IOException {
 
183
        impl.doConnect(address, port, timeout);
 
184
    }
 
185
 
 
186
    protected synchronized void bind(InetAddress address, int lport)
 
187
        throws IOException
 
188
    {
 
189
        impl.bind(address, lport);
 
190
    }
 
191
 
 
192
    protected synchronized void accept(SocketImpl s) throws IOException {
 
193
        // pass in the real impl not the wrapper.
 
194
        SocketImpl delegate = ((PlainSocketImpl)s).impl;
 
195
        delegate.address = new InetAddress();
 
196
        delegate.fd = new FileDescriptor();
 
197
        impl.accept(delegate);
 
198
 
 
199
        // set fd to delegate's fd to be compatible with older releases
 
200
        s.fd = delegate.fd;
 
201
    }
 
202
 
 
203
    void setFileDescriptor(FileDescriptor fd) {
 
204
        impl.setFileDescriptor(fd);
 
205
    }
 
206
 
 
207
    void setAddress(InetAddress address) {
 
208
        impl.setAddress(address);
 
209
    }
 
210
 
 
211
    void setPort(int port) {
 
212
        impl.setPort(port);
 
213
    }
 
214
 
 
215
    void setLocalPort(int localPort) {
 
216
        impl.setLocalPort(localPort);
 
217
    }
 
218
 
 
219
    protected synchronized InputStream getInputStream() throws IOException {
 
220
        return impl.getInputStream();
 
221
    }
 
222
 
 
223
    void setInputStream(SocketInputStream in) {
 
224
        impl.setInputStream(in);
 
225
    }
 
226
 
 
227
    protected synchronized OutputStream getOutputStream() throws IOException {
 
228
        return impl.getOutputStream();
 
229
    }
 
230
 
 
231
    protected void close() throws IOException {
 
232
        try {
 
233
            impl.close();
 
234
        } finally {
 
235
            // set fd to delegate's fd to be compatible with older releases
 
236
            this.fd = null;
 
237
        }
 
238
    }
 
239
 
 
240
    void reset() throws IOException {
 
241
        try {
 
242
            impl.reset();
 
243
        } finally {
 
244
            // set fd to delegate's fd to be compatible with older releases
 
245
            this.fd = null;
 
246
        }
 
247
    }
 
248
 
 
249
    protected void shutdownInput() throws IOException {
 
250
        impl.shutdownInput();
 
251
    }
 
252
 
 
253
    protected void shutdownOutput() throws IOException {
 
254
        impl.shutdownOutput();
 
255
    }
 
256
 
 
257
    protected void sendUrgentData(int data) throws IOException {
 
258
        impl.sendUrgentData(data);
 
259
    }
 
260
 
 
261
    FileDescriptor acquireFD() {
 
262
        return impl.acquireFD();
 
263
    }
 
264
 
 
265
    void releaseFD() {
 
266
        impl.releaseFD();
 
267
    }
 
268
 
 
269
    public boolean isConnectionReset() {
 
270
        return impl.isConnectionReset();
 
271
    }
 
272
 
 
273
    public boolean isConnectionResetPending() {
 
274
        return impl.isConnectionResetPending();
 
275
    }
 
276
 
 
277
    public void setConnectionReset() {
 
278
        impl.setConnectionReset();
 
279
    }
 
280
 
 
281
    public void setConnectionResetPending() {
 
282
        impl.setConnectionResetPending();
 
283
    }
 
284
 
 
285
    public boolean isClosedOrPending() {
 
286
        return impl.isClosedOrPending();
 
287
    }
 
288
 
 
289
    public int getTimeout() {
 
290
        return impl.getTimeout();
 
291
    }
 
292
 
 
293
    // Override methods in AbstractPlainSocketImpl that need to be implemented.
 
294
 
 
295
    void socketCreate(boolean isServer) throws IOException {
 
296
        impl.socketCreate(isServer);
 
297
    }
 
298
 
 
299
    void socketConnect(InetAddress address, int port, int timeout)
 
300
        throws IOException {
 
301
        impl.socketConnect(address, port, timeout);
 
302
    }
 
303
 
 
304
    void socketBind(InetAddress address, int port)
 
305
        throws IOException {
 
306
        impl.socketBind(address, port);
 
307
    }
 
308
 
 
309
    void socketListen(int count) throws IOException {
 
310
        impl.socketListen(count);
 
311
    }
 
312
 
 
313
    void socketAccept(SocketImpl s) throws IOException {
 
314
        impl.socketAccept(s);
 
315
    }
 
316
 
 
317
    int socketAvailable() throws IOException {
 
318
        return impl.socketAvailable();
 
319
    }
 
320
 
 
321
    void socketClose0(boolean useDeferredClose) throws IOException {
 
322
        impl.socketClose0(useDeferredClose);
 
323
    }
 
324
 
 
325
    void socketShutdown(int howto) throws IOException {
 
326
        impl.socketShutdown(howto);
 
327
    }
 
328
 
 
329
    void socketSetOption(int cmd, boolean on, Object value)
 
330
        throws SocketException {
 
331
        socketSetOption(cmd, on, value);
 
332
    }
 
333
 
 
334
    int socketGetOption(int opt, Object iaContainerObj) throws SocketException {
 
335
        return impl.socketGetOption(opt, iaContainerObj);
 
336
    }
 
337
 
 
338
    void socketSendUrgentData(int data) throws IOException {
 
339
        impl.socketSendUrgentData(data);
 
340
    }
 
341
}