FileOutputStream.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /* FileOutputStream.java -- Writes to a file on disk.
  2. Copyright (C) 1998, 2001, 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. * Status: Complete to version 1.1.
  38. */
  39. /**
  40. * This classes allows a stream of data to be written to a disk file or
  41. * any open <code>FileDescriptor</code>.
  42. *
  43. * @author Aaron M. Renn (arenn@urbanophile.com)
  44. * @author Tom Tromey (tromey@cygnus.com)
  45. */
  46. public class FileOutputStream extends OutputStream
  47. {
  48. private FileDescriptor fd;
  49. private final FileChannelImpl ch;
  50. /**
  51. * This method initializes a <code>FileOutputStream</code> object to write
  52. * to the named file. The file is created if it does not exist, and
  53. * the bytes written are written starting at the beginning of the file if
  54. * the <code>append</code> argument is <code>false</code> or at the end
  55. * of the file if the <code>append</code> argument is true.
  56. * <p>
  57. * Before opening a file, a security check is performed by calling the
  58. * <code>checkWrite</code> method of the <code>SecurityManager</code> (if
  59. * one exists) with the name of the file to be opened. An exception is
  60. * thrown if writing is not allowed.
  61. *
  62. * @param path The name of the file this stream should write to
  63. * @param append <code>true</code> to append bytes to the end of the file,
  64. * or <code>false</code> to write bytes to the beginning
  65. *
  66. * @exception SecurityException If write access to the file is not allowed
  67. * @exception FileNotFoundException If a non-security error occurs
  68. */
  69. public FileOutputStream (String path, boolean append)
  70. throws SecurityException, FileNotFoundException
  71. {
  72. this (new File(path), append);
  73. }
  74. /**
  75. * This method initializes a <code>FileOutputStream</code> object to write
  76. * to the named file. The file is created if it does not exist, and
  77. * the bytes written are written starting at the beginning of the file.
  78. * <p>
  79. * Before opening a file, a security check is performed by calling the
  80. * <code>checkWrite</code> method of the <code>SecurityManager</code> (if
  81. * one exists) with the name of the file to be opened. An exception is
  82. * thrown if writing is not allowed.
  83. *
  84. * @param path The name of the file this stream should write to
  85. *
  86. * @exception SecurityException If write access to the file is not allowed
  87. * @exception FileNotFoundException If a non-security error occurs
  88. */
  89. public FileOutputStream (String path)
  90. throws SecurityException, FileNotFoundException
  91. {
  92. this (path, false);
  93. }
  94. /**
  95. * This method initializes a <code>FileOutputStream</code> object to write
  96. * to the specified <code>File</code> object. The file is created if it
  97. * does not exist, and the bytes written are written starting at the
  98. * beginning of the file.
  99. * <p>
  100. * Before opening a file, a security check is performed by calling the
  101. * <code>checkWrite</code> method of the <code>SecurityManager</code> (if
  102. * one exists) with the name of the file to be opened. An exception is
  103. * thrown if writing is not allowed.
  104. *
  105. * @param file The <code>File</code> object this stream should write to
  106. *
  107. * @exception SecurityException If write access to the file is not allowed
  108. * @exception FileNotFoundException If a non-security error occurs
  109. */
  110. public FileOutputStream (File file)
  111. throws SecurityException, FileNotFoundException
  112. {
  113. this (file, false);
  114. }
  115. /**
  116. * This method initializes a <code>FileOutputStream</code> object to write
  117. * to the specified <code>File</code> object. The file is created if it
  118. * does not exist, and the bytes written are written starting at the
  119. * beginning of the file if the <code>append</code> parameter is
  120. * <code>false</code>. Otherwise bytes are written at the end of the
  121. * file.
  122. * <p>
  123. * Before opening a file, a security check is performed by calling the
  124. * <code>checkWrite</code> method of the <code>SecurityManager</code> (if
  125. * one exists) with the name of the file to be opened. An exception is
  126. * thrown if writing is not allowed.
  127. *
  128. * @param file The <code>File</code> object this stream should write to
  129. * @param append <code>true</code> to append bytes to the end of the file,
  130. * or <code>false</code> to write bytes to the beginning
  131. *
  132. * @exception SecurityException If write access to the file is not allowed
  133. * @exception FileNotFoundException If a non-security error occurs
  134. */
  135. public FileOutputStream (File file, boolean append)
  136. throws FileNotFoundException
  137. {
  138. SecurityManager s = System.getSecurityManager();
  139. if (s != null)
  140. s.checkWrite(file.getPath());
  141. try
  142. {
  143. ch = FileChannelImpl.create(file, (append
  144. ? FileChannelImpl.WRITE
  145. | FileChannelImpl.APPEND
  146. : FileChannelImpl.WRITE));
  147. }
  148. catch (FileNotFoundException fnfe)
  149. {
  150. throw fnfe;
  151. }
  152. catch (IOException ioe)
  153. {
  154. FileNotFoundException fnfe = new FileNotFoundException(file.getPath());
  155. fnfe.initCause(ioe);
  156. throw fnfe;
  157. }
  158. }
  159. /**
  160. * This method initializes a <code>FileOutputStream</code> object to write
  161. * to the file represented by the specified <code>FileDescriptor</code>
  162. * object. This method does not create any underlying disk file or
  163. * reposition the file pointer of the given descriptor. It assumes that
  164. * this descriptor is ready for writing as is.
  165. * <p>
  166. * Before opening a file, a security check is performed by calling the
  167. * <code>checkWrite</code> method of the <code>SecurityManager</code> (if
  168. * one exists) with the specified <code>FileDescriptor</code> as an argument.
  169. * An exception is thrown if writing is not allowed.
  170. *
  171. * @param fdObj The <code>FileDescriptor</code> this stream should write to
  172. *
  173. * @exception SecurityException If write access to the file is not allowed
  174. */
  175. public FileOutputStream (FileDescriptor fdObj)
  176. throws SecurityException
  177. {
  178. // Hmm, no other exception but this one to throw, but if the descriptor
  179. // isn't valid, we surely don't have "permission" to write to it.
  180. if (!fdObj.valid())
  181. throw new SecurityException("Invalid FileDescriptor");
  182. SecurityManager s = System.getSecurityManager();
  183. if (s != null)
  184. s.checkWrite(fdObj);
  185. fd = fdObj;
  186. ch = (FileChannelImpl) fdObj.channel;
  187. }
  188. FileOutputStream(FileChannelImpl ch)
  189. {
  190. this.ch = ch;
  191. }
  192. protected void finalize () throws IOException
  193. {
  194. // We don't actually need this, but we include it because it is
  195. // mentioned in the JCL.
  196. }
  197. /**
  198. * This method returns a <code>FileDescriptor</code> object representing
  199. * the file that is currently being written to
  200. *
  201. * @return A <code>FileDescriptor</code> object for this stream
  202. *
  203. * @exception IOException If an error occurs
  204. */
  205. public final FileDescriptor getFD () throws IOException
  206. {
  207. synchronized (this)
  208. {
  209. if (fd == null)
  210. fd = new FileDescriptor (ch);
  211. return fd;
  212. }
  213. }
  214. /**
  215. * This method writes a single byte of data to the file.
  216. *
  217. * @param b The byte of data to write, passed as an <code>int</code>
  218. *
  219. * @exception IOException If an error occurs
  220. */
  221. public void write (int b) throws IOException
  222. {
  223. ch.write (b);
  224. }
  225. /**
  226. * This method writes all the bytes in the specified array to the
  227. * file.
  228. *
  229. * @param buf The array of bytes to write to the file
  230. *
  231. * @exception IOException If an error occurs
  232. */
  233. public void write (byte[] buf)
  234. throws IOException
  235. {
  236. write (buf, 0, buf.length);
  237. }
  238. /**
  239. * This method writes <code>len</code> bytes from the byte array
  240. * <code>buf</code> to the file starting at index <code>offset</code>.
  241. *
  242. * @param buf The array of bytes to write to the file
  243. * @param offset The offset into the array to start writing bytes from
  244. * @param len The number of bytes to write to the file
  245. *
  246. * @exception IOException If an error occurs
  247. */
  248. public void write (byte[] buf, int offset, int len)
  249. throws IOException
  250. {
  251. if (offset < 0
  252. || len < 0
  253. || offset + len > buf.length)
  254. throw new ArrayIndexOutOfBoundsException ();
  255. ch.write(ByteBuffer.wrap(buf, offset, len));
  256. }
  257. /**
  258. * This method closes the underlying file. Any further attempts to
  259. * write to this stream will likely generate an exception since the
  260. * file is closed.
  261. *
  262. * @exception IOException If an error occurs
  263. */
  264. public void close () throws IOException
  265. {
  266. ch.close();
  267. }
  268. /**
  269. * This method creates a java.nio.channels.FileChannel.
  270. * Nio does not allow one to create a file channel directly.
  271. * A file channel must be created by first creating an instance of
  272. * Input/Output/RandomAccessFile and invoking the getChannel() method on it.
  273. */
  274. public synchronized FileChannel getChannel()
  275. {
  276. return ch;
  277. }
  278. } // class FileOutputStream