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 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
70 this(new SelectorProviderImpl(), create
);
73 public SocketChannelImpl(VMChannel channel
) throws IOException
75 this(new SelectorProviderImpl(), channel
, false);
78 SocketChannelImpl(SelectorProvider provider
) throws IOException
83 SocketChannelImpl(SelectorProvider provider
, boolean create
)
86 this(provider
, new VMChannel(), create
);
89 SocketChannelImpl(SelectorProvider provider
, VMChannel channel
, boolean create
)
93 this.channel
= channel
;
95 channel
.initSocket(true);
96 socket
= new NIOSocket(this);
97 configureBlocking(true);
100 /*SocketChannelImpl (SelectorProvider provider,
105 this.impl = socket.getPlainSocketImpl();
106 this.socket = socket;
109 public void finalizer()
123 //PlainSocketImpl getPlainSocketImpl()
125 // return null; // XXX
128 protected void implCloseSelectableChannel() throws IOException
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
146 throw new ClosedChannelException();
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
;
167 public boolean finishConnect()
171 throw new ClosedChannelException();
173 InetSocketAddress remote
= channel
.getPeerAddress();
176 connectionPending
= false;
180 if (!connectionPending
)
181 throw new NoConnectionPendingException();
186 public boolean isConnected()
188 // Wait until finishConnect is called before transitioning to
190 if (connectionPending
)
194 InetSocketAddress remote
= channel
.getPeerAddress();
195 return remote
!= null;
197 catch (IOException ioe
)
199 ioe
.printStackTrace(System
.out
);
204 public boolean isConnectionPending ()
206 return connectionPending
;
209 public Socket
socket ()
214 public int read(ByteBuffer dst
) throws IOException
217 throw new NotYetConnectedException();
219 return channel
.read(dst
);
222 public long read (ByteBuffer
[] dsts
, int offset
, int length
)
226 throw new NotYetConnectedException();
229 || (offset
> dsts
.length
)
231 || (length
> (dsts
.length
- offset
)))
232 throw new IndexOutOfBoundsException();
234 return channel
.readScattering(dsts
, offset
, length
);
237 public int write(ByteBuffer src
) throws IOException
240 throw new NotYetConnectedException();
242 return channel
.write(src
);
245 public long write(ByteBuffer
[] srcs
, int offset
, int length
)
249 throw new NotYetConnectedException();
252 || (offset
> srcs
.length
)
254 || (length
> (srcs
.length
- offset
)))
255 throw new IndexOutOfBoundsException();
257 return channel
.writeGathering(srcs
, offset
, length
);
260 public VMChannel
getVMChannel()
262 // XXX security check?