• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  * Copyright (c) 1995, 2013, 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  */
26 
27 package java.net;
28 
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.io.OutputStream;
32 import java.io.FileDescriptor;
33 
34 /**
35  * The abstract class {@code SocketImpl} is a common superclass
36  * of all classes that actually implement sockets. It is used to
37  * create both client and server sockets.
38  * <p>
39  * A "plain" socket implements these methods exactly as
40  * described, without attempting to go through a firewall or proxy.
41  *
42  * @author  unascribed
43  * @since   JDK1.0
44  */
45 public abstract class SocketImpl implements SocketOptions {
46     /**
47      * The actual Socket object.
48      */
49     Socket socket = null;
50     ServerSocket serverSocket = null;
51 
52     /**
53      * The file descriptor object for this socket.
54      */
55     protected FileDescriptor fd;
56 
57     /**
58      * The IP address of the remote end of this socket.
59      */
60     protected InetAddress address;
61 
62     /**
63      * The port number on the remote host to which this socket is connected.
64      */
65     protected int port;
66 
67     /**
68      * The local port number to which this socket is connected.
69      */
70     protected int localport;
71 
72     /**
73      * Creates either a stream or a datagram socket.
74      *
75      * @param      stream   if {@code true}, create a stream socket;
76      *                      otherwise, create a datagram socket.
77      * @exception  IOException  if an I/O error occurs while creating the
78      *               socket.
79      */
create(boolean stream)80     protected abstract void create(boolean stream) throws IOException;
81 
82     /**
83      * Connects this socket to the specified port on the named host.
84      *
85      * @param      host   the name of the remote host.
86      * @param      port   the port number.
87      * @exception  IOException  if an I/O error occurs when connecting to the
88      *               remote host.
89      */
connect(String host, int port)90     protected abstract void connect(String host, int port) throws IOException;
91 
92     /**
93      * Connects this socket to the specified port number on the specified host.
94      *
95      * @param      address   the IP address of the remote host.
96      * @param      port      the port number.
97      * @exception  IOException  if an I/O error occurs when attempting a
98      *               connection.
99      */
connect(InetAddress address, int port)100     protected abstract void connect(InetAddress address, int port) throws IOException;
101 
102     /**
103      * Connects this socket to the specified port number on the specified host.
104      * A timeout of zero is interpreted as an infinite timeout. The connection
105      * will then block until established or an error occurs.
106      *
107      * @param      address   the Socket address of the remote host.
108      * @param     timeout  the timeout value, in milliseconds, or zero for no timeout.
109      * @exception  IOException  if an I/O error occurs when attempting a
110      *               connection.
111      * @since 1.4
112      */
connect(SocketAddress address, int timeout)113     protected abstract void connect(SocketAddress address, int timeout) throws IOException;
114 
115     /**
116      * Binds this socket to the specified local IP address and port number.
117      *
118      * @param      host   an IP address that belongs to a local interface.
119      * @param      port   the port number.
120      * @exception  IOException  if an I/O error occurs when binding this socket.
121      */
bind(InetAddress host, int port)122     protected abstract void bind(InetAddress host, int port) throws IOException;
123 
124     /**
125      * Sets the maximum queue length for incoming connection indications
126      * (a request to connect) to the {@code count} argument. If a
127      * connection indication arrives when the queue is full, the
128      * connection is refused.
129      *
130      * @param      backlog   the maximum length of the queue.
131      * @exception  IOException  if an I/O error occurs when creating the queue.
132      */
listen(int backlog)133     protected abstract void listen(int backlog) throws IOException;
134 
135     /**
136      * Accepts a connection.
137      *
138      * @param      s   the accepted connection.
139      * @exception  IOException  if an I/O error occurs when accepting the
140      *               connection.
141      */
accept(SocketImpl s)142     protected abstract void accept(SocketImpl s) throws IOException;
143 
144     /**
145      * Returns an input stream for this socket.
146      *
147      * @return     a stream for reading from this socket.
148      * @exception  IOException  if an I/O error occurs when creating the
149      *               input stream.
150     */
getInputStream()151     protected abstract InputStream getInputStream() throws IOException;
152 
153     /**
154      * Returns an output stream for this socket.
155      *
156      * @return     an output stream for writing to this socket.
157      * @exception  IOException  if an I/O error occurs when creating the
158      *               output stream.
159      */
getOutputStream()160     protected abstract OutputStream getOutputStream() throws IOException;
161 
162     /**
163      * Returns the number of bytes that can be read from this socket
164      * without blocking.
165      *
166      * @return     the number of bytes that can be read from this socket
167      *             without blocking.
168      * @exception  IOException  if an I/O error occurs when determining the
169      *               number of bytes available.
170      */
available()171     protected abstract int available() throws IOException;
172 
173     /**
174      * Closes this socket.
175      *
176      * @exception  IOException  if an I/O error occurs when closing this socket.
177      */
close()178     protected abstract void close() throws IOException;
179 
180     /**
181      * Places the input stream for this socket at "end of stream".
182      * Any data sent to this socket is acknowledged and then
183      * silently discarded.
184      *
185      * If you read from a socket input stream after invoking this method on the
186      * socket, the stream's {@code available} method will return 0, and its
187      * {@code read} methods will return {@code -1} (end of stream).
188      *
189      * @exception IOException if an I/O error occurs when shutting down this
190      * socket.
191      * @see java.net.Socket#shutdownOutput()
192      * @see java.net.Socket#close()
193      * @see java.net.Socket#setSoLinger(boolean, int)
194      * @since 1.3
195      */
shutdownInput()196     protected void shutdownInput() throws IOException {
197       throw new IOException("Method not implemented!");
198     }
199 
200     /**
201      * Disables the output stream for this socket.
202      * For a TCP socket, any previously written data will be sent
203      * followed by TCP's normal connection termination sequence.
204      *
205      * If you write to a socket output stream after invoking
206      * shutdownOutput() on the socket, the stream will throw
207      * an IOException.
208      *
209      * @exception IOException if an I/O error occurs when shutting down this
210      * socket.
211      * @see java.net.Socket#shutdownInput()
212      * @see java.net.Socket#close()
213      * @see java.net.Socket#setSoLinger(boolean, int)
214      * @since 1.3
215      */
shutdownOutput()216     protected void shutdownOutput() throws IOException {
217       throw new IOException("Method not implemented!");
218     }
219 
220     /**
221      * Returns the value of this socket's {@code fd} field.
222      *
223      * @return  the value of this socket's {@code fd} field.
224      * @see     java.net.SocketImpl#fd
225      */
getFileDescriptor()226     protected FileDescriptor getFileDescriptor() {
227         return fd;
228     }
229 
230     // Android-added: getFD$() for testing.
231     /**
232      * Gets socket's underlying {@link FileDescriptor}.
233      *
234      * @hide used by java.nio tests
235      *
236      * @return socket's underlying {@link FileDescriptor}.
237      */
getFD$()238     public FileDescriptor getFD$() {
239         return fd;
240     }
241 
242     /**
243      * Returns the value of this socket's {@code address} field.
244      *
245      * @return  the value of this socket's {@code address} field.
246      * @see     java.net.SocketImpl#address
247      */
getInetAddress()248     protected InetAddress getInetAddress() {
249         return address;
250     }
251 
252     /**
253      * Returns the value of this socket's {@code port} field.
254      *
255      * @return  the value of this socket's {@code port} field.
256      * @see     java.net.SocketImpl#port
257      */
getPort()258     protected int getPort() {
259         return port;
260     }
261 
262     /**
263      * Returns whether or not this SocketImpl supports sending
264      * urgent data. By default, false is returned
265      * unless the method is overridden in a sub-class
266      *
267      * @return  true if urgent data supported
268      * @see     java.net.SocketImpl#address
269      * @since 1.4
270      */
supportsUrgentData()271     protected boolean supportsUrgentData () {
272         return false; // must be overridden in sub-class
273     }
274 
275     /**
276      * Send one byte of urgent data on the socket.
277      * The byte to be sent is the low eight bits of the parameter
278      * @param data The byte of data to send
279      * @exception IOException if there is an error
280      *  sending the data.
281      * @since 1.4
282      */
sendUrgentData(int data)283     protected abstract void sendUrgentData (int data) throws IOException;
284 
285     /**
286      * Returns the value of this socket's {@code localport} field.
287      *
288      * @return  the value of this socket's {@code localport} field.
289      * @see     java.net.SocketImpl#localport
290      */
getLocalPort()291     protected int getLocalPort() {
292         return localport;
293     }
294 
setSocket(Socket soc)295     void setSocket(Socket soc) {
296         this.socket = soc;
297     }
298 
getSocket()299     Socket getSocket() {
300         return socket;
301     }
302 
setServerSocket(ServerSocket soc)303     void setServerSocket(ServerSocket soc) {
304         this.serverSocket = soc;
305     }
306 
getServerSocket()307     ServerSocket getServerSocket() {
308         return serverSocket;
309     }
310 
311     /**
312      * Returns the address and port of this socket as a {@code String}.
313      *
314      * @return  a string representation of this socket.
315      */
toString()316     public String toString() {
317         return "Socket[addr=" + getInetAddress() +
318             ",port=" + getPort() + ",localport=" + getLocalPort()  + "]";
319     }
320 
reset()321     void reset() throws IOException {
322         address = null;
323         port = 0;
324         localport = 0;
325     }
326 
327     /**
328      * Sets performance preferences for this socket.
329      *
330      * <p> Sockets use the TCP/IP protocol by default.  Some implementations
331      * may offer alternative protocols which have different performance
332      * characteristics than TCP/IP.  This method allows the application to
333      * express its own preferences as to how these tradeoffs should be made
334      * when the implementation chooses from the available protocols.
335      *
336      * <p> Performance preferences are described by three integers
337      * whose values indicate the relative importance of short connection time,
338      * low latency, and high bandwidth.  The absolute values of the integers
339      * are irrelevant; in order to choose a protocol the values are simply
340      * compared, with larger values indicating stronger preferences. Negative
341      * values represent a lower priority than positive values. If the
342      * application prefers short connection time over both low latency and high
343      * bandwidth, for example, then it could invoke this method with the values
344      * {@code (1, 0, 0)}.  If the application prefers high bandwidth above low
345      * latency, and low latency above short connection time, then it could
346      * invoke this method with the values {@code (0, 1, 2)}.
347      *
348      * By default, this method does nothing, unless it is overridden in a
349      * a sub-class.
350      *
351      * @param  connectionTime
352      *         An {@code int} expressing the relative importance of a short
353      *         connection time
354      *
355      * @param  latency
356      *         An {@code int} expressing the relative importance of low
357      *         latency
358      *
359      * @param  bandwidth
360      *         An {@code int} expressing the relative importance of high
361      *         bandwidth
362      *
363      * @since 1.5
364      */
setPerformancePreferences(int connectionTime, int latency, int bandwidth)365     protected void setPerformancePreferences(int connectionTime,
366                                           int latency,
367                                           int bandwidth)
368     {
369         /* Not implemented yet */
370     }
371 
setOption(SocketOption<T> name, T value)372     <T> void setOption(SocketOption<T> name, T value) throws IOException {
373         if (name == StandardSocketOptions.SO_KEEPALIVE) {
374             setOption(SocketOptions.SO_KEEPALIVE, value);
375         } else if (name == StandardSocketOptions.SO_SNDBUF) {
376             setOption(SocketOptions.SO_SNDBUF, value);
377         } else if (name == StandardSocketOptions.SO_RCVBUF) {
378             setOption(SocketOptions.SO_RCVBUF, value);
379         } else if (name == StandardSocketOptions.SO_REUSEADDR) {
380             setOption(SocketOptions.SO_REUSEADDR, value);
381         } else if (name == StandardSocketOptions.SO_LINGER) {
382             setOption(SocketOptions.SO_LINGER, value);
383         } else if (name == StandardSocketOptions.IP_TOS) {
384             setOption(SocketOptions.IP_TOS, value);
385         } else if (name == StandardSocketOptions.TCP_NODELAY) {
386             setOption(SocketOptions.TCP_NODELAY, value);
387         } else {
388             throw new UnsupportedOperationException("unsupported option");
389         }
390     }
391 
getOption(SocketOption<T> name)392     <T> T getOption(SocketOption<T> name) throws IOException {
393         if (name == StandardSocketOptions.SO_KEEPALIVE) {
394             return (T)getOption(SocketOptions.SO_KEEPALIVE);
395         } else if (name == StandardSocketOptions.SO_SNDBUF) {
396             return (T)getOption(SocketOptions.SO_SNDBUF);
397         } else if (name == StandardSocketOptions.SO_RCVBUF) {
398             return (T)getOption(SocketOptions.SO_RCVBUF);
399         } else if (name == StandardSocketOptions.SO_REUSEADDR) {
400             return (T)getOption(SocketOptions.SO_REUSEADDR);
401         } else if (name == StandardSocketOptions.SO_LINGER) {
402             return (T)getOption(SocketOptions.SO_LINGER);
403         } else if (name == StandardSocketOptions.IP_TOS) {
404             return (T)getOption(SocketOptions.IP_TOS);
405         } else if (name == StandardSocketOptions.TCP_NODELAY) {
406             return (T)getOption(SocketOptions.TCP_NODELAY);
407         } else {
408             throw new UnsupportedOperationException("unsupported option");
409         }
410     }
411 }
412