1. /*
  2. * @(#)FileOutputStream.java 1.39 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 file output stream is an output stream for writing data to a
  13. * <code>File</code> or to a <code>FileDescriptor</code>. Whether or not
  14. * a file is available or may be created depends upon the underlying
  15. * platform. Some platforms, in particular, allow a file to be opened
  16. * for writing by only one <tt>FileOutputStream</tt> (or other
  17. * file-writing object) at a time. In such situations the constructors in
  18. * this class will fail if the file involved is already open.
  19. *
  20. * @author Arthur van Hoff
  21. * @version 1.39, 02/02/00
  22. * @see java.io.File
  23. * @see java.io.FileDescriptor
  24. * @see java.io.FileInputStream
  25. * @since JDK1.0
  26. */
  27. public
  28. class FileOutputStream extends OutputStream
  29. {
  30. /**
  31. * The system dependent file descriptor. The value is
  32. * 1 more than actual file descriptor. This means that
  33. * the default value 0 indicates that the file is not open.
  34. */
  35. private FileDescriptor fd;
  36. /**
  37. * Creates an output file stream to write to the file with the
  38. * specified name. A new <code>FileDescriptor</code> object is
  39. * created to represent this file connection.
  40. * <p>
  41. * First, if there is a security manager, its <code>checkWrite</code>
  42. * method is called with <code>name</code> as its argument.
  43. * <p>
  44. * If the file exists but is a directory rather than a regular file, does
  45. * not exist but cannot be created, or cannot be opened for any other
  46. * reason then a <code>FileNotFoundException</code> is thrown.
  47. *
  48. * @param name the system-dependent filename
  49. * @exception FileNotFoundException if the file exists but is a directory
  50. * rather than a regular file, does not exist but cannot
  51. * be created, or cannot be opened for any other reason
  52. * @exception SecurityException if a security manager exists and its
  53. * <code>checkWrite</code> method denies write access
  54. * to the file.
  55. * @see java.lang.SecurityManager#checkWrite(java.lang.String)
  56. */
  57. public FileOutputStream(String name) throws FileNotFoundException {
  58. this(name, false);
  59. }
  60. /**
  61. * Creates an output file stream to write to the file with the specified
  62. * <code>name</code>. If the second argument is <code>true</code>, then
  63. * bytes will be written to the end of the file rather than the beginning.
  64. * A new <code>FileDescriptor</code> object is created to represent this
  65. * file connection.
  66. * <p>
  67. * First, if there is a security manager, its <code>checkWrite</code>
  68. * method is called with <code>name</code> as its argument.
  69. * <p>
  70. * If the file exists but is a directory rather than a regular file, does
  71. * not exist but cannot be created, or cannot be opened for any other
  72. * reason then a <code>FileNotFoundException</code> is thrown.
  73. *
  74. * @param name the system-dependent file name
  75. * @param append if <code>true</code>, then bytes will be written
  76. * to the end of the file rather than the beginning
  77. * @exception FileNotFoundException if the file exists but is a directory
  78. * rather than a regular file, does not exist but cannot
  79. * be created, or cannot be opened for any other reason.
  80. * @exception SecurityException if a security manager exists and its
  81. * <code>checkWrite</code> method denies write access
  82. * to the file.
  83. * @see java.lang.SecurityManager#checkWrite(java.lang.String)
  84. * @since JDK1.1
  85. */
  86. public FileOutputStream(String name, boolean append)
  87. throws FileNotFoundException
  88. {
  89. SecurityManager security = System.getSecurityManager();
  90. if (security != null) {
  91. security.checkWrite(name);
  92. }
  93. fd = new FileDescriptor();
  94. if (append) {
  95. openAppend(name);
  96. } else {
  97. open(name);
  98. }
  99. }
  100. /**
  101. * Creates a file output stream to write to the file represented by
  102. * the specified <code>File</code> object. A new
  103. * <code>FileDescriptor</code> object is created to represent this
  104. * file connection.
  105. * <p>
  106. * First, if there is a security manager, its <code>checkWrite</code>
  107. * method is called with the path represented by the <code>file</code>
  108. * argument as its argument.
  109. * <p>
  110. * If the file exists but is a directory rather than a regular file, does
  111. * not exist but cannot be created, or cannot be opened for any other
  112. * reason then a <code>FileNotFoundException</code> is thrown.
  113. *
  114. * @param file the file to be opened for writing.
  115. * @exception FileNotFoundException if the file exists but is a directory
  116. * rather than a regular file, does not exist but cannot
  117. * be created, or cannot be opened for any other reason
  118. * @exception SecurityException if a security manager exists and its
  119. * <code>checkWrite</code> method denies write access
  120. * to the file.
  121. * @see java.io.File#getPath()
  122. * @see java.lang.SecurityException
  123. * @see java.lang.SecurityManager#checkWrite(java.lang.String)
  124. */
  125. public FileOutputStream(File file) throws FileNotFoundException {
  126. this(file.getPath());
  127. }
  128. /**
  129. * Creates an output file stream to write to the specified file
  130. * descriptor, which represents an existing connection to an actual
  131. * file in the file system.
  132. * <p>
  133. * First, if there is a security manager, its <code>checkWrite</code>
  134. * method is called with the file descriptor <code>fdObj</code>
  135. * argument as its argument.
  136. *
  137. * @param fdObj the file descriptor to be opened for writing.
  138. * @exception SecurityException if a security manager exists and its
  139. * <code>checkWrite</code> method denies
  140. * write access to the file descriptor.
  141. * @see java.lang.SecurityManager#checkWrite(java.io.FileDescriptor)
  142. */
  143. public FileOutputStream(FileDescriptor fdObj) {
  144. SecurityManager security = System.getSecurityManager();
  145. if (fdObj == null) {
  146. throw new NullPointerException();
  147. }
  148. if (security != null) {
  149. security.checkWrite(fdObj);
  150. }
  151. fd = fdObj;
  152. }
  153. /**
  154. * Opens a file, with the specified name, for writing.
  155. * @param name name of file to be opened
  156. */
  157. private native void open(String name) throws FileNotFoundException;
  158. /**
  159. * Opens a file, with the specified name, for appending.
  160. * @param name name of file to be opened
  161. */
  162. private native void openAppend(String name) throws FileNotFoundException;
  163. /**
  164. * Writes the specified byte to this file output stream. Implements
  165. * the <code>write</code> method of <code>OutputStream</code>.
  166. *
  167. * @param b the byte to be written.
  168. * @exception IOException if an I/O error occurs.
  169. */
  170. public native void write(int b) throws IOException;
  171. /**
  172. * Writes a sub array as a sequence of bytes.
  173. * @param b the data to be written
  174. * @param off the start offset in the data
  175. * @param len the number of bytes that are written
  176. * @exception IOException If an I/O error has occurred.
  177. */
  178. private native void writeBytes(byte b[], int off, int len) throws IOException;
  179. /**
  180. * Writes <code>b.length</code> bytes from the specified byte array
  181. * to this file output stream.
  182. *
  183. * @param b the data.
  184. * @exception IOException if an I/O error occurs.
  185. */
  186. public void write(byte b[]) throws IOException {
  187. writeBytes(b, 0, b.length);
  188. }
  189. /**
  190. * Writes <code>len</code> bytes from the specified byte array
  191. * starting at offset <code>off</code> to this file output stream.
  192. *
  193. * @param b the data.
  194. * @param off the start offset in the data.
  195. * @param len the number of bytes to write.
  196. * @exception IOException if an I/O error occurs.
  197. */
  198. public void write(byte b[], int off, int len) throws IOException {
  199. writeBytes(b, off, len);
  200. }
  201. /**
  202. * Closes this file output stream and releases any system resources
  203. * associated with this stream. This file output stream may no longer
  204. * be used for writing bytes.
  205. *
  206. * @exception IOException if an I/O error occurs.
  207. */
  208. public native void close() throws IOException;
  209. /**
  210. * Returns the file descriptor associated with this stream.
  211. *
  212. * @return the <code>FileDescriptor</code> object that represents
  213. * the connection to the file in the file system being used
  214. * by this <code>FileOutputStream</code> object.
  215. *
  216. * @exception IOException if an I/O error occurs.
  217. * @see java.io.FileDescriptor
  218. */
  219. public final FileDescriptor getFD() throws IOException {
  220. if (fd != null) return fd;
  221. throw new IOException();
  222. }
  223. /**
  224. * Cleans up the connection to the file, and ensures that the
  225. * <code>close</code> method of this file output stream is
  226. * called when there are no more references to this stream.
  227. *
  228. * @exception IOException if an I/O error occurs.
  229. * @see java.io.FileInputStream#close()
  230. */
  231. protected void finalize() throws IOException {
  232. if (fd != null) {
  233. if (fd == fd.out || fd == fd.err) {
  234. flush();
  235. } else {
  236. close();
  237. }
  238. }
  239. }
  240. private static native void initIDs();
  241. static {
  242. initIDs();
  243. }
  244. }