1 /* SocketChannelImpl.java --
2 Copyright (C) 2002, 2003, 2004, 2006 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();
74 socket
= new NIOSocket (impl
, this);
75 configureBlocking(true);
78 SocketChannelImpl (SelectorProvider provider
,
83 this.impl
= socket
.getPlainSocketImpl();
87 public void finalizer()
101 PlainSocketImpl
getPlainSocketImpl()
108 return socket
.getPlainSocketImpl().getNativeFD();
111 protected void implCloseSelectableChannel () throws IOException
116 protected void implConfigureBlocking (boolean blocking
) throws IOException
118 socket
.setSoTimeout (blocking ?
0 : NIOConstants
.DEFAULT_TIMEOUT
);
121 public boolean connect (SocketAddress remote
) throws IOException
124 throw new ClosedChannelException();
127 throw new AlreadyConnectedException();
129 if (connectionPending
)
130 throw new ConnectionPendingException();
132 if (!(remote
instanceof InetSocketAddress
))
133 throw new UnsupportedAddressTypeException();
135 if (((InetSocketAddress
) remote
).isUnresolved())
136 throw new UnresolvedAddressException();
140 socket
.getPlainSocketImpl().setInChannelOperation(true);
141 // indicate that a channel is initiating the accept operation
142 // so that the socket ignores the fact that we might be in
143 // non-blocking mode.
147 // Do blocking connect.
148 socket
.connect (remote
);
152 // Do non-blocking connect.
155 socket
.connect (remote
, NIOConstants
.DEFAULT_TIMEOUT
);
158 catch (SocketTimeoutException e
)
160 connectionPending
= true;
166 socket
.getPlainSocketImpl().setInChannelOperation(false);
170 public boolean finishConnect ()
174 throw new ClosedChannelException();
176 if (!isConnected() && !connectionPending
)
177 throw new NoConnectionPendingException();
182 // FIXME: Handle blocking/non-blocking mode.
184 Selector selector
= provider().openSelector();
185 register(selector
, SelectionKey
.OP_CONNECT
);
189 selector
.select(); // blocking until channel is connected.
190 connectionPending
= false;
194 int ready
= selector
.selectNow(); // non-blocking
197 connectionPending
= false;
204 public boolean isConnected ()
206 return socket
.isConnected();
209 public boolean isConnectionPending ()
211 return connectionPending
;
214 public Socket
socket ()
219 public int read(ByteBuffer dst
) throws IOException
222 throw new NotYetConnectedException();
226 InputStream input
= socket
.getInputStream();
227 int available
= input
.available();
228 int len
= dst
.remaining();
230 if ((! isBlocking()) && available
== 0)
235 offset
= dst
.arrayOffset() + dst
.position();
240 data
= new byte [len
];
244 boolean completed
= false;
249 socket
.getPlainSocketImpl().setInChannelOperation(true);
250 readBytes
= input
.read (data
, offset
, len
);
256 socket
.getPlainSocketImpl().setInChannelOperation(false);
262 dst
.position (dst
.position() + readBytes
);
266 dst
.put (data
, offset
, readBytes
);
272 public long read (ByteBuffer
[] dsts
, int offset
, int length
)
276 throw new NotYetConnectedException();
279 || (offset
> dsts
.length
)
281 || (length
> (dsts
.length
- offset
)))
282 throw new IndexOutOfBoundsException();
286 for (int index
= offset
; index
< length
; index
++)
287 readBytes
+= read (dsts
[index
]);
292 public int write (ByteBuffer src
)
296 throw new NotYetConnectedException();
300 int len
= src
.remaining();
304 data
= new byte [len
];
305 src
.get (data
, 0, len
);
309 offset
= src
.arrayOffset() + src
.position();
313 OutputStream output
= socket
.getOutputStream();
314 boolean completed
= false;
319 socket
.getPlainSocketImpl().setInChannelOperation(true);
320 output
.write (data
, offset
, len
);
326 socket
.getPlainSocketImpl().setInChannelOperation(false);
331 src
.position (src
.position() + len
);
337 public long write (ByteBuffer
[] srcs
, int offset
, int length
)
341 throw new NotYetConnectedException();
344 || (offset
> srcs
.length
)
346 || (length
> (srcs
.length
- offset
)))
347 throw new IndexOutOfBoundsException();
349 long writtenBytes
= 0;
351 for (int index
= offset
; index
< length
; index
++)
352 writtenBytes
+= write (srcs
[index
]);