File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\util\io\ReopenableFileOutputStream.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2010, CloudBees, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.util.io;
25
26 import hudson.util.IOException2;
27
28 import java.io.File;
29 import java.io.FileNotFoundException;
30 import java.io.FileOutputStream;
31 import java.io.IOException;
32 import java.io.OutputStream;
33
34 /**
35 * {@link OutputStream} that writes to a file.
36 *
37 * <p>
38 * Unlike regular {@link FileOutputStream}, this implementation allows the caller to close,
39 * and then keep writing.
40 *
41 * @author Kohsuke Kawaguchi
42 */
43 public class ReopenableFileOutputStream extends OutputStream {
44 protected final File out;
45
46 private OutputStream current;
47 private boolean appendOnNextOpen = false;
48
49 public ReopenableFileOutputStream(File out) {
50 this.out = out;
51 }
52
53 private synchronized OutputStream current() throws IOException {
54 if (current==null)
55 try {
56 current = new FileOutputStream(out,appendOnNextOpen);
57 } catch (FileNotFoundException e) {
58 throw new IOException2("Failed to open "+out,e);
59 }
60 return current;
61 }
62
63 @Override
64 public void write(int b) throws IOException {
65 current().write(b);
66 }
67
68 @Override
69 public void write(byte[] b) throws IOException {
70 current().write(b);
71 }
72
73 @Override
74 public void write(byte[] b, int off, int len) throws IOException {
75 current().write(b, off, len);
76 }
77
78 @Override
79 public void flush() throws IOException {
80 current().flush();
81 }
82
83 @Override
84 public synchronized void close() throws IOException {
85 if (current!=null) {
86 current.close();
87 appendOnNextOpen = true;
88 current = null;
89 }
90 }
91
92 /**
93 * In addition to close, ensure that the next "open" would truncate the file.
94 */
95 public synchronized void rewind() throws IOException {
96 close();
97 appendOnNextOpen = false;
98 }
99 }
100