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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
/*
 * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package java.io;

import cli.System.IO.FileAccess;
import cli.System.IO.FileMode;
import cli.System.IO.FileShare;
import cli.System.IO.FileStream;
import cli.System.IO.SeekOrigin;
import cli.System.Runtime.InteropServices.DllImportAttribute;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Instances of the file descriptor class serve as an opaque handle
 * to the underlying machine-specific structure representing an
 * open file, an open socket, or another source or sink of bytes.
 * The main practical use for a file descriptor is to create a
 * {@link FileInputStream} or {@link FileOutputStream} to contain it.
 *
 * <p>Applications should not create their own file descriptors.
 *
 * @author  Pavani Diwanji
 * @since   JDK1.0
 */
public final class FileDescriptor {

    private volatile cli.System.IO.Stream stream;
    private volatile cli.System.Net.Sockets.Socket socket;
    private volatile boolean nonBlockingSocket;
    private volatile cli.System.IAsyncResult asyncResult;

    /**
     * HACK
     *   JRuby uses reflection to get at the handle (on Windows) and fd (on non-Windows)
     *   fields to convert it into a native handle and query if it is a tty.
     */
    @ikvm.lang.Property(get = "get_handle")
    private long handle;
    
    @ikvm.lang.Property(get = "get_fd")
    private int fd;
    
    @cli.System.Security.SecurityCriticalAttribute.Annotation
    private long get_handle()
    {
        if (ikvm.internal.Util.WINDOWS)
        {
            if (stream instanceof cli.System.IO.FileStream)
            {
                cli.System.IO.FileStream fs = (cli.System.IO.FileStream)stream;
                return fs.get_Handle().ToInt64();
            }
            else if (this == in)
            {
                return GetStdHandle(-10).ToInt64();
            }
            else if (this == out)
            {
                return GetStdHandle(-11).ToInt64();
            }
            else if (this == err)
            {
                return GetStdHandle(-12).ToInt64();
            }
        }
        return -1;
    }
    
    @cli.System.Security.SecurityCriticalAttribute.Annotation
    private int get_fd()
    {
        if (!ikvm.internal.Util.WINDOWS)
        {
            if (stream instanceof cli.System.IO.FileStream)
            {
                cli.System.IO.FileStream fs = (cli.System.IO.FileStream)stream;
                return fs.get_Handle().ToInt32();
            }
            else if (this == in)
            {
                return 0;
            }
            else if (this == out)
            {
                return 1;
            }
            else if (this == err)
            {
                return 2;
            }
        }
        return -1;
    }
    
    @DllImportAttribute.Annotation("kernel32")
    private static native cli.System.IntPtr GetStdHandle(int nStdHandle);

    /**
     * A use counter for tracking the FIS/FOS/RAF instances that
     * use this FileDescriptor. The FIS/FOS.finalize() will not release
     * the FileDescriptor if it is still under use by any stream.
     */
    private AtomicInteger useCount;


    /**
     * Constructs an (invalid) FileDescriptor
     * object.
     */
    public /**/ FileDescriptor() {
        useCount = new AtomicInteger();
    }

    /**
     * A handle to the standard input stream. Usually, this file
     * descriptor is not used directly, but rather via the input stream
     * known as {@code System.in}.
     *
     * @see     java.lang.System#in
     */
    public static final FileDescriptor in = standardStream(0);

    /**
     * A handle to the standard output stream. Usually, this file
     * descriptor is not used directly, but rather via the output stream
     * known as {@code System.out}.
     * @see     java.lang.System#out
     */
    public static final FileDescriptor out = standardStream(1);

    /**
     * A handle to the standard error stream. Usually, this file
     * descriptor is not used directly, but rather via the output stream
     * known as {@code System.err}.
     *
     * @see     java.lang.System#err
     */
    public static final FileDescriptor err = standardStream(2);

