001: /*
002: * This file is part of DrFTPD, Distributed FTP Daemon.
003: *
004: * DrFTPD is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * DrFTPD is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with DrFTPD; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018: package org.drftpd.io;
019:
020: import java.io.File;
021: import java.io.FileOutputStream;
022: import java.io.IOException;
023: import java.io.OutputStream;
024: import java.io.OutputStreamWriter;
025:
026: import org.apache.log4j.Logger;
027: import org.drftpd.usermanager.javabeans.BeanUser;
028:
029: /**
030: * @author mog
031: * @version $Id: SafeFileOutputStream.java 1513 2006-10-13 22:41:08Z tdsoul $
032: */
033: public class SafeFileOutputStream extends OutputStream {
034: private File _actualFile;
035: private OutputStreamWriter _out;
036: private File _tempFile;
037: private static final Logger logger = Logger
038: .getLogger(SafeFileOutputStream.class);
039: // failed until it works
040: private boolean failed = true;
041:
042: public SafeFileOutputStream(File file) throws IOException {
043: _actualFile = file;
044:
045: if (!_actualFile.getAbsoluteFile().getParentFile().canWrite()) {
046: throw new IOException("Can't write to target dir");
047: }
048:
049: File dir = _actualFile.getParentFile();
050:
051: if (dir == null) {
052: dir = new File(".");
053: }
054:
055: _tempFile = File.createTempFile(_actualFile.getName(), null,
056: dir);
057: _out = new OutputStreamWriter(new FileOutputStream(_tempFile),
058: "UTF-8");
059: }
060:
061: public SafeFileOutputStream(String fileName) throws IOException {
062: this (new File(fileName));
063: }
064:
065: public void close() throws IOException {
066: if (_out == null) {
067: return;
068: }
069: _out.flush();
070: _out.close();
071: _out = null;
072: if (!failed) {
073: Logger.getLogger(SafeFileOutputStream.class).debug(
074: "Renaming " + _tempFile + " (" + _tempFile.length()
075: + ") to " + _actualFile);
076:
077: if (_actualFile.exists() && !_actualFile.delete()) {
078: throw new IOException("delete() failed");
079: }
080:
081: if (!_tempFile.exists()) {
082: throw new IOException("source doesn't exist");
083: }
084:
085: if (!_tempFile.renameTo(_actualFile)) {
086: throw new IOException("renameTo(" + _tempFile + ", "
087: + _actualFile + ") failed");
088: }
089: }
090: }
091:
092: public void flush() throws IOException {
093: _out.flush();
094: }
095:
096: public void write(int b) throws IOException {
097: _out.write(b);
098: // ensures the file gets written to
099: failed = false;
100: }
101: }
|