Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / gnu / java / net / PlainSocketImpl.java
blobf7e6cb89052a4129800c3b7ce648e1ec98a92203
1 /* PlainSocketImpl.java -- Default socket implementation
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
3 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA.
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
25 combination.
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version. */
40 package gnu.java.net;
42 import java.io.InputStream;
43 import java.io.IOException;
44 import java.io.OutputStream;
45 import java.net.InetAddress;
46 import java.net.InetSocketAddress;
47 import java.net.SocketAddress;
48 import java.net.SocketException;
49 import java.net.SocketImpl;
50 import java.net.SocketOptions;
51 import gnu.classpath.Configuration;
53 /**
54 * Written using on-line Java Platform 1.2 API Specification, as well
55 * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
56 * Status: Believed complete and correct.
59 /**
60 * Unless the application installs its own SocketImplFactory, this is the
61 * default socket implemetation that will be used. It simply uses a
62 * combination of Java and native routines to implement standard BSD
63 * style sockets of family AF_INET and types SOCK_STREAM and SOCK_DGRAM
65 * @author Per Bothner <bothner@cygnus.com>
66 * @author Nic Ferrier <nferrier@tapsellferrier.co.uk>
67 * @author Aaron M. Renn <arenn@urbanophile.com>
69 public final class PlainSocketImpl extends SocketImpl
71 // Static initializer to load native library.
72 static
74 if (Configuration.INIT_LOAD_LIBRARY)
76 System.loadLibrary("javanet");
80 // These fields are mirrored for use in native code to avoid cpp conflicts
81 // when the #defines in system header files are the same as the public fields.
82 static final int _Jv_TCP_NODELAY_ = SocketOptions.TCP_NODELAY,
83 _Jv_SO_BINDADDR_ = SocketOptions.SO_BINDADDR,
84 _Jv_SO_REUSEADDR_ = SocketOptions.SO_REUSEADDR,
85 _Jv_SO_BROADCAST_ = SocketOptions.SO_BROADCAST,
86 _Jv_SO_OOBINLINE_ = SocketOptions.SO_OOBINLINE,
87 _Jv_IP_MULTICAST_IF_ = SocketOptions.IP_MULTICAST_IF,
88 _Jv_IP_MULTICAST_IF2_ = SocketOptions.IP_MULTICAST_IF2,
89 _Jv_IP_MULTICAST_LOOP_ = SocketOptions.IP_MULTICAST_LOOP,
90 _Jv_IP_TOS_ = SocketOptions.IP_TOS,
91 _Jv_SO_LINGER_ = SocketOptions.SO_LINGER,
92 _Jv_SO_TIMEOUT_ = SocketOptions.SO_TIMEOUT,
93 _Jv_SO_SNDBUF_ = SocketOptions.SO_SNDBUF,
94 _Jv_SO_RCVBUF_ = SocketOptions.SO_RCVBUF,
95 _Jv_SO_KEEPALIVE_ = SocketOptions.SO_KEEPALIVE;
97 /**
98 * The OS file handle representing the socket.
99 * This is used for reads and writes to/from the socket and
100 * to close it.
102 * When the socket is closed this is reset to -1.
104 int native_fd = -1;
106 // This value is set/read by setOption/getOption.
107 int timeout = 0;
109 // localAddress cache
110 InetAddress localAddress;
113 * A cached copy of the in stream for reading from the socket.
115 private InputStream in;
118 * A cached copy of the out stream for writing to the socket.
120 private OutputStream out;
123 * Indicates whether a channel initiated whatever operation
124 * is being invoked on this socket.
126 private boolean inChannelOperation;
129 * Indicates whether we should ignore whether any associated
130 * channel is set to non-blocking mode. Certain operations
131 * throw an <code>IllegalBlockingModeException</code> if the
132 * associated channel is in non-blocking mode, <i>except</i>
133 * if the operation is invoked by the channel itself.
135 public final boolean isInChannelOperation()
137 return inChannelOperation;
141 * Sets our indicator of whether an I/O operation is being
142 * initiated by a channel.
144 public final void setInChannelOperation(boolean b)
146 inChannelOperation = b;
150 * Default do nothing constructor
152 public PlainSocketImpl()
156 protected void finalize() throws Throwable
158 synchronized (this)
160 if (native_fd != -1)
163 close();
165 catch (IOException ex)
169 super.finalize();
172 public int getNativeFD()
174 return native_fd;
178 * Sets the specified option on a socket to the passed in object. For
179 * options that take an integer argument, the passed in object is an
180 * Integer. The option_id parameter is one of the defined constants in
181 * this interface.
183 * @param option_id The identifier of the option
184 * @param val The value to set the option to
186 * @exception SocketException If an error occurs
188 public native void setOption(int optID, Object value) throws SocketException;
191 * Returns the current setting of the specified option. The Object returned
192 * will be an Integer for options that have integer values. The option_id
193 * is one of the defined constants in this interface.
195 * @param option_id The option identifier
197 * @return The current value of the option
199 * @exception SocketException If an error occurs
201 public native Object getOption(int optID) throws SocketException;
204 * Flushes the input stream and closes it. If you read from the input stream
205 * after calling this method a <code>IOException</code> will be thrown.
207 * @throws IOException if an error occurs
209 public native void shutdownInput() throws IOException;
212 * Flushes the output stream and closes it. If you write to the output stream
213 * after calling this method a <code>IOException</code> will be thrown.
215 * @throws IOException if an error occurs
217 public native void shutdownOutput() throws IOException;
220 * Creates a new socket that is not bound to any local address/port and
221 * is not connected to any remote address/port. This will be created as
222 * a stream socket if the stream parameter is true, or a datagram socket
223 * if the stream parameter is false.
225 * @param stream true for a stream socket, false for a datagram socket
227 protected native void create(boolean stream) throws IOException;
230 * Connects to the remote hostname and port specified as arguments.
232 * @param hostname The remote hostname to connect to
233 * @param port The remote port to connect to
235 * @exception IOException If an error occurs
237 protected void connect(String host, int port) throws IOException
239 connect(InetAddress.getByName(host), port);
243 * Connects to the remote address and port specified as arguments.
245 * @param addr The remote address to connect to
246 * @param port The remote port to connect to
248 * @exception IOException If an error occurs
250 protected void connect(InetAddress host, int port) throws IOException
252 connect (new InetSocketAddress (host, port), 0);
256 * Connects to the remote socket address with a specified timeout.
258 * @param timeout The timeout to use for this connect, 0 means infinite.
260 * @exception IOException If an error occurs
262 protected native void connect(SocketAddress addr, int timeout) throws IOException;
265 * Binds to the specified port on the specified addr. Note that this addr
266 * must represent a local IP address. **** How bind to INADDR_ANY? ****
268 * @param addr The address to bind to
269 * @param port The port number to bind to
271 * @exception IOException If an error occurs
273 protected native void bind(InetAddress host, int port)
274 throws IOException;
277 * Starts listening for connections on a socket. The queuelen parameter
278 * is how many pending connections will queue up waiting to be serviced
279 * before being accept'ed. If the queue of pending requests exceeds this
280 * number, additional connections will be refused.
282 * @param queuelen The length of the pending connection queue
284 * @exception IOException If an error occurs
286 protected native void listen(int queuelen)
287 throws IOException;
290 * Accepts a new connection on this socket and returns in in the
291 * passed in SocketImpl.
293 * @param impl The SocketImpl object to accept this connection.
295 protected void accept(SocketImpl impl)
296 throws IOException
298 accept((PlainSocketImpl) impl);
301 private native void accept(PlainSocketImpl impl)
302 throws IOException;
305 * Returns the number of bytes that the caller can read from this socket
306 * without blocking.
308 * @return The number of readable bytes before blocking
310 * @exception IOException If an error occurs
312 protected native int available() throws IOException;
315 * Closes the socket. This will cause any InputStream or OutputStream
316 * objects for this Socket to be closed as well.
317 * <p>
318 * Note that if the SO_LINGER option is set on this socket, then the
319 * operation could block.
321 * @exception IOException If an error occurs
323 protected native void close() throws IOException;
325 protected native void sendUrgentData(int data) throws IOException;
328 * Returns an InputStream object for reading from this socket. This will
329 * be an instance of SocketInputStream.
331 * @return An input stream attached to the socket.
333 * @exception IOException If an error occurs
335 protected synchronized InputStream getInputStream() throws IOException
337 if (in == null)
338 in = new SocketInputStream();
340 return in;
344 * Returns an OutputStream object for writing to this socket. This will
345 * be an instance of SocketOutputStream.
347 * @return An output stream attached to the socket.
349 * @exception IOException If an error occurs
351 protected synchronized OutputStream getOutputStream() throws IOException
353 if (out == null)
354 out = new SocketOutputStream();
356 return out;
360 * This class contains an implementation of <code>InputStream</code> for
361 * sockets. It in an internal only class used by <code>PlainSocketImpl</code>.
363 * @author Nic Ferrier <nferrier@tapsellferrier.co.uk>
365 final class SocketInputStream
366 extends InputStream
369 * Returns the number of bytes available to be read before blocking
371 public int available() throws IOException
373 return PlainSocketImpl.this.available();
377 * This method not only closes the stream, it closes the underlying socket
378 * (and thus any connection) and invalidates any other Input/Output streams
379 * for the underlying impl object
381 public void close() throws IOException
383 PlainSocketImpl.this.close();
387 * Reads the next byte of data and returns it as an int.
389 * @return The byte read (as an int) or -1 if end of stream);
391 * @exception IOException If an error occurs.
393 public native int read() throws IOException;
396 * Reads up to len bytes of data into the caller supplied buffer starting
397 * at offset bytes from the start of the buffer
399 * @param buf The buffer
400 * @param offset Offset into the buffer to start reading from
401 * @param len The number of bytes to read
403 * @return The number of bytes actually read or -1 if end of stream
405 * @exception IOException If an error occurs.
407 public native int read(byte[] buf, int offset, int len) throws IOException;
411 * This class is used internally by <code>PlainSocketImpl</code> to be the
412 * <code>OutputStream</code> subclass returned by its
413 * <code>getOutputStream method</code>. It expects only to be used in that
414 * context.
416 * @author Nic Ferrier <nferrier@tapsellferrier.co.uk>
418 final class SocketOutputStream
419 extends OutputStream
422 * This method closes the stream and the underlying socket connection. This
423 * action also effectively closes any other InputStream or OutputStream
424 * object associated with the connection.
426 * @exception IOException If an error occurs
428 public void close() throws IOException
430 PlainSocketImpl.this.close();
434 * Writes a byte (passed in as an int) to the given output stream
436 * @param b The byte to write
438 * @exception IOException If an error occurs
440 public native void write(int b) throws IOException;
443 * Writes len number of bytes from the array buf to the stream starting
444 * at offset bytes into the buffer.
446 * @param buf The buffer
447 * @param offset Offset into the buffer to start writing from
448 * @param len The number of bytes to write
450 * @exception IOException If an error occurs.
452 public native void write(byte[] buf, int offset, int len) throws IOException;