rt/emul/compact/src/main/java/java/io/FileInputStream.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 03 Oct 2013 17:36:44 +0200
changeset 1337 c794024954b5
parent 1334 588d5bf7a560
permissions -rw-r--r--
Implementation of few more JDK classes
jtulach@1334
     1
/*
jtulach@1334
     2
 * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved.
jtulach@1334
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jtulach@1334
     4
 *
jtulach@1334
     5
 * This code is free software; you can redistribute it and/or modify it
jtulach@1334
     6
 * under the terms of the GNU General Public License version 2 only, as
jtulach@1334
     7
 * published by the Free Software Foundation.  Oracle designates this
jtulach@1334
     8
 * particular file as subject to the "Classpath" exception as provided
jtulach@1334
     9
 * by Oracle in the LICENSE file that accompanied this code.
jtulach@1334
    10
 *
jtulach@1334
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jtulach@1334
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jtulach@1334
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jtulach@1334
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jtulach@1334
    15
 * accompanied this code).
jtulach@1334
    16
 *
jtulach@1334
    17
 * You should have received a copy of the GNU General Public License version
jtulach@1334
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jtulach@1334
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jtulach@1334
    20
 *
jtulach@1334
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jtulach@1334
    22
 * or visit www.oracle.com if you need additional information or have any
jtulach@1334
    23
 * questions.
jtulach@1334
    24
 */
jtulach@1334
    25
jtulach@1334
    26
package java.io;
jtulach@1334
    27
jtulach@1334
    28
jtulach@1334
    29
jtulach@1334
    30
/**
jtulach@1334
    31
 * A <code>FileInputStream</code> obtains input bytes
jtulach@1334
    32
 * from a file in a file system. What files
jtulach@1334
    33
 * are  available depends on the host environment.
jtulach@1334
    34
 *
jtulach@1334
    35
 * <p><code>FileInputStream</code> is meant for reading streams of raw bytes
jtulach@1334
    36
 * such as image data. For reading streams of characters, consider using
jtulach@1334
    37
 * <code>FileReader</code>.
jtulach@1334
    38
 *
jtulach@1334
    39
 * @author  Arthur van Hoff
jtulach@1334
    40
 * @see     java.io.File
jtulach@1334
    41
 * @see     java.io.FileDescriptor
jtulach@1334
    42
 * @see     java.io.FileOutputStream
jtulach@1334
    43
 * @see     java.nio.file.Files#newInputStream
jtulach@1334
    44
 * @since   JDK1.0
jtulach@1334
    45
 */
jtulach@1334
    46
public
jtulach@1334
    47
