OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [libjava/] [classpath/] [gnu/] [java/] [nio/] [DatagramChannelImpl.java] - Blame information for rev 14

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 jlechner
/* DatagramChannelImpl.java --
2
   Copyright (C) 2002, 2003, 2004  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., 51 Franklin Street, Fifth Floor, Boston, MA
19
02110-1301 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 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;
53
 
54
/**
55
 * @author Michael Koch
56
 */
57
public final class DatagramChannelImpl extends DatagramChannel
58
{
59
  private NIODatagramSocket socket;
60
 
61
  /**
62
   * Indicates whether this channel initiated whatever operation
63
   * is being invoked on our datagram socket.
64
   */
65
  private boolean inChannelOperation;
66
 
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.
73
   */
74
  public final boolean isInChannelOperation()
75
  {
76
    return inChannelOperation;
77
  }
78
 
79
  /**
80
   * Sets our indicator of whether we are initiating an I/O operation
81
   * on our socket.
82
   */
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 DatagramSocket socket ()
97
  {
98
    return socket;
99
  }
100
 
101
  protected void implCloseSelectableChannel ()
102
    throws IOException
103
  {
104
    socket.close ();
105
  }
106
 
107
  protected void implConfigureBlocking (boolean blocking)
108
    throws IOException
109
  {
110
    socket.setSoTimeout (blocking ? 0 : NIOConstants.DEFAULT_TIMEOUT);
111
  }
112
 
113
  public DatagramChannel connect (SocketAddress remote)
114
    throws IOException
115
  {
116
    if (!isOpen())
117
      throw new ClosedChannelException();
118
 
119
    socket.connect (remote);
120
    return this;
121
  }
122
 
123
  public DatagramChannel disconnect ()
124
    throws IOException
125
  {
126
    socket.disconnect ();
127
    return this;
128
  }
129
 
130
  public boolean isConnected ()
131
  {
132
    return socket.isConnected ();
133
  }
134
 
135
  public int write (ByteBuffer src)
136
    throws IOException
137
  {
138
    if (!isConnected ())
139
      throw new NotYetConnectedException ();
140
 
141
    return send (src, socket.getRemoteSocketAddress());
142
  }
143
 
144
  public long write (ByteBuffer[] srcs, int offset, int length)
145
    throws IOException
146
  {
147
    if (!isConnected())
148
      throw new NotYetConnectedException();
149
 
150
    if ((offset < 0)
151
        || (offset > srcs.length)
152
        || (length < 0)
153
        || (length > (srcs.length - offset)))
154
      throw new IndexOutOfBoundsException();
155
 
156
    long result = 0;
157
 
158
    for (int index = offset; index < offset + length; index++)
159
      result += write (srcs [index]);
160
 
161
    return result;
162
  }
163
 
164
  public int read (ByteBuffer dst)
165
    throws IOException
166
  {
167
    if (!isConnected ())
168
      throw new NotYetConnectedException ();
169
 
170
    int remaining = dst.remaining();
171
    receive (dst);
172
    return remaining - dst.remaining();
173
  }
174
 
175
  public long read (ByteBuffer[] dsts, int offset, int length)
176
    throws IOException
177
  {
178
    if (!isConnected())
179
      throw new NotYetConnectedException();
180
 
181
    if ((offset < 0)
182
        || (offset > dsts.length)
183
        || (length < 0)
184
        || (length > (dsts.length - offset)))
185
      throw new IndexOutOfBoundsException();
186
 
187
    long result = 0;
188
 
189
    for (int index = offset; index < offset + length; index++)
190
      result += read (dsts [index]);
191
 
192
    return result;
193
  }
194
 
195
  public SocketAddress receive (ByteBuffer dst)
196
    throws IOException
197
  {
198
    if (!isOpen())
199
      throw new ClosedChannelException();
200
 
201
    try
202
      {
203
        DatagramPacket packet;
204
        int len = dst.remaining();
205
 
206
        if (dst.hasArray())
207
          {
208
            packet = new DatagramPacket (dst.array(),
209
                                         dst.arrayOffset() + dst.position(),
210
                                         len);
211
          }
212
        else
213
          {
214
            packet = new DatagramPacket (new byte [len], len);
215
          }
216
 
217
        boolean completed = false;
218
 
219
        try
220
          {
221
            begin();
222
            setInChannelOperation(true);
223
            socket.receive (packet);
224
            completed = true;
225
          }
226
        finally
227
          {
228
            end (completed);
229
            setInChannelOperation(false);
230
          }
231
 
232
        if (!dst.hasArray())
233
          {
234
            dst.put (packet.getData(), packet.getOffset(), packet.getLength());
235
          }
236
        else
237
          {
238
            dst.position (dst.position() + packet.getLength());
239
          }
240
 
241
        return packet.getSocketAddress();
242
      }
243
    catch (SocketTimeoutException e)
244
      {
245
        return null;
246
      }
247
  }
248
 
249
  public int send (ByteBuffer src, SocketAddress target)
250
    throws IOException
251
  {
252
    if (!isOpen())
253
      throw new ClosedChannelException();
254
 
255
    if (target instanceof InetSocketAddress
256
        && ((InetSocketAddress) target).isUnresolved())
257
      throw new IOException("Target address not resolved");
258
 
259
    byte[] buffer;
260
    int offset = 0;
261
    int len = src.remaining();
262
 
263
    if (src.hasArray())
264
      {
265
        buffer = src.array();
266
        offset = src.arrayOffset() + src.position();
267
      }
268
    else
269
      {
270
        buffer = new byte [len];
271
        src.get (buffer);
272
      }
273
 
274
    DatagramPacket packet = new DatagramPacket (buffer, offset, len, target);
275
 
276
    boolean completed = false;
277
    try
278
      {
279
        begin();
280
        setInChannelOperation(true);
281
        socket.send(packet);
282
        completed = true;
283
      }
284
    finally
285
      {
286
        end (completed);
287
        setInChannelOperation(false);
288
      }
289
 
290
    if (src.hasArray())
291
      {
292
        src.position (src.position() + len);
293
      }
294
 
295
    return len;
296
  }
297
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.