KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > drftpd > util > SafeFileWriter


1 /*
2  * This file is part of DrFTPD, Distributed FTP Daemon.
3  *
4  * DrFTPD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * DrFTPD is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with DrFTPD; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package net.sf.drftpd.util;
19
20 import java.io.File JavaDoc;
21 import java.io.FileOutputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.OutputStreamWriter JavaDoc;
24 import java.io.Writer JavaDoc;
25
26 import org.apache.log4j.Logger;
27
28 /**
29  * @author mog
30  * @version $Id: SafeFileWriter.java,v 1.7 2004/04/22 02:10:12 mog Exp $
31  */

32 public class SafeFileWriter extends Writer JavaDoc {
33     private File JavaDoc _actualFile;
34     private OutputStreamWriter JavaDoc _out;
35     private File JavaDoc _tempFile;
36     private boolean failed = false;
37
38     /**
39      * @see java.io.File#File(java.io.File)
40      */

41     public SafeFileWriter(File JavaDoc file) throws IOException JavaDoc {
42         _actualFile = file;
43         if (!_actualFile.getAbsoluteFile().getParentFile().canWrite())
44             throw new IOException JavaDoc("Can't write to target dir");
45         
46         File JavaDoc dir = _actualFile.getParentFile();
47         if(dir == null) dir = new File JavaDoc(".");
48         _tempFile =
49             File.createTempFile(
50                 _actualFile.getName(),
51                 null,
52                 dir);
53         _out = new OutputStreamWriter JavaDoc(new FileOutputStream JavaDoc(_tempFile), "UTF-8");
54     }
55
56     /**
57      * @see java.io.File#File(java.lang.String)
58      */

59     public SafeFileWriter(String JavaDoc fileName) throws IOException JavaDoc {
60         this(new File JavaDoc(fileName));
61     }
62
63     public void close() throws IOException JavaDoc {
64         _out.flush();
65         _out.close();
66         if (!failed) {
67             Logger.getLogger(SafeFileWriter.class).debug("Renaming "+_tempFile+" ("+_tempFile.length()+") to "+_actualFile);
68             if (_actualFile.exists() && !_actualFile.delete())
69                 throw new IOException JavaDoc("delete() failed");
70             if (!_tempFile.exists())
71                 throw new IOException JavaDoc("source doesn't exist");
72             if (!_tempFile.renameTo(_actualFile))
73                 throw new IOException JavaDoc(
74                     "renameTo(" + _tempFile + ", " + _actualFile + ") failed");
75         }
76     }
77
78     public void flush() throws IOException JavaDoc {
79         _out.flush();
80     }
81
82     public void write(char[] cbuf, int off, int len) throws IOException JavaDoc {
83         try {
84             _out.write(cbuf, off, len);
85         } catch (IOException JavaDoc e) {
86             failed = true;
87             throw e;
88         }
89     }
90 }
91
Popular Tags