Merge with trank @ 137446
[official-gcc.git] / libjava / classpath / gnu / java / nio / SocketChannelImpl.java
blob9564592e0cf2d0fd95691cfbacc63f557f4ba8c1
1 /* SocketChannelImpl.java --
2 Copyright (C) 2002, 2003, 2004 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 java.io.IOException;
42 import java.net.InetSocketAddress;
43 import java.net.Socket;
44 import java.net.SocketAddress;
45 import java.nio.ByteBuffer;
46 import java.nio.channels.AlreadyConnectedException;
47 import java.nio.channels.ClosedChannelException;
48 import java.nio.channels.ConnectionPendingException;
49 import java.nio.channels.NoConnectionPendingException;
50 import java.nio.channels.NotYetConnectedException;
51 import java.nio.channels.SocketChannel;
52 import java.nio.channels.UnresolvedAddressException;
53 import java.nio.channels.UnsupportedAddressTypeException;
54 import java.nio.channels.spi.SelectorProvider;
56 public final class SocketChannelImpl extends SocketChannel
57 implements VMChannelOwner
59 private VMChannel channel;
60 //private PlainSocketImpl impl;
61 private NIOSocket socket;
62 private boolean connectionPending;
63 private boolean connected;
64 private InetSocketAddress connectAddress;
66 public SocketChannelImpl(boolean create) throws IOException
68 // XXX consider adding security check; this is used by
69 // PlainSocketImpl.
70 this(new SelectorProviderImpl(), create);
73 public SocketChannelImpl(VMChannel channel) throws IOException
75 this(new SelectorProviderImpl(), channel, false);
78 SocketChannelImpl(SelectorProvider provider) throws IOException
80 this(provider, true);
83 SocketChannelImpl(SelectorProvider provider, boolean create)
84 throws IOException
86 this(provider, new VMChannel(), create);
89 SocketChannelImpl(SelectorProvider provider, VMChannel channel, boolean create)
90 throws IOException
92 super (provider);
93 this.channel = channel;
94 if (create)
95 channel.initSocket(true);
96 socket = new NIOSocket(this);
97 configureBlocking(true);
100 /*SocketChannelImpl (SelectorProvider provider,
101 NIOSocket socket)
102 throws IOException
104 super (provider);
105 this.impl = socket.getPlainSocketImpl();
106 this.socket = socket;
109 public void finalizer()
111 if (isConnected())
115 close ();
117 catch (Exception e)
123 //PlainSocketImpl getPlainSocketImpl()
125 // return null; // XXX
128 protected void implCloseSelectableChannel() throws IOException
130 channel.close();
133 protected void implConfigureBlocking (boolean blocking) throws IOException
135 channel.setBlocking(blocking);
138 public boolean connect (SocketAddress remote) throws IOException
140 return connect(remote, 0);
143 public boolean connect (SocketAddress remote, int timeout) throws IOException
145 if (!isOpen())
146 throw new ClosedChannelException();
148 if (isConnected())
149 throw new AlreadyConnectedException();
151 if (connectionPending)
152 throw new ConnectionPendingException();
154 if (!(remote instanceof InetSocketAddress))
155 throw new UnsupportedAddressTypeException();
157 connectAddress = (InetSocketAddress) remote;
159 if (connectAddress.isUnresolved())
160 throw new UnresolvedAddressException();
162 connected = channel.connect(connectAddress, timeout);
163 connectionPending = !connected;
164 return connected;
167 public boolean finishConnect()
168 throws IOException
170 if (!isOpen())
171 throw new ClosedChannelException();
173 InetSocketAddress remote = channel.getPeerAddress();
174 if (remote != null)
176 connectionPending = false;
177 return true;
180 if (!connectionPending)
181 throw new NoConnectionPendingException();
183 return false;
186 public boolean isConnected()
188 // Wait until finishConnect is called before transitioning to
189 // connected.
190 if (connectionPending)
191 return false;
194 InetSocketAddress remote = channel.getPeerAddress();
195 return remote != null;
197 catch (IOException ioe)
199 ioe.printStackTrace(System.out);
200 return false;
204 public boolean isConnectionPending ()
206 return connectionPending;
209 public Socket socket ()
211 return socket;
214 public int read(ByteBuffer dst) throws IOException
216 if (!isConnected())
217 throw new NotYetConnectedException();
219 return channel.read(dst);
222 public long read (ByteBuffer[] dsts, int offset, int length)
223 throws IOException
225 if (!isConnected())
226 throw new NotYetConnectedException();
228 if ((offset < 0)
229 || (offset > dsts.length)
230 || (length < 0)
231 || (length > (dsts.length - offset)))
232 throw new IndexOutOfBoundsException();
234 return channel.readScattering(dsts, offset, length);
237 public int write(ByteBuffer src) throws IOException
239 if (!isConnected())
240 throw new NotYetConnectedException();
242 return channel.write(src);
245 public long write(ByteBuffer[] srcs, int offset, int length)
246 throws IOException
248 if (!isConnected())
249 throw new NotYetConnectedException();
251 if ((offset < 0)
252 || (offset > srcs.length)
253 || (length < 0)
254 || (length > (srcs.length - offset)))
255 throw new IndexOutOfBoundsException();
257 return channel.writeGathering(srcs, offset, length);
260 public VMChannel getVMChannel()
262 // XXX security check?
263 return channel;