    /**
     * Tests if this file descriptor object is valid.
     *
     * @return  {@code true} if the file descriptor object represents a
     *          valid, open file, socket, or other active I/O connection;
     *          {@code false} otherwise.
     */
    public boolean valid() {
        return stream != null || socket != null;
    }

    /**
     * Force all system buffers to synchronize with the underlying
     * device.  This method returns after all modified data and
     * attributes of this FileDescriptor have been written to the
     * relevant device(s).  In particular, if this FileDescriptor
     * refers to a physical storage medium, such as a file in a file
     * system, sync will not return until all in-memory modified copies
     * of buffers associated with this FileDesecriptor have been
     * written to the physical medium.
     *
     * sync is meant to be used by code that requires physical
     * storage (such as a file) to be in a known state  For
     * example, a class that provided a simple transaction facility
     * might use sync to ensure that all changes to a file caused
     * by a given transaction were recorded on a storage medium.
     *
     * sync only affects buffers downstream of this FileDescriptor.  If
     * any in-memory buffering is being done by the application (for
     * example, by a BufferedOutputStream object), those buffers must
     * be flushed into the FileDescriptor (for example, by invoking
     * OutputStream.flush) before that data will be affected by sync.
     *
     * @exception SyncFailedException
     *        Thrown when the buffers cannot be flushed,
     *        or because the system cannot guarantee that all the
     *        buffers have been synchronized with physical media.
     * @since     JDK1.1
     */
    public void sync() throws SyncFailedException
    {
        if (stream == null)
        {
            throw new SyncFailedException("sync failed");
        }

        if (!stream.get_CanWrite())
        {
            return;
        }

        try
        {
            if (false) throw new cli.System.IO.IOException();
            stream.Flush();
        }
        catch (cli.System.IO.IOException x)
        {
            throw new SyncFailedException(x.getMessage());
        }

        if (stream instanceof FileStream)
        {
            FileStream fs = (FileStream)stream;
            boolean ok = ikvm.internal.Util.WINDOWS ? flushWin32(fs) : flushPosix(fs);
            if (!ok)
            {
                throw new SyncFailedException("sync failed");
            }
        }
    }

    private static native boolean flushPosix(FileStream fs);

    @cli.System.Security.SecuritySafeCriticalAttribute.Annotation
    private static boolean flushWin32(FileStream fs)
    {
        return FlushFileBuffers(fs.get_SafeFileHandle()) != 0;
    }

    @DllImportAttribute.Annotation("kernel32")
    private static native int FlushFileBuffers(cli.Microsoft.Win32.SafeHandles.SafeFileHandle handle);

    private static FileDescriptor standardStream(int fd) {
        FileDescriptor desc = new FileDescriptor();
        try
        {
            desc.stream = getStandardStream(fd);
        }
        catch (cli.System.MissingMethodException _)
        {
            desc.stream = cli.System.IO.Stream.Null;
        }
        return desc;
    }

    private static cli.System.IO.Stream getStandardStream(int fd) throws cli.System.MissingMethodException
    {
        switch (fd)
        {
            case 0:
                return cli.System.Console.OpenStandardInput();
            case 1:
                return cli.System.Console.OpenStandardOutput();
            case 2:
                return cli.System.Console.OpenStandardError();
            default:
                throw new Error();
        }
    }

    // package private methods used by FIS, FOS and RAF.

    int incrementAndGetUseCount() {
        return useCount.incrementAndGet();
    }

    int decrementAndGetUseCount() {
        return useCount.decrementAndGet();
    }

    void openReadOnly(String name) throws FileNotFoundException
    {
        open(name, FileMode.Open, FileAccess.Read);
    }

    void openWriteOnly(String name) throws FileNotFoundException
    {
        open(name, FileMode.Create, FileAccess.Write);
    }

    void openReadWrite(String name) throws FileNotFoundException
    {
        open(name, FileMode.OpenOrCreate, FileAccess.ReadWrite);
    }

    void openAppend(String name) throws FileNotFoundException
    {
        open(name, FileMode.Append, FileAccess.Write);
    }