class FileInputStream extends InputStream
jtulach@1334
    48
{
jtulach@1334
    49
    /* File Descriptor - handle to the open file */
jtulach@1334
    50
    private final FileDescriptor fd;
jtulach@1334
    51
jaroslav@1337
    52
//    private FileChannel channel = null;
jtulach@1334
    53
jtulach@1334
    54
    private final Object closeLock = new Object();
jtulach@1334
    55
    private volatile boolean closed = false;
jtulach@1334
    56
jtulach@1334
    57
    private static final ThreadLocal<Boolean> runningFinalize =
jtulach@1334
    58
        new ThreadLocal<>();
jtulach@1334
    59
jtulach@1334
    60
    private static boolean isRunningFinalize() {
jtulach@1334
    61
        Boolean val;
jtulach@1334
    62
        if ((val = runningFinalize.get()) != null)
jtulach@1334
    63
            return val.booleanValue();
jtulach@1334
    64
        return false;
jtulach@1334
    65
    }
jtulach@1334
    66
jtulach@1334
    67
    /**
jtulach@1334
    68
     * Creates a <code>FileInputStream</code> by
jtulach@1334
    69
     * opening a connection to an actual file,
jtulach@1334
    70
     * the file named by the path name <code>name</code>
jtulach@1334
    71
     * in the file system.  A new <code>FileDescriptor</code>
jtulach@1334
    72
     * object is created to represent this file
jtulach@1334
    73
     * connection.
jtulach@1334
    74
     * <p>
jtulach@1334
    75
     * First, if there is a security
jtulach@1334
    76
     * manager, its <code>checkRead</code> method
jtulach@1334
    77
     * is called with the <code>name</code> argument
jtulach@1334
    78
     * as its argument.
jtulach@1334
    79
     * <p>
jtulach@1334
    80
     * If the named file does not exist, is a directory rather than a regular
jtulach@1334
    81
     * file, or for some other reason cannot be opened for reading then a
jtulach@1334
    82
     * <code>FileNotFoundException</code> is thrown.
jtulach@1334
    83
     *
jtulach@1334
    84
     * @param      name   the system-dependent file name.
jtulach@1334
    85
     * @exception  FileNotFoundException  if the file does not exist,
jtulach@1334
    86
     *                   is a directory rather than a regular file,
jtulach@1334
    87
     *                   or for some other reason cannot be opened for
jtulach@1334
    88
     *                   reading.
jtulach@1334
    89
     * @exception  SecurityException      if a security manager exists and its
jtulach@1334
    90
     *               <code>checkRead</code> method denies read access
jtulach@1334
    91
     *               to the file.
jtulach@1334
    92
     * @see        java.lang.SecurityManager#checkRead(java.lang.String)
jtulach@1334
    93
     */
jtulach@1334
    94
    public FileInputStream(String name) throws FileNotFoundException {
jtulach@1334
    95
        this(name != null ? new File(name) : null);
jtulach@1334
    96
    }
jtulach@1334
    97
jtulach@1334
    98
    /**
jtulach@1334
    99
     * Creates a <code>FileInputStream</code> by
jtulach@1334
   100
     * opening a connection to an actual file,
jtulach@1334
   101
     * the file named by the <code>File</code>
jtulach@1334
   102
     * object <code>file</code> in the file system.
jtulach@1334
   103
     * A new <code>FileDescriptor</code> object
jtulach@1334
   104
     * is created to represent this file connection.
jtulach@1334
   105
     * <p>
jtulach@1334
   106
     * First, if there is a security manager,
jtulach@1334
   107
     * its <code>checkRead</code> method  is called
jtulach@1334
   108
     * with the path represented by the <code>file</code>
jtulach@1334
   109
     * argument as its argument.
jtulach@1334
   110
     * <p>
jtulach@1334
   111
     * If the named file does not exist, is a directory rather than a regular
jtulach@1334
   112
     * file, or for some other reason cannot be opened for reading then a
jtulach@1334
   113
     * <code>FileNotFoundException</code> is thrown.
jtulach@1334
   114
     *
jtulach@1334
   115
     * @param      file   the file to be opened for reading.
jtulach@1334
   116
     * @exception  FileNotFoundException  if the file does not exist,
jtulach@1334
   117
     *                   is a directory rather than a regular file,
jtulach@1334
   118
     *                   or for some other reason cannot be opened for
jtulach@1334
   119
     *                   reading.
jtulach@1334
   120
     * @exception  SecurityException      if a security manager exists and its
jtulach@1334
   121
     *               <code>checkRead</code> method denies read access to the file.
jtulach@1334
   122
     * @see        java.io.File#getPath()
jtulach@1334
   123
     * @see        java.lang.SecurityManager#checkRead(java.lang.String)
jtulach@1334
   124
     */
jtulach@1334
   125
    public FileInputStream(File file) throws FileNotFoundException {
jaroslav@1337
   126
        throw new SecurityException();
jtulach@1334
   127
    }
jtulach@1334
   128
jtulach@1334
   129
    /**
jtulach@1334
   130
     * Creates a <code>FileInputStream</code> by using the file descriptor
jtulach@1334
   131
     * <code>fdObj</code>, which represents an existing connection to an
jtulach@1334
   132
     * actual file in the file system.
jtulach@1334
   133
     * <p>
jtulach@1334
   134
     * If there is a security manager, its <code>checkRead</code> method is
jtulach@1334
   135
     * called with the file descriptor <code>fdObj</code> as its argument to
jtulach@1334
   136
     * see if it's ok to read the file descriptor. If read access is denied
jtulach@1334
   137
     * to the file descriptor a <code>SecurityException</code> is thrown.
jtulach@1334
   138
     * <p>
jtulach@1334
   139
     * If <code>fdObj</code> is null then a <code>NullPointerException</code>
jtulach@1334
   140
     * is thrown.
jtulach@1334
   141
     * <p>
jtulach@1334
   142
     * This constructor does not throw an exception if <code>fdObj</code>
jtulach@1334
   143
     * is {@link java.io.FileDescriptor#valid() invalid}.
jtulach@1334
   144
     * However, if the methods are invoked on the resulting stream to attempt
jtulach@1334
   145
     * I/O on the stream, an <code>IOException</code> is thrown.
jtulach@1334
   146
     *
jtulach@1334
   147
     * @param      fdObj   the file descriptor to be opened for reading.
jtulach@1334
   148
     * @throws     SecurityException      if a security manager exists and its
jtulach@1334
   149
     *                 <code>checkRead</code> method denies read access to the
jtulach@1334
   150
     *                 file descriptor.
jtulach@1334
   151
     * @see        SecurityManager#checkRead(java.io.FileDescriptor)
jtulach@1334
   152
     */
jtulach@1334
   153
    public FileInputStream(FileDescriptor fdObj) {
jaroslav@1337
   154
        throw new SecurityException();
jtulach@1334
   155
    }
jtulach@1334
   156
jtulach@1334
   157
    /**
jtulach@1334
   158
     * Opens the specified file for reading.
jtulach@1334
   159
     * @param name the name of the file
jtulach@1334
   160
     */
jtulach@1334
   161
    private native void open(String name) throws FileNotFoundException;
jtulach@1334
   162
jtulach@1334
   163
    /**
jtulach@1334
   164
     * Reads a byte of data from this input stream. This method blocks
jtulach@1334
   165
     * if no input is yet available.
jtulach@1334
   166
     *
jtulach@1334
   167
     * @return     the next byte of data, or <code>-1</code> if the end of the
jtulach@1334
   168
     *             file is reached.
jtulach@1334
   169
     * @exception  IOException  if an I/O error occurs.
jtulach@1334
   170
     */
jtulach@1334
   171
    public native int read() throws IOException;
jtulach@1334
   172
jtulach@1334
   173
    /**
jtulach@1334
   174
     * Reads a subarray as a sequence of bytes.
jtulach@1334
   175
     * @param b the data to be written
jtulach@1334
   176
     * @param off the start offset in the data
jtulach@1334
   177
     * @param len the number of bytes that are written
jtulach@1334
   178
     * @exception IOException If an I/O error has occurred.
jtulach@1334
   179
     */
jtulach@1334
   180
    private native int readBytes(byte b[], int off, int len) throws IOException;
jtulach@1334
   181
jtulach@1334
   182
    /**
jtulach@1334
   183
     * Reads up to <code>b.length</code> bytes of data from this input
jtulach@1334
   184
     * stream into an array of bytes. This method blocks until some input
jtulach@1334
   185
     * is available.
jtulach@1334
   186
     *
jtulach@1334
   187
     * @param      b   the buffer into which the data is read.
jtulach@1334
   188
     * @return     the total number of bytes read into the buffer, or
jtulach@1334
   189
     *             <code>-1</code> if there is no more data because the end of
jtulach@1334
   190
     *             the file has been reached.
jtulach@1334
   191
     * @exception  IOException  if an I/O error occurs.
jtulach@1334
   192
     */
jtulach@1334
   193
    public int read(byte b[]) throws IOException {
jtulach@1334
   194
        return readBytes(b, 0, b.length);
jtulach@1334
   195
    }
jtulach@1334
   196
jtulach@1334
   197
    /**
jtulach@1334
   198
     * Reads up to <code>len</code> bytes of data from this input stream
jtulach@1334
   199
     * into an array of bytes. If <code>len</code> is not zero, the method
jtulach@1334
   200
     * blocks until some input is available; otherwise, no
jtulach@1334
   201
     * bytes are read and <code>0</code> is returned.
jtulach@1334
   202
     *
jtulach@1334
   203
     * @param      b     the buffer into which the data is read.
jtulach@1334
   204
     * @param      off   the start offset in the destination array <code>b</code>
jtulach@1334
   205
     * @param      len   the maximum number of bytes read.
jtulach@1334
   206
     * @return     the total number of bytes read into the buffer, or
jtulach@1334
   207
     *             <code>-1</code> if there is no more data because the end of
jtulach@1334
   208
     *             the file has been reached.
jtulach@1334
   209
     * @exception  NullPointerException If <code>b</code> is <code>null</code>.
jtulach@1334
   210
     * @exception  IndexOutOfBoundsException If <code>off</code> is negative,
jtulach@1334
   211
     * <code>len</code> is negative, or <code>len</code> is greater than
jtulach@1334
   212
     * <code>b.length - off</code>
jtulach@1334
   213
     * @exception  IOException  if an I/O error occurs.
jtulach@1334
   214
     */
jtulach@1334
   215
    public int read(byte b[], int off, int len) throws IOException {
jtulach@1334
   216
        return readBytes(b, off, len);
jtulach@1334
   217
    }
jtulach@1334
   218
jtulach@1334
   219
    /**
jtulach@1334
   220
     * Skips over and discards <code>n</code> bytes of data from the
jtulach@1334
   221
     * input stream.
jtulach@1334
   222
     *
jtulach@1334
   223
     * <p>The <code>skip</code> method may, for a variety of
jtulach@1334
   224
     * reasons, end up skipping over some smaller number of bytes,
jtulach@1334
   225
     * possibly <code>0</code>. If <code>n</code> is negative, an
jtulach@1334
   226
     * <code>IOException</code> is thrown, even though the <code>skip</code>
jtulach@1334
   227
     * method of the {@link InputStream} superclass does nothing in this case.
jtulach@1334
   228
     * The actual number of bytes skipped is returned.
jtulach@1334
   229
     *
jtulach@1334
   230
     * <p>This method may skip more bytes than are remaining in the backing
jtulach@1334
   231
     * file. This produces no exception and the number of bytes skipped
jtulach@1334
   232
     * may include some number of bytes that were beyond the EOF of the
jtulach@1334
   233
     * backing file. Attempting to read from the stream after skipping past
jtulach@1334
   234
     * the end will result in -1 indicating the end of the file.
jtulach@1334
   235
     *
jtulach@1334
   236
     * @param      n   the number of bytes to be skipped.
jtulach@1334
   237
     * @return     the actual number of bytes skipped.
jtulach@1334
   238
     * @exception  IOException  if n is negative, if the stream does not
jtulach@1334
   239
     *             support seek, or if an I/O error occurs.
jtulach@1334
   240
     */
jtulach@1334
   241
    public native long skip(long n) throws IOException;
jtulach@1334
   242
jtulach@1334
   243
    /**
jtulach@1334
   244
     * Returns an estimate of the number of remaining bytes that can be read (or
jtulach@1334
   245
     * skipped over) from this input stream without blocking by the next
jtulach@1334
   246
     * invocation of a method for this input stream. The next invocation might be
jtulach@1334
   247
     * the same thread or another thread.  A single read or skip of this
jtulach@1334
   248
     * many bytes will not block, but may read or skip fewer bytes.
jtulach@1334
   249
     *
jtulach@1334
   250
     * <p> In some cases, a non-blocking read (or skip) may appear to be
jtulach@1334
   251
     * blocked when it is merely slow, for example when reading large
jtulach@1334
   252
     * files over slow networks.
jtulach@1334
   253
     *
jtulach@1334
   254
     * @return     an estimate of the number of remaining bytes that can be read
jtulach@1334
   255
     *             (or skipped over) from this input stream without blocking.
jtulach@1334
   256
     * @exception  IOException  if this file input stream has been closed by calling
jtulach@1334
   257
     *             {@code close} or an I/O error occurs.
jtulach@1334
   258
     */
jtulach@1334
   259
    public native int available() throws IOException;
jtulach@1334
   260
jtulach@1334
   261
    /**
jtulach@1334
   262
     * Closes this file input stream and releases any system resources
jtulach@1334
   263
     * associated with the stream.
jtulach@1334
   264
     *
jtulach@1334
   265
     * <p> If this stream has an associated channel then the channel is closed
jtulach@1334
   266
     * as well.
jtulach@1334
   267
     *
jtulach@1334
   268
     * @exception  IOException  if an I/O error occurs.
jtulach@1334
   269
     *
jtulach@1334
   270
     * @revised 1.4
jtulach@1334
   271
     * @spec JSR-51
jtulach@1334
   272
     */
jtulach@1334
   273
    public void close() throws IOException {
jtulach@1334
   274
        synchronized (closeLock) {
jtulach@1334
   275
            if (closed) {
jtulach@1334
   276
                return;
jtulach@1334
   277
            }
jtulach@1334
   278
            closed = true;
jtulach@1334
   279
        }
jaroslav@1337
   280
//        if (channel != null) {
jaroslav@1337
   281
//            /*
jaroslav@1337
   282
//             * Decrement the FD use count associated with the channel
jaroslav@1337
   283
//             * The use count is incremented whenever a new channel
jaroslav@1337
   284
//             * is obtained from this stream.
jaroslav@1337
   285
//             */
jaroslav@1337
   286
//           fd.decrementAndGetUseCount();
jaroslav@1337
   287
//           channel.close();
jaroslav@1337
   288
//        }
jtulach@1334
   289
jtulach@1334
   290
        /*
jtulach@1334
   291
         * Decrement the FD use count associated with this stream
jtulach@1334
   292
         */
jtulach@1334
   293
        int useCount = fd.decrementAndGetUseCount();
jtulach@1334
   294
jtulach@1334
   295
        /*
jtulach@1334
   296
         * If FileDescriptor is still in use by another stream, the finalizer
jtulach@1334
   297
         * will not close it.
jtulach@1334
   298
         */
jtulach@1334
   299
        if ((useCount <= 0) || !isRunningFinalize()) {
jtulach@1334
   300
            close0();
jtulach@1334
   301
        }
jtulach@1334
   302
    }
jtulach@1334
   303
jtulach@1334
   304
    /**
jtulach@1334
   305
     * Returns the <code>FileDescriptor</code>
jtulach@1334
   306
     * object  that represents the connection to
jtulach@1334
   307
     * the actual file in the file system being
jtulach@1334
   308
     * used by this <code>FileInputStream</code>.
jtulach@1334
   309
     *
jtulach@1334
   310
     * @return     the file descriptor object associated with this stream.
jtulach@1334
   311
     * @exception  IOException  if an I/O error occurs.
jtulach@1334
   312
     * @see        java.io.FileDescriptor
jtulach@1334
   313
     */
jtulach@1334
   314
    public final FileDescriptor getFD() throws IOException {
jtulach@1334
   315
        if (fd != null) return fd;
jtulach@1334
   316
        throw new IOException();
jtulach@1334
   317
    }
jtulach@1334
   318
jtulach@1334
   319
    /**
jtulach@1334
   320
     * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
jtulach@1334
   321
     * object associated with this file input stream.
jtulach@1334
   322
     *
jtulach@1334
   323
     * <p> The initial {@link java.nio.channels.FileChannel#position()
jtulach@1334
   324
     * </code>position<code>} of the returned channel will be equal to the
jtulach@1334
   325
     * number of bytes read from the file so far.  Reading bytes from this
jtulach@1334
   326
     * stream will increment the channel's position.  Changing the channel's
jtulach@1334
   327
     * position, either explicitly or by reading, will change this stream's
jtulach@1334
   328
     * file position.
jtulach@1334
   329
     *
jtulach@1334
   330
     * @return  the file channel associated with this file input stream
jtulach@1334
   331
     *
jtulach@1334
   332
     * @since 1.4
jtulach@1334
   333
     * @spec JSR-51
jtulach@1334
   334
     */
jaroslav@1337
   335
//    public FileChannel getChannel() {
jaroslav@1337
   336
//        synchronized (this) {
jaroslav@1337
   337
//            if (channel == null) {
jaroslav@1337
   338
//                channel = FileChannelImpl.open(fd, true, false, this);
jaroslav@1337
   339
//
jaroslav@1337
   340
//                /*
jaroslav@1337
   341
//                 * Increment fd's use count. Invoking the channel's close()
jaroslav@1337
   342
//                 * method will result in decrementing the use count set for
jaroslav@1337
   343
//                 * the channel.
jaroslav@1337
   344
//                 */
jaroslav@1337
   345
//                fd.incrementAndGetUseCount();
jaroslav@1337
   346
//            }
jaroslav@1337
   347
//            return channel;
jaroslav@1337
   348
//        }
jaroslav@1337
   349
//    }
jtulach@1334
   350
jtulach@1334
   351
    private static native void initIDs();
jtulach@1334
   352
jtulach@1334
   353
    private native void close0() throws IOException;
jtulach@1334
   354
jtulach@1334
   355
    static {
jtulach@1334
   356
        initIDs();
jtulach@1334
   357
    }
jtulach@1334
   358
jtulach@1334
   359
    /**
jtulach@1334
   360
     * Ensures that the <code>close</code> method of this file input stream is
jtulach@1334
   361
     * called when there are no more references to it.
jtulach@1334
   362
     *
jtulach@1334
   363
     * @exception  IOException  if an I/O error occurs.
jtulach@1334
   364
     * @see        java.io.FileInputStream#close()
jtulach@1334
   365
     */
jtulach@1334
   366
    protected void finalize() throws IOException {
jtulach@1334
   367
        if ((fd != null) &&  (fd != FileDescriptor.in)) {
jtulach@1334
   368
jtulach@1334
   369
            /*
jtulach@1334
   370
             * Finalizer should not release the FileDescriptor if another
jtulach@1334
   371
             * stream is still using it. If the user directly invokes
jtulach@1334
   372
             * close() then the FileDescriptor is also released.
jtulach@1334
   373
             */
jtulach@1334
   374
            runningFinalize.set(Boolean.TRUE);
jtulach@1334
   375
            try {
jtulach@1334
   376
                close();
jtulach@1334
   377
            } finally {
jtulach@1334
   378
                runningFinalize.set(Boolean.FALSE);
jtulach@1334
   379
            }
jtulach@1334
   380
        }
jtulach@1334
   381
    }
jtulach@1334
   382
}