43 |
import gnu.java.nio.FileChannelImpl; |
import gnu.java.nio.FileChannelImpl; |
44 |
|
|
45 |
/** |
/** |
46 |
* This class is a stream that reads its bytes from a file. |
* This class is a stream that reads its bytes from a file. |
47 |
* |
* |
48 |
* @author Aaron M. Renn (arenn@urbanophile.com) |
* @author Aaron M. Renn (arenn@urbanophile.com) |
49 |
*/ |
*/ |
50 |
public class FileInputStream extends InputStream |
public class FileInputStream extends InputStream |
51 |
{ |
{ |
52 |
/* |
/* |
65 |
*/ |
*/ |
66 |
|
|
67 |
/** |
/** |
68 |
* This method initializes a <code>FileInputStream</code> to read from the |
* This method initializes a <code>FileInputStream</code> to read from the |
69 |
* specified <code>File</code> object. A security check is first |
* specified <code>File</code> object. A security check is first |
70 |
* made to determine |
* made to determine |
71 |
* whether or not access to this file is allowed. This is done by |
* whether or not access to this file is allowed. This is done by |
72 |
* calling the <code>checkRead()</code> method of the |
* calling the <code>checkRead()</code> method of the |
73 |
* <code>SecurityManager</code> |
* <code>SecurityManager</code> |
74 |
* (if one exists) with the name of this file. An exception is thrown |
* (if one exists) with the name of this file. An exception is thrown |
75 |
* if reading is not allowed. If the file does not exist, an exception |
* if reading is not allowed. If the file does not exist, an exception |
76 |
* is also thrown. |
* is also thrown. |
77 |
* |
* |
78 |
* @param file The <code>File</code> object this stream should read from |
* @param file The <code>File</code> object this stream should read from |
79 |
* |
* |
80 |
* @exception SecurityException If read access to the file is not allowed |
* @exception SecurityException If read access to the file is not allowed |
81 |
* @exception FileNotFoundException If the file does not exist. |
* @exception FileNotFoundException If the file does not exist. |
82 |
*/ |
*/ |
83 |
public FileInputStream(File file) throws FileNotFoundException |
public FileInputStream(File file) throws FileNotFoundException |
84 |
{ |
{ |
85 |
this(file.getPath()); |
this(file.getPath()); |
86 |
} |
} |
87 |
|
|
|
/*************************************************************************/ |
|
|
|
|
88 |
/** |
/** |
89 |
* This method initializes a <code>FileInputStream</code> to read from the |
* This method initializes a <code>FileInputStream</code> to read from the |
90 |
* specified named file. A security check is first made to determine |
* specified named file. A security check is first made to determine |
91 |
* whether or not access to this file is allowed. This is done by |
* whether or not access to this file is allowed. This is done by |
92 |
* calling the <code>checkRead()</code> method of the |
* calling the <code>checkRead()</code> method of the |
93 |
* <code>SecurityManager</code> |
* <code>SecurityManager</code> |
94 |
* (if one exists) with the name of this file. An exception is thrown |
* (if one exists) with the name of this file. An exception is thrown |
95 |
* if reading is not allowed. If the file does not exist, an exception |
* if reading is not allowed. If the file does not exist, an exception |
96 |
* is also thrown. |
* is also thrown. |
97 |
* |
* |
98 |
* @param name The name of the file this stream should read from |
* @param name The name of the file this stream should read from |
99 |
* |
* |
100 |
* @exception SecurityException If read access to the file is not allowed |
* @exception SecurityException If read access to the file is not allowed |
101 |
* @exception FileNotFoundException If the file does not exist. |
* @exception FileNotFoundException If the file does not exist. |
102 |
*/ |
*/ |
103 |
public FileInputStream(String name) throws FileNotFoundException |
public FileInputStream(String name) throws FileNotFoundException |
104 |
{ |
{ |
105 |
SecurityManager sm = System.getSecurityManager(); |
SecurityManager sm = System.getSecurityManager(); |
118 |
} |
} |
119 |
} |
} |
120 |
|
|
|
/*************************************************************************/ |
|
|
|
|
121 |
/** |
/** |
122 |
* This method initializes a <code>FileInputStream</code> to read from the |
* This method initializes a <code>FileInputStream</code> to read from the |
123 |
* specified <code>FileDescriptor</code> object. A security |
* specified <code>FileDescriptor</code> object. A security |
124 |
* check is first made to |
* check is first made to |
125 |
* determine whether or not access to this file is allowed. This is done by |
* determine whether or not access to this file is allowed. This is done by |
126 |
* calling the <code>checkRead()</code> method of the |
* calling the <code>checkRead()</code> method of the |
127 |
* <code>SecurityManager</code> |
* <code>SecurityManager</code> |
128 |
* (if one exists) with the specified <code>FileDescriptor</code> |
* (if one exists) with the specified <code>FileDescriptor</code> |
129 |
* An exception is |
* An exception is |
130 |
* thrown if reading is not allowed. |
* thrown if reading is not allowed. |
131 |
* |
* |
132 |
* @param fd The <code>FileDescriptor</code> object this stream |
* @param fd The <code>FileDescriptor</code> object this stream |
133 |
* should read from |
* should read from |
134 |
* |
* |
135 |
* @exception SecurityException If read access to the file is not allowed |
* @exception SecurityException If read access to the file is not allowed |
136 |
*/ |
*/ |
137 |
public FileInputStream(FileDescriptor fd) throws SecurityException |
public FileInputStream(FileDescriptor fd) throws SecurityException |
138 |
{ |
{ |
139 |
// Hmmm, no other exception but this one to throw, but if the descriptor |
// Hmmm, no other exception but this one to throw, but if the descriptor |
148 |
this.fd = fd; |
this.fd = fd; |
149 |
} |
} |
150 |
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/* |
|
|
* Instance Methods |
|
|
*/ |
|
|
|
|
151 |
/** |
/** |
152 |
* This method returns a <code>FileDescriptor</code> object representing the |
* This method returns a <code>FileDescriptor</code> object representing the |
153 |
* underlying native file handle of the file this stream is reading |
* underlying native file handle of the file this stream is reading |
154 |
* from |
* from |
155 |
* |
* |
156 |
* @return A <code>FileDescriptor</code> for this stream |
* @return A <code>FileDescriptor</code> for this stream |
157 |
* |
* |
158 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
159 |
*/ |
*/ |
160 |
public final FileDescriptor getFD() throws IOException |
public final FileDescriptor getFD() throws IOException |
161 |
{ |
{ |
162 |
return(fd); |
return(fd); |
163 |
} |
} |
164 |
|
|
|
/*************************************************************************/ |
|
|
|
|
165 |
/** |
/** |
166 |
* This method returns the number of bytes that can be read from this |
* This method returns the number of bytes that can be read from this |
167 |
* stream before a read can block. A return of 0 indicates that blocking |
* stream before a read can block. A return of 0 indicates that blocking |
168 |
* might (or might not) occur on the very next read attempt. |
* might (or might not) occur on the very next read attempt. |
169 |
* <p> |
* <p> |
170 |
* This method returns the number of unread bytes remaining in the file if |
* This method returns the number of unread bytes remaining in the file if |
171 |
* the descriptor being read from is an actual file. If this method is |
* the descriptor being read from is an actual file. If this method is |
172 |
* reading from a ''special'' file such a the standard input, this method |
* reading from a ''special'' file such a the standard input, this method |
173 |
* will return the appropriate value for the stream being read. |
* will return the appropriate value for the stream being read. |
174 |
* <p> |
* <p> |
175 |
* Be aware that reads on plain files that do not reside locally might |
* Be aware that reads on plain files that do not reside locally might |
176 |
* possibly block even if this method says they should not. For example, |
* possibly block even if this method says they should not. For example, |
177 |
* a remote server might crash, preventing an NFS mounted file from being |
* a remote server might crash, preventing an NFS mounted file from being |
178 |
* read. |
* read. |
179 |
* |
* |
180 |
* @return The number of bytes that can be read before blocking could occur |
* @return The number of bytes that can be read before blocking could occur |
181 |
* |
* |
182 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
183 |
*/ |
*/ |
184 |
public int available() throws IOException |
public int available() throws IOException |
185 |
{ |
{ |
186 |
return(fd.available()); |
return(fd.available()); |
187 |
} |
} |
188 |
|
|
|
/*************************************************************************/ |
|
|
|
|
189 |
/** |
/** |
190 |
* This method skips the specified number of bytes in the stream. It |
* This method skips the specified number of bytes in the stream. It |
191 |
* returns the actual number of bytes skipped, which may be less than the |
* returns the actual number of bytes skipped, which may be less than the |
192 |
* requested amount. |
* requested amount. |
193 |
* <p> |
* <p> |
194 |
* @param numBytes The requested number of bytes to skip |
* @param numBytes The requested number of bytes to skip |
195 |
* |
* |
196 |
* @return The actual number of bytes skipped. |
* @return The actual number of bytes skipped. |
197 |
* |
* |
198 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
199 |
*/ |
*/ |
200 |
public synchronized long skip(long numBytes) throws IOException |
public synchronized long skip(long numBytes) throws IOException |
201 |
{ |
{ |
202 |
if (numBytes < 0) |
if (numBytes < 0) |
212 |
return(newPos-curPos); |
return(newPos-curPos); |
213 |
} |
} |
214 |
|
|
|
/*************************************************************************/ |
|
|
|
|
215 |
/** |
/** |
216 |
* This method reads an unsigned byte from the input stream and returns it |
* This method reads an unsigned byte from the input stream and returns it |
217 |
* as an int in the range of 0-255. This method also will return -1 if |
* as an int in the range of 0-255. This method also will return -1 if |
218 |
* the end of the stream has been reached. |
* the end of the stream has been reached. |
219 |
* <p> |
* <p> |
220 |
* This method will block until the byte can be read. |
* This method will block until the byte can be read. |
221 |
* |
* |
222 |
* @return The byte read or -1 if end of stream |
* @return The byte read or -1 if end of stream |
223 |
* |
* |
224 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
225 |
*/ |
*/ |
226 |
public int read() throws IOException |
public int read() throws IOException |
227 |
{ |
{ |
228 |
return(fd.read()); |
return(fd.read()); |
229 |
} |
} |
230 |
|
|
|
/*************************************************************************/ |
|
|
|
|
231 |
/** |
/** |
232 |
* This method reads bytes from a stream and stores them into a caller |
* This method reads bytes from a stream and stores them into a caller |
233 |
* supplied buffer. This method attempts to completely fill the buffer, |
* supplied buffer. This method attempts to completely fill the buffer, |
234 |
* but can return before doing so. The actual number of bytes read is |
* but can return before doing so. The actual number of bytes read is |
235 |
* returned as an int. A -1 is returned to indicate the end of the stream. |
* returned as an int. A -1 is returned to indicate the end of the stream. |
236 |
* <p> |
* <p> |
237 |
* This method will block until some data can be read. |
* This method will block until some data can be read. |
238 |
* <p> |
* <p> |
239 |
* This method operates by calling an overloaded read method like so: |
* This method operates by calling an overloaded read method like so: |
240 |
* <code>read(buf, 0, buf.length)</code> |
* <code>read(buf, 0, buf.length)</code> |
241 |
* |
* |
242 |
* @param buf The buffer into which the bytes read will be stored. |
* @param buf The buffer into which the bytes read will be stored. |
243 |
* |
* |
244 |
* @return The number of bytes read or -1 if end of stream. |
* @return The number of bytes read or -1 if end of stream. |
245 |
* |
* |
246 |
* @exception IOException If an error occurs. |
* @exception IOException If an error occurs. |
247 |
*/ |
*/ |
248 |
public int read(byte[] buf) throws IOException |
public int read(byte[] buf) throws IOException |
249 |
{ |
{ |
250 |
return(read(buf, 0, buf.length)); |
return(read(buf, 0, buf.length)); |
251 |
} |
} |
252 |
|
|
|
/*************************************************************************/ |
|
|
|
|
253 |
/** |
/** |
254 |
* This method read bytes from a stream and stores them into a caller |
* This method read bytes from a stream and stores them into a caller |
255 |
* supplied buffer. It starts storing the data at index |
* supplied buffer. It starts storing the data at index |
256 |
* <code>offset</code> into |
* <code>offset</code> into |
257 |
* the buffer and attempts to read <code>len</code> bytes. This method can |
* the buffer and attempts to read <code>len</code> bytes. This method can |
258 |
* return before reading the number of bytes requested. The actual number |
* return before reading the number of bytes requested. The actual number |
259 |
* of bytes read is returned as an int. A -1 is returned to indicate the |
* of bytes read is returned as an int. A -1 is returned to indicate the |
260 |
* end of the stream. |
* end of the stream. |
261 |
* <p> |
* <p> |
262 |
* This method will block until some data can be read. |
* This method will block until some data can be read. |
263 |
* |
* |
264 |
* @param buf The array into which the bytes read should be stored |
* @param buf The array into which the bytes read should be stored |
265 |
* @param offset The offset into the array to start storing bytes |
* @param offset The offset into the array to start storing bytes |
266 |
* @param len The requested number of bytes to read |
* @param len The requested number of bytes to read |
267 |
* |
* |
268 |
* @return The actual number of bytes read, or -1 if end of stream. |
* @return The actual number of bytes read, or -1 if end of stream. |
269 |
* |
* |
270 |
* @exception IOException If an error occurs. |
* @exception IOException If an error occurs. |
271 |
*/ |
*/ |
272 |
public int read(byte[] buf, int offset, int len) throws IOException |
public int read(byte[] buf, int offset, int len) throws IOException |
273 |
{ |
{ |
274 |
return(fd.read(buf, offset, len)); |
return(fd.read(buf, offset, len)); |
275 |
} |
} |
276 |
|
|
|
/*************************************************************************/ |
|
|
|
|
277 |
/** |
/** |
278 |
* This method closes the stream. Any futher attempts to read from the |
* This method closes the stream. Any futher attempts to read from the |
279 |
* stream will likely generate an IOException since the underlying file |
* stream will likely generate an IOException since the underlying file |
280 |
* will be closed. |
* will be closed. |
281 |
* |
* |
282 |
* @exception IOException If an error occurs. |
* @exception IOException If an error occurs. |
283 |
*/ |
*/ |
284 |
public void close() throws IOException |
public void close() throws IOException |
285 |
{ |
{ |
286 |
fd.close(); |
fd.close(); |
287 |
} |
} |
288 |
|
|
|
/*************************************************************************/ |
|
|
|
|
289 |
/** |
/** |
290 |
* This method creates a java.nio.channels.FileChannel. |
* This method creates a java.nio.channels.FileChannel. |
291 |
* Nio does not allow one to create a file channel directly. |
* Nio does not allow one to create a file channel directly. |
292 |
* A file channel must be created by first creating an instance of |
* A file channel must be created by first creating an instance of |
293 |
* Input/Output/RandomAccessFile and invoking the getChannel() method on it. |
* Input/Output/RandomAccessFile and invoking the getChannel() method on it. |