1 // PlainSocketImpl.java - Implementation of SocketImpl.
3 /* Copyright (C) 1999 , 2002 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
16 * The standard GCJ socket implementation.
17 * Written using on-line Java Platform 1.2 API Specification, as well
18 * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
19 * Status: Believed complete and correct.
21 * @author Per Bothner <bothner@cygnus.com>
22 * @author Nic Ferrier <nferrier@tapsellferrier.co.uk>
24 class PlainSocketImpl extends SocketImpl
26 // These fields are mirrored for use in native code to avoid cpp conflicts
27 // when the #defines in system header files are the same as the public fields.
28 static final int _Jv_TCP_NODELAY_ = SocketOptions.TCP_NODELAY,
29 _Jv_SO_BINDADDR_ = SocketOptions.SO_BINDADDR,
30 _Jv_SO_REUSEADDR_ = SocketOptions.SO_REUSEADDR,
31 _Jv_IP_MULTICAST_IF_ = SocketOptions.IP_MULTICAST_IF,
32 _Jv_SO_LINGER_ = SocketOptions.SO_LINGER,
33 _Jv_SO_TIMEOUT_ = SocketOptions.SO_TIMEOUT,
34 _Jv_SO_SNDBUF_ = SocketOptions.SO_SNDBUF,
35 _Jv_SO_RCVBUF_ = SocketOptions.SO_RCVBUF;
38 * The OS file handle representing the socket.
39 * This is used for reads and writes to/from the socket and
42 * When the socket is closed this is reset to -1.
46 // This value is set/read by setOption/getOption.
50 InetAddress localAddress;
52 public native void setOption(int optID, Object value) throws SocketException;
54 public native Object getOption(int optID) throws SocketException;
56 protected native void create (boolean stream) throws IOException;
58 protected void connect (String host, int port) throws IOException
60 connect(InetAddress.getByName(host), port);
63 protected native void connect (InetAddress host, int port)
66 protected native void bind (InetAddress host, int port) throws IOException;
68 protected native void listen (int backlog) throws IOException;
70 private native void accept (PlainSocketImpl s) throws IOException;
72 protected void accept (SocketImpl s) throws IOException
74 accept((PlainSocketImpl) s);
77 protected native int available() throws IOException;
79 protected native void close () throws IOException;
84 /** A cached copy of the in stream for reading from the socket. */
85 private InputStream in;
87 /** A cached copy of the out stream for writing to the socket. */
88 private OutputStream out;
91 // The native read methods.
93 private native int read() throws IOException;
95 private native int read(byte[] buffer, int offset, int count)
99 // The native write methods.
101 private native void write(int c) throws IOException;
103 private native void write(byte[] buffer, int offset, int count)
106 protected void finalize() throws Throwable
115 catch (IOException ex)
123 /** @return the input stream attached to the socket.
125 protected InputStream getInputStream() throws IOException
128 in = new SocketInputStream();
132 /** @return the output stream attached to the socket.
134 protected OutputStream getOutputStream() throws IOException
137 out = new SocketOutputStream();
142 * A stream which reads from the socket implementation.
144 * @author Nic Ferrier <nferrier@tapsellferrier.co.uk>
146 class SocketInputStream
153 public final void close() throws IOException
155 PlainSocketImpl.this.close();
158 public final int available() throws IOException
160 return PlainSocketImpl.this.available();
163 public final int read() throws IOException
165 return PlainSocketImpl.this.read();
168 public final int read(byte[] buffer, int offset, int length)
171 return PlainSocketImpl.this.read(buffer, offset, length);
174 public final int read(byte[] buffer)
177 return PlainSocketImpl.this.read(buffer, 0, buffer.length);
181 /** A stream which writes to the socket implementation.
183 * @author Nic Ferrier <nferrier@tapsellferrier.co.uk>
185 class SocketOutputStream
188 public final void close() throws IOException
190 PlainSocketImpl.this.close();
193 public final void write(int c) throws IOException
195 PlainSocketImpl.this.write(c);
198 public final void write(byte[] buffer, int offset, int length)
201 PlainSocketImpl.this.write(buffer, offset, length);
204 public final void write(byte[] buffer)
207 PlainSocketImpl.this.write(buffer, 0, buffer.length);