Merge from mainline
[official-gcc.git] / libjava / classpath / vm / reference / gnu / java / net / VMPlainSocketImpl.java
blob76be558dcd411b7865bc67bb23f0a687692ffd23
1 /* VMPlainSocketImpl.java -- VM interface for default socket implementation
2 Copyright (C) 2005, 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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.
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.
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. */
38 package gnu.java.net;
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;
49 import gnu.classpath.Configuration;
51 /**
52 * The VM interface for {@link gnu.java.net.PlainSocketImpl}.
54 * @author Ingo Proetel (proetel@aicas.com)
55 * @author Roman Kennke (kennke@aicas.com)
57 public final class VMPlainSocketImpl
59 /**
60 * Static initializer to load native library.
62 static
64 if (Configuration.INIT_LOAD_LIBRARY)
66 System.loadLibrary("javanet");
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.
75 * @param socket the socket object
76 * @param optionId the identifier of the option
77 * @param value the value to set the option to
79 * @throws SocketException if an error occurs
81 static native void setOption(PlainSocketImpl socket, int optionId, Object value)
82 throws SocketException;
84 /**
85 * Returns the current setting of the specified option. The optionId
86 * is one of the defined constants in this interface.
88 * @param socket the socket object
89 * @param optionId the option identifier
91 * @return the current value of the option
93 * @throws SocketException ff an error occurs
95 static native Object getOption(PlainSocketImpl socket, int optionId)
96 throws SocketException;
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.
102 * @param socket the socket object
104 * @throws IOException if something goes wrong while creating the socket
106 static native void create(PlainSocketImpl socket) throws IOException;
109 * Connects to the remote address and port specified as arguments.
111 * @param socket the socket object
112 * @param addr the remote address to connect to
113 * @param port the remote port to connect to
115 * @throws IOException if an error occurs
117 static native void connect(PlainSocketImpl socket, InetAddress addr,
118 int port) throws IOException;
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? ****
124 * @param socket the socket object
125 * @param addr the address to bind to
126 * @param port the port number to bind to
128 * @exception IOException If an error occurs
130 static native void bind(PlainSocketImpl socket, InetAddress addr, int port)
131 throws IOException;
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.
139 * @param socket the socket object
140 * @param queueLen the length of the pending connection queue
142 * @exception IOException if an error occurs
144 static native void listen(PlainSocketImpl socket, int queueLen)
145 throws IOException;
148 * Accepts a new connection on this socket.
150 * @param socket the socket object
151 * @param impl the socket object to accept this connection.
153 static native void accept(PlainSocketImpl socket, SocketImpl impl)
154 throws IOException;
157 * Returns the number of bytes that the caller can read from this socket
158 * without blocking.
160 * @param socket the socket object
162 * @return the number of readable bytes before blocking
164 * @throws IOException If an error occurs
166 static native int available(PlainSocketImpl socket) throws IOException;
169 * Closes the socket. This will cause any InputStream or OutputStream
170 * objects for this Socket to be closed as well.
172 * <p>
173 * Note that if the SO_LINGER option is set on this socket, then the
174 * operation could block.
175 * </p>
177 * @param socket the socket object
179 * @throws IOException if an error occurs
181 static native void close(PlainSocketImpl socket) throws IOException;
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.
188 * @param socket the socket object
190 * @return the actual number of bytes read or -1 if end of stream.
192 * @throws IOException if an error occurs
194 static native int read(PlainSocketImpl socket, byte[] buf, int offset,
195 int len) throws IOException;
198 * Internal method used by SocketInputStream for reading data from
199 * the connection. Reads and returns one byte of data.
201 * @param socket the socket object
203 * @return read byte or -1 if end of stream.
205 * @throws IOException if an error occurs
207 static int read(PlainSocketImpl socket) throws IOException
209 byte[] buf = new byte[1];
210 if (read(socket, buf, 0, 1) > 0)
211 return buf[0] & 0xFF;
212 else
213 return -1;
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.
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
226 * @throws IOException if an error occurs
228 static native void write(PlainSocketImpl socket, byte[] buf, int offset,
229 int len) throws IOException;
232 * Internal method used by SocketOuputStream for writing data to
233 * the connection. Writes exactly one byte to the socket.
235 * @param socket the socket object
236 * @param data the byte to write to the socket
238 * @throws IOException if an error occurs
240 static void write(PlainSocketImpl socket, int data)
241 throws IOException
243 write(socket, new byte[]{ (byte) data }, 0, 1);
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.
250 * @param socket the socket object
252 * @throws IOException if I/O errors occur
254 static native void shutdownInput(PlainSocketImpl socket) throws IOException;
257 * Disables the output stream for this socket. Any attempt to write more
258 * data to the socket will throw an IOException.
260 * @param socket the socket object
262 * @throws IOException if I/O errors occur
264 static native void shutdownOutput(PlainSocketImpl socket) throws IOException;
267 * Connects to the remote socket address with a specified timeout.
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.
273 * @throws IOException if an error occurs
275 static synchronized void connect(PlainSocketImpl socket,
276 SocketAddress address, int timeout)
277 throws IOException
279 InetSocketAddress sockAddr = (InetSocketAddress) address;
280 InetAddress addr = sockAddr.getAddress();
282 if (addr == null)
283 throw new UnknownHostException(sockAddr.getHostName());
285 int port = sockAddr.getPort();
287 if (timeout < 0)
288 throw new IllegalArgumentException("negative timeout");
290 Object oldTimeoutObj = null;
293 oldTimeoutObj = getOption(socket, SocketOptions.SO_TIMEOUT);
294 setOption(socket, SocketOptions.SO_TIMEOUT, new Integer(timeout));
295 connect(socket, addr, port);
297 finally
299 if (oldTimeoutObj != null)
300 setOption(socket, SocketOptions.SO_TIMEOUT, oldTimeoutObj);
305 * Send one byte of urgent data over the socket.
307 * @param socket the socket object
308 * @param data the byte to send
310 static void sendUrgendData(PlainSocketImpl socket, int data)
312 throw new InternalError ("PlainSocketImpl::sendUrgentData not implemented");