1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53:
54:
57: public final class DatagramChannelImpl extends DatagramChannel
58: {
59: private NIODatagramSocket socket;
60:
61:
65: private boolean inChannelOperation;
66:
67:
74: public final boolean isInChannelOperation()
75: {
76: return inChannelOperation;
77: }
78:
79:
83: public final void setInChannelOperation(boolean b)
84: {
85: inChannelOperation = b;
86: }
87:
88: protected DatagramChannelImpl (SelectorProvider provider)
89: throws IOException
90: {
91: super (provider);
92: socket = new NIODatagramSocket (new PlainDatagramSocketImpl(), this);
93: configureBlocking(true);
94: }
95:
96: public int getNativeFD()
97: {
98: return socket.getPlainDatagramSocketImpl().getNativeFD();
99: }
100:
101: public DatagramSocket socket ()
102: {
103: return socket;
104: }
105:
106: protected void implCloseSelectableChannel ()
107: throws IOException
108: {
109: socket.close ();
110: }
111:
112: protected void implConfigureBlocking (boolean blocking)
113: throws IOException
114: {
115: socket.setSoTimeout (blocking ? 0 : NIOConstants.DEFAULT_TIMEOUT);
116: }
117:
118: public DatagramChannel connect (SocketAddress remote)
119: throws IOException
120: {
121: if (!isOpen())
122: throw new ClosedChannelException();
123:
124: socket.connect (remote);
125: return this;
126: }
127:
128: public DatagramChannel disconnect ()
129: throws IOException
130: {
131: socket.disconnect ();
132: return this;
133: }
134:
135: public boolean isConnected ()
136: {
137: return socket.isConnected ();
138: }
139:
140: public int write (ByteBuffer src)
141: throws IOException
142: {
143: if (!isConnected ())
144: throw new NotYetConnectedException ();
145:
146: return send (src, socket.getRemoteSocketAddress());
147: }
148:
149: public long write (ByteBuffer[] srcs, int offset, int length)
150: throws IOException
151: {
152: if (!isConnected())
153: throw new NotYetConnectedException();
154:
155: if ((offset < 0)
156: || (offset > srcs.length)
157: || (length < 0)
158: || (length > (srcs.length - offset)))
159: throw new IndexOutOfBoundsException();
160:
161: long result = 0;
162:
163: for (int index = offset; index < offset + length; index++)
164: result += write (srcs [index]);
165:
166: return result;
167: }
168:
169: public int read (ByteBuffer dst)
170: throws IOException
171: {
172: if (!isConnected ())
173: throw new NotYetConnectedException ();
174:
175: int remaining = dst.remaining();
176: receive (dst);
177: return remaining - dst.remaining();
178: }
179:
180: public long read (ByteBuffer[] dsts, int offset, int length)
181: throws IOException
182: {
183: if (!isConnected())
184: throw new NotYetConnectedException();
185:
186: if ((offset < 0)
187: || (offset > dsts.length)
188: || (length < 0)
189: || (length > (dsts.length - offset)))
190: throw new IndexOutOfBoundsException();
191:
192: long result = 0;
193:
194: for (int index = offset; index < offset + length; index++)
195: result += read (dsts [index]);
196:
197: return result;
198: }
199:
200: public SocketAddress receive (ByteBuffer dst)
201: throws IOException
202: {
203: if (!isOpen())
204: throw new ClosedChannelException();
205:
206: try
207: {
208: DatagramPacket packet;
209: int len = dst.remaining();
210:
211: if (dst.hasArray())
212: {
213: packet = new DatagramPacket (dst.array(),
214: dst.arrayOffset() + dst.position(),
215: len);
216: }
217: else
218: {
219: packet = new DatagramPacket (new byte [len], len);
220: }
221:
222: boolean completed = false;
223:
224: try
225: {
226: begin();
227: setInChannelOperation(true);
228: socket.receive (packet);
229: completed = true;
230: }
231: finally
232: {
233: end (completed);
234: setInChannelOperation(false);
235: }
236:
237: if (!dst.hasArray())
238: {
239: dst.put (packet.getData(), packet.getOffset(), packet.getLength());
240: }
241: else
242: {
243: dst.position (dst.position() + packet.getLength());
244: }
245:
246: return packet.getSocketAddress();
247: }
248: catch (SocketTimeoutException e)
249: {
250: return null;
251: }
252: }
253:
254: public int send (ByteBuffer src, SocketAddress target)
255: throws IOException
256: {
257: if (!isOpen())
258: throw new ClosedChannelException();
259:
260: if (target instanceof InetSocketAddress
261: && ((InetSocketAddress) target).isUnresolved())
262: throw new IOException("Target address not resolved");
263:
264: byte[] buffer;
265: int offset = 0;
266: int len = src.remaining();
267:
268: if (src.hasArray())
269: {
270: buffer = src.array();
271: offset = src.arrayOffset() + src.position();
272: }
273: else
274: {
275: buffer = new byte [len];
276: src.get (buffer);
277: }
278:
279: DatagramPacket packet = new DatagramPacket (buffer, offset, len, target);
280:
281: boolean completed = false;
282: try
283: {
284: begin();
285: setInChannelOperation(true);
286: socket.send(packet);
287: completed = true;
288: }
289: finally
290: {
291: end (completed);
292: setInChannelOperation(false);
293: }
294:
295: if (src.hasArray())
296: {
297: src.position (src.position() + len);
298: }
299:
300: return len;
301: }
302: }