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