    private static native cli.System.IO.Stream open(String name, FileMode fileMode, FileAccess fileAccess)
        throws cli.System.IO.IOException,
            cli.System.Security.SecurityException,
            cli.System.UnauthorizedAccessException,
            cli.System.ArgumentException,
            cli.System.NotSupportedException;
    
    private void open(String name, int fileMode, int fileAccess) throws FileNotFoundException
    {
        try
        {
            stream = open(name, FileMode.wrap(fileMode), FileAccess.wrap(fileAccess));
        }
        catch (cli.System.Security.SecurityException x1)
        {
            throw new SecurityException(x1.getMessage());
        }
        catch (cli.System.IO.IOException x2)
        {
            throw new FileNotFoundException(x2.getMessage());
        }
        catch (cli.System.UnauthorizedAccessException x3)
        {
            // this is caused by "name" being a directory instead of a file
            // or by name being a read-only file
            throw new FileNotFoundException(name + " (Access is denied)");
        }
        catch (cli.System.ArgumentException x4)
        {
            throw new FileNotFoundException(x4.getMessage());
        }
        catch (cli.System.NotSupportedException x5)
        {
            throw new FileNotFoundException(x5.getMessage());
        }
    }

    private void checkOpen() throws IOException
    {
        if (stream == null)
        {
            throw new IOException("Stream Closed");
        }
    }

    int read() throws IOException
    {
        checkOpen();
        try
        {
            if (false) throw new cli.System.NotSupportedException();
            if (false) throw new cli.System.IO.IOException();
            if (false) throw new cli.System.ObjectDisposedException(null);
            return stream.ReadByte();
        }
        catch (cli.System.NotSupportedException x)
        {
            throw new IOException(x.getMessage());
        }
        catch (cli.System.IO.IOException x)
        {
            throw new IOException(x.getMessage());
        }
        catch (cli.System.ObjectDisposedException x)
        {
            throw new java.nio.channels.ClosedChannelException();
        }
    }

    @ikvm.lang.Internal
    public int readBytes(byte buf[], int offset, int len) throws IOException
    {
        // NOTE we start by dereferencing buf, to make sure you get a NullPointerException first if you pass a null reference.
        int bufLen = buf.length;
        if ((offset < 0) || (offset > bufLen) || (len < 0) || (len > (bufLen - offset)))
        {
            throw new IndexOutOfBoundsException();
        }

        if (len == 0)
        {
            return 0;
        }

        checkOpen();

        try
        {
            if (false) throw new cli.System.NotSupportedException();
            if (false) throw new cli.System.IO.IOException();
            if (false) throw new cli.System.ObjectDisposedException(null);
            int count = stream.Read(buf, offset, len);
            if (count == 0)
            {
                count = -1;
            }
            return count;
        }
        catch (cli.System.NotSupportedException x)
        {
            throw new IOException(x.getMessage());
        }
        catch (cli.System.IO.IOException x)
        {
            throw new IOException(x.getMessage());
        }
        catch (cli.System.ObjectDisposedException x)
        {
            throw new java.nio.channels.ClosedChannelException();
        }
    }

    long skip(long n) throws IOException
    {
        checkOpen();
        if (!stream.get_CanSeek())
        {
            // in a somewhat bizar twist, for non-seekable streams the JDK throws an exception
            throw new IOException("The handle is invalid");
        }
        try
        {
            if (false) throw new cli.System.IO.IOException();
            if (false) throw new cli.System.NotSupportedException();
            if (false) throw new cli.System.ObjectDisposedException(null);
            long cur = stream.get_Position();
            long end = stream.Seek(n, SeekOrigin.wrap(SeekOrigin.Current));
            return end - cur;
        }
        catch (cli.System.IO.IOException x)
        {
            throw new IOException(x.getMessage());
        }
        catch (cli.System.NotSupportedException x1)
        {
            // this means we have a broken Stream, because if CanSeek returns true, it must
            // support Length and Position
            throw new IOException(x1);
        }
        catch (cli.System.ObjectDisposedException x)
        {
            throw new java.nio.channels.ClosedChannelException();
        }
    }

