This commit was manufactured by cvs2svn to create branch
[official-gcc.git] / libjava / gnu / java / net / PlainSocketImpl.java
blob4301a485cf3a747b6b2115917c80577ba2d3dbee
1 /* PlainSocketImpl.java -- Default socket implementation
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004
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;
203 public native void shutdownInput() throws IOException;
205 public native void shutdownOutput() throws IOException;
208 * Creates a new socket that is not bound to any local address/port and
209 * is not connected to any remote address/port. This will be created as
210 * a stream socket if the stream parameter is true, or a datagram socket
211 * if the stream parameter is false.
213 * @param stream true for a stream socket, false for a datagram socket
215 protected native void create(boolean stream) throws IOException;
218 * Connects to the remote hostname and port specified as arguments.
220 * @param hostname The remote hostname to connect to
221 * @param port The remote port to connect to
223 * @exception IOException If an error occurs
225 protected void connect(String host, int port) throws IOException
227 connect(InetAddress.getByName(host), port);
231 * Connects to the remote address and port specified as arguments.
233 * @param addr The remote address to connect to
234 * @param port The remote port to connect to
236 * @exception IOException If an error occurs
238 protected void connect(InetAddress host, int port) throws IOException
240 connect (new InetSocketAddress (host, port), 0);
244 * Connects to the remote socket address with a specified timeout.
246 * @param timeout The timeout to use for this connect, 0 means infinite.
248 * @exception IOException If an error occurs
250 protected native void connect(SocketAddress addr, int timeout) throws IOException;
253 * Binds to the specified port on the specified addr. Note that this addr
254 * must represent a local IP address. **** How bind to INADDR_ANY? ****
256 * @param addr The address to bind to
257 * @param port The port number to bind to
259 * @exception IOException If an error occurs
261 protected native void bind(InetAddress host, int port)
262 throws IOException;
265 * Starts listening for connections on a socket. The queuelen parameter
266 * is how many pending connections will queue up waiting to be serviced
267 * before being accept'ed. If the queue of pending requests exceeds this
268 * number, additional connections will be refused.
270 * @param queuelen The length of the pending connection queue
272 * @exception IOException If an error occurs
274 protected native void listen(int queuelen)
275 throws IOException;
278 * Accepts a new connection on this socket and returns in in the
279 * passed in SocketImpl.
281 * @param impl The SocketImpl object to accept this connection.
283 protected void accept(SocketImpl impl)
284 throws IOException
286 accept((PlainSocketImpl) impl);
289 private native void accept(PlainSocketImpl impl)
290 throws IOException;
293 * Returns the number of bytes that the caller can read from this socket
294 * without blocking.
296 * @return The number of readable bytes before blocking
298 * @exception IOException If an error occurs
300 protected native int available() throws IOException;
303 * Closes the socket. This will cause any InputStream or OutputStream
304 * objects for this Socket to be closed as well.
305 * <p>
306 * Note that if the SO_LINGER option is set on this socket, then the
307 * operation could block.
309 * @exception IOException If an error occurs
311 protected native void close() throws IOException;
313 protected native void sendUrgentData(int data) throws IOException;
316 * Returns an InputStream object for reading from this socket. This will
317 * be an instance of SocketInputStream.
319 * @return An input stream attached to the socket.
321 * @exception IOException If an error occurs
323 protected synchronized InputStream getInputStream() throws IOException
325 if (in == null)
326 in = new SocketInputStream();
328 return in;
332 * Returns an OutputStream object for writing to this socket. This will
333 * be an instance of SocketOutputStream.
335 * @return An output stream attached to the socket.
337 * @exception IOException If an error occurs
339 protected synchronized OutputStream getOutputStream() throws IOException
341 if (out == null)
342 out = new SocketOutputStream();
344 return out;
348 * This class contains an implementation of <code>InputStream</code> for
349 * sockets. It in an internal only class used by <code>PlainSocketImpl</code>.
351 * @author Nic Ferrier <nferrier@tapsellferrier.co.uk>
353 final class SocketInputStream
354 extends InputStream
357 * Returns the number of bytes available to be read before blocking
359 public int available() throws IOException
361 return PlainSocketImpl.this.available();
365 * This method not only closes the stream, it closes the underlying socket
366 * (and thus any connection) and invalidates any other Input/Output streams
367 * for the underlying impl object
369 public void close() throws IOException
371 PlainSocketImpl.this.close();
375 * Reads the next byte of data and returns it as an int.
377 * @return The byte read (as an int) or -1 if end of stream);
379 * @exception IOException If an error occurs.
381 public native int read() throws IOException;
384 * Reads up to len bytes of data into the caller supplied buffer starting
385 * at offset bytes from the start of the buffer
387 * @param buf The buffer
388 * @param offset Offset into the buffer to start reading from
389 * @param len The number of bytes to read
391 * @return The number of bytes actually read or -1 if end of stream
393 * @exception IOException If an error occurs.
395 public native int read(byte[] buf, int offset, int len) throws IOException;
399 * This class is used internally by <code>PlainSocketImpl</code> to be the
400 * <code>OutputStream</code> subclass returned by its
401 * <code>getOutputStream method</code>. It expects only to be used in that
402 * context.
404 * @author Nic Ferrier <nferrier@tapsellferrier.co.uk>
406 final class SocketOutputStream
407 extends OutputStream
410 * This method closes the stream and the underlying socket connection. This
411 * action also effectively closes any other InputStream or OutputStream
412 * object associated with the connection.
414 * @exception IOException If an error occurs
416 public void close() throws IOException
418 PlainSocketImpl.this.close();
422 * Writes a byte (passed in as an int) to the given output stream
424 * @param b The byte to write
426 * @exception IOException If an error occurs
428 public native void write(int b) throws IOException;
431 * Writes len number of bytes from the array buf to the stream starting
432 * at offset bytes into the buffer.
434 * @param buf The buffer
435 * @param offset Offset into the buffer to start writing from
436 * @param len The number of bytes to write
438 * @exception IOException If an error occurs.
440 public native void write(byte[] buf, int offset, int len) throws IOException;