Remove old autovect-branch by moving to "dead" directory.
[official-gcc.git] / old-autovect-branch / libjava / gnu / java / nio / DatagramChannelImpl.java
blobcb2a607934efcc67ecc5274d304c9f156e15ddf4
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)
9 any later version.
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
19 02110-1301 USA.
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
24 combination.
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. */
39 package gnu.java.nio;
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;
54 /**
55 * @author Michael Koch
57 public final class DatagramChannelImpl extends DatagramChannel
59 private NIODatagramSocket socket;
61 /**
62 * Indicates whether this channel initiated whatever operation
63 * is being invoked on our datagram socket.
65 private boolean inChannelOperation;
67 /**
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
72 * is initiated by us.
74 public final boolean isInChannelOperation()
76 return inChannelOperation;
79 /**
80 * Sets our indicator of whether we are initiating an I/O operation
81 * on our socket.
83 public final void setInChannelOperation(boolean b)
85 inChannelOperation = b;
88 protected DatagramChannelImpl (SelectorProvider provider)
89 throws IOException
91 super (provider);
92 socket = new NIODatagramSocket (new PlainDatagramSocketImpl(), this);
93 configureBlocking(true);
96 public int getNativeFD()
98 return socket.getPlainDatagramSocketImpl().getNativeFD();
101 public DatagramSocket socket ()
103 return socket;
106 protected void implCloseSelectableChannel ()
107 throws IOException
109 socket.close ();
112 protected void implConfigureBlocking (boolean blocking)
113 throws IOException
115 socket.setSoTimeout (blocking ? 0 : NIOConstants.DEFAULT_TIMEOUT);
118 public DatagramChannel connect (SocketAddress remote)
119 throws IOException
121 if (!isOpen())
122 throw new ClosedChannelException();
124 socket.connect (remote);
125 return this;
128 public DatagramChannel disconnect ()
129 throws IOException
131 socket.disconnect ();
132 return this;
135 public boolean isConnected ()
137 return socket.isConnected ();
140 public int write (ByteBuffer src)
141 throws IOException
143 if (!isConnected ())
144 throw new NotYetConnectedException ();
146 return send (src, socket.getRemoteSocketAddress());
149 public long write (ByteBuffer[] srcs, int offset, int length)
150 throws IOException
152 if (!isConnected())
153 throw new NotYetConnectedException();
155 if ((offset < 0)
156 || (offset > srcs.length)
157 || (length < 0)
158 || (length > (srcs.length - offset)))
159 throw new IndexOutOfBoundsException();
161 long result = 0;
163 for (int index = offset; index < offset + length; index++)
164 result += write (srcs [index]);
166 return result;
169 public int read (ByteBuffer dst)
170 throws IOException
172 if (!isConnected ())
173 throw new NotYetConnectedException ();
175 int remaining = dst.remaining();
176 receive (dst);
177 return remaining - dst.remaining();
180 public long read (ByteBuffer[] dsts, int offset, int length)
181 throws IOException
183 if (!isConnected())
184 throw new NotYetConnectedException();
186 if ((offset < 0)
187 || (offset > dsts.length)
188 || (length < 0)
189 || (length > (dsts.length - offset)))
190 throw new IndexOutOfBoundsException();
192 long result = 0;
194 for (int index = offset; index < offset + length; index++)
195 result += read (dsts [index]);
197 return result;
200 public SocketAddress receive (ByteBuffer dst)
201 throws IOException
203 if (!isOpen())
204 throw new ClosedChannelException();
208 DatagramPacket packet;
209 int len = dst.capacity() - dst.position();
211 if (dst.hasArray())
213 packet = new DatagramPacket (dst.array(),
214 dst.arrayOffset() + dst.position(),
215 len);
217 else
219 packet = new DatagramPacket (new byte [len], len);
222 boolean completed = false;
226 begin();
227 setInChannelOperation(true);
228 socket.receive (packet);
229 completed = true;
231 finally
233 end (completed);
234 setInChannelOperation(false);
237 if (!dst.hasArray())
239 dst.put (packet.getData(), packet.getOffset(), packet.getLength());
241 else
243 dst.position (dst.position() + packet.getLength());
246 return packet.getSocketAddress();
248 catch (SocketTimeoutException e)
250 return null;
254 public int send (ByteBuffer src, SocketAddress target)
255 throws IOException
257 if (!isOpen())
258 throw new ClosedChannelException();
260 if (target instanceof InetSocketAddress
261 && ((InetSocketAddress) target).isUnresolved())
262 throw new IOException("Target address not resolved");
264 byte[] buffer;
265 int offset = 0;
266 int len = src.remaining();
268 if (src.hasArray())
270 buffer = src.array();
271 offset = src.arrayOffset() + src.position();
273 else
275 buffer = new byte [len];
276 src.get (buffer);
279 DatagramPacket packet = new DatagramPacket (buffer, offset, len, target);
281 boolean completed = false;
284 begin();
285 setInChannelOperation(true);
286 socket.send(packet);
287 completed = true;
289 finally
291 end (completed);
292 setInChannelOperation(false);
295 if (src.hasArray())
297 src.position (src.position() + len);
300 return len;