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