| 1 | // FileOutputStream.java - Write bytes to a file.
|
|---|
| 2 |
|
|---|
| 3 | /* Copyright (C) 1998, 1999, 2001 Free Software Foundation
|
|---|
| 4 |
|
|---|
| 5 | This file is part of libgcj.
|
|---|
| 6 |
|
|---|
| 7 | This software is copyrighted work licensed under the terms of the
|
|---|
| 8 | Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
|---|
| 9 | details. */
|
|---|
| 10 |
|
|---|
| 11 | package java.io;
|
|---|
| 12 |
|
|---|
| 13 | /**
|
|---|
| 14 | * @author Tom Tromey <[email protected]>
|
|---|
| 15 | * @date September 24, 1998
|
|---|
| 16 | */
|
|---|
| 17 |
|
|---|
| 18 | /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
|
|---|
| 19 | * "The Java Language Specification", ISBN 0-201-63451-1
|
|---|
| 20 | * Status: Complete to version 1.1.
|
|---|
| 21 | */
|
|---|
| 22 |
|
|---|
| 23 | public class FileOutputStream extends OutputStream
|
|---|
| 24 | {
|
|---|
| 25 | public FileOutputStream (String path, boolean append)
|
|---|
| 26 | throws SecurityException, FileNotFoundException
|
|---|
| 27 | {
|
|---|
| 28 | SecurityManager s = System.getSecurityManager();
|
|---|
| 29 | if (s != null)
|
|---|
| 30 | s.checkWrite(path);
|
|---|
| 31 | fd = new FileDescriptor (path, (append
|
|---|
| 32 | ? FileDescriptor.APPEND
|
|---|
| 33 | : FileDescriptor.WRITE));
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | public FileOutputStream (String path)
|
|---|
| 37 | throws SecurityException, FileNotFoundException
|
|---|
| 38 | {
|
|---|
| 39 | this (path, false);
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | public FileOutputStream (File file)
|
|---|
| 43 | throws SecurityException, FileNotFoundException
|
|---|
| 44 | {
|
|---|
| 45 | this (file.getPath(), false);
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | public FileOutputStream (FileDescriptor fdObj)
|
|---|
| 49 | throws SecurityException
|
|---|
| 50 | {
|
|---|
| 51 | SecurityManager s = System.getSecurityManager();
|
|---|
| 52 | if (s != null)
|
|---|
| 53 | s.checkWrite(fdObj);
|
|---|
| 54 | fd = fdObj;
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | protected void finalize () throws IOException
|
|---|
| 58 | {
|
|---|
| 59 | // We don't actually need this, but we include it because it is
|
|---|
| 60 | // mentioned in the JCL.
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | public final FileDescriptor getFD () throws IOException
|
|---|
| 64 | {
|
|---|
| 65 | if (! fd.valid())
|
|---|
| 66 | throw new IOException ();
|
|---|
| 67 | return fd;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | public void write (int b) throws IOException
|
|---|
| 71 | {
|
|---|
| 72 | fd.write (b);
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | public void write (byte[] b) throws IOException, NullPointerException
|
|---|
| 76 | {
|
|---|
| 77 | fd.write (b, 0, b.length);
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | public void write (byte[] b, int off, int len)
|
|---|
| 81 | throws IOException, NullPointerException, IndexOutOfBoundsException
|
|---|
| 82 | {
|
|---|
| 83 | if (off < 0 || len < 0 || off + len > b.length)
|
|---|
| 84 | throw new ArrayIndexOutOfBoundsException ();
|
|---|
| 85 | fd.write (b, off, len);
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | public void close () throws IOException
|
|---|
| 89 | {
|
|---|
| 90 | if (fd.valid())
|
|---|
| 91 | fd.close();
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | // Instance variables.
|
|---|
| 95 | private FileDescriptor fd;
|
|---|
| 96 | }
|
|---|