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