1 /* DatagramChannelImpl.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
.PlainDatagramSocketImpl
;
42 import java
.io
.IOException
;
43 import java
.net
.DatagramPacket
;
44 import java
.net
.DatagramSocket
;
45 import java
.net
.InetSocketAddress
;
46 import java
.net
.SocketAddress
;
47 import java
.net
.SocketTimeoutException
;
48 import java
.nio
.ByteBuffer
;
49 import java
.nio
.channels
.ClosedChannelException
;
50 import java
.nio
.channels
.DatagramChannel
;
51 import java
.nio
.channels
.NotYetConnectedException
;
52 import java
.nio
.channels
.spi
.SelectorProvider
;
55 * @author Michael Koch
57 public final class DatagramChannelImpl
extends DatagramChannel
59 private NIODatagramSocket socket
;
62 * Indicates whether this channel initiated whatever operation
63 * is being invoked on our datagram socket.
65 private boolean inChannelOperation
;
68 * Indicates whether our datagram socket should ignore whether
69 * we are set to non-blocking mode. Certain operations on our
70 * socket throw an <code>IllegalBlockingModeException</code> if
71 * we are in non-blocking mode, <i>except</i> if the operation
74 public final boolean isInChannelOperation()
76 return inChannelOperation
;
80 * Sets our indicator of whether we are initiating an I/O operation
83 public final void setInChannelOperation(boolean b
)
85 inChannelOperation
= b
;
88 protected DatagramChannelImpl (SelectorProvider provider
)
92 socket
= new NIODatagramSocket (new PlainDatagramSocketImpl(), this);
93 configureBlocking(true);
96 public int getNativeFD()
98 return socket
.getPlainDatagramSocketImpl().getNativeFD();
101 public DatagramSocket
socket ()
106 protected void implCloseSelectableChannel ()
112 protected void implConfigureBlocking (boolean blocking
)
115 socket
.setSoTimeout (blocking ?
0 : NIOConstants
.DEFAULT_TIMEOUT
);
118 public DatagramChannel
connect (SocketAddress remote
)
122 throw new ClosedChannelException();
124 socket
.connect (remote
);
128 public DatagramChannel
disconnect ()
131 socket
.disconnect ();
135 public boolean isConnected ()
137 return socket
.isConnected ();
140 public int write (ByteBuffer src
)
144 throw new NotYetConnectedException ();
146 return send (src
, socket
.getRemoteSocketAddress());
149 public long write (ByteBuffer
[] srcs
, int offset
, int length
)
153 throw new NotYetConnectedException();
156 || (offset
> srcs
.length
)
158 || (length
> (srcs
.length
- offset
)))
159 throw new IndexOutOfBoundsException();
163 for (int index
= offset
; index
< offset
+ length
; index
++)
164 result
+= write (srcs
[index
]);
169 public int read (ByteBuffer dst
)
173 throw new NotYetConnectedException ();
175 int remaining
= dst
.remaining();
177 return remaining
- dst
.remaining();
180 public long read (ByteBuffer
[] dsts
, int offset
, int length
)
184 throw new NotYetConnectedException();
187 || (offset
> dsts
.length
)
189 || (length
> (dsts
.length
- offset
)))
190 throw new IndexOutOfBoundsException();
194 for (int index
= offset
; index
< offset
+ length
; index
++)
195 result
+= read (dsts
[index
]);
200 public SocketAddress
receive (ByteBuffer dst
)
204 throw new ClosedChannelException();
208 DatagramPacket packet
;
209 int len
= dst
.capacity() - dst
.position();
213 packet
= new DatagramPacket (dst
.array(),
214 dst
.arrayOffset() + dst
.position(),
219 packet
= new DatagramPacket (new byte [len
], len
);
222 boolean completed
= false;
227 setInChannelOperation(true);
228 socket
.receive (packet
);
234 setInChannelOperation(false);
239 dst
.put (packet
.getData(), packet
.getOffset(), packet
.getLength());
243 dst
.position (dst
.position() + packet
.getLength());
246 return packet
.getSocketAddress();
248 catch (SocketTimeoutException e
)
254 public int send (ByteBuffer src
, SocketAddress target
)
258 throw new ClosedChannelException();
260 if (target
instanceof InetSocketAddress
261 && ((InetSocketAddress
) target
).isUnresolved())
262 throw new IOException("Target address not resolved");
266 int len
= src
.remaining();
270 buffer
= src
.array();
271 offset
= src
.arrayOffset() + src
.position();
275 buffer
= new byte [len
];
279 DatagramPacket packet
= new DatagramPacket (buffer
, offset
, len
, target
);
281 boolean completed
= false;
285 setInChannelOperation(true);
292 setInChannelOperation(false);
297 src
.position (src
.position() + len
);