FileDocCategorySizeDatePackage
LazyFileOutputStream.javaAPI DocApache Ant 1.704944Wed Dec 13 06:16:24 GMT 2006org.apache.tools.ant.util

LazyFileOutputStream

public class LazyFileOutputStream extends OutputStream
Class that delays opening the output file until the first bytes shall be written or the method {@link #open open} has been invoked explicitly.
since
Ant 1.6

Fields Summary
private FileOutputStream
fos
private File
file
private boolean
append
private boolean
alwaysCreate
private boolean
opened
private boolean
closed
Constructors Summary
public LazyFileOutputStream(String name)
Creates a stream that will eventually write to the file with the given name and replace it.

param
name the filename.


                              
       
        this(name, false);
    
public LazyFileOutputStream(String name, boolean append)
Creates a stream that will eventually write to the file with the given name and optionally append to instead of replacing it.

param
name the filename.
param
append if true append rather than replace.

        this(new File(name), append);
    
public LazyFileOutputStream(File f)
Creates a stream that will eventually write to the file with the given name and replace it.

param
f the file to create.

        this(f, false);
    
public LazyFileOutputStream(File file, boolean append)
Creates a stream that will eventually write to the file with the given name and optionally append to instead of replacing it.

param
file the file to create.
param
append if true append rather than replace.

        this(file, append, false);
    
public LazyFileOutputStream(File file, boolean append, boolean alwaysCreate)
Creates a stream that will eventually write to the file with the given name, optionally append to instead of replacing it, and optionally always create a file (even if zero length).

param
file the file to create.
param
append if true append rather than replace.
param
alwaysCreate if true create the file even if nothing to write.

        this.file = file;
        this.append = append;
        this.alwaysCreate = alwaysCreate;
    
Methods Summary
public synchronized voidclose()
Close the file.

throws
IOException if there is an error.

        if (alwaysCreate && !closed) {
            ensureOpened();
        }
        if (opened) {
            fos.close();
        }
        closed = true;
    
private synchronized voidensureOpened()

        if (closed) {
            throw new IOException(file + " has already been closed.");
        }

        if (!opened) {
            fos = new FileOutputStream(file.getAbsolutePath(), append);
            opened = true;
        }
    
public voidopen()
Explicitly open the file for writing.

Returns silently if the file has already been opened.

throws
IOException if there is an error.

        ensureOpened();
    
public synchronized voidwrite(int b)
Write a byte.

param
b the byte to write.
throws
IOException if there is a problem.

        ensureOpened();
        fos.write(b);
    
public voidwrite(byte[] b)
Delegates to the three-arg version.

param
b the bytearray to write.
throws
IOException if there is a problem.

        write(b, 0, b.length);
    
public synchronized voidwrite(byte[] b, int offset, int len)
Write part of a byte array.

param
b the byte array.
param
offset write from this index.
param
len the number of bytes to write.
throws
IOException if there is a probem.

        ensureOpened();
        fos.write(b, offset, len);