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)
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
;
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
82 this(new SelectorProviderImpl(), create
);
85 public SocketChannelImpl(VMChannel channel
) throws IOException
87 this(new SelectorProviderImpl(), channel
, false);
90 SocketChannelImpl(SelectorProvider provider
) throws IOException
95 SocketChannelImpl(SelectorProvider provider
, boolean create
)
98 this(provider
, new VMChannel(), create
);
101 SocketChannelImpl(SelectorProvider provider
, VMChannel channel
, boolean create
)
105 this.channel
= channel
;
107 channel
.initSocket(true);
108 socket
= new NIOSocket(this);
109 configureBlocking(true);
112 /*SocketChannelImpl (SelectorProvider provider,
117 this.impl = socket.getPlainSocketImpl();
118 this.socket = socket;
121 public void finalizer()
135 //PlainSocketImpl getPlainSocketImpl()
137 // return null; // XXX
140 protected void implCloseSelectableChannel() throws IOException
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
158 throw new ClosedChannelException();
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
;
179 public boolean finishConnect()
183 throw new ClosedChannelException();
185 InetSocketAddress remote
= channel
.getPeerAddress();
188 connectionPending
= false;
192 if (!connectionPending
)
193 throw new NoConnectionPendingException();
198 public boolean isConnected()
200 // Wait until finishConnect is called before transitioning to
202 if (connectionPending
)
206 InetSocketAddress remote
= channel
.getPeerAddress();
207 return remote
!= null;
209 catch (IOException ioe
)
211 ioe
.printStackTrace(System
.out
);
216 public boolean isConnectionPending ()
218 return connectionPending
;
221 public Socket
socket ()
226 public int read(ByteBuffer dst
) throws IOException
229 throw new NotYetConnectedException();
231 return channel
.read(dst
);
234 public long read (ByteBuffer
[] dsts
, int offset
, int length
)
238 throw new NotYetConnectedException();
241 || (offset
> dsts
.length
)
243 || (length
> (dsts
.length
- offset
)))
244 throw new IndexOutOfBoundsException();
246 return channel
.readScattering(dsts
, offset
, length
);
249 public int write(ByteBuffer src
) throws IOException
252 throw new NotYetConnectedException();
254 return channel
.write(src
);
257 public long write(ByteBuffer
[] srcs
, int offset
, int length
)
261 throw new NotYetConnectedException();
264 || (offset
> srcs
.length
)
266 || (length
> (srcs
.length
- offset
)))
267 throw new IndexOutOfBoundsException();
269 return channel
.writeGathering(srcs
, offset
, length
);
272 public VMChannel
getVMChannel()
274 // XXX security check?