001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.recovery;
015
016import java.io.*;
017import java.nio.channels.FileChannel;
018
019public class ResilientFileOutputStream extends ResilientOutputStreamBase {
020
021    private File file;
022    private FileOutputStream fos;
023
024    public ResilientFileOutputStream(File file, boolean append, long bufferSize) throws FileNotFoundException {
025        this.file = file;
026        fos = new FileOutputStream(file, append);
027        this.os = new BufferedOutputStream(fos, (int) bufferSize);
028        this.presumedClean = true;
029    }
030
031    public FileChannel getChannel() {
032        if (os == null) {
033            return null;
034        }
035        return fos.getChannel();
036    }
037
038    public File getFile() {
039        return file;
040    }
041
042    @Override
043    String getDescription() {
044        return "file [" + file + "]";
045    }
046
047    @Override
048    OutputStream openNewOutputStream() throws IOException {
049        // see LOGBACK-765
050        fos = new FileOutputStream(file, true);
051        return new BufferedOutputStream(fos);
052    }
053
054    @Override
055    public String toString() {
056        return "c.q.l.c.recovery.ResilientFileOutputStream@" + System.identityHashCode(this);
057    }
058
059}