78 private final Object closeLock = new Object(); |
78 private final Object closeLock = new Object(); |
79 |
79 |
80 private volatile boolean closed; |
80 private volatile boolean closed; |
81 |
81 |
82 /** |
82 /** |
83 * Creates a <code>FileInputStream</code> by |
83 * Creates a {@code FileInputStream} by |
84 * opening a connection to an actual file, |
84 * opening a connection to an actual file, |
85 * the file named by the path name <code>name</code> |
85 * the file named by the path name {@code name} |
86 * in the file system. A new <code>FileDescriptor</code> |
86 * in the file system. A new {@code FileDescriptor} |
87 * object is created to represent this file |
87 * object is created to represent this file |
88 * connection. |
88 * connection. |
89 * <p> |
89 * <p> |
90 * First, if there is a security |
90 * First, if there is a security |
91 * manager, its <code>checkRead</code> method |
91 * manager, its {@code checkRead} method |
92 * is called with the <code>name</code> argument |
92 * is called with the {@code name} argument |
93 * as its argument. |
93 * as its argument. |
94 * <p> |
94 * <p> |
95 * If the named file does not exist, is a directory rather than a regular |
95 * If the named file does not exist, is a directory rather than a regular |
96 * file, or for some other reason cannot be opened for reading then a |
96 * file, or for some other reason cannot be opened for reading then a |
97 * <code>FileNotFoundException</code> is thrown. |
97 * {@code FileNotFoundException} is thrown. |
98 * |
98 * |
99 * @param name the system-dependent file name. |
99 * @param name the system-dependent file name. |
100 * @exception FileNotFoundException if the file does not exist, |
100 * @throws FileNotFoundException if the file does not exist, |
101 * is a directory rather than a regular file, |
101 * is a directory rather than a regular file, |
102 * or for some other reason cannot be opened for |
102 * or for some other reason cannot be opened for |
103 * reading. |
103 * reading. |
104 * @exception SecurityException if a security manager exists and its |
104 * @throws SecurityException if a security manager exists and its |
105 * <code>checkRead</code> method denies read access |
105 * {@code checkRead} method denies read access |
106 * to the file. |
106 * to the file. |
107 * @see java.lang.SecurityManager#checkRead(java.lang.String) |
107 * @see java.lang.SecurityManager#checkRead(java.lang.String) |
108 */ |
108 */ |
109 public FileInputStream(String name) throws FileNotFoundException { |
109 public FileInputStream(String name) throws FileNotFoundException { |
110 this(name != null ? new File(name) : null); |
110 this(name != null ? new File(name) : null); |
111 } |
111 } |
112 |
112 |
113 /** |
113 /** |
114 * Creates a <code>FileInputStream</code> by |
114 * Creates a {@code FileInputStream} by |
115 * opening a connection to an actual file, |
115 * opening a connection to an actual file, |
116 * the file named by the <code>File</code> |
116 * the file named by the {@code File} |
117 * object <code>file</code> in the file system. |
117 * object {@code file} in the file system. |
118 * A new <code>FileDescriptor</code> object |
118 * A new {@code FileDescriptor} object |
119 * is created to represent this file connection. |
119 * is created to represent this file connection. |
120 * <p> |
120 * <p> |
121 * First, if there is a security manager, |
121 * First, if there is a security manager, |
122 * its <code>checkRead</code> method is called |
122 * its {@code checkRead} method is called |
123 * with the path represented by the <code>file</code> |
123 * with the path represented by the {@code file} |
124 * argument as its argument. |
124 * argument as its argument. |
125 * <p> |
125 * <p> |
126 * If the named file does not exist, is a directory rather than a regular |
126 * If the named file does not exist, is a directory rather than a regular |
127 * file, or for some other reason cannot be opened for reading then a |
127 * file, or for some other reason cannot be opened for reading then a |
128 * <code>FileNotFoundException</code> is thrown. |
128 * {@code FileNotFoundException} is thrown. |
129 * |
129 * |
130 * @param file the file to be opened for reading. |
130 * @param file the file to be opened for reading. |
131 * @exception FileNotFoundException if the file does not exist, |
131 * @throws FileNotFoundException if the file does not exist, |
132 * is a directory rather than a regular file, |
132 * is a directory rather than a regular file, |
133 * or for some other reason cannot be opened for |
133 * or for some other reason cannot be opened for |
134 * reading. |
134 * reading. |
135 * @exception SecurityException if a security manager exists and its |
135 * @throws SecurityException if a security manager exists and its |
136 * <code>checkRead</code> method denies read access to the file. |
136 * {@code checkRead} method denies read access to the file. |
137 * @see java.io.File#getPath() |
137 * @see java.io.File#getPath() |
138 * @see java.lang.SecurityManager#checkRead(java.lang.String) |
138 * @see java.lang.SecurityManager#checkRead(java.lang.String) |
139 */ |
139 */ |
140 public FileInputStream(File file) throws FileNotFoundException { |
140 public FileInputStream(File file) throws FileNotFoundException { |
141 String name = (file != null ? file.getPath() : null); |
141 String name = (file != null ? file.getPath() : null); |
155 open(name); |
155 open(name); |
156 FileCleanable.register(fd); // open set the fd, register the cleanup |
156 FileCleanable.register(fd); // open set the fd, register the cleanup |
157 } |
157 } |
158 |
158 |
159 /** |
159 /** |
160 * Creates a <code>FileInputStream</code> by using the file descriptor |
160 * Creates a {@code FileInputStream} by using the file descriptor |
161 * <code>fdObj</code>, which represents an existing connection to an |
161 * {@code fdObj}, which represents an existing connection to an |
162 * actual file in the file system. |
162 * actual file in the file system. |
163 * <p> |
163 * <p> |
164 * If there is a security manager, its <code>checkRead</code> method is |
164 * If there is a security manager, its {@code checkRead} method is |
165 * called with the file descriptor <code>fdObj</code> as its argument to |
165 * called with the file descriptor {@code fdObj} as its argument to |
166 * see if it's ok to read the file descriptor. If read access is denied |
166 * see if it's ok to read the file descriptor. If read access is denied |
167 * to the file descriptor a <code>SecurityException</code> is thrown. |
167 * to the file descriptor a {@code SecurityException} is thrown. |
168 * <p> |
168 * <p> |
169 * If <code>fdObj</code> is null then a <code>NullPointerException</code> |
169 * If {@code fdObj} is null then a {@code NullPointerException} |
170 * is thrown. |
170 * is thrown. |
171 * <p> |
171 * <p> |
172 * This constructor does not throw an exception if <code>fdObj</code> |
172 * This constructor does not throw an exception if {@code fdObj} |
173 * is {@link java.io.FileDescriptor#valid() invalid}. |
173 * is {@link java.io.FileDescriptor#valid() invalid}. |
174 * However, if the methods are invoked on the resulting stream to attempt |
174 * However, if the methods are invoked on the resulting stream to attempt |
175 * I/O on the stream, an <code>IOException</code> is thrown. |
175 * I/O on the stream, an {@code IOException} is thrown. |
176 * |
176 * |
177 * @param fdObj the file descriptor to be opened for reading. |
177 * @param fdObj the file descriptor to be opened for reading. |
178 * @throws SecurityException if a security manager exists and its |
178 * @throws SecurityException if a security manager exists and its |
179 * <code>checkRead</code> method denies read access to the |
179 * {@code checkRead} method denies read access to the |
180 * file descriptor. |
180 * file descriptor. |
181 * @see SecurityManager#checkRead(java.io.FileDescriptor) |
181 * @see SecurityManager#checkRead(java.io.FileDescriptor) |
182 */ |
182 */ |
183 public FileInputStream(FileDescriptor fdObj) { |
183 public FileInputStream(FileDescriptor fdObj) { |
184 SecurityManager security = System.getSecurityManager(); |
184 SecurityManager security = System.getSecurityManager(); |
185 if (fdObj == null) { |
185 if (fdObj == null) { |
215 |
215 |
216 /** |
216 /** |
217 * Reads a byte of data from this input stream. This method blocks |
217 * Reads a byte of data from this input stream. This method blocks |
218 * if no input is yet available. |
218 * if no input is yet available. |
219 * |
219 * |
220 * @return the next byte of data, or <code>-1</code> if the end of the |
220 * @return the next byte of data, or {@code -1} if the end of the |
221 * file is reached. |
221 * file is reached. |
222 * @exception IOException if an I/O error occurs. |
222 * @throws IOException if an I/O error occurs. |
223 */ |
223 */ |
224 public int read() throws IOException { |
224 public int read() throws IOException { |
225 return read0(); |
225 return read0(); |
226 } |
226 } |
227 |
227 |
228 private native int read0() throws IOException; |
228 private native int read0() throws IOException; |
229 |
229 |
230 /** |
230 /** |
231 * Reads a subarray as a sequence of bytes. |
231 * Reads a subarray as a sequence of bytes. |
232 * @param b the data to be written |
232 * @param b the data to be written |
233 * @param off the start offset in the data |
233 * @param off the start offset in the data |
234 * @param len the number of bytes that are written |
234 * @param len the number of bytes that are written |
235 * @exception IOException If an I/O error has occurred. |
235 * @throws IOException If an I/O error has occurred. |
236 */ |
236 */ |
237 private native int readBytes(byte b[], int off, int len) throws IOException; |
237 private native int readBytes(byte b[], int off, int len) throws IOException; |
238 |
238 |
239 /** |
239 /** |
240 * Reads up to <code>b.length</code> bytes of data from this input |
240 * Reads up to {@code b.length} bytes of data from this input |
241 * stream into an array of bytes. This method blocks until some input |
241 * stream into an array of bytes. This method blocks until some input |
242 * is available. |
242 * is available. |
243 * |
243 * |
244 * @param b the buffer into which the data is read. |
244 * @param b the buffer into which the data is read. |
245 * @return the total number of bytes read into the buffer, or |
245 * @return the total number of bytes read into the buffer, or |
246 * <code>-1</code> if there is no more data because the end of |
246 * {@code -1} if there is no more data because the end of |
247 * the file has been reached. |
247 * the file has been reached. |
248 * @exception IOException if an I/O error occurs. |
248 * @throws IOException if an I/O error occurs. |
249 */ |
249 */ |
250 public int read(byte b[]) throws IOException { |
250 public int read(byte b[]) throws IOException { |
251 return readBytes(b, 0, b.length); |
251 return readBytes(b, 0, b.length); |
252 } |
252 } |
253 |
253 |
254 /** |
254 /** |
255 * Reads up to <code>len</code> bytes of data from this input stream |
255 * Reads up to {@code len} bytes of data from this input stream |
256 * into an array of bytes. If <code>len</code> is not zero, the method |
256 * into an array of bytes. If {@code len} is not zero, the method |
257 * blocks until some input is available; otherwise, no |
257 * blocks until some input is available; otherwise, no |
258 * bytes are read and <code>0</code> is returned. |
258 * bytes are read and {@code 0} is returned. |
259 * |
259 * |
260 * @param b the buffer into which the data is read. |
260 * @param b the buffer into which the data is read. |
261 * @param off the start offset in the destination array <code>b</code> |
261 * @param off the start offset in the destination array {@code b} |
262 * @param len the maximum number of bytes read. |
262 * @param len the maximum number of bytes read. |
263 * @return the total number of bytes read into the buffer, or |
263 * @return the total number of bytes read into the buffer, or |
264 * <code>-1</code> if there is no more data because the end of |
264 * {@code -1} if there is no more data because the end of |
265 * the file has been reached. |
265 * the file has been reached. |
266 * @exception NullPointerException If <code>b</code> is <code>null</code>. |
266 * @throws NullPointerException If {@code b} is {@code null}. |
267 * @exception IndexOutOfBoundsException If <code>off</code> is negative, |
267 * @throws IndexOutOfBoundsException If {@code off} is negative, |
268 * <code>len</code> is negative, or <code>len</code> is greater than |
268 * {@code len} is negative, or {@code len} is greater than |
269 * <code>b.length - off</code> |
269 * {@code b.length - off} |
270 * @exception IOException if an I/O error occurs. |
270 * @throws IOException if an I/O error occurs. |
271 */ |
271 */ |
272 public int read(byte b[], int off, int len) throws IOException { |
272 public int read(byte b[], int off, int len) throws IOException { |
273 return readBytes(b, off, len); |
273 return readBytes(b, off, len); |
274 } |
274 } |
275 |
275 |
276 /** |
276 /** |
277 * Skips over and discards <code>n</code> bytes of data from the |
277 * Skips over and discards {@code n} bytes of data from the |
278 * input stream. |
278 * input stream. |
279 * |
279 * |
280 * <p>The <code>skip</code> method may, for a variety of |
280 * <p>The {@code skip} method may, for a variety of |
281 * reasons, end up skipping over some smaller number of bytes, |
281 * reasons, end up skipping over some smaller number of bytes, |
282 * possibly <code>0</code>. If <code>n</code> is negative, the method |
282 * possibly {@code 0}. If {@code n} is negative, the method |
283 * will try to skip backwards. In case the backing file does not support |
283 * will try to skip backwards. In case the backing file does not support |
284 * backward skip at its current position, an <code>IOException</code> is |
284 * backward skip at its current position, an {@code IOException} is |
285 * thrown. The actual number of bytes skipped is returned. If it skips |
285 * thrown. The actual number of bytes skipped is returned. If it skips |
286 * forwards, it returns a positive value. If it skips backwards, it |
286 * forwards, it returns a positive value. If it skips backwards, it |
287 * returns a negative value. |
287 * returns a negative value. |
288 * |
288 * |
289 * <p>This method may skip more bytes than what are remaining in the |
289 * <p>This method may skip more bytes than what are remaining in the |