| 1 | /* SocketImpl.java -- Abstract socket implementation class
|
|---|
| 2 | Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
|
|---|
| 3 |
|
|---|
| 4 | This file is part of GNU Classpath.
|
|---|
| 5 |
|
|---|
| 6 | GNU Classpath is free software; you can redistribute it and/or modify
|
|---|
| 7 | it under the terms of the GNU General Public License as published by
|
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option)
|
|---|
| 9 | any later version.
|
|---|
| 10 |
|
|---|
| 11 | GNU Classpath is distributed in the hope that it will be useful, but
|
|---|
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 14 | General Public License for more details.
|
|---|
| 15 |
|
|---|
| 16 | You should have received a copy of the GNU General Public License
|
|---|
| 17 | along with GNU Classpath; see the file COPYING. If not, write to the
|
|---|
| 18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 19 | 02111-1307 USA.
|
|---|
| 20 |
|
|---|
| 21 | Linking this library statically or dynamically with other modules is
|
|---|
| 22 | making a combined work based on this library. Thus, the terms and
|
|---|
| 23 | conditions of the GNU General Public License cover the whole
|
|---|
| 24 | combination.
|
|---|
| 25 |
|
|---|
| 26 | As a special exception, the copyright holders of this library give you
|
|---|
| 27 | permission to link this library with independent modules to produce an
|
|---|
| 28 | executable, regardless of the license terms of these independent
|
|---|
| 29 | modules, and to copy and distribute the resulting executable under
|
|---|
| 30 | terms of your choice, provided that you also meet, for each linked
|
|---|
| 31 | independent module, the terms and conditions of the license of that
|
|---|
| 32 | module. An independent module is a module which is not derived from
|
|---|
| 33 | or based on this library. If you modify this library, you may extend
|
|---|
| 34 | this exception to your version of the library, but you are not
|
|---|
| 35 | obligated to do so. If you do not wish to do so, delete this
|
|---|
| 36 | exception statement from your version. */
|
|---|
| 37 |
|
|---|
| 38 | package java.net;
|
|---|
| 39 |
|
|---|
| 40 | import java.io.*;
|
|---|
| 41 |
|
|---|
| 42 | /* Written using on-line Java Platform 1.2 API Specification.
|
|---|
| 43 | * Believed complete and correct.
|
|---|
| 44 | */
|
|---|
| 45 |
|
|---|
| 46 | /**
|
|---|
| 47 | * This abstract class serves as the parent class for socket implementations.
|
|---|
| 48 | * The implementation class serves an intermediary to native routines that
|
|---|
| 49 | * perform system specific socket operations.
|
|---|
| 50 | * <p>
|
|---|
| 51 | * A default implementation is provided by the system, but this can be
|
|---|
| 52 | * changed via installing a <code>SocketImplFactory</code> (through a call
|
|---|
| 53 | * to the static method <code>Socket.setSocketImplFactory</code>). A
|
|---|
| 54 | * subclass of <code>Socket</code> can also pass in a <code>SocketImpl</code>
|
|---|
| 55 | * to the <code>Socket(SocketImpl)</code> constructor to use an
|
|---|
| 56 | * implementation different from the system default without installing
|
|---|
| 57 | * a factory.
|
|---|
| 58 | *
|
|---|
| 59 | * @author Aaron M. Renn ([email protected])
|
|---|
| 60 | * @author Per Bothner <[email protected]>
|
|---|
| 61 | */
|
|---|
| 62 | public abstract class SocketImpl implements SocketOptions
|
|---|
| 63 | {
|
|---|
| 64 | /**
|
|---|
| 65 | * The address of the remote end of the socket connection
|
|---|
| 66 | */
|
|---|
| 67 | protected InetAddress address;
|
|---|
| 68 |
|
|---|
| 69 | /**
|
|---|
| 70 | * A FileDescriptor object representing this socket connection.
|
|---|
| 71 | */
|
|---|
| 72 | protected FileDescriptor fd;
|
|---|
| 73 |
|
|---|
| 74 | /**
|
|---|
| 75 | * The port number the socket is bound to locally
|
|---|
| 76 | */
|
|---|
| 77 | protected int localport = -1;
|
|---|
| 78 |
|
|---|
| 79 | /**
|
|---|
| 80 | * The port number of the remote end of the socket connection
|
|---|
| 81 | */
|
|---|
| 82 | protected int port;
|
|---|
| 83 |
|
|---|
| 84 | /**
|
|---|
| 85 | * Default, no-argument constructor for use by subclasses.
|
|---|
| 86 | */
|
|---|
| 87 | public SocketImpl()
|
|---|
| 88 | {
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | /**
|
|---|
| 92 | * Creates a new socket that is not bound to any local address/port and
|
|---|
| 93 | * is not connected to any remote address/port. This will be created as
|
|---|
| 94 | * a stream socket if the stream parameter is true, or a datagram socket
|
|---|
| 95 | * if the stream parameter is false.
|
|---|
| 96 | *
|
|---|
| 97 | * @param stream true for a stream socket, false for a datagram socket
|
|---|
| 98 | *
|
|---|
| 99 | * @exception IOException If an error occurs
|
|---|
| 100 | */
|
|---|
| 101 | protected abstract void create(boolean stream) throws IOException;
|
|---|
| 102 |
|
|---|
| 103 | /**
|
|---|
| 104 | * Connects to the remote hostname and port specified as arguments.
|
|---|
| 105 | *
|
|---|
| 106 | * @param host The remote hostname to connect to
|
|---|
| 107 | * @param port The remote port to connect to
|
|---|
| 108 | *
|
|---|
| 109 | * @exception IOException If an error occurs
|
|---|
| 110 | */
|
|---|
| 111 | protected abstract void connect(String host, int port) throws IOException;
|
|---|
| 112 |
|
|---|
| 113 | /**
|
|---|
| 114 | * Connects to the remote address and port specified as arguments.
|
|---|
| 115 | *
|
|---|
| 116 | * @param host The remote address to connect to
|
|---|
| 117 | * @param port The remote port to connect to
|
|---|
| 118 | *
|
|---|
| 119 | * @exception IOException If an error occurs
|
|---|
| 120 | */
|
|---|
| 121 | protected abstract void connect(InetAddress host, int port)
|
|---|
| 122 | throws IOException;
|
|---|
| 123 |
|
|---|
| 124 | /**
|
|---|
| 125 | * Connects to the socket to the host specified in address. This
|
|---|
| 126 | * method blocks until successful connected or the timeout occurs.
|
|---|
| 127 | * A timeout of zero means no timout.
|
|---|
| 128 | *
|
|---|
| 129 | * @param address Data of remote host
|
|---|
| 130 | * @param timeout time to wait to stop connecting
|
|---|
| 131 | *
|
|---|
| 132 | * @exception IOException If an error occurs
|
|---|
| 133 | *
|
|---|
| 134 | * @since 1.4
|
|---|
| 135 | */
|
|---|
| 136 | protected abstract void connect(SocketAddress address, int timeout)
|
|---|
| 137 | throws IOException;
|
|---|
| 138 |
|
|---|
| 139 | /**
|
|---|
| 140 | * Binds to the specified port on the specified addr. Note that this addr
|
|---|
| 141 | * must represent a local IP address.
|
|---|
| 142 | * <p>
|
|---|
| 143 | * Note that it is unspecified how to bind to all interfaces on the localhost
|
|---|
| 144 | * (INADDR_ANY).
|
|---|
| 145 | *
|
|---|
| 146 | * @param host The address to bind to
|
|---|
| 147 | * @param port The port number to bind to
|
|---|
| 148 | *
|
|---|
| 149 | * @exception IOException If an error occurs
|
|---|
| 150 | */
|
|---|
| 151 | protected abstract void bind(InetAddress host, int port) throws IOException;
|
|---|
| 152 |
|
|---|
| 153 | /**
|
|---|
| 154 | * Starts listening for connections on a socket. The backlog parameter
|
|---|
| 155 | * is how many pending connections will queue up waiting to be serviced
|
|---|
| 156 | * before being accept'ed. If the queue of pending requests exceeds this
|
|---|
| 157 | * number, additional connections will be refused.
|
|---|
| 158 | *
|
|---|
| 159 | * @param backlog The length of the pending connection queue
|
|---|
| 160 | *
|
|---|
| 161 | * @exception IOException If an error occurs
|
|---|
| 162 | */
|
|---|
| 163 | protected abstract void listen(int backlog) throws IOException;
|
|---|
| 164 |
|
|---|
| 165 | /**
|
|---|
| 166 | * Accepts a connection on this socket.
|
|---|
| 167 | *
|
|---|
| 168 | * @param s The implementation object for the accepted connection.
|
|---|
| 169 | *
|
|---|
| 170 | * @exception IOException If an error occurs
|
|---|
| 171 | */
|
|---|
| 172 | protected abstract void accept(SocketImpl s) throws IOException;
|
|---|
| 173 |
|
|---|
| 174 | /**
|
|---|
| 175 | * Returns an <code>InputStream</code> object for reading from this socket.
|
|---|
| 176 | *
|
|---|
| 177 | * @return An <code>InputStream</code> for reading from this socket.
|
|---|
| 178 | *
|
|---|
| 179 | * @exception IOException If an error occurs
|
|---|
| 180 | */
|
|---|
| 181 | protected abstract InputStream getInputStream() throws IOException;
|
|---|
| 182 |
|
|---|
| 183 | /**
|
|---|
| 184 | * Returns an <code>OutputStream</code> object for writing to this socket
|
|---|
| 185 | *
|
|---|
| 186 | * @return An <code>OutputStream</code> for writing to this socket.
|
|---|
| 187 | *
|
|---|
| 188 | * @exception IOException If an error occurs.
|
|---|
| 189 | */
|
|---|
| 190 | protected abstract OutputStream getOutputStream() throws IOException;
|
|---|
| 191 |
|
|---|
| 192 | /**
|
|---|
| 193 | * Returns the number of bytes that the caller can read from this socket
|
|---|
| 194 | * without blocking.
|
|---|
| 195 | *
|
|---|
| 196 | * @return The number of readable bytes before blocking
|
|---|
| 197 | *
|
|---|
| 198 | * @exception IOException If an error occurs
|
|---|
| 199 | */
|
|---|
| 200 | protected abstract int available() throws IOException;
|
|---|
| 201 |
|
|---|
| 202 | /**
|
|---|
| 203 | * Closes the socket. This will normally cause any resources, such as the
|
|---|
| 204 | * InputStream, OutputStream and associated file descriptors to be freed.
|
|---|
| 205 | * <p>
|
|---|
| 206 | * Note that if the SO_LINGER option is set on this socket, then the
|
|---|
| 207 | * operation could block.
|
|---|
| 208 | *
|
|---|
| 209 | * @exception IOException If an error occurs
|
|---|
| 210 | */
|
|---|
| 211 | protected abstract void close() throws IOException;
|
|---|
| 212 |
|
|---|
| 213 | /**
|
|---|
| 214 | * Returns the FileDescriptor objects for this socket.
|
|---|
| 215 | *
|
|---|
| 216 | * @return A FileDescriptor for this socket.
|
|---|
| 217 | */
|
|---|
| 218 | protected FileDescriptor getFileDescriptor() { return fd; }
|
|---|
| 219 |
|
|---|
| 220 | /**
|
|---|
| 221 | * Returns the remote address this socket is connected to
|
|---|
| 222 | *
|
|---|
| 223 | * @return The remote address
|
|---|
| 224 | */
|
|---|
| 225 | protected InetAddress getInetAddress() { return address; }
|
|---|
| 226 |
|
|---|
| 227 | /**
|
|---|
| 228 | * Returns the remote port this socket is connected to
|
|---|
| 229 | *
|
|---|
| 230 | * @return The remote port
|
|---|
| 231 | */
|
|---|
| 232 | protected int getPort() { return port; }
|
|---|
| 233 |
|
|---|
| 234 | /**
|
|---|
| 235 | * Returns true or false when this socket supports sending urgent data
|
|---|
| 236 | * or not.
|
|---|
| 237 | *
|
|---|
| 238 | * @since 1.4
|
|---|
| 239 | */
|
|---|
| 240 | protected boolean supportsUrgentData()
|
|---|
| 241 | {
|
|---|
| 242 | // This method has to be overwritten by socket classes that support
|
|---|
| 243 | // sending urgend data.
|
|---|
| 244 | return false;
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | /**
|
|---|
| 248 | * Sends one byte of urgent data to the socket.
|
|---|
| 249 | *
|
|---|
| 250 | * @param data The byte to send, the low eight bits of it
|
|---|
| 251 | *
|
|---|
| 252 | * @exception IOException If an error occurs
|
|---|
| 253 | *
|
|---|
| 254 | * @since 1.4
|
|---|
| 255 | */
|
|---|
| 256 | protected abstract void sendUrgentData(int data)
|
|---|
| 257 | throws IOException;
|
|---|
| 258 |
|
|---|
| 259 | /**
|
|---|
| 260 | * Returns the local port this socket is bound to
|
|---|
| 261 | *
|
|---|
| 262 | * @return The local port
|
|---|
| 263 | */
|
|---|
| 264 | protected int getLocalPort() { return localport; }
|
|---|
| 265 |
|
|---|
| 266 | /**
|
|---|
| 267 | * Returns a <code>String</code> representing the remote host and port of
|
|---|
| 268 | * this socket.
|
|---|
| 269 | *
|
|---|
| 270 | * @return A <code>String</code> for this socket.
|
|---|
| 271 | */
|
|---|
| 272 | public String toString()
|
|---|
| 273 | {
|
|---|
| 274 | return "[addr=" + address
|
|---|
| 275 | + ",port=" + port
|
|---|
| 276 | + ",localport=" + localport + "]";
|
|---|
| 277 | }
|
|---|
| 278 |
|
|---|
| 279 | /**
|
|---|
| 280 | * Sets the specified option on a socket to the passed in object. For
|
|---|
| 281 | * options that take an integer argument, the passed in object is an
|
|---|
| 282 | * <code>Integer</code>. For options that are set to on or off, the
|
|---|
| 283 | * value passed will be a <code>Boolean</code>. The <code>option_id</code>
|
|---|
| 284 | * parameter is one of the defined constants in the superinterface.
|
|---|
| 285 | *
|
|---|
| 286 | * @param option_id The identifier of the option
|
|---|
| 287 | * @param val The value to set the option to
|
|---|
| 288 | *
|
|---|
| 289 | * @exception SocketException If an error occurs
|
|---|
| 290 | * @XXX This redeclaration from SocketOptions is a workaround to a gcj bug.
|
|---|
| 291 | */
|
|---|
| 292 | public abstract void setOption(int option_id, Object val)
|
|---|
| 293 | throws SocketException;
|
|---|
| 294 |
|
|---|
| 295 | /**
|
|---|
| 296 | * Returns the current setting of the specified option. The
|
|---|
| 297 | * <code>Object</code> returned will be an <code>Integer</code> for options
|
|---|
| 298 | * that have integer values. For options that are set to on or off, a
|
|---|
| 299 | * <code>Boolean</code> will be returned. The <code>option_id</code>
|
|---|
| 300 | * is one of the defined constants in the superinterface.
|
|---|
| 301 | *
|
|---|
| 302 | * @param option_id The option identifier
|
|---|
| 303 | *
|
|---|
| 304 | * @return The current value of the option
|
|---|
| 305 | *
|
|---|
| 306 | * @exception SocketException If an error occurs
|
|---|
| 307 | * @XXX This redeclaration from SocketOptions is a workaround to a gcj bug.
|
|---|
| 308 | */
|
|---|
| 309 | public abstract Object getOption(int option_id) throws SocketException;
|
|---|
| 310 |
|
|---|
| 311 | /**
|
|---|
| 312 | * Shut down the input side of this socket. Subsequent reads will
|
|---|
| 313 | * return end-of-file.
|
|---|
| 314 | *
|
|---|
| 315 | * @exception IOException if an error occurs
|
|---|
| 316 | */
|
|---|
| 317 | protected abstract void shutdownInput () throws IOException;
|
|---|
| 318 |
|
|---|
| 319 | /**
|
|---|
| 320 | * Shut down the output side of this socket. Subsequent writes will
|
|---|
| 321 | * fail with an IOException.
|
|---|
| 322 | *
|
|---|
| 323 | * @exception IOException if an error occurs
|
|---|
| 324 | */
|
|---|
| 325 | protected abstract void shutdownOutput () throws IOException;
|
|---|
| 326 | }
|
|---|