Imported GNU Classpath 0.20
[official-gcc.git] / libjava / classpath / gnu / java / net / PlainSocketImpl.java
blob47d05aa410d850cc6ece2cbb51a5438ceeb154ab
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., 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301 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.SocketAddress;
47 import java.net.SocketException;
48 import java.net.SocketImpl;
50 /**
51 * Written using on-line Java Platform 1.2 API Specification, as well
52 * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
53 * Status: Believed complete and correct.
56 /**
57 * Unless the application installs its own SocketImplFactory, this is the
58 * default socket implemetation that will be used. It simply uses a
59 * combination of Java and native routines to implement standard BSD
60 * style sockets of family AF_INET and types SOCK_STREAM and SOCK_DGRAM
62 * @author Per Bothner (bothner@cygnus.com)
63 * @author Nic Ferrier (nferrier@tapsellferrier.co.uk)
64 * @author Aaron M. Renn (arenn@urbanophile.com)
66 public final class PlainSocketImpl extends SocketImpl
69 /**
70 * The OS file handle representing the socket.
71 * This is used for reads and writes to/from the socket and
72 * to close it.
74 * When the socket is closed this is reset to -1.
76 int native_fd = -1;
78 /**
79 * A cached copy of the in stream for reading from the socket.
81 private InputStream in;
83 /**
84 * A cached copy of the out stream for writing to the socket.
86 private OutputStream out;
88 /**
89 * Indicates whether a channel initiated whatever operation
90 * is being invoked on this socket.
92 private boolean inChannelOperation;
94 /**
95 * Indicates whether we should ignore whether any associated
96 * channel is set to non-blocking mode. Certain operations
97 * throw an <code>IllegalBlockingModeException</code> if the
98 * associated channel is in non-blocking mode, <i>except</i>
99 * if the operation is invoked by the channel itself.
101 public final boolean isInChannelOperation()
103 return inChannelOperation;
107 * Sets our indicator of whether an I/O operation is being
108 * initiated by a channel.
110 public final void setInChannelOperation(boolean b)
112 inChannelOperation = b;
116 * Default do nothing constructor.
118 public PlainSocketImpl()
120 // Nothing to do here.
123 protected void finalize() throws Throwable
125 synchronized (this)
127 if (native_fd != -1)
130 close();
132 catch (IOException ex)
134 // Nothing we can do about it.
137 super.finalize();
140 public int getNativeFD()
142 return native_fd;
146 * Sets the specified option on a socket to the passed in object. For
147 * options that take an integer argument, the passed in object is an
148 * Integer. The option_id parameter is one of the defined constants in
149 * this interface.
151 * @param optionId The identifier of the option
152 * @param value The value to set the option to
154 * @throws SocketException if an error occurs
156 public void setOption(int optionId, Object value) throws SocketException
158 VMPlainSocketImpl.setOption(this, optionId, value);
162 * Returns the current setting of the specified option. The Object returned
163 * will be an Integer for options that have integer values. The option_id
164 * is one of the defined constants in this interface.
166 * @param optionId the option identifier
168 * @return the current value of the option
170 * @throws SocketException if an error occurs
172 public Object getOption(int optionId) throws SocketException
174 return VMPlainSocketImpl.getOption(this, optionId);
177 public void shutdownInput() throws IOException
179 VMPlainSocketImpl.shutdownInput(this);
182 public void shutdownOutput() throws IOException
184 VMPlainSocketImpl.shutdownOutput(this);
188 * Creates a new socket that is not bound to any local address/port and
189 * is not connected to any remote address/port. The stream parameter will be
190 * ignored since PlainSocketImpl always is a stream socket. Datagram sockets
191 * are handled by PlainDatagramSocketImpl.
193 * @param stream <code>true</code> for stream sockets, <code>false</code> for
194 * datagram sockets
196 protected synchronized void create(boolean stream) throws IOException
198 VMPlainSocketImpl.create(this);
202 * Connects to the remote hostname and port specified as arguments.
204 * @param hostname the remote hostname to connect to
205 * @param port the remote port to connect to
207 * @throws IOException If an error occurs
209 protected synchronized void connect(String hostname, int port)
210 throws IOException
212 connect(InetAddress.getByName(hostname), port);
216 * Connects to the remote address and port specified as arguments.
218 * @param addr the remote address to connect to
219 * @param port the remote port to connect to
221 * @throws IOException If an error occurs
223 protected void connect(InetAddress addr, int port) throws IOException
225 VMPlainSocketImpl.connect(this, addr, port);
229 * Connects to the remote socket address with a specified timeout.
231 * @param address the remote address to connect to
232 * @param timeout the timeout to use for this connect, 0 means infinite.
234 * @throws IOException If an error occurs
236 protected synchronized void connect(SocketAddress address, int timeout)
237 throws IOException
239 VMPlainSocketImpl.connect(this, address, timeout);
243 * Binds to the specified port on the specified addr. Note that this addr
244 * must represent a local IP address. **** How bind to INADDR_ANY? ****
246 * @param addr the address to bind to
247 * @param port the port number to bind to
249 * @throws IOException if an error occurs
251 protected synchronized void bind(InetAddress addr, int port)
252 throws IOException
254 VMPlainSocketImpl.bind(this, addr, port);
258 * Starts listening for connections on a socket. The queuelen parameter
259 * is how many pending connections will queue up waiting to be serviced
260 * before being accept'ed. If the queue of pending requests exceeds this
261 * number, additional connections will be refused.
263 * @param queuelen The length of the pending connection queue
265 * @throws IOException If an error occurs
267 protected synchronized void listen(int queuelen)
268 throws IOException
270 VMPlainSocketImpl.listen(this, queuelen);
274 * Accepts a new connection on this socket and returns in in the
275 * passed in SocketImpl.
277 * @param impl The SocketImpl object to accept this connection.
279 protected synchronized void accept(SocketImpl impl)
280 throws IOException
282 VMPlainSocketImpl.accept(this, impl);
286 * Returns the number of bytes that the caller can read from this socket
287 * without blocking.
289 * @return the number of readable bytes before blocking
291 * @throws IOException if an error occurs
293 protected int available() throws IOException
295 return VMPlainSocketImpl.available(this);
299 * Closes the socket. This will cause any InputStream or OutputStream
300 * objects for this Socket to be closed as well.
302 * <p>
303 * Note that if the SO_LINGER option is set on this socket, then the
304 * operation could block.
305 * </p>
307 * @throws IOException if an error occurs
309 protected void close() throws IOException
311 VMPlainSocketImpl.close(this);
314 public void sendUrgentData(int data)
316 VMPlainSocketImpl.sendUrgendData(this, data);
320 * Internal method used by SocketInputStream for reading data from
321 * the connection. Reads up to len bytes of data into the buffer
322 * buf starting at offset bytes into the buffer.
324 * @return the actual number of bytes read or -1 if end of stream.
326 * @throws IOException if an error occurs
328 protected int read(byte[] buf, int offset, int len)
329 throws IOException
331 return VMPlainSocketImpl.read(this, buf, offset, len);
335 * Internal method used by SocketInputStream for reading data from
336 * the connection. Reads and returns one byte of data.
338 * @return the read byte
340 * @throws IOException if an error occurs
342 protected int read()
343 throws IOException
345 return VMPlainSocketImpl.read(this);
349 * Internal method used by SocketOuputStream for writing data to
350 * the connection. Writes up to len bytes of data from the buffer
351 * buf starting at offset bytes into the buffer.
353 * @throws IOException If an error occurs
355 protected void write(byte[] buf, int offset, int len)
356 throws IOException
358 VMPlainSocketImpl.write(this, buf, offset, len);
362 * Internal method used by SocketOuputStream for writing data to
363 * the connection. Writes up one byte to the socket.
365 * @throws IOException If an error occurs
367 protected void write(int data) throws IOException
369 VMPlainSocketImpl.write(this, data);
373 * Returns an InputStream object for reading from this socket. This will
374 * be an instance of SocketInputStream.
376 * @return An input stream attached to the socket.
378 * @exception IOException If an error occurs
380 protected synchronized InputStream getInputStream() throws IOException
382 if (in == null)
383 in = new SocketInputStream();
385 return in;
389 * Returns an OutputStream object for writing to this socket. This will
390 * be an instance of SocketOutputStream.
392 * @return An output stream attached to the socket.
394 * @exception IOException If an error occurs
396 protected synchronized OutputStream getOutputStream() throws IOException
398 if (out == null)
399 out = new SocketOutputStream();
401 return out;
405 * This class contains an implementation of <code>InputStream</code> for
406 * sockets. It in an internal only class used by <code>PlainSocketImpl</code>.
408 * @author Nic Ferrier <nferrier@tapsellferrier.co.uk>
410 final class SocketInputStream
411 extends InputStream
414 * Returns the number of bytes available to be read before blocking
416 public int available() throws IOException
418 return PlainSocketImpl.this.available();
422 * This method not only closes the stream, it closes the underlying socket
423 * (and thus any connection) and invalidates any other Input/Output streams
424 * for the underlying impl object
426 public void close() throws IOException
428 PlainSocketImpl.this.close();
432 * Reads the next byte of data and returns it as an int.
434 * @return The byte read (as an int) or -1 if end of stream);
436 * @exception IOException If an error occurs.
438 public int read() throws IOException
440 return PlainSocketImpl.this.read();
444 * Reads up to len bytes of data into the caller supplied buffer starting
445 * at offset bytes from the start of the buffer
447 * @param buf The buffer
448 * @param offset Offset into the buffer to start reading from
449 * @param len The number of bytes to read
451 * @return The number of bytes actually read or -1 if end of stream
453 * @exception IOException If an error occurs.
455 public int read (byte[] buf, int offset, int len) throws IOException
457 int bytes_read = PlainSocketImpl.this.read (buf, offset, len);
459 if (bytes_read == 0)
460 return -1;
462 return bytes_read;
467 * This class is used internally by <code>PlainSocketImpl</code> to be the
468 * <code>OutputStream</code> subclass returned by its
469 * <code>getOutputStream method</code>. It expects only to be used in that
470 * context.
472 * @author Nic Ferrier <nferrier@tapsellferrier.co.uk>
474 final class SocketOutputStream
475 extends OutputStream
478 * This method closes the stream and the underlying socket connection. This
479 * action also effectively closes any other InputStream or OutputStream
480 * object associated with the connection.
482 * @exception IOException If an error occurs
484 public void close() throws IOException
486 PlainSocketImpl.this.close();
490 * Writes a byte (passed in as an int) to the given output stream
492 * @param b The byte to write
494 * @exception IOException If an error occurs
496 public void write(int b) throws IOException
498 PlainSocketImpl.this.write(b);
502 * Writes len number of bytes from the array buf to the stream starting
503 * at offset bytes into the buffer.
505 * @param buf The buffer
506 * @param offset Offset into the buffer to start writing from
507 * @param len The number of bytes to write
509 * @exception IOException If an error occurs.
511 public void write (byte[] buf, int offset, int len) throws IOException
513 PlainSocketImpl.this.write (buf, offset, len);