2003-09-25 Michael Koch <konqueror@gmx.de>
[official-gcc.git] / libjava / gnu / java / nio / SocketChannelImpl.java
blob589296f516639972aa91d7adaa89ab25bcdac80b
1 /* SocketChannelImpl.java --
2 Copyright (C) 2002, 2003 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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 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 java.io.InputStream;
42 import java.io.IOException;
43 import java.io.OutputStream;
44 import java.net.InetAddress;
45 import java.net.InetSocketAddress;
46 import gnu.java.net.PlainSocketImpl;
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.UnresolvedAddressException;
57 import java.nio.channels.UnsupportedAddressTypeException;
58 import java.nio.channels.SocketChannel;
59 import java.nio.channels.Selector;
60 import java.nio.channels.SelectionKey;
61 import java.nio.channels.spi.SelectorProvider;
62 import gnu.classpath.Configuration;
64 public final class SocketChannelImpl extends SocketChannel
66 private NIOSocket socket;
67 private boolean blocking = true;
68 private boolean connected = false;
69 private boolean connectionPending = false;
71 SocketChannelImpl (SelectorProvider provider)
72 throws IOException
74 super (provider);
75 socket = new NIOSocket (new PlainSocketImpl(), this);
78 SocketChannelImpl (SelectorProvider provider,
79 NIOSocket socket)
80 throws IOException
82 super (provider);
83 this.socket = socket;
84 this.connected = socket.isConnected();
87 public void finalizer()
89 if (isConnected())
91 try
93 close ();
95 catch (Exception e)
101 int getNativeFD()
103 return socket.getImpl().getNativeFD();
106 protected void implCloseSelectableChannel () throws IOException
108 connected = false;
109 socket.close();
112 protected void implConfigureBlocking (boolean blocking) throws IOException
114 socket.setSoTimeout (blocking ? 0 : NIOConstants.DEFAULT_TIMEOUT);
115 this.blocking = blocking;
118 public boolean connect (SocketAddress remote) throws IOException
120 if (!isOpen())
121 throw new ClosedChannelException();
123 if (isConnected())
124 throw new AlreadyConnectedException();
126 if (connectionPending)
127 throw new ConnectionPendingException();
129 if (!(remote instanceof InetSocketAddress))
130 throw new UnsupportedAddressTypeException();
132 if (((InetSocketAddress) remote).isUnresolved())
133 throw new UnresolvedAddressException();
135 if (blocking)
137 // Do blocking connect.
138 socket.connect (remote);
139 connected = true;
140 return true;
143 // Do non-blocking connect.
146 socket.connect (remote, NIOConstants.DEFAULT_TIMEOUT);
147 connected = true;
148 return true;
150 catch (SocketTimeoutException e)
152 connectionPending = true;
153 return false;
157 public boolean finishConnect ()
158 throws IOException
160 if (!isOpen())
161 throw new ClosedChannelException();
163 if (!connectionPending)
164 throw new NoConnectionPendingException();
166 if (isConnected())
167 return true;
169 // FIXME: Handle blocking/non-blocking mode.
171 Selector selector = provider().openSelector();
172 register (selector, SelectionKey.OP_CONNECT);
174 if (isBlocking())
176 selector.select(); // blocking until channel is connected.
177 connected = true;
178 connectionPending = false;
179 return true;
182 int ready = selector.selectNow(); // non-blocking
183 if (ready == 1)
185 connected = true;
186 connectionPending = false;
187 return true;
190 return false;
193 public boolean isConnected ()
195 return connected;
198 public boolean isConnectionPending ()
200 return connectionPending;
203 public Socket socket ()
205 return socket;
208 public int read (ByteBuffer dst) throws IOException
210 if (!connected)
211 throw new NotYetConnectedException();
213 byte[] data;
214 int offset = 0;
215 int len = dst.remaining();
217 if (dst.hasArray())
219 offset = dst.arrayOffset() + dst.position();
220 data = dst.array();
222 else
224 data = new byte [len];
227 InputStream input = socket.getInputStream();
228 int available = input.available();
230 if (available == 0)
231 return 0;
233 if (len > available)
234 len = available;
236 int readBytes = 0;
237 boolean completed = false;
241 begin();
242 readBytes = input.read (data, offset, len);
243 completed = true;
245 finally
247 end (completed);
250 if (readBytes > 0
251 && !dst.hasArray())
253 dst.put (data);
256 return readBytes;
259 public long read (ByteBuffer[] dsts, int offset, int length)
260 throws IOException
262 if (!connected)
263 throw new NotYetConnectedException();
265 if ((offset < 0)
266 || (offset > dsts.length)
267 || (length < 0)
268 || (length > (dsts.length - offset)))
269 throw new IndexOutOfBoundsException();
271 long readBytes = 0;
273 for (int index = offset; index < length; index++)
274 readBytes += read (dsts [index]);
276 return readBytes;
279 public int write (ByteBuffer src)
280 throws IOException
282 if (!connected)
283 throw new NotYetConnectedException();
285 byte[] data;
286 int offset = 0;
287 int len = src.remaining();
289 if (!src.hasArray())
291 data = new byte [len];
292 src.get (data, 0, len);
294 else
296 offset = src.arrayOffset() + src.position();
297 data = src.array();
300 System.out.println ("INTERNAL: writing to socket outputstream");
302 OutputStream output = socket.getOutputStream();
303 output.write (data, offset, len);
304 return len;
307 public long write (ByteBuffer[] srcs, int offset, int length)
308 throws IOException
310 if (!connected)
311 throw new NotYetConnectedException();
313 if ((offset < 0)
314 || (offset > srcs.length)
315 || (length < 0)
316 || (length > (srcs.length - offset)))
317 throw new IndexOutOfBoundsException();
319 long writtenBytes = 0;
321 for (int index = offset; index < length; index++)
322 writtenBytes += write (srcs [index]);
324 return writtenBytes;