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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [gnu/] [java/] [nio/] [DatagramChannelImpl.java] - Blame information for rev 769

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 769 jeremybenn
/* 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.DatagramSocket;
44
import java.net.InetSocketAddress;
45
import java.net.SocketAddress;
46
import java.nio.ByteBuffer;
47
import java.nio.channels.ClosedChannelException;
48
import java.nio.channels.DatagramChannel;
49
import java.nio.channels.NotYetConnectedException;
50
import java.nio.channels.spi.SelectorProvider;
51
 
52
/**
53
 * @author Michael Koch
54
 */
55
public final class DatagramChannelImpl extends DatagramChannel
56
  implements VMChannelOwner
57
{
58
  private NIODatagramSocket socket;
59
  private VMChannel channel;
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
  protected DatagramChannelImpl (SelectorProvider provider)
68
    throws IOException
69
  {
70
    super (provider);
71
    socket = new NIODatagramSocket (new PlainDatagramSocketImpl(), this);
72
    channel = new VMChannel();
73
    channel.initSocket(false);
74
    configureBlocking(true);
75
  }
76
 
77
  /**
78
   * Indicates whether our datagram socket should ignore whether
79
   * we are set to non-blocking mode. Certain operations on our
80
   * socket throw an <code>IllegalBlockingModeException</code> if
81
   * we are in non-blocking mode, <i>except</i> if the operation
82
   * is initiated by us.
83
   */
84
  public final boolean isInChannelOperation()
85
  {
86
    return inChannelOperation;
87
  }
88
 
89
  /**
90
   * Sets our indicator of whether we are initiating an I/O operation
91
   * on our socket.
92
   */
93
  public final void setInChannelOperation(boolean b)
94
  {
95
    inChannelOperation = b;
96
  }
97
 
98
  public DatagramSocket socket ()
99
  {
100
    return socket;
101
  }
102
 
103
  protected void implCloseSelectableChannel ()
104
    throws IOException
105
  {
106
    channel.close();
107
  }
108
 
109
  protected void implConfigureBlocking (boolean blocking)
110
    throws IOException
111
  {
112
    channel.setBlocking(blocking);
113
  }
114
 
115
  public DatagramChannel connect (SocketAddress remote)
116
    throws IOException
117
  {
118
    if (!isOpen())
119
      throw new ClosedChannelException();
120
 
121
    try
122
      {
123
        channel.connect((InetSocketAddress) remote, 0);
124
      }
125
    catch (ClassCastException cce)
126
      {
127
        throw new IOException("unsupported socked address type");
128
      }
129
    return this;
130
  }
131
 
132
  public DatagramChannel disconnect ()
133
    throws IOException
134
  {
135
    channel.disconnect();
136
    return this;
137
  }
138
 
139
  public boolean isConnected()
140
  {
141
    try
142
      {
143
        return channel.getPeerAddress() != null;
144
      }
145
    catch (IOException ioe)
146
      {
147
        return false;
148
      }
149
  }
150
 
151
  public int write (ByteBuffer src)
152
    throws IOException
153
  {
154
    if (!isConnected ())
155
      throw new NotYetConnectedException ();
156
 
157
    return channel.write(src);
158
  }
159
 
160
  public long write (ByteBuffer[] srcs, int offset, int length)
161
    throws IOException
162
  {
163
    if (!isConnected())
164
      throw new NotYetConnectedException();
165
 
166
    if ((offset < 0)
167
        || (offset > srcs.length)
168
        || (length < 0)
169
        || (length > (srcs.length - offset)))
170
      throw new IndexOutOfBoundsException();
171
 
172
    /* We are connected, meaning we will write these bytes to
173
     * the host we connected to, so we don't need to explicitly
174
     * give the host. */
175
    return channel.writeGathering(srcs, offset, length);
176
  }
177
 
178
  public int read (ByteBuffer dst)
179
    throws IOException
180
  {
181
    if (!isConnected ())
182
      throw new NotYetConnectedException ();
183
 
184
    return channel.read(dst);
185
  }
186
 
187
  public long read (ByteBuffer[] dsts, int offset, int length)
188
    throws IOException
189
  {
190
    if (!isConnected())
191
      throw new NotYetConnectedException();
192
 
193
    if ((offset < 0)
194
        || (offset > dsts.length)
195
        || (length < 0)
196
        || (length > (dsts.length - offset)))
197
      throw new IndexOutOfBoundsException();
198
 
199
    /* Likewise, see the comment int write above. */
200
    return channel.readScattering(dsts, offset, length);
201
  }
202
 
203
  public SocketAddress receive (ByteBuffer dst)
204
    throws IOException
205
  {
206
    if (!isOpen())
207
      throw new ClosedChannelException();
208
 
209
    try
210
      {
211
        begin();
212
        return channel.receive(dst);
213
      }
214
    finally
215
      {
216
        end(true);
217
      }
218
  }
219
 
220
  public int send (ByteBuffer src, SocketAddress target)
221
    throws IOException
222
  {
223
    if (!isOpen())
224
      throw new ClosedChannelException();
225
 
226
    if (!(target instanceof InetSocketAddress))
227
      throw new IOException("can only send to inet socket addresses");
228
 
229
    InetSocketAddress dst = (InetSocketAddress) target;
230
    if (dst.isUnresolved())
231
      throw new IOException("Target address not resolved");
232
 
233
    return channel.send(src, dst);
234
  }
235
 
236
  public VMChannel getVMChannel()
237
  {
238
    return channel;
239
  }
240
}

powered by: WebSVN 2.1.0

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