1 /* SocketChannelImpl.java --
2 Copyright (C) 2002, 2003, 2004, 2006, 2007 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)
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
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
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. */
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)
73 impl = new PlainSocketImpl();
75 socket = new NIOSocket (impl, this);
76 configureBlocking(true);
79 SocketChannelImpl (SelectorProvider provider,
84 this.impl = socket.getPlainSocketImpl();
88 public void finalizer()
102 PlainSocketImpl getPlainSocketImpl()
109 return socket.getPlainSocketImpl().getNativeFD();
112 protected void implCloseSelectableChannel () throws IOException
117 protected void implConfigureBlocking (boolean blocking) throws IOException
119 socket.setSoTimeout (blocking ? 0 : NIOConstants.DEFAULT_TIMEOUT);
122 public boolean connect (SocketAddress remote) throws IOException
125 throw new ClosedChannelException();
128 throw new AlreadyConnectedException();
130 if (connectionPending)
131 throw new ConnectionPendingException();
133 if (!(remote instanceof InetSocketAddress))
134 throw new UnsupportedAddressTypeException();
136 if (((InetSocketAddress) remote).isUnresolved())
137 throw new UnresolvedAddressException();
141 socket.getPlainSocketImpl().setInChannelOperation(true);
142 // indicate that a channel is initiating the accept operation
143 // so that the socket ignores the fact that we might be in
144 // non-blocking mode.
148 // Do blocking connect.
149 socket.connect (remote);
153 // Do non-blocking connect.
156 socket.connect (remote, NIOConstants.DEFAULT_TIMEOUT);
159 catch (SocketTimeoutException e)
161 connectionPending = true;
167 socket.getPlainSocketImpl().setInChannelOperation(false);
171 public boolean finishConnect ()
175 throw new ClosedChannelException();
177 if (!isConnected() && !connectionPending)
178 throw new NoConnectionPendingException();
183 // FIXME: Handle blocking/non-blocking mode.
185 Selector selector = provider().openSelector();
186 register(selector, SelectionKey.OP_CONNECT);
190 selector.select(); // blocking until channel is connected.
191 connectionPending = false;
195 int ready = selector.selectNow(); // non-blocking
198 connectionPending = false;
205 public boolean isConnected ()
207 return socket.isConnected();
210 public boolean isConnectionPending ()
212 return connectionPending;
215 public Socket socket ()
220 public int read(ByteBuffer dst) throws IOException
223 throw new NotYetConnectedException();
227 InputStream input = socket.getInputStream();
228 int available = input.available();
229 int len = dst.remaining();
231 if ((! isBlocking()) && available == 0)
236 offset = dst.arrayOffset() + dst.position();
241 data = new byte [len];
245 boolean completed = false;
250 socket.getPlainSocketImpl().setInChannelOperation(true);
251 readBytes = input.read (data, offset, len);
257 socket.getPlainSocketImpl().setInChannelOperation(false);
263 dst.position (dst.position() + readBytes);
267 dst.put (data, offset, readBytes);
273 public long read (ByteBuffer[] dsts, int offset, int length)
277 throw new NotYetConnectedException();
280 || (offset > dsts.length)
282 || (length > (dsts.length - offset)))
283 throw new IndexOutOfBoundsException();
287 for (int index = offset; index < length; index++)
288 readBytes += read (dsts [index]);
293 public int write (ByteBuffer src)
297 throw new NotYetConnectedException();
301 int len = src.remaining();
305 data = new byte [len];
306 src.get (data, 0, len);
310 offset = src.arrayOffset() + src.position();
314 OutputStream output = socket.getOutputStream();
315 boolean completed = false;
320 socket.getPlainSocketImpl().setInChannelOperation(true);
321 output.write (data, offset, len);
327 socket.getPlainSocketImpl().setInChannelOperation(false);
332 src.position (src.position() + len);
338 public long write (ByteBuffer[] srcs, int offset, int length)
342 throw new NotYetConnectedException();
345 || (offset > srcs.length)
347 || (length > (srcs.length - offset)))
348 throw new IndexOutOfBoundsException();
350 long writtenBytes = 0;
352 for (int index = offset; index < length; index++)
353 writtenBytes += write (srcs [index]);