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
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 if (file.isInvalid()) {
127 throw new FileNotFoundException("Invalid file path");
128 }
129 fd = new FileDescriptor();
130 fd.attach(this);
131 open(name);
132 }
133
134 /**
135 * Creates a <code>FileInputStream</code> by using the file descriptor
136 * <code>fdObj</code>, which represents an existing connection to an
137 * actual file in the file system.
138 * <p>
139 * If there is a security manager, its <code>checkRead</code> method is
140 * called with the file descriptor <code>fdObj</code> as its argument to
141 * see if it's ok to read the file descriptor. If read access is denied
142 * to the file descriptor a <code>SecurityException</code> is thrown.
143 * <p>
144 * If <code>fdObj</code> is null then a <code>NullPointerException</code>
145 * is thrown.
146 * <p>
147 * This constructor does not throw an exception if <code>fdObj</code>
148 * is {@link java.io.FileDescriptor#valid() invalid}.
149 * However, if the methods are invoked on the resulting stream to attempt
150 * I/O on the stream, an <code>IOException</code> is thrown.
151 *
152 * @param fdObj the file descriptor to be opened for reading.
153 * @throws SecurityException if a security manager exists and its
154 * <code>checkRead</code> method denies read access to the
155 * file descriptor.
156 * @see SecurityManager#checkRead(java.io.FileDescriptor)
157 */
158 public FileInputStream(FileDescriptor fdObj) {
159 SecurityManager security = System.getSecurityManager();
160 if (fdObj == null) {
161 throw new NullPointerException();
162 }
163 if (security != null) {
164 security.checkRead(fdObj);
165 }
166 fd = fdObj;
167
168 /*
169 * FileDescriptor is being shared by streams.
170 * Register this stream with FileDescriptor tracker.
171 */
172 fd.attach(this);
173 }
174
175 /**
176 * Opens the specified file for reading.
177 * @param name the name of the file
178 */
179 private native void open(String name) throws FileNotFoundException;
180
181 /**
182 * Reads a byte of data from this input stream. This method blocks
183 * if no input is yet available.
184 *
185 * @return the next byte of data, or <code>-1</code> if the end of the
186 * file is reached.
328
329 /**
330 * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
331 * object associated with this file input stream.
332 *
333 * <p> The initial {@link java.nio.channels.FileChannel#position()
334 * position} of the returned channel will be equal to the
335 * number of bytes read from the file so far. Reading bytes from this
336 * stream will increment the channel's position. Changing the channel's
337 * position, either explicitly or by reading, will change this stream's
338 * file position.
339 *
340 * @return the file channel associated with this file input stream
341 *
342 * @since 1.4
343 * @spec JSR-51
344 */
345 public FileChannel getChannel() {
346 synchronized (this) {
347 if (channel == null) {
348 channel = FileChannelImpl.open(fd, true, false, this);
349 }
350 return channel;
351 }
352 }
353
354 private static native void initIDs();
355
356 private native void close0() throws IOException;
357
358 static {
359 initIDs();
360 }
361
362 /**
363 * Ensures that the <code>close</code> method of this file input stream is
364 * called when there are no more references to it.
365 *
366 * @exception IOException if an I/O error occurs.
367 * @see java.io.FileInputStream#close()
368 */
|
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 /* The path of the referenced file (null if the stream is created with a file descriptor) */
55 private final String path;
56
57 private FileChannel channel = null;
58
59 private final Object closeLock = new Object();
60 private volatile boolean closed = false;
61
62 /**
63 * Creates a <code>FileInputStream</code> by
64 * opening a connection to an actual file,
65 * the file named by the path name <code>name</code>
66 * in the file system. A new <code>FileDescriptor</code>
67 * object is created to represent this file
68 * connection.
69 * <p>
70 * First, if there is a security
71 * manager, its <code>checkRead</code> method
72 * is called with the <code>name</code> argument
73 * as its argument.
74 * <p>
75 * If the named file does not exist, is a directory rather than a regular
76 * file, or for some other reason cannot be opened for reading then a
114 * reading.
115 * @exception SecurityException if a security manager exists and its
116 * <code>checkRead</code> method denies read access to the file.
117 * @see java.io.File#getPath()
118 * @see java.lang.SecurityManager#checkRead(java.lang.String)
119 */
120 public FileInputStream(File file) throws FileNotFoundException {
121 String name = (file != null ? file.getPath() : null);
122 SecurityManager security = System.getSecurityManager();
123 if (security != null) {
124 security.checkRead(name);
125 }
126 if (name == null) {
127 throw new NullPointerException();
128 }
129 if (file.isInvalid()) {
130 throw new FileNotFoundException("Invalid file path");
131 }
132 fd = new FileDescriptor();
133 fd.attach(this);
134 path = name;
135 open(name);
136 }
137
138 /**
139 * Creates a <code>FileInputStream</code> by using the file descriptor
140 * <code>fdObj</code>, which represents an existing connection to an
141 * actual file in the file system.
142 * <p>
143 * If there is a security manager, its <code>checkRead</code> method is
144 * called with the file descriptor <code>fdObj</code> as its argument to
145 * see if it's ok to read the file descriptor. If read access is denied
146 * to the file descriptor a <code>SecurityException</code> is thrown.
147 * <p>
148 * If <code>fdObj</code> is null then a <code>NullPointerException</code>
149 * is thrown.
150 * <p>
151 * This constructor does not throw an exception if <code>fdObj</code>
152 * is {@link java.io.FileDescriptor#valid() invalid}.
153 * However, if the methods are invoked on the resulting stream to attempt
154 * I/O on the stream, an <code>IOException</code> is thrown.
155 *
156 * @param fdObj the file descriptor to be opened for reading.
157 * @throws SecurityException if a security manager exists and its
158 * <code>checkRead</code> method denies read access to the
159 * file descriptor.
160 * @see SecurityManager#checkRead(java.io.FileDescriptor)
161 */
162 public FileInputStream(FileDescriptor fdObj) {
163 SecurityManager security = System.getSecurityManager();
164 if (fdObj == null) {
165 throw new NullPointerException();
166 }
167 if (security != null) {
168 security.checkRead(fdObj);
169 }
170 fd = fdObj;
171 path = null;
172
173 /*
174 * FileDescriptor is being shared by streams.
175 * Register this stream with FileDescriptor tracker.
176 */
177 fd.attach(this);
178 }
179
180 /**
181 * Opens the specified file for reading.
182 * @param name the name of the file
183 */
184 private native void open(String name) throws FileNotFoundException;
185
186 /**
187 * Reads a byte of data from this input stream. This method blocks
188 * if no input is yet available.
189 *
190 * @return the next byte of data, or <code>-1</code> if the end of the
191 * file is reached.
333
334 /**
335 * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
336 * object associated with this file input stream.
337 *
338 * <p> The initial {@link java.nio.channels.FileChannel#position()
339 * position} of the returned channel will be equal to the
340 * number of bytes read from the file so far. Reading bytes from this
341 * stream will increment the channel's position. Changing the channel's
342 * position, either explicitly or by reading, will change this stream's
343 * file position.
344 *
345 * @return the file channel associated with this file input stream
346 *
347 * @since 1.4
348 * @spec JSR-51
349 */
350 public FileChannel getChannel() {
351 synchronized (this) {
352 if (channel == null) {
353 channel = FileChannelImpl.open(fd, path, true, false, this);
354 }
355 return channel;
356 }
357 }
358
359 private static native void initIDs();
360
361 private native void close0() throws IOException;
362
363 static {
364 initIDs();
365 }
366
367 /**
368 * Ensures that the <code>close</code> method of this file input stream is
369 * called when there are no more references to it.
370 *
371 * @exception IOException if an I/O error occurs.
372 * @see java.io.FileInputStream#close()
373 */
|