~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/ikvm/openjdk/java/io/FileDescriptor.java

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
 
3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 
4
 *
 
5
 * This code is free software; you can redistribute it and/or modify it
 
6
 * under the terms of the GNU General Public License version 2 only, as
 
7
 * published by the Free Software Foundation.  Oracle designates this
 
8
 * particular file as subject to the "Classpath" exception as provided
 
9
 * by Oracle in the LICENSE file that accompanied this code.
 
10
 *
 
11
 * This code is distributed in the hope that it will be useful, but WITHOUT
 
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 
14
 * version 2 for more details (a copy is included in the LICENSE file that
 
15
 * accompanied this code).
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License version
 
18
 * 2 along with this work; if not, write to the Free Software Foundation,
 
19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 
20
 *
 
21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 
22
 * or visit www.oracle.com if you need additional information or have any
 
23
 * questions.
 
24
 */
 
25
 
 
26
package java.io;
 
27
 
 
28
import cli.System.IO.FileAccess;
 
29
import cli.System.IO.FileMode;
 
30
import cli.System.IO.FileShare;
 
31
import cli.System.IO.FileStream;
 
32
import cli.System.IO.SeekOrigin;
 
33
import cli.System.Runtime.InteropServices.DllImportAttribute;
 
34
import java.util.concurrent.atomic.AtomicInteger;
 
35
 
 
36
/**
 
37
 * Instances of the file descriptor class serve as an opaque handle
 
38
 * to the underlying machine-specific structure representing an
 
39
 * open file, an open socket, or another source or sink of bytes.
 
40
 * The main practical use for a file descriptor is to create a
 
41
 * {@link FileInputStream} or {@link FileOutputStream} to contain it.
 
42
 *
 
43
 * <p>Applications should not create their own file descriptors.
 
44
 *
 
45
 * @author  Pavani Diwanji
 
46
 * @since   JDK1.0
 
47
 */
 