    @ikvm.lang.Internal
    public int available() throws IOException
    {
        checkOpen();
        try
        {
            if (false) throw new cli.System.IO.IOException();
            if (false) throw new cli.System.NotSupportedException();
            if (false) throw new cli.System.ObjectDisposedException(null);
            if (stream.get_CanSeek())
            {
                return (int)Math.min(Integer.MAX_VALUE, Math.max(0, stream.get_Length() - stream.get_Position()));
            }
            return 0;
        }
        catch (cli.System.IO.IOException x)
        {
            throw new IOException(x.getMessage());
        }
        catch (cli.System.NotSupportedException x1)
        {
            // this means we have a broken Stream, because if CanSeek returns true, it must
            // support Length and Position
            throw new IOException(x1);
        }
        catch (cli.System.ObjectDisposedException x)
        {
            throw new java.nio.channels.ClosedChannelException();
        }
    }

    void write(int b) throws IOException
    {
        checkOpen();
        try
        {
            if (false) throw new cli.System.NotSupportedException();
            if (false) throw new cli.System.IO.IOException();
            if (false) throw new cli.System.ObjectDisposedException(null);
            stream.WriteByte((byte)b);
            // NOTE FileStream buffers the output, so we have to flush explicitly
            stream.Flush();
        }
        catch (cli.System.NotSupportedException x)
        {
            throw new IOException(x.getMessage());
        }
        catch (cli.System.IO.IOException x)
        {
            throw new IOException(x.getMessage());
        }
        catch (cli.System.ObjectDisposedException x)
        {
            throw new java.nio.channels.ClosedChannelException();
        }
    }

    @ikvm.lang.Internal
    public void writeBytes(byte buf[], int offset, int len) throws IOException
    {
        // NOTE we start by dereferencing buf, to make sure you get a NullPointerException first if you pass a null reference.
        int bufLen = buf.length;
        if ((offset < 0) || (offset > bufLen) || (len < 0) || (len > (bufLen - offset)))
        {
            throw new IndexOutOfBoundsException();
        }

        if (len == 0)
        {
            return;
        }

        checkOpen();

        try
        {
            if (false) throw new cli.System.NotSupportedException();
            if (false) throw new cli.System.IO.IOException();
            if (false) throw new cli.System.ObjectDisposedException(null);
            stream.Write(buf, offset, len);
            // NOTE FileStream buffers the output, so we have to flush explicitly
            stream.Flush();
        }
        catch (cli.System.NotSupportedException x)
        {
            throw new IOException(x.getMessage());
        }
        catch (cli.System.IO.IOException x)
        {
            throw new IOException(x.getMessage());
        }
        catch (cli.System.ObjectDisposedException x)
        {
            throw new java.nio.channels.ClosedChannelException();
        }
    }

    @ikvm.lang.Internal
    public long getFilePointer() throws IOException
    {
        checkOpen();
        try
        {
            if (false) throw new cli.System.IO.IOException();
            if (false) throw new cli.System.NotSupportedException();
            if (false) throw new cli.System.ObjectDisposedException(null);
            return stream.get_Position();
        }
        catch (cli.System.IO.IOException x)
        {
            throw new IOException(x.getMessage());
        }
        catch (cli.System.NotSupportedException x1)
        {
            throw new IOException(x1);
        }
        catch (cli.System.ObjectDisposedException x)
        {
            throw new java.nio.channels.ClosedChannelException();
        }
    }

