FSF GCC merge 02/23/03
[official-gcc.git] / libjava / java / net / SocketImpl.java
blob12dcb0b9a5c72c383ccdfaea01caefa699c82566
1 /* SocketImpl.java -- Abstract socket implementation class
2 Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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.
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.
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. */
38 package java.net;
40 import java.io.*;
42 /* Written using on-line Java Platform 1.2 API Specification.
43 * Believed complete and correct.
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.
59 * @author Aaron M. Renn (arenn@urbanophile.com)
60 * @author Per Bothner <bothner@cygnus.com>
62 public abstract class SocketImpl implements SocketOptions
64 /**
65 * The address of the remote end of the socket connection
67 protected InetAddress address;
69 /**
70 * A FileDescriptor object representing this socket connection.
72 protected FileDescriptor fd;
74 /**
75 * The port number the socket is bound to locally
77 protected int localport = -1;
79 /**
80 * The port number of the remote end of the socket connection
82 protected int port;
84 /**
85 * Default, no-argument constructor for use by subclasses.
87 public SocketImpl()
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.
97 * @param stream true for a stream socket, false for a datagram socket
99 * @exception IOException If an error occurs
101 protected abstract void create(boolean stream) throws IOException;
104 * Connects to the remote hostname and port specified as arguments.
106 * @param host The remote hostname to connect to
107 * @param port The remote port to connect to
109 * @exception IOException If an error occurs
111 protected abstract void connect(String host, int port) throws IOException;
114 * Connects to the remote address and port specified as arguments.
116 * @param host The remote address to connect to
117 * @param port The remote port to connect to
119 * @exception IOException If an error occurs
121 protected abstract void connect(InetAddress host, int port)
122 throws IOException;
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.
129 * @param address Data of remote host
130 * @param timeout time to wait to stop connecting
132 * @exception IOException If an error occurs
134 * @since 1.4
136 protected abstract void connect(SocketAddress address, int timeout)
137 throws IOException;
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).
146 * @param host The address to bind to
147 * @param port The port number to bind to
149 * @exception IOException If an error occurs
151 protected abstract void bind(InetAddress host, int port) throws IOException;
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.
159 * @param backlog The length of the pending connection queue
161 * @exception IOException If an error occurs
163 protected abstract void listen(int backlog) throws IOException;
166 * Accepts a connection on this socket.
168 * @param s The implementation object for the accepted connection.
170 * @exception IOException If an error occurs
172 protected abstract void accept(SocketImpl s) throws IOException;
175 * Returns an <code>InputStream</code> object for reading from this socket.
177 * @return An <code>InputStream</code> for reading from this socket.
179 * @exception IOException If an error occurs
181 protected abstract InputStream getInputStream() throws IOException;
184 * Returns an <code>OutputStream</code> object for writing to this socket
186 * @return An <code>OutputStream</code> for writing to this socket.
188 * @exception IOException If an error occurs.
190 protected abstract OutputStream getOutputStream() throws IOException;
193 * Returns the number of bytes that the caller can read from this socket
194 * without blocking.
196 * @return The number of readable bytes before blocking
198 * @exception IOException If an error occurs
200 protected abstract int available() throws IOException;
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.
209 * @exception IOException If an error occurs
211 protected abstract void close() throws IOException;
214 * Returns the FileDescriptor objects for this socket.
216 * @return A FileDescriptor for this socket.
218 protected FileDescriptor getFileDescriptor() { return fd; }
221 * Returns the remote address this socket is connected to
223 * @return The remote address
225 protected InetAddress getInetAddress() { return address; }
228 * Returns the remote port this socket is connected to
230 * @return The remote port
232 protected int getPort() { return port; }
235 * Returns true or false when this socket supports sending urgent data
236 * or not.
238 * @since 1.4
240 protected boolean supportsUrgentData()
242 // This method has to be overwritten by socket classes that support
243 // sending urgend data.
244 return false;
248 * Sends one byte of urgent data to the socket.
250 * @param data The byte to send, the low eight bits of it
252 * @exception IOException If an error occurs
254 * @since 1.4
256 protected abstract void sendUrgentData(int data)
257 throws IOException;
260 * Returns the local port this socket is bound to
262 * @return The local port
264 protected int getLocalPort() { return localport; }
267 * Returns a <code>String</code> representing the remote host and port of
268 * this socket.
270 * @return A <code>String</code> for this socket.
272 public String toString()
274 return "[addr=" + address
275 + ",port=" + port
276 + ",localport=" + localport + "]";
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.
286 * @param option_id The identifier of the option
287 * @param val The value to set the option to
289 * @exception SocketException If an error occurs
290 * @XXX This redeclaration from SocketOptions is a workaround to a gcj bug.
292 public abstract void setOption(int option_id, Object val)
293 throws SocketException;
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.
302 * @param option_id The option identifier
304 * @return The current value of the option
306 * @exception SocketException If an error occurs
307 * @XXX This redeclaration from SocketOptions is a workaround to a gcj bug.
309 public abstract Object getOption(int option_id) throws SocketException;
312 * Shut down the input side of this socket. Subsequent reads will
313 * return end-of-file.
315 * @exception IOException if an error occurs
317 protected abstract void shutdownInput () throws IOException;
320 * Shut down the output side of this socket. Subsequent writes will
321 * fail with an IOException.
323 * @exception IOException if an error occurs
325 protected abstract void shutdownOutput () throws IOException;