1 /* SocketChannelImpl.java --
2 Copyright (C) 2002, 2003 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., 59 Temple Place, Suite 330, 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
.InputStream
;
42 import java
.io
.IOException
;
43 import java
.io
.OutputStream
;
44 import java
.net
.InetAddress
;
45 import java
.net
.InetSocketAddress
;
46 import gnu
.java
.net
.PlainSocketImpl
;
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
.UnresolvedAddressException
;
57 import java
.nio
.channels
.UnsupportedAddressTypeException
;
58 import java
.nio
.channels
.SocketChannel
;
59 import java
.nio
.channels
.Selector
;
60 import java
.nio
.channels
.SelectionKey
;
61 import java
.nio
.channels
.spi
.SelectorProvider
;
62 import gnu
.classpath
.Configuration
;
64 public final class SocketChannelImpl
extends SocketChannel
66 private NIOSocket socket
;
67 private boolean blocking
= true;
68 private boolean connected
= false;
69 private boolean connectionPending
= false;
71 SocketChannelImpl (SelectorProvider provider
)
75 socket
= new NIOSocket (new PlainSocketImpl(), this);
78 SocketChannelImpl (SelectorProvider provider
,
84 this.connected
= socket
.isConnected();
87 public void finalizer()
103 return socket
.getImpl().getNativeFD();
106 protected void implCloseSelectableChannel () throws IOException
112 protected void implConfigureBlocking (boolean blocking
) throws IOException
114 socket
.setSoTimeout (blocking ?
0 : NIOConstants
.DEFAULT_TIMEOUT
);
115 this.blocking
= blocking
;
118 public boolean connect (SocketAddress remote
) throws IOException
121 throw new ClosedChannelException();
124 throw new AlreadyConnectedException();
126 if (connectionPending
)
127 throw new ConnectionPendingException();
129 if (!(remote
instanceof InetSocketAddress
))
130 throw new UnsupportedAddressTypeException();
132 if (((InetSocketAddress
) remote
).isUnresolved())
133 throw new UnresolvedAddressException();
137 // Do blocking connect.
138 socket
.connect (remote
);
143 // Do non-blocking connect.
146 socket
.connect (remote
, NIOConstants
.DEFAULT_TIMEOUT
);
150 catch (SocketTimeoutException e
)
152 connectionPending
= true;
157 public boolean finishConnect ()
161 throw new ClosedChannelException();
163 if (!connectionPending
)
164 throw new NoConnectionPendingException();
169 // FIXME: Handle blocking/non-blocking mode.
171 Selector selector
= provider().openSelector();
172 register (selector
, SelectionKey
.OP_CONNECT
);
176 selector
.select(); // blocking until channel is connected.
178 connectionPending
= false;
182 int ready
= selector
.selectNow(); // non-blocking
186 connectionPending
= false;
193 public boolean isConnected ()
198 public boolean isConnectionPending ()
200 return connectionPending
;
203 public Socket
socket ()
208 public int read (ByteBuffer dst
) throws IOException
211 throw new NotYetConnectedException();
215 int len
= dst
.remaining();
219 offset
= dst
.arrayOffset() + dst
.position();
224 data
= new byte [len
];
227 InputStream input
= socket
.getInputStream();
228 int available
= input
.available();
237 boolean completed
= false;
242 readBytes
= input
.read (data
, offset
, len
);
259 public long read (ByteBuffer
[] dsts
, int offset
, int length
)
263 throw new NotYetConnectedException();
266 || (offset
> dsts
.length
)
268 || (length
> (dsts
.length
- offset
)))
269 throw new IndexOutOfBoundsException();
273 for (int index
= offset
; index
< length
; index
++)
274 readBytes
+= read (dsts
[index
]);
279 public int write (ByteBuffer src
)
283 throw new NotYetConnectedException();
287 int len
= src
.remaining();
291 data
= new byte [len
];
292 src
.get (data
, 0, len
);
296 offset
= src
.arrayOffset() + src
.position();
300 System
.out
.println ("INTERNAL: writing to socket outputstream");
302 OutputStream output
= socket
.getOutputStream();
303 output
.write (data
, offset
, len
);
307 public long write (ByteBuffer
[] srcs
, int offset
, int length
)
311 throw new NotYetConnectedException();
314 || (offset
> srcs
.length
)
316 || (length
> (srcs
.length
- offset
)))
317 throw new IndexOutOfBoundsException();
319 long writtenBytes
= 0;
321 for (int index
= offset
; index
< length
; index
++)
322 writtenBytes
+= write (srcs
[index
]);