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/] [SocketChannelImpl.java] - Blame information for rev 14

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 jlechner
/* 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 gnu.java.net.PlainSocketImpl;
42
 
43
import java.io.IOException;
44
import java.io.InputStream;
45
import java.io.OutputStream;
46
import java.net.InetSocketAddress;
47
import java.net.Socket;
48
import java.net.SocketAddress;
49
import java.net.SocketTimeoutException;
50
import java.nio.ByteBuffer;
51
import java.nio.channels.AlreadyConnectedException;
52
import java.nio.channels.ClosedChannelException;
53
import java.nio.channels.ConnectionPendingException;
54
import java.nio.channels.NoConnectionPendingException;
55
import java.nio.channels.NotYetConnectedException;
56
import java.nio.channels.SelectionKey;
57
import java.nio.channels.Selector;
58
import java.nio.channels.SocketChannel;
59
import java.nio.channels.UnresolvedAddressException;
60
import java.nio.channels.UnsupportedAddressTypeException;
61
import java.nio.channels.spi.SelectorProvider;
62
 
63
public final class SocketChannelImpl extends SocketChannel
64
{
65
  private PlainSocketImpl impl;
66
  private NIOSocket socket;
67
  private boolean connectionPending;
68
 
69
  SocketChannelImpl (SelectorProvider provider)
70
    throws IOException
71
  {
72
    super (provider);
73
    impl = new PlainSocketImpl();
74
    socket = new NIOSocket (impl, this);
75
    configureBlocking(true);
76
  }
77
 
78
  SocketChannelImpl (SelectorProvider provider,
79
                     NIOSocket socket)
80
    throws IOException
81
  {
82
    super (provider);
83
    this.impl = socket.getPlainSocketImpl();
84
    this.socket = socket;
85
  }
86
 
87
  public void finalizer()
88
  {
89
    if (isConnected())
90
      {
91
        try
92
          {
93
            close ();
94
          }
95
        catch (Exception e)
96
          {
97
          }
98
      }
99
  }
100
 
101
  PlainSocketImpl getPlainSocketImpl()
102
  {
103
    return impl;
104
  }
105
 
106
  protected void implCloseSelectableChannel () throws IOException
107
  {
108
    socket.close();
109
  }
110
 
111
  protected void implConfigureBlocking (boolean blocking) throws IOException
112
  {
113
    socket.setSoTimeout (blocking ? 0 : NIOConstants.DEFAULT_TIMEOUT);
114
  }
115
 
116
  public boolean connect (SocketAddress remote) throws IOException
117
  {
118
    if (!isOpen())
119
      throw new ClosedChannelException();
120
 
121
    if (isConnected())
122
      throw new AlreadyConnectedException();
123
 
124
    if (connectionPending)
125
      throw new ConnectionPendingException();
126
 
127
    if (!(remote instanceof InetSocketAddress))
128
      throw new UnsupportedAddressTypeException();
129
 
130
    if (((InetSocketAddress) remote).isUnresolved())
131
      throw new UnresolvedAddressException();
132
 
133
    try
134
      {
135
        socket.getPlainSocketImpl().setInChannelOperation(true);
136
          // indicate that a channel is initiating the accept operation
137
          // so that the socket ignores the fact that we might be in
138
          // non-blocking mode.
139
 
140
        if (isBlocking())
141
          {
142
            // Do blocking connect.
143
            socket.connect (remote);
144
            return true;
145
          }
146
 
147
        // Do non-blocking connect.
148
        try
149
          {
150
            socket.connect (remote, NIOConstants.DEFAULT_TIMEOUT);
151
            return true;
152
          }
153
        catch (SocketTimeoutException e)
154
          {
155
            connectionPending = true;
156
            return false;
157
          }
158
      }
159
    finally
160
      {
161
        socket.getPlainSocketImpl().setInChannelOperation(false);
162
      }
163
  }
164
 
165
  public boolean finishConnect ()
166
    throws IOException
167
  {
168
    if (!isOpen())
169
      throw new ClosedChannelException();
170
 
171
    if (!isConnected() && !connectionPending)
172
      throw new NoConnectionPendingException();
173
 
174
    if (isConnected())
175
      return true;
176
 
177
    // FIXME: Handle blocking/non-blocking mode.
178
 
179
    Selector selector = provider().openSelector();
180
    register(selector, SelectionKey.OP_CONNECT);
181
 
182
    if (isBlocking())
183
      {
184
        selector.select(); // blocking until channel is connected.
185
        connectionPending = false;
186
        return true;
187
      }
188
 
189
    int ready = selector.selectNow(); // non-blocking
190
    if (ready == 1)
191
      {
192
        connectionPending = false;
193
        return true;
194
      }
195
 
196
    return false;
197
  }
198
 
199
  public boolean isConnected ()
200
  {
201
    return socket.isConnected();
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
    byte[] data;
220
    int offset = 0;
221
    InputStream input = socket.getInputStream();
222
    int available = input.available();
223
    int len = dst.remaining();
224
 
225
    if ((! isBlocking()) && available == 0)
226
      return 0;
227
 
228
    if (dst.hasArray())
229
      {
230
        offset = dst.arrayOffset() + dst.position();
231
        data = dst.array();
232
      }
233
    else
234
      {
235
        data = new byte [len];
236
      }
237
 
238
    int readBytes = 0;
239
    boolean completed = false;
240
 
241
    try
242
      {
243
        begin();
244
        socket.getPlainSocketImpl().setInChannelOperation(true);
245
        readBytes = input.read (data, offset, len);
246
        completed = true;
247
      }
248
    finally
249
      {
250
        end (completed);
251
        socket.getPlainSocketImpl().setInChannelOperation(false);
252
      }
253
 
254
    if (readBytes > 0)
255
      if (dst.hasArray())
256
        {
257
          dst.position (dst.position() + readBytes);
258
        }
259
      else
260
        {
261
          dst.put (data, offset, readBytes);
262
        }
263
 
264
    return readBytes;
265
  }
266
 
267
  public long read (ByteBuffer[] dsts, int offset, int length)
268
    throws IOException
269
  {
270
    if (!isConnected())
271
      throw new NotYetConnectedException();
272
 
273
    if ((offset < 0)
274
        || (offset > dsts.length)
275
        || (length < 0)
276
        || (length > (dsts.length - offset)))
277
      throw new IndexOutOfBoundsException();
278
 
279
    long readBytes = 0;
280
 
281
    for (int index = offset; index < length; index++)
282
      readBytes += read (dsts [index]);
283
 
284
    return readBytes;
285
  }
286
 
287
  public int write (ByteBuffer src)
288
    throws IOException
289
  {
290
    if (!isConnected())
291
      throw new NotYetConnectedException();
292
 
293
    byte[] data;
294
    int offset = 0;
295
    int len = src.remaining();
296
 
297
    if (!src.hasArray())
298
      {
299
        data = new byte [len];
300
        src.get (data, 0, len);
301
      }
302
    else
303
      {
304
        offset = src.arrayOffset() + src.position();
305
        data = src.array();
306
      }
307
 
308
    OutputStream output = socket.getOutputStream();
309
    boolean completed = false;
310
 
311
    try
312
      {
313
        begin();
314
        socket.getPlainSocketImpl().setInChannelOperation(true);
315
        output.write (data, offset, len);
316
        completed = true;
317
      }
318
    finally
319
      {
320
        end (completed);
321
        socket.getPlainSocketImpl().setInChannelOperation(false);
322
      }
323
 
324
    if (src.hasArray())
325
      {
326
        src.position (src.position() + len);
327
      }
328
 
329
    return len;
330
  }
331
 
332
  public long write (ByteBuffer[] srcs, int offset, int length)
333
    throws IOException
334
  {
335
    if (!isConnected())
336
      throw new NotYetConnectedException();
337
 
338
    if ((offset < 0)
339
        || (offset > srcs.length)
340
        || (length < 0)
341
        || (length > (srcs.length - offset)))
342
      throw new IndexOutOfBoundsException();
343
 
344
    long writtenBytes = 0;
345
 
346
    for (int index = offset; index < length; index++)
347
      writtenBytes += write (srcs [index]);
348
 
349
    return writtenBytes;
350
  }
351
}

powered by: WebSVN 2.1.0

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