1. /*
  2. * @(#)FileInputStream.java 1.45 00/02/02
  3. *
  4. * Copyright 1994-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.io;
  11. /**
  12. * A <code>FileInputStream</code> obtains input bytes
  13. * from a file in a file system. What files
  14. * are available depends on the host environment.
  15. *
  16. * @author Arthur van Hoff
  17. * @version 1.45, 02/02/00
  18. * @see java.io.File
  19. * @see java.io.FileDescriptor
  20. * @see java.io.FileOutputStream
  21. * @since JDK1.0
  22. */
  23. public
  24. class FileInputStream extends InputStream
  25. {
  26. /* File Descriptor - handle to the open file */
  27. private FileDescriptor fd;
  28. /**
  29. * Creates a <code>FileInputStream</code> by
  30. * opening a connection to an actual file,
  31. * the file named by the path name <code>name</code>
  32. * in the file system. A new <code>FileDescriptor</code>
  33. * object is created to represent this file
  34. * connection.
  35. * <p>
  36. * First, if there is a security
  37. * manager, its <code>checkRead</code> method
  38. * is called with the <code>name</code> argument
  39. * as its argument.
  40. * <p>
  41. * If the named file does not exist, is a directory rather than a regular
  42. * file, or for some other reason cannot be opened for reading then a
  43. * <code>FileNotFoundException</code> is thrown.
  44. *
  45. * @param name the system-dependent file name.
  46. * @exception FileNotFoundException if the file does not exist,
  47. * is a directory rather than a regular file,
  48. * or for some other reason cannot be opened for
  49. * reading.
  50. * @exception SecurityException if a security manager exists and its
  51. * <code>checkRead</code> method denies read access
  52. * to the file.
  53. * @see java.lang.SecurityManager#checkRead(java.lang.String)
  54. */
  55. public FileInputStream(String name) throws FileNotFoundException {
  56. SecurityManager security = System.getSecurityManager();
  57. if (security != null) {
  58. security.checkRead(name);
  59. }
  60. fd = new FileDescriptor();
  61. open(name);
  62. }
  63. /**
  64. * Creates a <code>FileInputStream</code> by
  65. * opening a connection to an actual file,
  66. * the file named by the <code>File</code>
  67. * object <code>file</code> in the file system.
  68. * A new <code>FileDescriptor</code> object
  69. * is created to represent this file connection.
  70. * <p>
  71. * First, if there is a security manager,
  72. * its <code>checkRead</code> method is called
  73. * with the path represented by the <code>file</code>
  74. * argument 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
  78. * <code>FileNotFoundException</code> is thrown.
  79. *
  80. * @param file the file to be opened for reading.
  81. * @exception FileNotFoundException if the file does not exist,
  82. * is a directory rather than a regular file,
  83. * or for some other reason cannot be opened for
  84. * reading.
  85. * @exception SecurityException if a security manager exists and its
  86. * <code>checkRead</code> method denies read access to the file.
  87. * @see java.io.File#getPath()
  88. * @see java.lang.SecurityManager#checkRead(java.lang.String)
  89. */
  90. public FileInputStream(File file) throws FileNotFoundException {
  91. this(file.getPath());
  92. }
  93. /**
  94. * Creates a <code>FileInputStream</code> by using the file descriptor
  95. * <code>fdObj</code>, which represents an existing connection to an
  96. * actual file in the file system.
  97. * <p>
  98. * If there is a security manager, its <code>checkRead</code> method is
  99. * called with the file descriptor <code>fdObj</code> as its argument to
  100. * see if it's ok to read the file descriptor. If read access is denied
  101. * to the file descriptor a <code>SecurityException</code> is thrown.
  102. * <p>
  103. * If <code>fdObj</code> is null then a <code>NullPointerException</code>
  104. * is thrown.
  105. *
  106. * @param fdObj the file descriptor to be opened for reading.
  107. * @throws SecurityException if a security manager exists and its
  108. * <code>checkRead</code> method denies read access to the
  109. * file descriptor.
  110. * @see SecurityManager#checkRead(java.io.FileDescriptor)
  111. */
  112. public FileInputStream(FileDescriptor fdObj) {
  113. SecurityManager security = System.getSecurityManager();
  114. if (fdObj == null) {
  115. throw new NullPointerException();
  116. }
  117. if (security != null) {
  118. security.checkRead(fdObj);
  119. }
  120. fd = fdObj;
  121. }
  122. /**
  123. * Opens the specified file for reading.
  124. * @param name the name of the file
  125. */
  126. private native void open(String name) throws FileNotFoundException;
  127. /**
  128. * Reads a byte of data from this input stream. This method blocks
  129. * if no input is yet available.
  130. *
  131. * @return the next byte of data, or <code>-1</code> if the end of the
  132. * file is reached.
  133. * @exception IOException if an I/O error occurs.
  134. */
  135. public native int read() throws IOException;
  136. /**
  137. * Reads a subarray as a sequence of bytes.
  138. * @param b the data to be written
  139. * @param off the start offset in the data
  140. * @param len the number of bytes that are written
  141. * @exception IOException If an I/O error has occurred.
  142. */
  143. private native int readBytes(byte b[], int off, int len) throws IOException;
  144. /**
  145. * Reads up to <code>b.length</code> bytes of data from this input
  146. * stream into an array of bytes. This method blocks until some input
  147. * is available.
  148. *
  149. * @param b the buffer into which the data is read.
  150. * @return the total number of bytes read into the buffer, or
  151. * <code>-1</code> if there is no more data because the end of
  152. * the file has been reached.
  153. * @exception IOException if an I/O error occurs.
  154. */
  155. public int read(byte b[]) throws IOException {
  156. return readBytes(b, 0, b.length);
  157. }
  158. /**
  159. * Reads up to <code>len</code> bytes of data from this input stream
  160. * into an array of bytes. This method blocks until some input is
  161. * available.
  162. *
  163. * @param b the buffer into which the data is read.
  164. * @param off the start offset of the data.
  165. * @param len the maximum number of bytes read.
  166. * @return the total number of bytes read into the buffer, or
  167. * <code>-1</code> if there is no more data because the end of
  168. * the file has been reached.
  169. * @exception IOException if an I/O error occurs.
  170. */
  171. public int read(byte b[], int off, int len) throws IOException {
  172. return readBytes(b, off, len);
  173. }
  174. /**
  175. * Skips over and discards <code>n</code> bytes of data from the
  176. * input stream. The <code>skip</code> method may, for a variety of
  177. * reasons, end up skipping over some smaller number of bytes,
  178. * possibly <code>0</code>. The actual number of bytes skipped is returned.
  179. *
  180. * @param n the number of bytes to be skipped.
  181. * @return the actual number of bytes skipped.
  182. * @exception IOException if an I/O error occurs.
  183. */
  184. public native long skip(long n) throws IOException;
  185. /**
  186. * Returns the number of bytes that can be read from this file input
  187. * stream without blocking.
  188. *
  189. * @return the number of bytes that can be read from this file input
  190. * stream without blocking.
  191. * @exception IOException if an I/O error occurs.
  192. */
  193. public native int available() throws IOException;
  194. /**
  195. * Closes this file input stream and releases any system resources
  196. * associated with the stream.
  197. *
  198. * @exception IOException if an I/O error occurs.
  199. */
  200. public native void close() throws IOException;
  201. /**
  202. * Returns the <code>FileDescriptor</code>
  203. * object that represents the connection to
  204. * the actual file in the file system being
  205. * used by this <code>FileInputStream</code>.
  206. *
  207. * @return the file descriptor object associated with this stream.
  208. * @exception IOException if an I/O error occurs.
  209. * @see java.io.FileDescriptor
  210. */
  211. public final FileDescriptor getFD() throws IOException {
  212. if (fd != null) return fd;
  213. throw new IOException();
  214. }
  215. private static native void initIDs();
  216. static {
  217. initIDs();
  218. }
  219. /**
  220. * Ensures that the <code>close</code> method of this file input stream is
  221. * called when there are no more references to it.
  222. *
  223. * @exception IOException if an I/O error occurs.
  224. * @see java.io.FileInputStream#close()
  225. */
  226. protected void finalize() throws IOException {
  227. if (fd != null) {
  228. if (fd != fd.in) {
  229. close();
  230. }
  231. }
  232. }
  233. }