1 | // PlainSocketImpl.java - Implementation of SocketImpl.
|
---|
2 |
|
---|
3 | /* Copyright (C) 1999 , 2002 Free Software Foundation
|
---|
4 |
|
---|
5 | This file is part of libgcj.
|
---|
6 |
|
---|
7 | This software is copyrighted work licensed under the terms of the
|
---|
8 | Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
---|
9 | details. */
|
---|
10 |
|
---|
11 | package java.net;
|
---|
12 |
|
---|
13 | import java.io.InputStream;
|
---|
14 | import java.io.IOException;
|
---|
15 | import java.io.OutputStream;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * The standard GCJ socket implementation.
|
---|
19 | * Written using on-line Java Platform 1.2 API Specification, as well
|
---|
20 | * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
|
---|
21 | * Status: Believed complete and correct.
|
---|
22 | *
|
---|
23 | * @author Per Bothner <bothner@cygnus.com>
|
---|
24 | * @author Nic Ferrier <nferrier@tapsellferrier.co.uk>
|
---|
25 | */
|
---|
26 | class PlainSocketImpl extends SocketImpl
|
---|
27 | {
|
---|
28 | // These fields are mirrored for use in native code to avoid cpp conflicts
|
---|
29 | // when the #defines in system header files are the same as the public fields.
|
---|
30 | static final int _Jv_TCP_NODELAY_ = SocketOptions.TCP_NODELAY,
|
---|
31 | _Jv_SO_BINDADDR_ = SocketOptions.SO_BINDADDR,
|
---|
32 | _Jv_SO_REUSEADDR_ = SocketOptions.SO_REUSEADDR,
|
---|
33 | _Jv_SO_BROADCAST_ = SocketOptions.SO_BROADCAST,
|
---|
34 | _Jv_SO_OOBINLINE_ = SocketOptions.SO_OOBINLINE,
|
---|
35 | _Jv_IP_MULTICAST_IF_ = SocketOptions.IP_MULTICAST_IF,
|
---|
36 | _Jv_IP_MULTICAST_IF2_ = SocketOptions.IP_MULTICAST_IF2,
|
---|
37 | _Jv_IP_MULTICAST_LOOP_ = SocketOptions.IP_MULTICAST_LOOP,
|
---|
38 | _Jv_IP_TOS_ = SocketOptions.IP_TOS,
|
---|
39 | _Jv_SO_LINGER_ = SocketOptions.SO_LINGER,
|
---|
40 | _Jv_SO_TIMEOUT_ = SocketOptions.SO_TIMEOUT,
|
---|
41 | _Jv_SO_SNDBUF_ = SocketOptions.SO_SNDBUF,
|
---|
42 | _Jv_SO_RCVBUF_ = SocketOptions.SO_RCVBUF,
|
---|
43 | _Jv_SO_KEEPALIVE_ = SocketOptions.SO_KEEPALIVE;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * The OS file handle representing the socket.
|
---|
47 | * This is used for reads and writes to/from the socket and
|
---|
48 | * to close it.
|
---|
49 | *
|
---|
50 | * When the socket is closed this is reset to -1.
|
---|
51 | */
|
---|
52 | int fnum = -1;
|
---|
53 |
|
---|
54 | // This value is set/read by setOption/getOption.
|
---|
55 | int timeout = 0;
|
---|
56 |
|
---|
57 | // localAddress cache
|
---|
58 | InetAddress localAddress;
|
---|
59 |
|
---|
60 | public native void setOption(int optID, Object value) throws SocketException;
|
---|
61 |
|
---|
62 | public native Object getOption(int optID) throws SocketException;
|
---|
63 |
|
---|
64 | public native void shutdownInput () throws IOException;
|
---|
65 |
|
---|
66 | public native void shutdownOutput () throws IOException;
|
---|
67 |
|
---|
68 | protected native void create (boolean stream) throws IOException;
|
---|
69 |
|
---|
70 | protected void connect (String host, int port) throws IOException
|
---|
71 | {
|
---|
72 | connect (new InetSocketAddress (InetAddress.getByName(host), port), 0);
|
---|
73 | }
|
---|
74 |
|
---|
75 | protected void connect (InetAddress host, int port) throws IOException
|
---|
76 | {
|
---|
77 | connect (new InetSocketAddress (host, port), 0);
|
---|
78 | }
|
---|
79 |
|
---|
80 | protected native void connect (SocketAddress addr, int timeout)
|
---|
81 | throws IOException;
|
---|
82 |
|
---|
83 | protected native void bind (InetAddress host, int port) throws IOException;
|
---|
84 |
|
---|
85 | protected native void listen (int backlog) throws IOException;
|
---|
86 |
|
---|
87 | private native void accept (PlainSocketImpl s) throws IOException;
|
---|
88 |
|
---|
89 | protected void accept (SocketImpl s) throws IOException
|
---|
90 | {
|
---|
91 | accept((PlainSocketImpl) s);
|
---|
92 | }
|
---|
93 |
|
---|
94 | protected native int available() throws IOException;
|
---|
95 |
|
---|
96 | protected native void close () throws IOException;
|
---|
97 |
|
---|
98 | protected native void sendUrgentData(int data)
|
---|
99 | throws IOException;
|
---|
100 |
|
---|
101 | // Stream handling.
|
---|
102 |
|
---|
103 | /** A cached copy of the in stream for reading from the socket. */
|
---|
104 | private InputStream in;
|
---|
105 |
|
---|
106 | /** A cached copy of the out stream for writing to the socket. */
|
---|
107 | private OutputStream out;
|
---|
108 |
|
---|
109 |
|
---|
110 | // The native read methods.
|
---|
111 |
|
---|
112 | private native int read() throws IOException;
|
---|
113 |
|
---|
114 | private native int read(byte[] buffer, int offset, int count)
|
---|
115 | throws IOException;
|
---|
116 |
|
---|
117 |
|
---|
118 | // The native write methods.
|
---|
119 |
|
---|
120 | private native void write(int c) throws IOException;
|
---|
121 |
|
---|
122 | private native void write(byte[] buffer, int offset, int count)
|
---|
123 | throws IOException;
|
---|
124 |
|
---|
125 | protected void finalize() throws Throwable
|
---|
126 | {
|
---|
127 | synchronized (this)
|
---|
128 | {
|
---|
129 | if (fnum != -1)
|
---|
130 | try
|
---|
131 | {
|
---|
132 | close();
|
---|
133 | }
|
---|
134 | catch (IOException ex)
|
---|
135 | {
|
---|
136 | // ignore
|
---|
137 | }
|
---|
138 | }
|
---|
139 | super.finalize();
|
---|
140 | }
|
---|
141 |
|
---|
142 | /** @return the input stream attached to the socket.
|
---|
143 | */
|
---|
144 | protected InputStream getInputStream() throws IOException
|
---|
145 | {
|
---|
146 | if (in == null)
|
---|
147 | in = new SocketInputStream();
|
---|
148 | return in;
|
---|
149 | }
|
---|
150 |
|
---|
151 | /** @return the output stream attached to the socket.
|
---|
152 | */
|
---|
153 | protected OutputStream getOutputStream() throws IOException
|
---|
154 | {
|
---|
155 | if (out == null)
|
---|
156 | out = new SocketOutputStream();
|
---|
157 | return out;
|
---|
158 | }
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * A stream which reads from the socket implementation.
|
---|
162 | *
|
---|
163 | * @author Nic Ferrier <nferrier@tapsellferrier.co.uk>
|
---|
164 | */
|
---|
165 | class SocketInputStream
|
---|
166 | extends InputStream
|
---|
167 | {
|
---|
168 | SocketInputStream()
|
---|
169 | {
|
---|
170 | }
|
---|
171 |
|
---|
172 | public final void close() throws IOException
|
---|
173 | {
|
---|
174 | PlainSocketImpl.this.close();
|
---|
175 | }
|
---|
176 |
|
---|
177 | public final int available() throws IOException
|
---|
178 | {
|
---|
179 | return PlainSocketImpl.this.available();
|
---|
180 | }
|
---|
181 |
|
---|
182 | public final int read() throws IOException
|
---|
183 | {
|
---|
184 | return PlainSocketImpl.this.read();
|
---|
185 | }
|
---|
186 |
|
---|
187 | public final int read(byte[] buffer, int offset, int length)
|
---|
188 | throws IOException
|
---|
189 | {
|
---|
190 | return PlainSocketImpl.this.read(buffer, offset, length);
|
---|
191 | }
|
---|
192 |
|
---|
193 | public final int read(byte[] buffer)
|
---|
194 | throws IOException
|
---|
195 | {
|
---|
196 | return PlainSocketImpl.this.read(buffer, 0, buffer.length);
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 | /** A stream which writes to the socket implementation.
|
---|
201 | *
|
---|
202 | * @author Nic Ferrier <nferrier@tapsellferrier.co.uk>
|
---|
203 | */
|
---|
204 | class SocketOutputStream
|
---|
205 | extends OutputStream
|
---|
206 | {
|
---|
207 | public final void close() throws IOException
|
---|
208 | {
|
---|
209 | PlainSocketImpl.this.close();
|
---|
210 | }
|
---|
211 |
|
---|
212 | public final void write(int c) throws IOException
|
---|
213 | {
|
---|
214 | PlainSocketImpl.this.write(c);
|
---|
215 | }
|
---|
216 |
|
---|
217 | public final void write(byte[] buffer, int offset, int length)
|
---|
218 | throws IOException
|
---|
219 | {
|
---|
220 | PlainSocketImpl.this.write(buffer, offset, length);
|
---|
221 | }
|
---|
222 |
|
---|
223 | public final void write(byte[] buffer)
|
---|
224 | throws IOException
|
---|
225 | {
|
---|
226 | PlainSocketImpl.this.write(buffer, 0, buffer.length);
|
---|
227 | }
|
---|
228 | }
|
---|
229 | }
|
---|