KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > util > LazyFileOutputStream


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.util;
19
20 import java.io.File JavaDoc;
21 import java.io.FileOutputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.OutputStream JavaDoc;
24
25 /**
26  * Class that delays opening the output file until the first bytes
27  * shall be written or the method {@link #open open} has been invoked
28  * explicitly.
29  *
30  * @since Ant 1.6
31  */

32 public class LazyFileOutputStream extends OutputStream JavaDoc {
33
34     private FileOutputStream JavaDoc fos;
35     private File JavaDoc file;
36     private boolean append;
37     private boolean alwaysCreate;
38     private boolean opened = false;
39     private boolean closed = false;
40
41     /**
42      * Creates a stream that will eventually write to the file with
43      * the given name and replace it.
44      * @param name the filename.
45      */

46     public LazyFileOutputStream(String JavaDoc name) {
47         this(name, false);
48     }
49
50     /**
51      * Creates a stream that will eventually write to the file with
52      * the given name and optionally append to instead of replacing
53      * it.
54      * @param name the filename.
55      * @param append if true append rather than replace.
56      */

57     public LazyFileOutputStream(String JavaDoc name, boolean append) {
58         this(new File JavaDoc(name), append);
59     }
60
61     /**
62      * Creates a stream that will eventually write to the file with
63      * the given name and replace it.
64      * @param f the file to create.
65      */

66     public LazyFileOutputStream(File JavaDoc f) {
67         this(f, false);
68     }
69
70     /**
71      * Creates a stream that will eventually write to the file with
72      * the given name and optionally append to instead of replacing
73      * it.
74      * @param file the file to create.
75      * @param append if true append rather than replace.
76      */

77     public LazyFileOutputStream(File JavaDoc file, boolean append) {
78         this(file, append, false);
79     }
80
81     /**
82      * Creates a stream that will eventually write to the file with
83      * the given name, optionally append to instead of replacing
84      * it, and optionally always create a file (even if zero length).
85      * @param file the file to create.
86      * @param append if true append rather than replace.
87      * @param alwaysCreate if true create the file even if nothing to write.
88      */

89     public LazyFileOutputStream(File JavaDoc file, boolean append,
90                                 boolean alwaysCreate) {
91         this.file = file;
92         this.append = append;
93         this.alwaysCreate = alwaysCreate;
94     }
95
96     /**
97      * Explicitly open the file for writing.
98      *
99      * <p>Returns silently if the file has already been opened.</p>
100      * @throws IOException if there is an error.
101      */

102     public void open() throws IOException JavaDoc {
103         ensureOpened();
104     }
105
106     /**
107      * Close the file.
108      * @throws IOException if there is an error.
109      */

110     public synchronized void close() throws IOException JavaDoc {
111         if (alwaysCreate && !closed) {
112             ensureOpened();
113         }
114         if (opened) {
115             fos.close();
116         }
117         closed = true;
118     }
119
120     /**
121      * Delegates to the three-arg version.
122      * @param b the bytearray to write.
123      * @throws IOException if there is a problem.
124      */

125     public void write(byte[] b) throws IOException JavaDoc {
126         write(b, 0, b.length);
127     }
128
129     /**
130      * Write part of a byte array.
131      * @param b the byte array.
132      * @param offset write from this index.
133      * @param len the number of bytes to write.
134      * @throws IOException if there is a probem.
135      */

136     public synchronized void write(byte[] b, int offset, int len)
137         throws IOException JavaDoc {
138         ensureOpened();
139         fos.write(b, offset, len);
140     }
141
142     /**
143      * Write a byte.
144      * @param b the byte to write.
145      * @throws IOException if there is a problem.
146      */

147     public synchronized void write(int b) throws IOException JavaDoc {
148         ensureOpened();
149         fos.write(b);
150     }
151
152     private synchronized void ensureOpened() throws IOException JavaDoc {
153         if (closed) {
154             throw new IOException JavaDoc(file + " has already been closed.");
155         }
156
157         if (!opened) {
158             fos = new FileOutputStream JavaDoc(file.getAbsolutePath(), append);
159             opened = true;
160         }
161     }
162 }
163
Popular Tags