    @ikvm.lang.Internal
    public void seek(long newPosition) throws IOException
    {
        checkOpen();
        try
        {
            if (false) throw new cli.System.IO.IOException();
            if (false) throw new cli.System.NotSupportedException();
            if (false) throw new cli.System.ObjectDisposedException(null);
            if (false) throw new cli.System.ArgumentOutOfRangeException();
            stream.set_Position(newPosition);
        }
        catch (cli.System.IO.IOException x)
        {
            throw new IOException(x.getMessage());
        }
        catch (cli.System.NotSupportedException x1)
        {
            throw new IOException(x1);
        }
        catch (cli.System.ObjectDisposedException x)
        {
            throw new java.nio.channels.ClosedChannelException();
        }
        catch (cli.System.ArgumentOutOfRangeException _)
        {
            throw new IOException("Negative seek offset");
        }
    }

    @ikvm.lang.Internal
    public long length() throws IOException
    {
        checkOpen();
        try
        {
            if (false) throw new cli.System.IO.IOException();
            if (false) throw new cli.System.NotSupportedException();
            if (false) throw new cli.System.ObjectDisposedException(null);
            return stream.get_Length();
        }
        catch (cli.System.IO.IOException x)
        {
            throw new IOException(x.getMessage());
        }
        catch (cli.System.NotSupportedException x1)
        {
            throw new IOException(x1);
        }
        catch (cli.System.ObjectDisposedException x)
        {
            throw new java.nio.channels.ClosedChannelException();
        }
    }

    @ikvm.lang.Internal
    public void setLength(long newLength) throws IOException
    {
        checkOpen();
        try
        {
            if (false) throw new cli.System.IO.IOException();
            if (false) throw new cli.System.NotSupportedException();
            if (false) throw new cli.System.ObjectDisposedException(null);
            stream.SetLength(newLength);
        }
        catch (cli.System.IO.IOException x)
        {
            throw new IOException(x.getMessage());
        }
        catch (cli.System.NotSupportedException x1)
        {
            throw new IOException(x1);
        }
        catch (cli.System.ObjectDisposedException x)
        {
            throw new java.nio.channels.ClosedChannelException();
        }
    }

    @ikvm.lang.Internal
    public void close() throws IOException
    {
        cli.System.IO.Stream s = stream;
        stream = null;
        if (s != null)
        {
            s.Close();
        }
    }

    @ikvm.lang.Internal
    public cli.System.IO.Stream getStream()
    {
        return stream;
    }

    @ikvm.lang.Internal
    public static FileDescriptor fromStream(cli.System.IO.Stream stream)
    {
        FileDescriptor desc = new FileDescriptor();
        desc.stream = stream;
        return desc;
    }

    @ikvm.lang.Internal
    public cli.System.Net.Sockets.Socket getSocket()
    {
        return socket;
    }

    @ikvm.lang.Internal
    public void setSocket(cli.System.Net.Sockets.Socket socket)
    {
        this.socket = socket;
    }

    @ikvm.lang.Internal
    public void setSocketBlocking(boolean blocking) throws IOException
    {
        this.nonBlockingSocket = !blocking;
        try
        {
            if (false) throw new cli.System.Net.Sockets.SocketException();
            if (false) throw new cli.System.ObjectDisposedException("");
            socket.set_Blocking(blocking);
        }
        catch (cli.System.Net.Sockets.SocketException x)
        {
            if (x.get_ErrorCode() == java.net.SocketUtil.WSAEINVAL)
            {
                // Work around for winsock issue. You can't set a socket to blocking if a connection request is pending,
                // so we'll have to set the blocking again in SocketChannelImpl.checkConnect().
                return;
            }
            throw java.net.SocketUtil.convertSocketExceptionToIOException(x);
        }
        catch (cli.System.ObjectDisposedException _)
        {
            throw new java.net.SocketException("Socket is closed");
        }
    }
    
    @ikvm.lang.Internal
    public boolean isSocketBlocking()
    {
        return !nonBlockingSocket;
    }

    @ikvm.lang.Internal
    public cli.System.IAsyncResult getAsyncResult()
    {
        return asyncResult;
    }

    @ikvm.lang.Internal
    public void setAsyncResult(cli.System.IAsyncResult asyncResult)
    {
        this.asyncResult = asyncResult;
    }
}