source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/util/LazyFileOutputStream.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 3.8 KB
Line 
1/*
2 * Copyright 2003-2005 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17package org.apache.tools.ant.util;
18
19import java.io.File;
20import java.io.FileOutputStream;
21import java.io.IOException;
22import java.io.OutputStream;
23
24/**
25 * Class that delays opening the output file until the first bytes
26 * shall be written or the method {@link #open open} has been invoked
27 * explicitly.
28 *
29 * @since Ant 1.6
30 */
31public class LazyFileOutputStream extends OutputStream {
32
33 private FileOutputStream fos;
34 private File file;
35 private boolean append;
36 private boolean alwaysCreate;
37 private boolean opened = false;
38 private boolean closed = false;
39
40 /**
41 * Creates a stream that will eventually write to the file with
42 * the given name and replace it.
43 */
44 public LazyFileOutputStream(String name) {
45 this(name, false);
46 }
47
48 /**
49 * Creates a stream that will eventually write to the file with
50 * the given name and optionally append to instead of replacing
51 * it.
52 */
53 public LazyFileOutputStream(String name, boolean append) {
54 this(new File(name), append);
55 }
56
57 /**
58 * Creates a stream that will eventually write to the file with
59 * the given name and replace it.
60 */
61 public LazyFileOutputStream(File f) {
62 this(f, false);
63 }
64
65 /**
66 * Creates a stream that will eventually write to the file with
67 * the given name and optionally append to instead of replacing
68 * it.
69 */
70 public LazyFileOutputStream(File file, boolean append) {
71 this(file, append, false);
72 }
73
74 /**
75 * Creates a stream that will eventually write to the file with
76 * the given name, optionally append to instead of replacing
77 * it, and optionally always create a file (even if zero length).
78 */
79 public LazyFileOutputStream(File file, boolean append,
80 boolean alwaysCreate) {
81 this.file = file;
82 this.append = append;
83 this.alwaysCreate = alwaysCreate;
84 }
85
86 /**
87 * Explicitly open the file for writing.
88 *
89 * <p>Returns silently if the file has already been opened.</p>
90 */
91 public void open() throws IOException {
92 ensureOpened();
93 }
94
95 public synchronized void close() throws IOException {
96 if (alwaysCreate && !closed) {
97 ensureOpened();
98 }
99 if (opened) {
100 fos.close();
101 }
102 closed = true;
103 }
104
105 /**
106 * Delegates to the three-arg version.
107 */
108 public void write(byte[] b) throws IOException {
109 write(b, 0, b.length);
110 }
111
112 //inherit doc
113 public synchronized void write(byte[] b, int offset, int len)
114 throws IOException {
115 ensureOpened();
116 fos.write(b, offset, len);
117 }
118
119 //inherit doc
120 public synchronized void write(int b) throws IOException {
121 ensureOpened();
122 fos.write(b);
123 }
124
125 private synchronized void ensureOpened() throws IOException {
126 if (closed) {
127 throw new IOException(file + " has already been closed.");
128 }
129
130 if (!opened) {
131 fos = new FileOutputStream(file.getAbsolutePath(), append);
132 opened = true;
133 }
134 }
135}
Note: See TracBrowser for help on using the repository browser.