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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
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;
25
26 import java.io.BufferedWriter;
27 import java.io.File;
28 import java.io.FileOutputStream;
29 import java.io.FileWriter;
30 import java.io.IOException;
31 import java.io.OutputStreamWriter;
32 import java.io.Writer;
33 import java.nio.charset.Charset;
34
35 /**
36 * Buffered {@link FileWriter} that uses UTF-8.
37 *
38 * <p>
39 * The write operation is atomic when used for overwriting;
40 * it either leaves the original file intact, or it completely rewrites it with new contents.
41 *
42 * @author Kohsuke Kawaguchi
43 */
44 public class AtomicFileWriter extends Writer {
45
46 private final Writer core;
47 private final File tmpFile;
48 private final File destFile;
49
50 /**
51 * Writes with UTF-8 encoding.
52 */
53 public AtomicFileWriter(File f) throws IOException {
54 this(f,"UTF-8");
55 }
56
57 /**
58 * @param encoding
59 * File encoding to write. If null, platform default encoding is chosen.
60 */
61 public AtomicFileWriter(File f, String encoding) throws IOException {
62 File dir = f.getParentFile();
63 try {
64 dir.mkdirs();
65 tmpFile = File.createTempFile("atomic",null, dir);
66 } catch (IOException e) {
67 throw new IOException2("Failed to create a temporary file in "+ dir,e);
68 }
69 destFile = f;
70 if (encoding==null)
71 encoding = Charset.defaultCharset().name();
72 core = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tmpFile),encoding));
73 }
74
75 @Override
76 public void write(int c) throws IOException {
77 core.write(c);
78 }
79
80 @Override
81 public void write(String str, int off, int len) throws IOException {
82 core.write(str,off,len);
83 }
84
85 public void write(char cbuf[], int off, int len) throws IOException {
86 core.write(cbuf,off,len);
87 }
88
89 public void flush() throws IOException {
90 core.flush();
91 }
92
93 public void close() throws IOException {
94 core.close();
95 }
96
97 /**
98 * When the write operation failed, call this method to
99 * leave the original file intact and remove the temporary file.
100 * This method can be safely invoked from the "finally" block, even after
101 * the {@link #commit()} is called, to simplify coding.
102 */
103 public void abort() throws IOException {
104 close();
105 tmpFile.delete();
106 }
107
108 public void commit() throws IOException {
109 close();
110 if(destFile.exists() && !destFile.delete()) {
111 tmpFile.delete();
112 throw new IOException("Unable to delete "+destFile);
113 }
114 tmpFile.renameTo(destFile);
115 }
116
117 @Override
118 protected void finalize() throws Throwable {
119 // one way or the other, temporary file should be deleted.
120 close();
121 tmpFile.delete();
122 }
123
124 /**
125 * Until the data is committed, this file captures
126 * the written content.
127 */
128 public File getTemporaryFile() {
129 return tmpFile;
130 }
131 }
132