a4f46df18611b7862b693089ef73e461302c2e22
[platform/upstream/gcc.git] / libjava / gnu / java / nio / DatagramChannelImpl.java
1 /* DatagramChannelImpl.java -- 
2    Copyright (C) 2002, 2003 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
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.
10
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.
15
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
19 02111-1307 USA.
20
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.
25
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. */
37
38
39 package gnu.java.nio;
40
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;
52
53 /**
54  * @author Michael Koch
55  */
56 public final class DatagramChannelImpl extends DatagramChannel
57 {
58   private NIODatagramSocket socket;
59   private boolean blocking = false;
60   
61   protected DatagramChannelImpl (SelectorProvider provider)
62     throws IOException
63   {
64     super (provider);
65     socket = new NIODatagramSocket (new PlainDatagramSocketImpl(), this);
66   }
67     
68   public DatagramSocket socket ()
69   {
70     return socket;
71   }
72     
73   protected void implCloseSelectableChannel ()
74     throws IOException
75   {
76     socket.close ();
77   }
78     
79   protected void implConfigureBlocking (boolean blocking)
80     throws IOException
81   {
82     socket.setSoTimeout (blocking ? 0 : NIOConstants.DEFAULT_TIMEOUT);
83     this.blocking = blocking;
84   }
85
86   public DatagramChannel connect (SocketAddress remote)
87     throws IOException
88   {
89     if (!isOpen())
90       throw new ClosedChannelException();
91     
92     socket.connect (remote);
93     return this;
94   }
95     
96   public DatagramChannel disconnect ()
97     throws IOException
98   {
99     socket.disconnect ();
100     return this;
101   }
102     
103   public boolean isConnected ()
104   {
105     return socket.isConnected ();
106   }
107     
108   public int write (ByteBuffer src)
109     throws IOException
110   {
111     if (!isConnected ())
112       throw new NotYetConnectedException ();
113     
114     return send (src, socket.getRemoteSocketAddress());
115   }
116
117   public long write (ByteBuffer[] srcs, int offset, int length)
118     throws IOException
119   {
120     if (!isConnected())
121       throw new NotYetConnectedException();
122
123     if ((offset < 0)
124         || (offset > srcs.length)
125         || (length < 0)
126         || (length > (srcs.length - offset)))
127       throw new IndexOutOfBoundsException();
128       
129     long result = 0;
130
131     for (int index = offset; index < offset + length; index++)
132       result += write (srcs [index]);
133
134     return result;
135   }
136
137   public int read (ByteBuffer dst)
138     throws IOException
139   {
140     if (!isConnected ())
141       throw new NotYetConnectedException ();
142     
143     int remaining = dst.remaining();
144     receive (dst);
145     return remaining - dst.remaining();
146   }
147     
148   public long read (ByteBuffer[] dsts, int offset, int length)
149     throws IOException
150   {
151     if (!isConnected())
152       throw new NotYetConnectedException();
153     
154     if ((offset < 0)
155         || (offset > dsts.length)
156         || (length < 0)
157         || (length > (dsts.length - offset)))
158       throw new IndexOutOfBoundsException();
159       
160     long result = 0;
161
162     for (int index = offset; index < offset + length; index++)
163       result += read (dsts [index]);
164
165     return result;
166   }
167     
168   public SocketAddress receive (ByteBuffer dst)
169     throws IOException
170   {
171     if (!isOpen())
172       throw new ClosedChannelException();
173     
174     try
175       {
176         DatagramPacket packet;
177         int len = dst.remaining();
178         
179         if (dst.hasArray())
180           {
181             packet = new DatagramPacket (dst.array(),
182                                          dst.arrayOffset() + dst.position(),
183                                          len);
184           }
185         else
186           {
187             packet = new DatagramPacket (new byte [len], len);
188           }
189
190         boolean completed = false;
191
192         try
193           {
194             begin();
195             socket.receive (packet);
196             completed = true;
197           }
198         finally
199           {
200             end (completed);
201           }
202
203         if (!dst.hasArray())
204           {
205             dst.put (packet.getData(), packet.getOffset(), packet.getLength());
206           }
207
208         // FIMXE: remove this testing code.
209         for (int i = 0; i < packet.getLength(); i++)
210           {
211             System.out.println ("Byte " + i + " has value " + packet.getData() [packet.getOffset() + i]);
212           }
213
214         return packet.getSocketAddress();
215       }
216     catch (SocketTimeoutException e)
217       {
218         return null;
219       }
220   }
221     
222   public int send (ByteBuffer src, SocketAddress target)
223     throws IOException
224   {
225     if (!isOpen())
226       throw new ClosedChannelException();
227     
228     byte[] buffer;
229     int offset = 0;
230     int len = src.remaining();
231     
232     if (src.hasArray())
233       {
234         buffer = src.array();
235         offset = src.arrayOffset() + src.position();
236       }
237     else
238       {
239         buffer = new byte [len];
240         src.get (buffer);
241       }
242
243     DatagramPacket packet = new DatagramPacket (buffer, offset, len, target);
244
245     // FIMXE: remove this testing code.
246     for (int i = 0; i < packet.getLength(); i++)
247       {
248         System.out.println ("Byte " + i + " has value " + packet.getData() [packet.getOffset() + i]);
249       }
250
251     socket.send (packet);
252     return len;
253   }
254 }