~ubuntu-branches/ubuntu/precise/classpath/precise

« back to all changes in this revision

Viewing changes to vm/reference/gnu/java/net/VMPlainSocketImpl.java

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2006-05-27 16:11:15 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060527161115-h6e39eposdt5snb6
Tags: 2:0.91-3
* Install header files to /usr/include/classpath.
* debian/control: classpath: Conflict with jamvm < 1.4.3 and
  cacao < 0.96 (Closes: #368172).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* VMPlainSocketImpl.java -- VM interface for default socket implementation
 
2
   Copyright (C) 2005, 2006 Free Software Foundation, Inc.
 
3
 
 
4
This file is part of GNU Classpath.
 
5
 
 
6
GNU Classpath is free software; you can redistribute it and/or modify
 
7
it under the terms of the GNU General Public License as published by
 
8
the Free Software Foundation; either version 2, or (at your option)
 
9
any later version.
 
10
 
 
11
GNU Classpath is distributed in the hope that it will be useful, but
 
12
WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
General Public License for more details.
 
15
 
 
16
You should have received a copy of the GNU General Public License
 
17
along with GNU Classpath; see the file COPYING.  If not, write to the
 
18
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
19
02110-1301 USA.
 
20
 
 
21
Linking this library statically or dynamically with other modules is
 
22
making a combined work based on this library.  Thus, the terms and
 
23
conditions of the GNU General Public License cover the whole
 
24
combination.
 
25
 
 
26
As a special exception, the copyright holders of this library give you
 
27
permission to link this library with independent modules to produce an
 
28
executable, regardless of the license terms of these independent
 
29
modules, and to copy and distribute the resulting executable under
 
30
terms of your choice, provided that you also meet, for each linked
 
31
independent module, the terms and conditions of the license of that
 
32
module.  An independent module is a module which is not derived from
 
33
or based on this library.  If you modify this library, you may extend
 
34
this exception to your version of the library, but you are not
 
35
obligated to do so.  If you do not wish to do so, delete this
 
36
exception statement from your version. */
 
37
 
 
38
package gnu.java.net;
 
39
 
 
40
import java.io.IOException;
 
41
import java.net.InetAddress;
 
42
import java.net.InetSocketAddress;
 
43
import java.net.SocketAddress;
 
44
import java.net.SocketException;
 
45
import java.net.SocketImpl;
 
46
import java.net.SocketOptions;
 
47
import java.net.UnknownHostException;
 
48
 
 
49
import gnu.classpath.Configuration;
 
50
 
 
51
/**
 
52
 * The VM interface for {@link gnu.java.net.PlainSocketImpl}.
 
53
 *
 
54
 * @author Ingo Proetel (proetel@aicas.com)
 
55
 * @author Roman Kennke (kennke@aicas.com)
 
56
 */
 
57
public final class VMPlainSocketImpl
 
58
{
 
59
  /**
 
60
   * Static initializer to load native library.
 
61
   */
 
62
  static
 
63
  {
 
64
    if (Configuration.INIT_LOAD_LIBRARY)
 
65
      {
 
66
        System.loadLibrary("javanet");
 
67
      }
 
68
  }
 
69
 
 
70
  /**
 
71
   * Sets the specified option on a socket to the passed in object.
 
72
   * The optionId parameter is one of the defined constants in
 
73
   * the SocketImpl interface.
 
74
   *
 
75
   * @param socket the socket object
 
76
   * @param optionId the identifier of the option
 
77
   * @param value the value to set the option to
 
78
   *
 
79
   * @throws SocketException if an error occurs
 
80
   */
 
81
  static native void setOption(PlainSocketImpl socket, int optionId, Object value)
 
82
    throws SocketException;
 
83
 
 
84
  /**
 
85
   * Returns the current setting of the specified option. The optionId
 
86
   * is one of the defined constants in this interface.
 
87
   *
 
88
   * @param socket the socket object
 
89
   * @param optionId the option identifier
 
90
   *
 
91
   * @return the current value of the option
 
92
   *
 
93
   * @throws SocketException ff an error occurs
 
94
   */
 
95
  static native Object getOption(PlainSocketImpl socket, int optionId)
 
96
    throws SocketException;
 
97
 
 
98
  /**
 
99
   * Creates a new socket that is not bound to any local address/port and
 
100
   * is not connected to any remote address/port.
 
101
   *
 
102
   * @param socket the socket object
 
103
   *
 
104
   * @throws IOException if something goes wrong while creating the socket
 
105
   */
 
106
  static native void create(PlainSocketImpl socket) throws IOException;
 
107
 
 
108
  /**
 
109
   * Connects to the remote address and port specified as arguments.
 
110
   *
 
111
   * @param socket the socket object
 
112
   * @param addr the remote address to connect to
 
113
   * @param port the remote port to connect to
 
114
   *
 
115
   * @throws IOException if an error occurs
 
116
   */
 
117
  static native void connect(PlainSocketImpl socket, InetAddress addr,
 
118
                             int port) throws IOException;
 
119
 
 
120
  /**
 
121
   * Binds to the specified port on the specified addr.  Note that this addr
 
122
   * must represent a local IP address.  **** How bind to INADDR_ANY? ****
 
123
   *
 
124
   * @param socket the socket object
 
125
   * @param addr the address to bind to
 
126
   * @param port the port number to bind to
 
127
   *
 
128
   * @exception IOException If an error occurs
 
129
   */
 
130
  static native void bind(PlainSocketImpl socket, InetAddress addr, int port)
 
131
    throws IOException;
 
132
 
 
133
  /**
 
134
   * Starts listening for connections on a socket. The queueLen parameter
 
135
   * is how many pending connections will queue up waiting to be serviced
 
136
   * before being accepted.  If the queue of pending requests exceeds this
 
137
   * number, additional connections will be refused.
 
138
   *
 
139
   * @param socket the socket object
 
140
   * @param queueLen the length of the pending connection queue
 
141
   * 
 
142
   * @exception IOException if an error occurs
 
143
   */
 
144
  static native void listen(PlainSocketImpl socket, int queueLen)
 
145
    throws IOException;
 
146
 
 
147
  /**
 
148
   * Accepts a new connection on this socket.
 
149
   *
 
150
   * @param socket the socket object
 
151
   * @param impl the socket object to accept this connection.
 
152
   */
 
153
  static native void accept(PlainSocketImpl socket, SocketImpl impl)
 
154
    throws IOException;
 
155
 
 
156
  /**
 
157
   * Returns the number of bytes that the caller can read from this socket
 
158
   * without blocking. 
 
159
   *
 
160
   * @param socket the socket object
 
161
   *
 
162
   * @return the number of readable bytes before blocking
 
163
   *
 
164
   * @throws IOException If an error occurs
 
165
   */
 
166
  static native int available(PlainSocketImpl socket) throws IOException;
 
167
 
 
168
  /**
 
169
   * Closes the socket.  This will cause any InputStream or OutputStream
 
170
   * objects for this Socket to be closed as well.
 
171
   *
 
172
   * <p>
 
173
   * Note that if the SO_LINGER option is set on this socket, then the
 
174
   * operation could block.
 
175
   * </p>
 
176
   *
 
177
   * @param socket the socket object
 
178
   *
 
179
   * @throws IOException if an error occurs
 
180
   */
 
181
  static native void close(PlainSocketImpl socket) throws IOException;
 
182
 
 
183
  /**
 
184
   * Internal method used by SocketInputStream for reading data from
 
185
   * the connection.  Reads up to len bytes of data into the buffer
 
186
   * buf starting at offset bytes into the buffer.
 
187
   *
 
188
   * @param socket the socket object
 
189
   *
 
190
   * @return the actual number of bytes read or -1 if end of stream.
 
191
   *
 
192
   * @throws IOException if an error occurs
 
193
   */
 
194
  static native int read(PlainSocketImpl socket, byte[] buf, int offset,
 
195
                         int len) throws IOException;
 
196
 
 
197
  /**
 
198
   * Internal method used by SocketInputStream for reading data from
 
199
   * the connection.  Reads and returns one byte of data.
 
200
   *
 
201
   * @param socket the socket object
 
202
   *
 
203
   * @return read byte or -1 if end of stream.
 
204
   *
 
205
   * @throws IOException if an error occurs
 
206
   */
 
207
  static int read(PlainSocketImpl socket) throws IOException
 
208
  {
 
209
    byte[] buf = new byte[1];
 
210
    if (read(socket, buf, 0, 1) > 0)
 
211
      return buf[0] & 0xFF;
 
212
    else
 
213
      return -1;
 
214
  }
 
215
 
 
216
  /**
 
217
   * Internal method used by SocketOuputStream for writing data to
 
218
   * the connection. Writes up to len bytes of data from the buffer
 
219
   * <code>buf</code> starting at <cod>offset</code> bytes into the buffer.
 
220
   *
 
221
   * @param socket the socket object
 
222
   * @param buf the buffer to write to the stream
 
223
   * @param offset the start offset in the buffer
 
224
   * @param len the number of bytes to write
 
225
   *
 
226
   * @throws IOException if an error occurs
 
227
   */
 
228
  static native void write(PlainSocketImpl socket, byte[] buf, int offset,
 
229
                           int len) throws IOException;
 
230
 
 
231
  /**
 
232
   * Internal method used by SocketOuputStream for writing data to
 
233
   * the connection. Writes exactly one byte to the socket.
 
234
   *
 
235
   * @param socket the socket object
 
236
   * @param data the byte to write to the socket
 
237
   *
 
238
   * @throws IOException if an error occurs
 
239
   */
 
240
  static void write(PlainSocketImpl socket, int data)
 
241
    throws IOException
 
242
  {
 
243
    write(socket, new byte[]{ (byte) data }, 0, 1);
 
244
  }
 
245
 
 
246
  /**
 
247
   * Sets the input stream for this socket to the end of the stream. Any
 
248
   * attempts to read more bytes from the stream will return an EOF.
 
249
   *
 
250
   * @param socket the socket object
 
251
   *
 
252
   * @throws IOException if I/O errors occur
 
253
   */
 
254
  static native void shutdownInput(PlainSocketImpl socket) throws IOException;
 
255
 
 
256
  /**
 
257
   * Disables the output stream for this socket. Any attempt to write more
 
258
   * data to the socket will throw an IOException.
 
259
   *
 
260
   * @param socket the socket object
 
261
   *
 
262
   * @throws IOException if I/O errors occur
 
263
   */
 
264
  static native void shutdownOutput(PlainSocketImpl socket) throws IOException;
 
265
 
 
266
  /**
 
267
   * Connects to the remote socket address with a specified timeout.
 
268
   *
 
269
   * @param socket the socket object
 
270
   * @param address the remote address to connect to
 
271
   * @param timeout the timeout to use for this connect, 0 means infinite.
 
272
   *
 
273
   * @throws IOException if an error occurs
 
274
   */
 
275
  static synchronized void connect(PlainSocketImpl socket,
 
276
                                      SocketAddress address, int timeout)
 
277
    throws IOException
 
278
  {
 
279
    InetSocketAddress sockAddr = (InetSocketAddress) address;
 
280
    InetAddress addr = sockAddr.getAddress();
 
281
 
 
282
    if (addr == null)
 
283
      throw new UnknownHostException(sockAddr.getHostName());
 
284
 
 
285
    int port = sockAddr.getPort();
 
286
 
 
287
    if (timeout < 0)
 
288
      throw new IllegalArgumentException("negative timeout");
 
289
 
 
290
    Object oldTimeoutObj = null;
 
291
    try
 
292
      {
 
293
        oldTimeoutObj = getOption(socket, SocketOptions.SO_TIMEOUT);
 
294
        setOption(socket, SocketOptions.SO_TIMEOUT, new Integer(timeout));
 
295
        connect(socket, addr, port);
 
296
      }
 
297
    finally
 
298
      {
 
299
        if (oldTimeoutObj != null)
 
300
          setOption(socket, SocketOptions.SO_TIMEOUT, oldTimeoutObj);
 
301
      }
 
302
  }
 
303
 
 
304
  /**
 
305
   * Send one byte of urgent data over the socket.
 
306
   *
 
307
   * @param socket the socket object
 
308
   * @param data the byte to send
 
309
   */
 
310
  static void sendUrgendData(PlainSocketImpl socket, int data)
 
311
  {
 
312
    throw new InternalError ("PlainSocketImpl::sendUrgentData not implemented");
 
313
  }
 
314
}