FileInputStream.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /* FileInputStream.java -- An input stream that reads from disk files.
  2. Copyright (C) 1998, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.io;
  32. import gnu.java.nio.FileChannelImpl;
  33. import java.nio.ByteBuffer;
  34. import java.nio.channels.FileChannel;
  35. /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
  36. * "The Java Language Specification", ISBN 0-201-63451-1
  37. * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
  38. * Status: Believed complete and correct.
  39. */
  40. /**
  41. * This class is a stream that reads its bytes from a file.
  42. *
  43. * @author Aaron M. Renn (arenn@urbanophile.com)
  44. * @author Warren Levy (warrenl@cygnus.com)
  45. */
  46. public class FileInputStream extends InputStream
  47. {
  48. /**
  49. * This is the native file handle for the file this stream is reading from
  50. */
  51. private FileDescriptor fd;
  52. private FileChannelImpl ch;
  53. /**
  54. * This method initializes a <code>FileInputStream</code> to read from the
  55. * specified named file. A security check is first made to determine
  56. * whether or not access to this file is allowed. This is done by
  57. * calling the <code>checkRead()</code> method of the
  58. * <code>SecurityManager</code>
  59. * (if one exists) with the name of this file. An exception is thrown
  60. * if reading is not allowed. If the file does not exist, an exception
  61. * is also thrown.
  62. *
  63. * @param name The name of the file this stream should read from
  64. *
  65. * @exception SecurityException If read access to the file is not allowed
  66. * @exception FileNotFoundException If the file does not exist
  67. * or if it is a directory
  68. */
  69. public FileInputStream(String name) throws FileNotFoundException
  70. {
  71. this(new File(name));
  72. }
  73. /**
  74. * This method initializes a <code>FileInputStream</code> to read from the
  75. * specified <code>File</code> object. A security check is first
  76. * made to determine
  77. * whether or not access to this file is allowed. This is done by
  78. * calling the <code>checkRead()</code> method of the
  79. * <code>SecurityManager</code>
  80. * (if one exists) with the name of this file. An exception is thrown
  81. * if reading is not allowed. If the file does not exist, an exception
  82. * is also thrown.
  83. *
  84. * @param file The <code>File</code> object this stream should read from
  85. *
  86. * @exception SecurityException If read access to the file is not allowed
  87. * @exception FileNotFoundException If the file does not exist
  88. * or if it is a directory.
  89. */
  90. public FileInputStream(File file) throws FileNotFoundException
  91. {
  92. SecurityManager s = System.getSecurityManager();
  93. if (s != null)
  94. s.checkRead(file.getPath());
  95. try
  96. {
  97. ch = FileChannelImpl.create(file, FileChannelImpl.READ);
  98. }
  99. catch (FileNotFoundException fnfe)
  100. {
  101. throw fnfe;
  102. }
  103. catch (IOException ioe)
  104. {
  105. FileNotFoundException fnfe = new FileNotFoundException(file.getPath());
  106. fnfe.initCause(ioe);
  107. throw fnfe;
  108. }
  109. }
  110. /**
  111. * This method initializes a <code>FileInputStream</code> to read from the
  112. * specified <code>FileDescriptor</code> object. A security
  113. * check is first made to
  114. * determine whether or not access to this file is allowed. This is done by
  115. * calling the <code>checkRead()</code> method of the
  116. * <code>SecurityManager</code>
  117. * (if one exists) with the specified <code>FileDescriptor</code>
  118. * An exception is
  119. * thrown if reading is not allowed.
  120. *
  121. * @param fdObj The <code>FileDescriptor</code> object this stream
  122. * should read from
  123. *
  124. * @exception SecurityException If read access to the file is not allowed
  125. */
  126. public FileInputStream(FileDescriptor fdObj)
  127. {
  128. SecurityManager s = System.getSecurityManager();
  129. if (s != null)
  130. s.checkRead(fdObj);
  131. fd = fdObj;
  132. ch = (FileChannelImpl) fdObj.channel;
  133. }
  134. FileInputStream(FileChannelImpl ch)
  135. {
  136. this.ch = ch;
  137. }
  138. /**
  139. * This method returns the number of bytes that can be read from this
  140. * stream before a read can block. A return of 0 indicates that blocking
  141. * might (or might not) occur on the very next read attempt.
  142. * <p>
  143. * This method returns the number of unread bytes remaining in the file if
  144. * the descriptor being read from is an actual file. If this method is
  145. * reading from a ''special'' file such a the standard input, this method
  146. * will return the appropriate value for the stream being read.
  147. * <p>
  148. * Be aware that reads on plain files that do not reside locally might
  149. * possibly block even if this method says they should not. For example,
  150. * a remote server might crash, preventing an NFS mounted file from being
  151. * read.
  152. *
  153. * @return The number of bytes that can be read before blocking could occur
  154. *
  155. * @exception IOException If an error occurs
  156. */
  157. public int available() throws IOException
  158. {
  159. return ch.available();
  160. }
  161. /**
  162. * This method closes the stream. Any futher attempts to read from the
  163. * stream will likely generate an IOException since the underlying file
  164. * will be closed.
  165. *
  166. * @exception IOException If an error occurs.
  167. */
  168. public void close() throws IOException
  169. {
  170. ch.close();
  171. }
  172. protected void finalize() throws IOException
  173. {
  174. // We don't actually need this, but we include it because it is
  175. // mentioned in the JCL.
  176. }
  177. /**
  178. * This method returns a <code>FileDescriptor</code> object representing the
  179. * underlying native file handle of the file this stream is reading
  180. * from
  181. *
  182. * @return A <code>FileDescriptor</code> for this stream
  183. *
  184. * @exception IOException If an error occurs
  185. */
  186. public final FileDescriptor getFD() throws IOException
  187. {
  188. synchronized (this)
  189. {
  190. if (fd == null)
  191. fd = new FileDescriptor (ch);
  192. return fd;
  193. }
  194. }
  195. /**
  196. * This method reads an unsigned byte from the input stream and returns it
  197. * as an int in the range of 0-255. This method also will return -1 if
  198. * the end of the stream has been reached.
  199. * <p>
  200. * This method will block until the byte can be read.
  201. *
  202. * @return The byte read or -1 if end of stream
  203. *
  204. * @exception IOException If an error occurs
  205. */
  206. public int read() throws IOException
  207. {
  208. return ch.read();
  209. }
  210. /**
  211. * This method reads bytes from a stream and stores them into a caller
  212. * supplied buffer. This method attempts to completely fill the buffer,
  213. * but can return before doing so. The actual number of bytes read is
  214. * returned as an int. A -1 is returned to indicate the end of the stream.
  215. * <p>
  216. * This method will block until some data can be read.
  217. * <p>
  218. * This method operates by calling an overloaded read method like so:
  219. * <code>read(buf, 0, buf.length)</code>
  220. *
  221. * @param buf The buffer into which the bytes read will be stored.
  222. *
  223. * @return The number of bytes read or -1 if end of stream.
  224. *
  225. * @exception IOException If an error occurs.
  226. */
  227. public int read(byte[] buf) throws IOException
  228. {
  229. return read(buf, 0, buf.length);
  230. }
  231. /**
  232. * This method read bytes from a stream and stores them into a caller
  233. * supplied buffer. It starts storing the data at index
  234. * <code>offset</code> into
  235. * the buffer and attempts to read <code>len</code> bytes. This method can
  236. * return before reading the number of bytes requested. The actual number
  237. * of bytes read is returned as an int. A -1 is returned to indicate the
  238. * end of the stream.
  239. * <p>
  240. * This method will block until some data can be read.
  241. *
  242. * @param buf The array into which the bytes read should be stored
  243. * @param offset The offset into the array to start storing bytes
  244. * @param len The requested number of bytes to read
  245. *
  246. * @return The actual number of bytes read, or -1 if end of stream.
  247. *
  248. * @exception IOException If an error occurs.
  249. */
  250. public int read(byte[] buf, int offset, int len) throws IOException
  251. {
  252. if (offset < 0
  253. || len < 0
  254. || offset + len > buf.length)
  255. throw new ArrayIndexOutOfBoundsException();
  256. return ch.read(ByteBuffer.wrap(buf, offset, len));
  257. }
  258. /**
  259. * This method skips the specified number of bytes in the stream. It
  260. * returns the actual number of bytes skipped, which may be less than the
  261. * requested amount.
  262. * <p>
  263. * @param numBytes The requested number of bytes to skip
  264. *
  265. * @return The actual number of bytes skipped.
  266. *
  267. * @exception IOException If an error occurs
  268. */
  269. public synchronized long skip (long numBytes) throws IOException
  270. {
  271. if (numBytes < 0)
  272. throw new IllegalArgumentException ("Can't skip negative bytes: " +
  273. numBytes);
  274. if (numBytes == 0)
  275. return 0;
  276. long oldPos = ch.position ();
  277. ch.position(oldPos + numBytes);
  278. return ch.position() - oldPos;
  279. }
  280. /**
  281. * This method creates a java.nio.channels.FileChannel.
  282. * Nio does not allow one to create a file channel directly.
  283. * A file channel must be created by first creating an instance of
  284. * Input/Output/RandomAccessFile and invoking the getChannel() method on it.
  285. */
  286. public synchronized FileChannel getChannel ()
  287. {
  288. return ch;
  289. }
  290. } // class FileInputStream