1 |
/* DatagramChannelImpl.java -- |
/* DatagramChannelImpl.java -- |
2 |
Copyright (C) 2002 Free Software Foundation, Inc. |
Copyright (C) 2002, 2003 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
39 |
package gnu.java.nio; |
package gnu.java.nio; |
40 |
|
|
41 |
import java.io.IOException; |
import java.io.IOException; |
42 |
|
import java.net.DatagramPacket; |
43 |
import java.net.DatagramSocket; |
import java.net.DatagramSocket; |
44 |
|
import gnu.java.net.PlainDatagramSocketImpl; |
45 |
import java.net.SocketAddress; |
import java.net.SocketAddress; |
46 |
|
import java.net.SocketTimeoutException; |
47 |
import java.nio.ByteBuffer; |
import java.nio.ByteBuffer; |
48 |
|
import java.nio.channels.ClosedChannelException; |
49 |
import java.nio.channels.DatagramChannel; |
import java.nio.channels.DatagramChannel; |
50 |
import java.nio.channels.NotYetConnectedException; |
import java.nio.channels.NotYetConnectedException; |
51 |
import java.nio.channels.spi.SelectorProvider; |
import java.nio.channels.spi.SelectorProvider; |
52 |
|
|
53 |
public class DatagramChannelImpl extends DatagramChannel |
/** |
54 |
|
* @author Michael Koch |
55 |
|
*/ |
56 |
|
public final class DatagramChannelImpl extends DatagramChannel |
57 |
{ |
{ |
58 |
boolean blocking = false; |
private NIODatagramSocket socket; |
59 |
DatagramSocket socket; |
private boolean blocking = false; |
60 |
|
|
61 |
protected DatagramChannelImpl (SelectorProvider provider) |
protected DatagramChannelImpl (SelectorProvider provider) |
62 |
throws IOException |
throws IOException |
63 |
{ |
{ |
64 |
super (provider); |
super (provider); |
65 |
socket = new DatagramSocket (); |
socket = new NIODatagramSocket (new PlainDatagramSocketImpl(), this); |
66 |
} |
} |
67 |
|
|
68 |
public DatagramSocket socket () |
public DatagramSocket socket () |
79 |
protected void implConfigureBlocking (boolean blocking) |
protected void implConfigureBlocking (boolean blocking) |
80 |
throws IOException |
throws IOException |
81 |
{ |
{ |
82 |
this.blocking = blocking; // FIXME |
socket.setSoTimeout (blocking ? 0 : NIOConstants.DEFAULT_TIMEOUT); |
83 |
|
this.blocking = blocking; |
84 |
} |
} |
85 |
|
|
86 |
public DatagramChannel connect (SocketAddress remote) |
public DatagramChannel connect (SocketAddress remote) |
87 |
throws IOException |
throws IOException |
88 |
{ |
{ |
89 |
|
if (!isOpen()) |
90 |
|
throw new ClosedChannelException(); |
91 |
|
|
92 |
socket.connect (remote); |
socket.connect (remote); |
93 |
return this; |
return this; |
94 |
} |
} |
111 |
if (!isConnected ()) |
if (!isConnected ()) |
112 |
throw new NotYetConnectedException (); |
throw new NotYetConnectedException (); |
113 |
|
|
114 |
throw new Error ("Not implemented"); |
return send (src, socket.getRemoteSocketAddress()); |
115 |
} |
} |
116 |
|
|
117 |
public long write (ByteBuffer[] srcs, int offset, int length) |
public long write (ByteBuffer[] srcs, int offset, int length) |
118 |
throws IOException |
throws IOException |
119 |
{ |
{ |
120 |
// FIXME: Should we throw an exception if offset and/or length |
if (!isConnected()) |
121 |
// have wrong values ? |
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; |
long result = 0; |
130 |
|
|
131 |
for (int i = offset; i < offset + length; i++) |
for (int index = offset; index < offset + length; index++) |
132 |
result += write (srcs [i]); |
result += write (srcs [index]); |
133 |
|
|
134 |
return result; |
return result; |
135 |
} |
} |
140 |
if (!isConnected ()) |
if (!isConnected ()) |
141 |
throw new NotYetConnectedException (); |
throw new NotYetConnectedException (); |
142 |
|
|
143 |
throw new Error ("Not implemented"); |
int remaining = dst.remaining(); |
144 |
|
receive (dst); |
145 |
|
return remaining - dst.remaining(); |
146 |
} |
} |
147 |
|
|
148 |
public long read (ByteBuffer[] dsts, int offset, int length) |
public long read (ByteBuffer[] dsts, int offset, int length) |
149 |
throws IOException |
throws IOException |
150 |
{ |
{ |
151 |
// FIXME: Should we throw an exception if offset and/or length |
if (!isConnected()) |
152 |
// have wrong values ? |
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; |
long result = 0; |
161 |
|
|
162 |
for (int i = offset; i < offset + length; i++) |
for (int index = offset; index < offset + length; index++) |
163 |
result += read (dsts [i]); |
result += read (dsts [index]); |
164 |
|
|
165 |
return result; |
return result; |
166 |
} |
} |
168 |
public SocketAddress receive (ByteBuffer dst) |
public SocketAddress receive (ByteBuffer dst) |
169 |
throws IOException |
throws IOException |
170 |
{ |
{ |
171 |
throw new Error ("Not implemented"); |
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) |
public int send (ByteBuffer src, SocketAddress target) |
223 |
throws IOException |
throws IOException |
224 |
{ |
{ |
225 |
throw new Error ("Not implemented"); |
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 |
} |
} |