< prev index next >

src/java.base/share/classes/java/io/FileInputStream.java

Print this page

        

*** 60,69 **** --- 60,72 ---- private volatile FileChannel channel; private final AtomicBoolean closed = new AtomicBoolean(false); + private boolean direct = false; + + private int pageSize = -1; /** * Creates a <code>FileInputStream</code> by * opening a connection to an actual file, * the file named by the path name <code>name</code> * in the file system. A new <code>FileDescriptor</code>
*** 94,103 **** --- 97,141 ---- } /** * Creates a <code>FileInputStream</code> by * opening a connection to an actual file, + * the file named by the path name <code>name</code> + * in the file system. If the second parameter is + * <code>true</code>, bytes are directly read from + * storage media. A new <code>FileDescriptor</code> + * object is created to represent this file connection. + * <p> + * First, if there is a security + * manager, its <code>checkRead</code> method + * is called with the <code>name</code> argument + * as its argument. + * <p> + * If the named file does not exist, is a directory rather than a regular + * file, or for some other reason cannot be opened for reading then a + * <code>FileNotFoundException</code> is thrown. + * + * @param name the system-dependent file name. + * @param direct if <code>true</code>, then bytes will be read + * directly to storage media + * @exception FileNotFoundException if the file does not exist, + * is a directory rather than a regular file, + * or for some other reason cannot be opened for + * reading. + * @exception SecurityException if a security manager exists and its + * <code>checkRead</code> method denies read access + * to the file. + * @see java.lang.SecurityManager#checkRead(java.lang.String) + * @since 1.9 + */ + public FileInputStream(String name, boolean direct) throws FileNotFoundException { + this(name != null ? new File(name): null, direct); + } + + /** + * Creates a <code>FileInputStream</code> by + * opening a connection to an actual file, * the file named by the <code>File</code> * object <code>file</code> in the file system. * A new <code>FileDescriptor</code> object * is created to represent this file connection. * <p>
*** 119,143 **** * <code>checkRead</code> method denies read access to the file. * @see java.io.File#getPath() * @see java.lang.SecurityManager#checkRead(java.lang.String) */ public FileInputStream(File file) throws FileNotFoundException { ! String name = (file != null ? file.getPath() : null); ! SecurityManager security = System.getSecurityManager(); ! if (security != null) { ! security.checkRead(name); ! } ! if (name == null) { ! throw new NullPointerException(); ! } ! if (file.isInvalid()) { ! throw new FileNotFoundException("Invalid file path"); ! } ! fd = new FileDescriptor(); ! fd.attach(this); ! path = name; ! open(name); } /** * Creates a <code>FileInputStream</code> by using the file descriptor * <code>fdObj</code>, which represents an existing connection to an --- 157,167 ---- * <code>checkRead</code> method denies read access to the file. * @see java.io.File#getPath() * @see java.lang.SecurityManager#checkRead(java.lang.String) */ public FileInputStream(File file) throws FileNotFoundException { ! this(file, false); } /** * Creates a <code>FileInputStream</code> by using the file descriptor * <code>fdObj</code>, which represents an existing connection to an
*** 179,200 **** */ fd.attach(this); } /** * Opens the specified file for reading. * @param name the name of the file */ ! private native void open0(String name) throws FileNotFoundException; // wrap native call to allow instrumentation /** * Opens the specified file for reading. * @param name the name of the file */ private void open(String name) throws FileNotFoundException { ! open0(name); } /** * Reads a byte of data from this input stream. This method blocks * if no input is yet available. --- 203,276 ---- */ fd.attach(this); } /** + * Creates a <code>FileInputStream</code> by opening a connection to an + * actual file with O_DIRECT option, the file named by the <code> + * File</code> object <code>file </code> in the file system. A new <code> + * FileDescriptor</code> object is created to represent this file connection. + * <p> + * First, if there is a security manager, its <code>checkRead</code> + * method is called with the path represented by the <code>file</code> + * argument as its argument. + * <p> + * If the named file does not exist, is a directory rather than a regular + * file, or for some other reason cannot be opened for reading then a + * <code>FileNotFoundException</code> is thrown. + * @param file the file to be opened for reading. + * @param direct if <code>true</code>, then bytes will be read + * directly to storage media + * @exception FileNotFoundException if the file does not exist, is a + * directory rather than a regular file, or for some + * other reason cannot be opened for reading. + * @exception SecurityException if a security manager exists + */ + public FileInputStream(File file, boolean direct) throws FileNotFoundException { + this.direct = direct; + if (direct) { + getPageSize(); + } + String name = (file != null ? file.getPath() : null); + SecurityManager security = System.getSecurityManager(); + if (security != null) { + security.checkRead(name); + } + if (name == null) { + throw new NullPointerException(); + } + if (file.isInvalid()) { + throw new FileNotFoundException("Invalid file path"); + } + fd = new FileDescriptor(); + fd.attach(this); + path = name; + open(name); + } + + /** * Opens the specified file for reading. * @param name the name of the file */ ! private native void open0(String name, boolean direct) throws FileNotFoundException; // wrap native call to allow instrumentation /** * Opens the specified file for reading. * @param name the name of the file */ private void open(String name) throws FileNotFoundException { ! open0(name, direct); ! } ! ! private native int getPageSize0(); ! ! private int getPageSize() { ! if (pageSize == -1) { ! pageSize = getPageSize0(); ! } ! return this.pageSize; } /** * Reads a byte of data from this input stream. This method blocks * if no input is yet available.
*** 217,226 **** --- 293,311 ---- * @exception IOException If an I/O error has occurred. */ private native int readBytes(byte b[], int off, int len) throws IOException; /** + * Reads a subarray as a sequence of bytes with DirectIO. + * @param b the data to be written + * @param off the start offset in the data + * @param len the number of bytes that are written + * @exception IOException If an I/O error has occurred. + */ + private native int readBytesD(byte b[], int off, int len) throws IOException; + + /** * Reads up to <code>b.length</code> bytes of data from this input * stream into an array of bytes. This method blocks until some input * is available. * * @param b the buffer into which the data is read.
*** 228,237 **** --- 313,324 ---- * <code>-1</code> if there is no more data because the end of * the file has been reached. * @exception IOException if an I/O error occurs. */ public int read(byte b[]) throws IOException { + if (direct) + return readBytesD(b, 0, b.length); return readBytes(b, 0, b.length); } /** * Reads up to <code>len</code> bytes of data from this input stream
*** 250,259 **** --- 337,348 ---- * <code>len</code> is negative, or <code>len</code> is greater than * <code>b.length - off</code> * @exception IOException if an I/O error occurs. */ public int read(byte b[], int off, int len) throws IOException { + if (direct) + return readBytesD(b, off, len); return readBytes(b, off, len); } /** * Skips over and discards <code>n</code> bytes of data from the
< prev index next >