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

Subversion Repositories openrisc

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 769 jeremybenn
/* SocketChannelImpl.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 java.io.IOException;
42
import java.net.InetSocketAddress;
43
import java.net.Socket;
44
import java.net.SocketAddress;
45
import java.nio.ByteBuffer;
46
import java.nio.channels.AlreadyConnectedException;
47
import java.nio.channels.ClosedChannelException;
48
import java.nio.channels.ConnectionPendingException;
49
import java.nio.channels.NoConnectionPendingException;
50
import java.nio.channels.NotYetConnectedException;
51
import java.nio.channels.SocketChannel;
52
import java.nio.channels.UnresolvedAddressException;
53
import java.nio.channels.UnsupportedAddressTypeException;
54
import java.nio.channels.spi.SelectorProvider;
55
 
56
public final class SocketChannelImpl extends SocketChannel
57
  implements VMChannelOwner
58
{
59
  private VMChannel channel;
60
  //private PlainSocketImpl impl;
61
  private NIOSocket socket;
62
  private boolean connectionPending;
63
  private boolean connected;
64
  private InetSocketAddress connectAddress;
65
 
66
  public SocketChannelImpl(boolean create) throws IOException
67
  {
68
    // XXX consider adding security check; this is used by
69
    // PlainSocketImpl.
70
    this(new SelectorProviderImpl(), create);
71
  }
72
 
73
  public SocketChannelImpl(VMChannel channel) throws IOException
74
  {
75
    this(new SelectorProviderImpl(), channel, false);
76
  }
77
 
78
  SocketChannelImpl(SelectorProvider provider) throws IOException
79
  {
80
    this(provider, true);
81
  }
82
 
83
  SocketChannelImpl(SelectorProvider provider, boolean create)
84
    throws IOException
85
  {
86
    this(provider, new VMChannel(), create);
87
  }
88
 
89
  SocketChannelImpl(SelectorProvider provider, VMChannel channel, boolean create)
90
    throws IOException
91
  {
92
    super (provider);
93
    this.channel = channel;
94
    if (create)
95
      channel.initSocket(true);
96
    socket = new NIOSocket(this);
97
    configureBlocking(true);
98
  }
99
 
100
  /*SocketChannelImpl (SelectorProvider provider,
101
                     NIOSocket socket)
102
    throws IOException
103
  {
104
    super (provider);
105
    this.impl = socket.getPlainSocketImpl();
106
    this.socket = socket;
107
  }*/
108
 
109
  public void finalizer()
110
  {
111
    if (isConnected())
112
      {
113
        try
114
          {
115
            close ();
116
          }
117
        catch (Exception e)
118
          {
119
          }
120
      }
121
  }
122
 
123
  //PlainSocketImpl getPlainSocketImpl()
124
  //{
125
  //  return null; // XXX
126
  //}
127
 
128
  protected void implCloseSelectableChannel() throws IOException
129
  {
130
    channel.close();
131
  }
132
 
133
  protected void implConfigureBlocking (boolean blocking) throws IOException
134
  {
135
    channel.setBlocking(blocking);
136
  }
137
 
138
  public boolean connect (SocketAddress remote) throws IOException
139
  {
140
    return connect(remote, 0);
141
  }
142
 
143
  public boolean connect (SocketAddress remote, int timeout) throws IOException
144
  {
145
    if (!isOpen())
146
      throw new ClosedChannelException();
147
 
148
    if (isConnected())
149
      throw new AlreadyConnectedException();
150
 
151
    if (connectionPending)
152
      throw new ConnectionPendingException();
153
 
154
    if (!(remote instanceof InetSocketAddress))
155
      throw new UnsupportedAddressTypeException();
156
 
157
    connectAddress = (InetSocketAddress) remote;
158
 
159
    if (connectAddress.isUnresolved())
160
      throw new UnresolvedAddressException();
161
 
162
    connected = channel.connect(connectAddress, timeout);
163
    connectionPending = !connected;
164
    return connected;
165
  }
166
 
167
  public boolean finishConnect()
168
    throws IOException
169
  {
170
    if (!isOpen())
171
      throw new ClosedChannelException();
172
 
173
    InetSocketAddress remote = channel.getPeerAddress();
174
    if (remote != null)
175
      {
176
        connectionPending = false;
177
        return true;
178
      }
179
 
180
    if (!connectionPending)
181
      throw new NoConnectionPendingException();
182
 
183
    return false;
184
  }
185
 
186
  public boolean isConnected()
187
  {
188
    // Wait until finishConnect is called before transitioning to
189
    // connected.
190
    if (connectionPending)
191
      return false;
192
    try
193
      {
194
        InetSocketAddress remote = channel.getPeerAddress();
195
        return remote != null;
196
      }
197
    catch (IOException ioe)
198
      {
199
        ioe.printStackTrace(System.out);
200
        return false;
201
      }
202
  }
203
 
204
  public boolean isConnectionPending ()
205
  {
206
    return connectionPending;
207
  }
208
 
209
  public Socket socket ()
210
  {
211
    return socket;
212
  }
213
 
214
  public int read(ByteBuffer dst) throws IOException
215
  {
216
    if (!isConnected())
217
      throw new NotYetConnectedException();
218
 
219
    return channel.read(dst);
220
  }
221
 
222
  public long read (ByteBuffer[] dsts, int offset, int length)
223
    throws IOException
224
  {
225
    if (!isConnected())
226
      throw new NotYetConnectedException();
227
 
228
    if ((offset < 0)
229
        || (offset > dsts.length)
230
        || (length < 0)
231
        || (length > (dsts.length - offset)))
232
      throw new IndexOutOfBoundsException();
233
 
234
    return channel.readScattering(dsts, offset, length);
235
  }
236
 
237
  public int write(ByteBuffer src) throws IOException
238
  {
239
    if (!isConnected())
240
      throw new NotYetConnectedException();
241
 
242
    return channel.write(src);
243
  }
244
 
245
  public long write(ByteBuffer[] srcs, int offset, int length)
246
    throws IOException
247
  {
248
    if (!isConnected())
249
      throw new NotYetConnectedException();
250
 
251
    if ((offset < 0)
252
        || (offset > srcs.length)
253
        || (length < 0)
254
        || (length > (srcs.length - offset)))
255
      throw new IndexOutOfBoundsException();
256
 
257
    return channel.writeGathering(srcs, offset, length);
258
  }
259
 
260
  public VMChannel getVMChannel()
261
  {
262
    // XXX security check?
263
    return channel;
264
  }
265
}

powered by: WebSVN 2.1.0

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