48
public final class FileDescriptor {
 
49
 
 
50
    private volatile cli.System.IO.Stream stream;
 
51
    private volatile cli.System.Net.Sockets.Socket socket;
 
52
    private volatile boolean nonBlockingSocket;
 
53
    private volatile cli.System.IAsyncResult asyncResult;
 
54
 
 
55
    /**
 
56
     * HACK
 
57
     *   JRuby uses reflection to get at the handle (on Windows) and fd (on non-Windows)
 
58
     *   fields to convert it into a native handle and query if it is a tty.
 
59
     */
 
60
    @ikvm.lang.Property(get = "get_handle")
 
61
    private long handle;
 
62
    
 
63
    @ikvm.lang.Property(get = "get_fd")
 
64
    private int fd;
 
65
    
 
66
    @cli.System.Security.SecurityCriticalAttribute.Annotation
 
67
    private long get_handle()
 
68
    {
 
69
        if (ikvm.internal.Util.WINDOWS)
 
70
        {
 
71
            if (stream instanceof cli.System.IO.FileStream)
 
72
            {
 
73
                cli.System.IO.FileStream fs = (cli.System.IO.FileStream)stream;
 
74
                return fs.get_Handle().ToInt64();
 
75
            }
 
76
            else if (this == in)
 
77
            {
 
78
                return GetStdHandle(-10).ToInt64();
 
79
            }
 
80
            else if (this == out)
 
81
            {
 
82
                return GetStdHandle(-11).ToInt64();
 
83
            }
 
84
            else if (this == err)
 
85
            {
 
86
                return GetStdHandle(-12).ToInt64();
 
87
            }
 
88
        }
 
89
        return -1;
 
90
    }
 
91
    
 
92
    @cli.System.Security.SecurityCriticalAttribute.Annotation
 
93
    private int get_fd()
 
94
    {
 
95
        if (!ikvm.internal.Util.WINDOWS)
 
96
        {
 
97
            if (stream instanceof cli.System.IO.FileStream)
 
98
            {
 
99
                cli.System.IO.FileStream fs = (cli.System.IO.FileStream)stream;
 
100
                return fs.get_Handle().ToInt32();
 
101
            }
 
102
            else if (this == in)
 
103
            {
 
104
                return 0;
 
105
            }
 
106
            else if (this == out)
 
107
            {
 
108
                return 1;
 
109
            }
 
110
            else if (this == err)
 
111
            {
 
112
                return 2;
 
113
            }
 
114
        }
 
115
        return -1;
 
116
    }
 
117
    
 
118
    @DllImportAttribute.Annotation("kernel32")
 
119
    private static native cli.System.IntPtr GetStdHandle(int nStdHandle);
 
120
 
 
121
    /**
 
122
     * A use counter for tracking the FIS/FOS/RAF instances that
 
123
     * use this FileDescriptor. The FIS/FOS.finalize() will not release
 
124
     * the FileDescriptor if it is still under use by any stream.
 
125
     */
 
126
    private AtomicInteger useCount;
 
127
 
 
128
 
 
129
    /**
 
130
     * Constructs an (invalid) FileDescriptor
 
131
     * object.
 
132
     */
 
133
    public /**/ FileDescriptor() {
 
134
        useCount = new AtomicInteger();
 
135
    }
 
136
 
 
137
    /**
 
138
     * A handle to the standard input stream. Usually, this file
 
139
     * descriptor is not used directly, but rather via the input stream
 
140
     * known as {@code System.in}.
 
141
     *
 
142
     * @see     java.lang.System#in
 
143
     */
 
144
    public static final FileDescriptor in = standardStream(0);
 
145
 
 
146
    /**
 
147
     * A handle to the standard output stream. Usually, this file
 
148
     * descriptor is not used directly, but rather via the output stream
 
149
     * known as {@code System.out}.
 
150
     * @see     java.lang.System#out
 
151
     */
 
152
    public static final FileDescriptor out = standardStream(1);
 
153
 
 
154
    /**
 
155
     * A handle to the standard error stream. Usually, this file
 
156
     * descriptor is not used directly, but rather via the output stream
 
157
     * known as {@code System.err}.
 
158
     *
 
159
     * @see     java.lang.System#err
 
160
     */
 
161
    public static final FileDescriptor err = standardStream(2);
 
162
 
 
163
    /**
 
164
     * Tests if this file descriptor object is valid.
 
165
     *
 
166
     * @return  {@code true} if the file descriptor object represents a
 
167
     *          valid, open file, socket, or other active I/O connection;
 
168
     *          {@code false} otherwise.
 
169
     */
 
170
    public boolean valid() {
 
171
        return stream != null || socket != null;
 
172
    }
 
173
 
 
174
    /**
 
175
     * Force all system buffers to synchronize with the underlying
 
176
     * device.  This method returns after all modified data and
 
177
     * attributes of this FileDescriptor have been written to the
 
178
     * relevant device(s).  In particular, if this FileDescriptor
 
179
     * refers to a physical storage medium, such as a file in a file
 
180
     * system, sync will not return until all in-memory modified copies
 
181
     * of buffers associated with this FileDesecriptor have been
 
182
     * written to the physical medium.
 
183
     *
 
184
     * sync is meant to be used by code that requires physical
 
185
     * storage (such as a file) to be in a known state  For
 
186
     * example, a class that provided a simple transaction facility
 
187
     * might use sync to ensure that all changes to a file caused
 
188
     * by a given transaction were recorded on a storage medium.
 
189
     *
 
190
     * sync only affects buffers downstream of this FileDescriptor.  If
 
191
     * any in-memory buffering is being done by the application (for
 
192
     * example, by a BufferedOutputStream object), those buffers must
 
193
     * be flushed into the FileDescriptor (for example, by invoking
 
194
     * OutputStream.flush) before that data will be affected by sync.
 
195
     *
 
196
     * @exception SyncFailedException
 
197
     *        Thrown when the buffers cannot be flushed,
 
198
     *        or because the system cannot guarantee that all the
 
199
     *        buffers have been synchronized with physical media.
 
200
     * @since     JDK1.1
 
201
     */
 
202
    public void sync() throws SyncFailedException
 
203
    {
 
204
        if (stream == null)
 
205
        {
 
206
            throw new SyncFailedException("sync failed");
 
207
        }
 
208
 
 
209
        if (!stream.get_CanWrite())
 
210
        {
 
211
            return;
 
212
        }
 
213
 
 
214
        try
 
215
        {
 
216
            if (false) throw new cli.System.IO.IOException();
 
217
            stream.Flush();
 
218
        }
 
219
        catch (cli.System.IO.IOException x)
 
220
        {
 
221
            throw new SyncFailedException(x.getMessage());
 
222
        }
 
223
 
 
224
        if (stream instanceof FileStream)
 
225
        {
 
226
            FileStream fs = (FileStream)stream;
 
227
            boolean ok = ikvm.internal.Util.WINDOWS ? flushWin32(fs) : flushPosix(fs);
 
228
            if (!ok)
 
229
            {
 
230
                throw new SyncFailedException("sync failed");
 
231
            }
 
232
        }
 
233
    }
 
234
 
 
235
    private static native boolean flushPosix(FileStream fs);
 
236
 
 
237
    @cli.System.Security.SecuritySafeCriticalAttribute.Annotation
 
238
    private static boolean flushWin32(FileStream fs)
 
239
    {
 
240
        return FlushFileBuffers(fs.get_SafeFileHandle()) != 0;
 
241
    }
 
242
 
 
243
    @DllImportAttribute.Annotation("kernel32")
 
244
    private static native int FlushFileBuffers(cli.Microsoft.Win32.SafeHandles.SafeFileHandle handle);
 
245
 
 
246
    private static FileDescriptor standardStream(int fd) {
 
247
        FileDescriptor desc = new FileDescriptor();
 
248
        try
 
249
        {
 
250
            desc.stream = getStandardStream(fd);
 
251
        }
 
252
        catch (cli.System.MissingMethodException _)
 
253
        {
 
254
            desc.stream = cli.System.IO.Stream.Null;
 
255
        }
 
256
        return desc;
 
257
    }
 
258
 
 
259
    private static cli.System.IO.Stream getStandardStream(int fd) throws cli.System.MissingMethodException
 
260
    {
 
261
        switch (fd)
 
262
        {
 
263
            case 0:
 
264
                return cli.System.Console.OpenStandardInput();
 
265
            case 1:
 
266
                return cli.System.Console.OpenStandardOutput();
 
267
            case 2:
 
268
                return cli.System.Console.OpenStandardError();
 
269
            default:
 
270
                throw new Error();
 
271
        }
 
272
    }
 
273
 
 
274
    // package private methods used by FIS, FOS and RAF.
 
275
 
 
276
    int incrementAndGetUseCount() {
 
277
        return useCount.incrementAndGet();
 
278
    }
 
279
 
 
280
    int decrementAndGetUseCount() {
 
281
        return useCount.decrementAndGet();
 
282
    }
 
283
 
 
284
    void openReadOnly(String name) throws FileNotFoundException
 
285
    {
 
286
        open(name, FileMode.Open, FileAccess.Read);
 
287
    }
 
288
 
 
289
    void openWriteOnly(String name) throws FileNotFoundException
 
290
    {
 
291
        open(name, FileMode.Create, FileAccess.Write);
 
292
    }
 
293
 
 
294
    void openReadWrite(String name) throws FileNotFoundException
 
295
    {
 
296
        open(name, FileMode.OpenOrCreate, FileAccess.ReadWrite);
 
297
    }
 
298
 
 
299
    void openAppend(String name) throws FileNotFoundException
 
300
    {
 
301
        open(name, FileMode.Append, FileAccess.Write);
 
302
    }
 
303
 
 
304
    private static native cli.System.IO.Stream open(String name, FileMode fileMode, FileAccess fileAccess)
 
305
        throws cli.System.IO.IOException,
 
306
            cli.System.Security.SecurityException,
 
307
            cli.System.UnauthorizedAccessException,
 
308
            cli.System.ArgumentException,
 
309
            cli.System.NotSupportedException;
 
310
    
 
311
    private void open(String name, int fileMode, int fileAccess) throws FileNotFoundException
 
312
    {
 
313
        try
 
314
        {
 
315
            stream = open(name, FileMode.wrap(fileMode), FileAccess.wrap(fileAccess));
 
316
        }
 
317
        catch (cli.System.Security.SecurityException x1)
 
318
        {
 
319
            throw new SecurityException(x1.getMessage());
 
320
        }
 
321
        catch (cli.System.IO.IOException x2)
 
322
        {
 
323
            throw new FileNotFoundException(x2.getMessage());
 
324
        }
 
325
        catch (cli.System.UnauthorizedAccessException x3)
 
326
        {
 
327
            // this is caused by "name" being a directory instead of a file
 
328
            // or by name being a read-only file
 
329
            throw new FileNotFoundException(name + " (Access is denied)");
 
330
        }
 
331
        catch (cli.System.ArgumentException x4)
 
332
        {
 
333
            throw new FileNotFoundException(x4.getMessage());
 
334
        }
 
335
        catch (cli.System.NotSupportedException x5)
 
336
        {
 
337
            throw new FileNotFoundException(x5.getMessage());
 
338
        }
 
339
    }
 
340
 
 
341
    private void checkOpen() throws IOException
 
342
    {
 
343
        if (stream == null)
 
344
        {
 
345
            throw new IOException("Stream Closed");
 
346
        }
 
347
    }
 
348
 
 
349
    int read() throws IOException
 
350
    {
 
351
        checkOpen();
 
352
        try
 
353
        {
 
354
            if (false) throw new cli.System.NotSupportedException();
 
355
            if (false) throw new cli.System.IO.IOException();
 
356
            if (false) throw new cli.System.ObjectDisposedException(null);
 
357
            return stream.ReadByte();
 
358
        }
 
359
        catch (cli.System.NotSupportedException x)
 
360
        {
 
361
            throw new IOException(x.getMessage());
 
362
        }
 
363
        catch (cli.System.IO.IOException x)
 
364
        {
 
365
            throw new IOException(x.getMessage());
 
366
        }
 
367
        catch (cli.System.ObjectDisposedException x)
 
368
        {
 
369
            throw new java.nio.channels.ClosedChannelException();
 
370
        }
 
371
    }
 
372
 
 
373
    @ikvm.lang.Internal
 
374
    public int readBytes(byte buf[], int offset, int len) throws IOException
 
375
    {
 
376
        // NOTE we start by dereferencing buf, to make sure you get a NullPointerException first if you pass a null reference.
 
377
        int bufLen = buf.length;
 
378
        if ((offset < 0) || (offset > bufLen) || (len < 0) || (len > (bufLen - offset)))
 
379
        {
 
380
            throw new IndexOutOfBoundsException();
 
381
        }
 
382
 
 
383
        if (len == 0)
 
384
        {
 
385
            return 0;
 
386
        }
 
387
 
 
388
        checkOpen();
 
389
 
 
390
        try
 
391
        {
 
392
            if (false) throw new cli.System.NotSupportedException();
 
393
            if (false) throw new cli.System.IO.IOException();
 
394
            if (false) throw new cli.System.ObjectDisposedException(null);
 
395
            int count = stream.Read(buf, offset, len);
 
396
            if (count == 0)
 
397
            {
 
398
                count = -1;
 
399
            }
 
400
            return count;
 
401
        }
 
402
        catch (cli.System.NotSupportedException x)
 
403
        {
 
404
            throw new IOException(x.getMessage());
 
405
        }
 
406
        catch (cli.System.IO.IOException x)
 
407
        {
 
408
            throw new IOException(x.getMessage());
 
409
        }
 
410
        catch (cli.System.ObjectDisposedException x)
 
411
        {
 
412
            throw new java.nio.channels.ClosedChannelException();
 
413
        }
 
414
    }
 
415
 
 
416
    long skip(long n) throws IOException
 
417
    {
 
418
        checkOpen();
 
419
        if (!stream.get_CanSeek())
 
420
        {
 
421
            // in a somewhat bizar twist, for non-seekable streams the JDK throws an exception
 
422
            throw new IOException("The handle is invalid");
 
423
        }
 
424
        try
 
425
        {
 
426
            if (false) throw new cli.System.IO.IOException();
 
427
            if (false) throw new cli.System.NotSupportedException();
 
428
            if (false) throw new cli.System.ObjectDisposedException(null);
 
429
            long cur = stream.get_Position();
 
430
            long end = stream.Seek(n, SeekOrigin.wrap(SeekOrigin.Current));
 
431
            return end - cur;
 
432
        }
 
433
        catch (cli.System.IO.IOException x)
 
434
        {
 
435
            throw new IOException(x.getMessage());
 
436
        }
 
437
        catch (cli.System.NotSupportedException x1)
 
438
        {
 
439
            // this means we have a broken Stream, because if CanSeek returns true, it must
 
440
            // support Length and Position
 
441
            throw new IOException(x1);
 
442
        }
 
443
        catch (cli.System.ObjectDisposedException x)
 
444
        {
 
445
            throw new java.nio.channels.ClosedChannelException();
 
446
        }
 
447
    }
 
448
 
 
449
    @ikvm.lang.Internal
 
450
    public int available() throws IOException
 
451
    {
 
452
        checkOpen();
 
453
        try
 
454
        {
 
455
            if (false) throw new cli.System.IO.IOException();
 
456
            if (false) throw new cli.System.NotSupportedException();
 
457
            if (false) throw new cli.System.ObjectDisposedException(null);
 
458
            if (stream.get_CanSeek())
 
459
            {
 
460
                return (int)Math.min(Integer.MAX_VALUE, Math.max(0, stream.get_Length() - stream.get_Position()));
 
461
            }
 
462
            return 0;
 
463
        }
 
464
        catch (cli.System.IO.IOException x)
 
465
        {
 
466
            throw new IOException(x.getMessage());
 
467
        }
 
468
        catch (cli.System.NotSupportedException x1)
 
469
        {
 
470
            // this means we have a broken Stream, because if CanSeek returns true, it must
 
471
            // support Length and Position
 
472
            throw new IOException(x1);
 
473
        }
 
474
        catch (cli.System.ObjectDisposedException x)
 
475
        {
 
476
            throw new java.nio.channels.ClosedChannelException();
 
477
        }
 
478
    }
 
479
 
 
480
    void write(int b) throws IOException
 
481
    {
 
482
        checkOpen();
 
483
        try
 
484
        {
 
485
            if (false) throw new cli.System.NotSupportedException();
 
486
            if (false) throw new cli.System.IO.IOException();
 
487
            if (false) throw new cli.System.ObjectDisposedException(null);
 
488
            stream.WriteByte((byte)b);
 
489
            // NOTE FileStream buffers the output, so we have to flush explicitly
 
490
            stream.Flush();
 
491
        }
 
492
        catch (cli.System.NotSupportedException x)
 
493
        {
 
494
            throw new IOException(x.getMessage());
 
495
        }
 
496
        catch (cli.System.IO.IOException x)
 
497
        {
 
498
            throw new IOException(x.getMessage());
 
499
        }
 
500
        catch (cli.System.ObjectDisposedException x)
 
501
        {
 
502
            throw new java.nio.channels.ClosedChannelException();
 
503
        }
 
504
    }
 
505
 
 
506
    @ikvm.lang.Internal
 
507
    public void writeBytes(byte buf[], int offset, int len) throws IOException
 
508
    {
 
509
        // NOTE we start by dereferencing buf, to make sure you get a NullPointerException first if you pass a null reference.
 
510
        int bufLen = buf.length;
 
511
        if ((offset < 0) || (offset > bufLen) || (len < 0) || (len > (bufLen - offset)))
 
512
        {
 
513
            throw new IndexOutOfBoundsException();
 
514
        }
 
515
 
 
516
        if (len == 0)
 
517
        {
 
518
            return;
 
519
        }
 
520
 
 
521
        checkOpen();
 
522
 
 
523
        try
 
524
        {
 
525
            if (false) throw new cli.System.NotSupportedException();
 
526
            if (false) throw new cli.System.IO.IOException();
 
527
            if (false) throw new cli.System.ObjectDisposedException(null);
 
528
            stream.Write(buf, offset, len);
 
529
            // NOTE FileStream buffers the output, so we have to flush explicitly
 
530
            stream.Flush();
 
531
        }
 
532
        catch (cli.System.NotSupportedException x)
 
533
        {
 
534
            throw new IOException(x.getMessage());
 
535
        }
 
536
        catch (cli.System.IO.IOException x)
 
537
        {
 
538
            throw new IOException(x.getMessage());
 
539
        }
 
540
        catch (cli.System.ObjectDisposedException x)
 
541
        {
 
542
            throw new java.nio.channels.ClosedChannelException();
 
543
        }
 
544
    }
 
545
 
 
546
    @ikvm.lang.Internal
 
547
    public long getFilePointer() throws IOException
 
548
    {
 
549
        checkOpen();
 
550
        try
 
551
        {
 
552
            if (false) throw new cli.System.IO.IOException();
 
553
            if (false) throw new cli.System.NotSupportedException();
 
554
            if (false) throw new cli.System.ObjectDisposedException(null);
 
555
            return stream.get_Position();
 
556
        }
 
557
        catch (cli.System.IO.IOException x)
 
558
        {
 
559
            throw new IOException(x.getMessage());
 
560
        }
 
561
        catch (cli.System.NotSupportedException x1)
 
562
        {
 
563
            throw new IOException(x1);
 
564
        }
 
565
        catch (cli.System.ObjectDisposedException x)
 
566
        {
 
567
            throw new java.nio.channels.ClosedChannelException();
 
568
        }
 
569
    }
 
570
 
 
571
    @ikvm.lang.Internal
 
572
    public void seek(long newPosition) throws IOException
 
573
    {
 
574
        checkOpen();
 
575
        try
 
576
        {
 
577
            if (false) throw new cli.System.IO.IOException();
 
578
            if (false) throw new cli.System.NotSupportedException();
 
579
            if (false) throw new cli.System.ObjectDisposedException(null);
 
580
            if (false) throw new cli.System.ArgumentOutOfRangeException();
 
581
            stream.set_Position(newPosition);
 
582
        }
 
583
        catch (cli.System.IO.IOException x)
 
584
        {
 
585
            throw new IOException(x.getMessage());
 
586
        }
 
587
        catch (cli.System.NotSupportedException x1)
 
588
        {
 
589
            throw new IOException(x1);
 
590
        }
 
591
        catch (cli.System.ObjectDisposedException x)
 
592
        {
 
593
            throw new java.nio.channels.ClosedChannelException();
 
594
        }
 
595
        catch (cli.System.ArgumentOutOfRangeException _)
 
596
        {
 
597
            throw new IOException("Negative seek offset");
 
598
        }
 
599
    }
 
600
 
 
601
    @ikvm.lang.Internal
 
602
    public long length() throws IOException
 
603
    {
 
604
        checkOpen();
 
605
        try
 
606
        {
 
607
            if (false) throw new cli.System.IO.IOException();
 
608
            if (false) throw new cli.System.NotSupportedException();
 
609
            if (false) throw new cli.System.ObjectDisposedException(null);
 
610
            return stream.get_Length();
 
611
        }
 
612
        catch (cli.System.IO.IOException x)
 
613
        {
 
614
            throw new IOException(x.getMessage());
 
615
        }
 
616
        catch (cli.System.NotSupportedException x1)
 
617
        {
 
618
            throw new IOException(x1);
 
619
        }
 
620
        catch (cli.System.ObjectDisposedException x)
 
621
        {
 
622
            throw new java.nio.channels.ClosedChannelException();
 
623
        }
 
624
    }
 
625
 
 
626
    @ikvm.lang.Internal
 
627
    public void setLength(long newLength) throws IOException
 
628
    {
 
629
        checkOpen();
 
630
        try
 
631
        {
 
632
            if (false) throw new cli.System.IO.IOException();
 
633
            if (false) throw new cli.System.NotSupportedException();
 
634
            if (false) throw new cli.System.ObjectDisposedException(null);
 
635
            stream.SetLength(newLength);
 
636
        }
 
637
        catch (cli.System.IO.IOException x)
 
638
        {
 
639
            throw new IOException(x.getMessage());
 
640
        }
 
641
        catch (cli.System.NotSupportedException x1)
 
642
        {
 
643
            throw new IOException(x1);
 
644
        }
 
645
        catch (cli.System.ObjectDisposedException x)
 
646
        {
 
647
            throw new java.nio.channels.ClosedChannelException();
 
648
        }
 
649
    }
 
650
 
 
651
    @ikvm.lang.Internal
 
652
    public void close() throws IOException
 
653
    {
 
654
        cli.System.IO.Stream s = stream;
 
655
        stream = null;
 
656
        if (s != null)
 
657
        {
 
658
            s.Close();
 
659
        }
 
660
    }
 
661
 
 
662
    @ikvm.lang.Internal
 
663
    public cli.System.IO.Stream getStream()
 
664
    {
 
665
        return stream;
 
666
    }
 
667
 
 
668
    @ikvm.lang.Internal
 
669
    public static FileDescriptor fromStream(cli.System.IO.Stream stream)
 
670
    {
 
671
        FileDescriptor desc = new FileDescriptor();
 
672
        desc.stream = stream;
 
673
        return desc;
 
674
    }
 
675
 
 
676
    @ikvm.lang.Internal
 
677
    public cli.System.Net.Sockets.Socket getSocket()
 
678
    {
 
679
        return socket;
 
680
    }
 
681
 
 
682
    @ikvm.lang.Internal
 
683
    public void setSocket(cli.System.Net.Sockets.Socket socket)
 
684
    {
 
685
        this.socket = socket;
 
686
    }
 
687
 
 
688
    @ikvm.lang.Internal
 
689
    public void setSocketBlocking(boolean blocking) throws IOException
 
690
    {
 
691
        this.nonBlockingSocket = !blocking;
 
692
        try
 
693
        {
 
694
            if (false) throw new cli.System.Net.Sockets.SocketException();
 
695
            if (false) throw new cli.System.ObjectDisposedException("");
 
696
            socket.set_Blocking(blocking);
 
697
        }
 
698
        catch (cli.System.Net.Sockets.SocketException x)
 
699
        {
 
700
            if (x.get_ErrorCode() == java.net.SocketUtil.WSAEINVAL)
 
701
            {
 
702
                // Work around for winsock issue. You can't set a socket to blocking if a connection request is pending,
 
703
                // so we'll have to set the blocking again in SocketChannelImpl.checkConnect().
 
704
                return;
 
705
            }
 
706
            throw java.net.SocketUtil.convertSocketExceptionToIOException(x);
 
707
        }
 
708
        catch (cli.System.ObjectDisposedException _)
 
709
        {
 
710
            throw new java.net.SocketException("Socket is closed");
 
711
        }
 
712
    }
 
713
    
 
714
    @ikvm.lang.Internal
 
715
    public boolean isSocketBlocking()
 
716
    {
 
717
        return !nonBlockingSocket;
 
718
    }
 
719
 
 
720
    @ikvm.lang.Internal
 
721
    public cli.System.IAsyncResult getAsyncResult()
 
722
    {
 
723
        return asyncResult;
 
724
    }
 
725
 
 
726
    @ikvm.lang.Internal
 
727
    public void setAsyncResult(cli.System.IAsyncResult asyncResult)
 
728
    {
 
729
        this.asyncResult = asyncResult;
 
730
    }
 
731
}