Merged gcj-eclipse branch to trunk.
[official-gcc.git] / libjava / classpath / gnu / java / nio / SocketChannelImpl.java
blob1c563ac097c0aa5e2e2d9aade753a2278bda2bc4
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 gnu.java.net.PlainSocketImpl;
42 import gnu.java.net.VMPlainSocketImpl;
44 import java.io.IOException;
45 import java.io.InputStream;
46 import java.io.OutputStream;
47 import java.lang.reflect.InvocationTargetException;
48 import java.lang.reflect.Method;
49 import java.net.InetSocketAddress;
50 import java.net.Socket;
51 import java.net.SocketAddress;
52 import java.net.SocketException;
53 import java.net.SocketTimeoutException;
54 import java.nio.ByteBuffer;
55 import java.nio.ReadOnlyBufferException;
56 import java.nio.channels.AlreadyConnectedException;
57 import java.nio.channels.ClosedChannelException;
58 import java.nio.channels.ConnectionPendingException;
59 import java.nio.channels.NoConnectionPendingException;
60 import java.nio.channels.NotYetConnectedException;
61 import java.nio.channels.SelectionKey;
62 import java.nio.channels.Selector;
63 import java.nio.channels.SocketChannel;
64 import java.nio.channels.UnresolvedAddressException;
65 import java.nio.channels.UnsupportedAddressTypeException;
66 import java.nio.channels.spi.SelectorProvider;
68 public final class SocketChannelImpl extends SocketChannel
69 implements VMChannelOwner
71 private VMChannel channel;
72 //private PlainSocketImpl impl;
73 private NIOSocket socket;
74 private boolean connectionPending;
75 private boolean connected;
76 private InetSocketAddress connectAddress;
78 public SocketChannelImpl(boolean create) throws IOException
80 // XXX consider adding security check; this is used by
81 // PlainSocketImpl.
82 this(new SelectorProviderImpl(), create);
85 public SocketChannelImpl(VMChannel channel) throws IOException
87 this(new SelectorProviderImpl(), channel, false);
90 SocketChannelImpl(SelectorProvider provider) throws IOException
92 this(provider, true);
95 SocketChannelImpl(SelectorProvider provider, boolean create)
96 throws IOException
98 this(provider, new VMChannel(), create);
101 SocketChannelImpl(SelectorProvider provider, VMChannel channel, boolean create)
102 throws IOException
104 super (provider);
105 this.channel = channel;
106 if (create)
107 channel.initSocket(true);
108 socket = new NIOSocket(this);
109 configureBlocking(true);
112 /*SocketChannelImpl (SelectorProvider provider,
113 NIOSocket socket)
114 throws IOException
116 super (provider);
117 this.impl = socket.getPlainSocketImpl();
118 this.socket = socket;
121 public void finalizer()
123 if (isConnected())
127 close ();
129 catch (Exception e)
135 //PlainSocketImpl getPlainSocketImpl()
137 // return null; // XXX
140 protected void implCloseSelectableChannel() throws IOException
142 channel.close();
145 protected void implConfigureBlocking (boolean blocking) throws IOException
147 channel.setBlocking(blocking);
150 public boolean connect (SocketAddress remote) throws IOException
152 return connect(remote, 0);
155 public boolean connect (SocketAddress remote, int timeout) throws IOException
157 if (!isOpen())
158 throw new ClosedChannelException();
160 if (isConnected())
161 throw new AlreadyConnectedException();
163 if (connectionPending)
164 throw new ConnectionPendingException();
166 if (!(remote instanceof InetSocketAddress))
167 throw new UnsupportedAddressTypeException();
169 connectAddress = (InetSocketAddress) remote;
171 if (connectAddress.isUnresolved())
172 throw new UnresolvedAddressException();
174 connected = channel.connect(connectAddress, timeout);
175 connectionPending = !connected;
176 return connected;
179 public boolean finishConnect()
180 throws IOException
182 if (!isOpen())
183 throw new ClosedChannelException();
185 InetSocketAddress remote = channel.getPeerAddress();
186 if (remote != null)
188 connectionPending = false;
189 return true;
192 if (!connectionPending)
193 throw new NoConnectionPendingException();
195 return false;
198 public boolean isConnected()
200 // Wait until finishConnect is called before transitioning to
201 // connected.
202 if (connectionPending)
203 return false;
206 InetSocketAddress remote = channel.getPeerAddress();
207 return remote != null;
209 catch (IOException ioe)
211 ioe.printStackTrace(System.out);
212 return false;
216 public boolean isConnectionPending ()
218 return connectionPending;
221 public Socket socket ()
223 return socket;
226 public int read(ByteBuffer dst) throws IOException
228 if (!isConnected())
229 throw new NotYetConnectedException();
231 return channel.read(dst);
234 public long read (ByteBuffer[] dsts, int offset, int length)
235 throws IOException
237 if (!isConnected())
238 throw new NotYetConnectedException();
240 if ((offset < 0)
241 || (offset > dsts.length)
242 || (length < 0)
243 || (length > (dsts.length - offset)))
244 throw new IndexOutOfBoundsException();
246 return channel.readScattering(dsts, offset, length);
249 public int write(ByteBuffer src) throws IOException
251 if (!isConnected())
252 throw new NotYetConnectedException();
254 return channel.write(src);
257 public long write(ByteBuffer[] srcs, int offset, int length)
258 throws IOException
260 if (!isConnected())
261 throw new NotYetConnectedException();
263 if ((offset < 0)
264 || (offset > srcs.length)
265 || (length < 0)
266 || (length > (srcs.length - offset)))
267 throw new IndexOutOfBoundsException();
269 return channel.writeGathering(srcs, offset, length);
272 public VMChannel getVMChannel()
274 // XXX security check?
275 return channel;