1 /* DatagramChannelImpl.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.IOException;
42 import java.net.DatagramPacket;
43 import java.net.DatagramSocket;
44 import gnu.java.net.PlainDatagramSocketImpl;
45 import java.net.SocketAddress;
46 import java.net.SocketTimeoutException;
47 import java.nio.ByteBuffer;
48 import java.nio.channels.ClosedChannelException;
49 import java.nio.channels.DatagramChannel;
50 import java.nio.channels.NotYetConnectedException;
51 import java.nio.channels.spi.SelectorProvider;
54 * @author Michael Koch
56 public final class DatagramChannelImpl extends DatagramChannel
58 private NIODatagramSocket socket;
59 private boolean blocking = false;
61 protected DatagramChannelImpl (SelectorProvider provider)
65 socket = new NIODatagramSocket (new PlainDatagramSocketImpl(), this);
68 public DatagramSocket socket ()
73 protected void implCloseSelectableChannel ()
79 protected void implConfigureBlocking (boolean blocking)
82 socket.setSoTimeout (blocking ? 0 : NIOConstants.DEFAULT_TIMEOUT);
83 this.blocking = blocking;
86 public DatagramChannel connect (SocketAddress remote)
90 throw new ClosedChannelException();
92 socket.connect (remote);
96 public DatagramChannel disconnect ()
103 public boolean isConnected ()
105 return socket.isConnected ();
108 public int write (ByteBuffer src)
112 throw new NotYetConnectedException ();
114 return send (src, socket.getRemoteSocketAddress());
117 public long write (ByteBuffer[] srcs, int offset, int length)
121 throw new NotYetConnectedException();
124 || (offset > srcs.length)
126 || (length > (srcs.length - offset)))
127 throw new IndexOutOfBoundsException();
131 for (int index = offset; index < offset + length; index++)
132 result += write (srcs [index]);
137 public int read (ByteBuffer dst)
141 throw new NotYetConnectedException ();
143 int remaining = dst.remaining();
145 return remaining - dst.remaining();
148 public long read (ByteBuffer[] dsts, int offset, int length)
152 throw new NotYetConnectedException();
155 || (offset > dsts.length)
157 || (length > (dsts.length - offset)))
158 throw new IndexOutOfBoundsException();
162 for (int index = offset; index < offset + length; index++)
163 result += read (dsts [index]);
168 public SocketAddress receive (ByteBuffer dst)
172 throw new ClosedChannelException();
176 DatagramPacket packet;
177 int len = dst.remaining();
181 packet = new DatagramPacket (dst.array(),
182 dst.arrayOffset() + dst.position(),
187 packet = new DatagramPacket (new byte [len], len);
190 boolean completed = false;
195 socket.receive (packet);
205 dst.put (packet.getData(), packet.getOffset(), packet.getLength());
208 // FIMXE: remove this testing code.
209 for (int i = 0; i < packet.getLength(); i++)
211 System.out.println ("Byte " + i + " has value " + packet.getData() [packet.getOffset() + i]);
214 return packet.getSocketAddress();
216 catch (SocketTimeoutException e)
222 public int send (ByteBuffer src, SocketAddress target)
226 throw new ClosedChannelException();
230 int len = src.remaining();
234 buffer = src.array();
235 offset = src.arrayOffset() + src.position();
239 buffer = new byte [len];
243 DatagramPacket packet = new DatagramPacket (buffer, offset, len, target);
245 // FIMXE: remove this testing code.
246 for (int i = 0; i < packet.getLength(); i++)
248 System.out.println ("Byte " + i + " has value " + packet.getData() [packet.getOffset() + i]);
251 socket.send (packet);