PlainSocketImpl.java revision 5dd14d677fbbc0ad77b4eefa82a6f7c0a17f9a37
1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 * Copyright (c) 2007, 2008, Oracle and/or its affiliates. All rights reserved.
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.  Oracle designates this
9 * particular file as subject to the "Classpath" exception as provided
10 * by Oracle in the LICENSE file that accompanied this code.
11 *
12 * This code is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 * version 2 for more details (a copy is included in the LICENSE file that
16 * accompanied this code).
17 *
18 * You should have received a copy of the GNU General Public License version
19 * 2 along with this work; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23 * or visit www.oracle.com if you need additional information or have any
24 * questions.
25 */
26package java.net;
27
28import java.io.IOException;
29import java.io.FileDescriptor;
30import java.util.Set;
31import java.util.HashSet;
32import java.util.Collections;
33import jdk.net.*;
34
35import static sun.net.ExtendedOptionsImpl.*;
36
37/*
38 * On Unix systems we simply delegate to native methods.
39 *
40 * @author Chris Hegarty
41 */
42
43class PlainSocketImpl extends AbstractPlainSocketImpl
44{
45    /**
46     * Constructs an empty instance.
47     */
48    PlainSocketImpl() {
49        this(new FileDescriptor());
50    }
51
52    /**
53     * Constructs an instance with the given file descriptor.
54     */
55    PlainSocketImpl(FileDescriptor fd) {
56        this.fd = fd;
57    }
58
59    protected <T> void setOption(SocketOption<T> name, T value) throws IOException {
60        if (!name.equals(ExtendedSocketOptions.SO_FLOW_SLA)) {
61            super.setOption(name, value);
62        } else {
63            if (isClosedOrPending()) {
64                throw new SocketException("Socket closed");
65            }
66            checkSetOptionPermission(name);
67            checkValueType(value, SocketFlow.class);
68            setFlowOption(getFileDescriptor(), (SocketFlow)value);
69        }
70    }
71
72    protected <T> T getOption(SocketOption<T> name) throws IOException {
73        if (!name.equals(ExtendedSocketOptions.SO_FLOW_SLA)) {
74            return super.getOption(name);
75        }
76        if (isClosedOrPending()) {
77            throw new SocketException("Socket closed");
78        }
79        checkGetOptionPermission(name);
80        SocketFlow flow = SocketFlow.create();
81        getFlowOption(getFileDescriptor(), flow);
82        return (T)flow;
83    }
84
85    native void socketCreate(boolean isServer) throws IOException;
86
87    native void socketConnect(InetAddress address, int port, int timeout)
88        throws IOException;
89
90    native void socketBind(InetAddress address, int port)
91        throws IOException;
92
93    native void socketListen(int count) throws IOException;
94
95    native void socketAccept(SocketImpl s) throws IOException;
96
97    native int socketAvailable() throws IOException;
98
99    native void socketClose0() throws IOException;
100
101    native void socketShutdown(int howto) throws IOException;
102
103    native void socketSetOption(int cmd, boolean on, Object value)
104        throws SocketException;
105
106    native int socketGetOption(int opt, Object iaContainerObj) throws SocketException;
107
108    native void socketSendUrgentData(int data) throws IOException;
109
110}
111