Dead
[official-gcc.git] / gomp-20050608-branch / libjava / gnu / java / nio / SocketChannelImpl.java
blobcda86e80723310d7a574ebf7e04433d595deafb3
1 /* SocketChannelImpl.java --
2 Copyright (C) 2002, 2003, 2004, 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. */
39 package gnu.java.nio;
41 import gnu.java.net.PlainSocketImpl;
43 import java.io.IOException;
44 import java.io.InputStream;
45 import java.io.OutputStream;
46 import java.net.InetSocketAddress;
47 import java.net.Socket;
48 import java.net.SocketAddress;
49 import java.net.SocketTimeoutException;
50 import java.nio.ByteBuffer;
51 import java.nio.channels.AlreadyConnectedException;
52 import java.nio.channels.ClosedChannelException;
53 import java.nio.channels.ConnectionPendingException;
54 import java.nio.channels.NoConnectionPendingException;
55 import java.nio.channels.NotYetConnectedException;
56 import java.nio.channels.SelectionKey;
57 import java.nio.channels.Selector;
58 import java.nio.channels.SocketChannel;
59 import java.nio.channels.UnresolvedAddressException;
60 import java.nio.channels.UnsupportedAddressTypeException;
61 import java.nio.channels.spi.SelectorProvider;
63 public final class SocketChannelImpl extends SocketChannel
65 private PlainSocketImpl impl;
66 private NIOSocket socket;
67 private boolean connectionPending;
69 SocketChannelImpl (SelectorProvider provider)
70 throws IOException
72 super (provider);
73 impl = new PlainSocketImpl();
74 socket = new NIOSocket (impl, this);
75 configureBlocking(true);
78 SocketChannelImpl (SelectorProvider provider,
79 NIOSocket socket)
80 throws IOException
82 super (provider);
83 this.impl = socket.getPlainSocketImpl();
84 this.socket = socket;
87 public void finalizer()
89 if (isConnected())
91 try
93 close ();
95 catch (Exception e)
101 PlainSocketImpl getPlainSocketImpl()
103 return impl;
106 int getNativeFD()
108 return socket.getPlainSocketImpl().getNativeFD();
111 protected void implCloseSelectableChannel () throws IOException
113 socket.close();
116 protected void implConfigureBlocking (boolean blocking) throws IOException
118 socket.setSoTimeout (blocking ? 0 : NIOConstants.DEFAULT_TIMEOUT);
121 public boolean connect (SocketAddress remote) throws IOException
123 if (!isOpen())
124 throw new ClosedChannelException();
126 if (isConnected())
127 throw new AlreadyConnectedException();
129 if (connectionPending)
130 throw new ConnectionPendingException();
132 if (!(remote instanceof InetSocketAddress))
133 throw new UnsupportedAddressTypeException();
135 if (((InetSocketAddress) remote).isUnresolved())
136 throw new UnresolvedAddressException();
140 socket.getPlainSocketImpl().setInChannelOperation(true);
141 // indicate that a channel is initiating the accept operation
142 // so that the socket ignores the fact that we might be in
143 // non-blocking mode.
145 if (isBlocking())
147 // Do blocking connect.
148 socket.connect (remote);
149 return true;
152 // Do non-blocking connect.
155 socket.connect (remote, NIOConstants.DEFAULT_TIMEOUT);
156 return true;
158 catch (SocketTimeoutException e)
160 connectionPending = true;
161 return false;
164 finally
166 socket.getPlainSocketImpl().setInChannelOperation(false);
170 public boolean finishConnect ()
171 throws IOException
173 if (!isOpen())
174 throw new ClosedChannelException();
176 if (!isConnected() && !connectionPending)
177 throw new NoConnectionPendingException();
179 if (isConnected())
180 return true;
182 // FIXME: Handle blocking/non-blocking mode.
184 Selector selector = provider().openSelector();
185 register(selector, SelectionKey.OP_CONNECT);
187 if (isBlocking())
189 selector.select(); // blocking until channel is connected.
190 connectionPending = false;
191 return true;
194 int ready = selector.selectNow(); // non-blocking
195 if (ready == 1)
197 connectionPending = false;
198 return true;
201 return false;
204 public boolean isConnected ()
206 return socket.isConnected();
209 public boolean isConnectionPending ()
211 return connectionPending;
214 public Socket socket ()
216 return socket;
219 public int read(ByteBuffer dst) throws IOException
221 if (!isConnected())
222 throw new NotYetConnectedException();
224 byte[] data;
225 int offset = 0;
226 InputStream input = socket.getInputStream();
227 int available = input.available();
228 int len = dst.remaining();
230 if ((! isBlocking()) && available == 0)
231 return 0;
233 if (dst.hasArray())
235 offset = dst.arrayOffset() + dst.position();
236 data = dst.array();
238 else
240 data = new byte [len];
243 int readBytes = 0;
244 boolean completed = false;
248 begin();
249 socket.getPlainSocketImpl().setInChannelOperation(true);
250 readBytes = input.read (data, offset, len);
251 completed = true;
253 finally
255 end (completed);
256 socket.getPlainSocketImpl().setInChannelOperation(false);
259 if (readBytes > 0)
260 if (dst.hasArray())
262 dst.position (dst.position() + readBytes);
264 else
266 dst.put (data, offset, readBytes);
269 return readBytes;
272 public long read (ByteBuffer[] dsts, int offset, int length)
273 throws IOException
275 if (!isConnected())
276 throw new NotYetConnectedException();
278 if ((offset < 0)
279 || (offset > dsts.length)
280 || (length < 0)
281 || (length > (dsts.length - offset)))
282 throw new IndexOutOfBoundsException();
284 long readBytes = 0;
286 for (int index = offset; index < length; index++)
287 readBytes += read (dsts [index]);
289 return readBytes;
292 public int write (ByteBuffer src)
293 throws IOException
295 if (!isConnected())
296 throw new NotYetConnectedException();
298 byte[] data;
299 int offset = 0;
300 int len = src.remaining();
302 if (!src.hasArray())
304 data = new byte [len];
305 src.get (data, 0, len);
307 else
309 offset = src.arrayOffset() + src.position();
310 data = src.array();
313 OutputStream output = socket.getOutputStream();
314 boolean completed = false;
318 begin();
319 socket.getPlainSocketImpl().setInChannelOperation(true);
320 output.write (data, offset, len);
321 completed = true;
323 finally
325 end (completed);
326 socket.getPlainSocketImpl().setInChannelOperation(false);
329 if (src.hasArray())
331 src.position (src.position() + len);
334 return len;
337 public long write (ByteBuffer[] srcs, int offset, int length)
338 throws IOException
340 if (!isConnected())
341 throw new NotYetConnectedException();
343 if ((offset < 0)
344 || (offset > srcs.length)
345 || (length < 0)
346 || (length > (srcs.length - offset)))
347 throw new IndexOutOfBoundsException();
349 long writtenBytes = 0;
351 for (int index = offset; index < length; index++)
352 writtenBytes += write (srcs [index]);
354 return writtenBytes;