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 java.nio.channels.FileChannel;
29 import sun.nio.ch.FileChannelImpl;
30
31
32 /**
33 * A <code>FileInputStream</code> obtains input bytes
34 * from a file in a file system. What files
35 * are available depends on the host environment.
36 *
37 * <p><code>FileInputStream</code> is meant for reading streams of raw bytes
38 * such as image data. For reading streams of characters, consider using
39 * <code>FileReader</code>.
40 *
41 * @author Arthur van Hoff
42 * @see java.io.File
43 * @see java.io.FileDescriptor
44 * @see java.io.FileOutputStream
45 * @see java.nio.file.Files#newInputStream
46 * @since JDK1.0
47 */
48 public
49 class FileInputStream extends InputStream
50 {
51 /* File Descriptor - handle to the open file */
52 private final FileDescriptor fd;
53
54 private FileChannel channel = null;
55
56 private final Object closeLock = new Object();
57 private volatile boolean closed = false;
58
59 /**
60 * Creates a <code>FileInputStream</code> by
61 * opening a connection to an actual file,
62 * the file named by the path name <code>name</code>
63 * in the file system. A new <code>FileDescriptor</code>
64 * object is created to represent this file
65 * connection.
66 * <p>
67 * First, if there is a security
68 * manager, its <code>checkRead</code> method
69 * is called with the <code>name</code> argument
70 * as its argument.
71 * <p>
72 * If the named file does not exist, is a directory rather than a regular
73 * file, or for some other reason cannot be opened for reading then a
108 * @exception FileNotFoundException if the file does not exist,
109 * is a directory rather than a regular file,
110 * or for some other reason cannot be opened for
111 * reading.
112 * @exception SecurityException if a security manager exists and its
113 * <code>checkRead</code> method denies read access to the file.
114 * @see java.io.File#getPath()
115 * @see java.lang.SecurityManager#checkRead(java.lang.String)
116 */
117 public FileInputStream(File file) throws FileNotFoundException {
118 String name = (file != null ? file.getPath() : null);
119 SecurityManager security = System.getSecurityManager();
120 if (security != null) {
121 security.checkRead(name);
122 }
123 if (name == null) {
124 throw new NullPointerException();
125 }
126 fd = new FileDescriptor();
127 fd.attach(this);
128 open(name);
129 }
130
131 /**
132 * Creates a <code>FileInputStream</code> by using the file descriptor
133 * <code>fdObj</code>, which represents an existing connection to an
134 * actual file in the file system.
135 * <p>
136 * If there is a security manager, its <code>checkRead</code> method is
137 * called with the file descriptor <code>fdObj</code> as its argument to
138 * see if it's ok to read the file descriptor. If read access is denied
139 * to the file descriptor a <code>SecurityException</code> is thrown.
140 * <p>
141 * If <code>fdObj</code> is null then a <code>NullPointerException</code>
142 * is thrown.
143 * <p>
144 * This constructor does not throw an exception if <code>fdObj</code>
145 * is {@link java.io.FileDescriptor#valid() invalid}.
146 * However, if the methods are invoked on the resulting stream to attempt
147 * I/O on the stream, an <code>IOException</code> is thrown.
148 *
149 * @param fdObj the file descriptor to be opened for reading.
150 * @throws SecurityException if a security manager exists and its
151 * <code>checkRead</code> method denies read access to the
152 * file descriptor.
153 * @see SecurityManager#checkRead(java.io.FileDescriptor)
154 */
155 public FileInputStream(FileDescriptor fdObj) {
156 SecurityManager security = System.getSecurityManager();
157 if (fdObj == null) {
158 throw new NullPointerException();
159 }
160 if (security != null) {
161 security.checkRead(fdObj);
162 }
163 fd = fdObj;
164
165 /*
166 * FileDescriptor is being shared by streams.
167 * Register this stream with FileDescriptor tracker.
168 */
169 fd.attach(this);
170 }
171
172 /**
173 * Opens the specified file for reading.
174 * @param name the name of the file
175 */
176 private native void open(String name) throws FileNotFoundException;
177
178 /**
179 * Reads a byte of data from this input stream. This method blocks
180 * if no input is yet available.
181 *
182 * @return the next byte of data, or <code>-1</code> if the end of the
183 * file is reached.
184 * @exception IOException if an I/O error occurs.
185 */
186 public native int read() throws IOException;
187
188 /**
189 * Reads a subarray as a sequence of bytes.
190 * @param b the data to be written
191 * @param off the start offset in the data
192 * @param len the number of bytes that are written
193 * @exception IOException If an I/O error has occurred.
194 */
195 private native int readBytes(byte b[], int off, int len) throws IOException;
196
197 /**
198 * Reads up to <code>b.length</code> bytes of data from this input
199 * stream into an array of bytes. This method blocks until some input
200 * is available.
201 *
202 * @param b the buffer into which the data is read.
203 * @return the total number of bytes read into the buffer, or
204 * <code>-1</code> if there is no more data because the end of
205 * the file has been reached.
206 * @exception IOException if an I/O error occurs.
207 */
208 public int read(byte b[]) throws IOException {
209 return readBytes(b, 0, b.length);
210 }
211
212 /**
213 * Reads up to <code>len</code> bytes of data from this input stream
214 * into an array of bytes. If <code>len</code> is not zero, the method
215 * blocks until some input is available; otherwise, no
216 * bytes are read and <code>0</code> is returned.
217 *
218 * @param b the buffer into which the data is read.
219 * @param off the start offset in the destination array <code>b</code>
220 * @param len the maximum number of bytes read.
221 * @return the total number of bytes read into the buffer, or
222 * <code>-1</code> if there is no more data because the end of
223 * the file has been reached.
224 * @exception NullPointerException If <code>b</code> is <code>null</code>.
225 * @exception IndexOutOfBoundsException If <code>off</code> is negative,
226 * <code>len</code> is negative, or <code>len</code> is greater than
227 * <code>b.length - off</code>
228 * @exception IOException if an I/O error occurs.
229 */
230 public int read(byte b[], int off, int len) throws IOException {
231 return readBytes(b, off, len);
232 }
233
234 /**
235 * Skips over and discards <code>n</code> bytes of data from the
236 * input stream.
237 *
238 * <p>The <code>skip</code> method may, for a variety of
239 * reasons, end up skipping over some smaller number of bytes,
240 * possibly <code>0</code>. If <code>n</code> is negative, an
241 * <code>IOException</code> is thrown, even though the <code>skip</code>
242 * method of the {@link InputStream} superclass does nothing in this case.
243 * The actual number of bytes skipped is returned.
244 *
245 * <p>This method may skip more bytes than are remaining in the backing
246 * file. This produces no exception and the number of bytes skipped
247 * may include some number of bytes that were beyond the EOF of the
248 * backing file. Attempting to read from the stream after skipping past
249 * the end will result in -1 indicating the end of the file.
250 *
251 * @param n the number of bytes to be skipped.
322
323 /**
324 * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
325 * object associated with this file input stream.
326 *
327 * <p> The initial {@link java.nio.channels.FileChannel#position()
328 * </code>position<code>} of the returned channel will be equal to the
329 * number of bytes read from the file so far. Reading bytes from this
330 * stream will increment the channel's position. Changing the channel's
331 * position, either explicitly or by reading, will change this stream's
332 * file position.
333 *
334 * @return the file channel associated with this file input stream
335 *
336 * @since 1.4
337 * @spec JSR-51
338 */
339 public FileChannel getChannel() {
340 synchronized (this) {
341 if (channel == null) {
342 channel = FileChannelImpl.open(fd, true, false, this);
343 }
344 return channel;
345 }
346 }
347
348 private static native void initIDs();
349
350 private native void close0() throws IOException;
351
352 static {
353 initIDs();
354 }
355
356 /**
357 * Ensures that the <code>close</code> method of this file input stream is
358 * called when there are no more references to it.
359 *
360 * @exception IOException if an I/O error occurs.
361 * @see java.io.FileInputStream#close()
362 */
|
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 java.nio.channels.FileChannel;
29 import sun.nio.ch.FileChannelImpl;
30 import sun.misc.IoTrace;
31
32
33 /**
34 * A <code>FileInputStream</code> obtains input bytes
35 * from a file in a file system. What files
36 * are available depends on the host environment.
37 *
38 * <p><code>FileInputStream</code> is meant for reading streams of raw bytes
39 * such as image data. For reading streams of characters, consider using
40 * <code>FileReader</code>.
41 *
42 * @author Arthur van Hoff
43 * @see java.io.File
44 * @see java.io.FileDescriptor
45 * @see java.io.FileOutputStream
46 * @see java.nio.file.Files#newInputStream
47 * @since JDK1.0
48 */
49 public
50 class FileInputStream extends InputStream
51 {
52 /* File Descriptor - handle to the open file */
53 private final FileDescriptor fd;
54
55 /* The path of the referenced file (null if there is no file) */
56 private final String path;
57
58 private FileChannel channel = null;
59
60 private final Object closeLock = new Object();
61 private volatile boolean closed = false;
62
63 /**
64 * Creates a <code>FileInputStream</code> by
65 * opening a connection to an actual file,
66 * the file named by the path name <code>name</code>
67 * in the file system. A new <code>FileDescriptor</code>
68 * object is created to represent this file
69 * connection.
70 * <p>
71 * First, if there is a security
72 * manager, its <code>checkRead</code> method
73 * is called with the <code>name</code> argument
74 * as its argument.
75 * <p>
76 * If the named file does not exist, is a directory rather than a regular
77 * file, or for some other reason cannot be opened for reading then a
112 * @exception FileNotFoundException if the file does not exist,
113 * is a directory rather than a regular file,
114 * or for some other reason cannot be opened for
115 * reading.
116 * @exception SecurityException if a security manager exists and its
117 * <code>checkRead</code> method denies read access to the file.
118 * @see java.io.File#getPath()
119 * @see java.lang.SecurityManager#checkRead(java.lang.String)
120 */
121 public FileInputStream(File file) throws FileNotFoundException {
122 String name = (file != null ? file.getPath() : null);
123 SecurityManager security = System.getSecurityManager();
124 if (security != null) {
125 security.checkRead(name);
126 }
127 if (name == null) {
128 throw new NullPointerException();
129 }
130 fd = new FileDescriptor();
131 fd.attach(this);
132 this.path = name;
133 open(name);
134 }
135
136 /**
137 * Creates a <code>FileInputStream</code> by using the file descriptor
138 * <code>fdObj</code>, which represents an existing connection to an
139 * actual file in the file system.
140 * <p>
141 * If there is a security manager, its <code>checkRead</code> method is
142 * called with the file descriptor <code>fdObj</code> as its argument to
143 * see if it's ok to read the file descriptor. If read access is denied
144 * to the file descriptor a <code>SecurityException</code> is thrown.
145 * <p>
146 * If <code>fdObj</code> is null then a <code>NullPointerException</code>
147 * is thrown.
148 * <p>
149 * This constructor does not throw an exception if <code>fdObj</code>
150 * is {@link java.io.FileDescriptor#valid() invalid}.
151 * However, if the methods are invoked on the resulting stream to attempt
152 * I/O on the stream, an <code>IOException</code> is thrown.
153 *
154 * @param fdObj the file descriptor to be opened for reading.
155 * @throws SecurityException if a security manager exists and its
156 * <code>checkRead</code> method denies read access to the
157 * file descriptor.
158 * @see SecurityManager#checkRead(java.io.FileDescriptor)
159 */
160 public FileInputStream(FileDescriptor fdObj) {
161 SecurityManager security = System.getSecurityManager();
162 if (fdObj == null) {
163 throw new NullPointerException();
164 }
165 if (security != null) {
166 security.checkRead(fdObj);
167 }
168 fd = fdObj;
169 path = null;
170
171 /*
172 * FileDescriptor is being shared by streams.
173 * Register this stream with FileDescriptor tracker.
174 */
175 fd.attach(this);
176 }
177
178 /**
179 * Opens the specified file for reading.
180 * @param name the name of the file
181 */
182 private native void open(String name) throws FileNotFoundException;
183
184 /**
185 * Reads a byte of data from this input stream. This method blocks
186 * if no input is yet available.
187 *
188 * @return the next byte of data, or <code>-1</code> if the end of the
189 * file is reached.
190 * @exception IOException if an I/O error occurs.
191 */
192 public int read() throws IOException {
193 Object traceHandle = IoTrace.fileReadBegin(path);
194 int b = read0();
195 IoTrace.fileReadEnd(traceHandle, b == -1 ? -1 : 1);
196 return b;
197 }
198
199 private native int read0() throws IOException;
200
201 /**
202 * Reads a subarray as a sequence of bytes.
203 * @param b the data to be written
204 * @param off the start offset in the data
205 * @param len the number of bytes that are written
206 * @exception IOException If an I/O error has occurred.
207 */
208 private native int readBytes(byte b[], int off, int len) throws IOException;
209
210 /**
211 * Reads up to <code>b.length</code> bytes of data from this input
212 * stream into an array of bytes. This method blocks until some input
213 * is available.
214 *
215 * @param b the buffer into which the data is read.
216 * @return the total number of bytes read into the buffer, or
217 * <code>-1</code> if there is no more data because the end of
218 * the file has been reached.
219 * @exception IOException if an I/O error occurs.
220 */
221 public int read(byte b[]) throws IOException {
222 Object traceHandle = IoTrace.fileReadBegin(path);
223 int bytesRead = readBytes(b, 0, b.length);
224 IoTrace.fileReadEnd(traceHandle, bytesRead);
225 return bytesRead;
226 }
227
228 /**
229 * Reads up to <code>len</code> bytes of data from this input stream
230 * into an array of bytes. If <code>len</code> is not zero, the method
231 * blocks until some input is available; otherwise, no
232 * bytes are read and <code>0</code> is returned.
233 *
234 * @param b the buffer into which the data is read.
235 * @param off the start offset in the destination array <code>b</code>
236 * @param len the maximum number of bytes read.
237 * @return the total number of bytes read into the buffer, or
238 * <code>-1</code> if there is no more data because the end of
239 * the file has been reached.
240 * @exception NullPointerException If <code>b</code> is <code>null</code>.
241 * @exception IndexOutOfBoundsException If <code>off</code> is negative,
242 * <code>len</code> is negative, or <code>len</code> is greater than
243 * <code>b.length - off</code>
244 * @exception IOException if an I/O error occurs.
245 */
246 public int read(byte b[], int off, int len) throws IOException {
247 Object traceHandle = IoTrace.fileReadBegin(path);
248 int bytesRead = readBytes(b, off, len);
249 IoTrace.fileReadEnd(traceHandle, bytesRead);
250 return bytesRead;
251 }
252
253 /**
254 * Skips over and discards <code>n</code> bytes of data from the
255 * input stream.
256 *
257 * <p>The <code>skip</code> method may, for a variety of
258 * reasons, end up skipping over some smaller number of bytes,
259 * possibly <code>0</code>. If <code>n</code> is negative, an
260 * <code>IOException</code> is thrown, even though the <code>skip</code>
261 * method of the {@link InputStream} superclass does nothing in this case.
262 * The actual number of bytes skipped is returned.
263 *
264 * <p>This method may skip more bytes than are remaining in the backing
265 * file. This produces no exception and the number of bytes skipped
266 * may include some number of bytes that were beyond the EOF of the
267 * backing file. Attempting to read from the stream after skipping past
268 * the end will result in -1 indicating the end of the file.
269 *
270 * @param n the number of bytes to be skipped.
341
342 /**
343 * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
344 * object associated with this file input stream.
345 *
346 * <p> The initial {@link java.nio.channels.FileChannel#position()
347 * </code>position<code>} of the returned channel will be equal to the
348 * number of bytes read from the file so far. Reading bytes from this
349 * stream will increment the channel's position. Changing the channel's
350 * position, either explicitly or by reading, will change this stream's
351 * file position.
352 *
353 * @return the file channel associated with this file input stream
354 *
355 * @since 1.4
356 * @spec JSR-51
357 */
358 public FileChannel getChannel() {
359 synchronized (this) {
360 if (channel == null) {
361 channel = FileChannelImpl.open(fd, path, true, false, this);
362 }
363 return channel;
364 }
365 }
366
367 private static native void initIDs();
368
369 private native void close0() throws IOException;
370
371 static {
372 initIDs();
373 }
374
375 /**
376 * Ensures that the <code>close</code> method of this file input stream is
377 * called when there are no more references to it.
378 *
379 * @exception IOException if an I/O error occurs.
380 * @see java.io.FileInputStream#close()
381 */
|