View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.recovery;
15  
16  import java.io.*;
17  import java.nio.channels.FileChannel;
18  
19  public class ResilientFileOutputStream extends ResilientOutputStreamBase {
20  
21      private File file;
22      private FileOutputStream fos;
23  
24      public ResilientFileOutputStream(File file, boolean append, long bufferSize) throws FileNotFoundException {
25          this.file = file;
26          fos = new FileOutputStream(file, append);
27          this.os = new BufferedOutputStream(fos, (int) bufferSize);
28          this.presumedClean = true;
29      }
30  
31      public FileChannel getChannel() {
32          if (os == null) {
33              return null;
34          }
35          return fos.getChannel();
36      }
37  
38      public File getFile() {
39          return file;
40      }
41  
42      @Override
43      String getDescription() {
44          return "file [" + file + "]";
45      }
46  
47      @Override
48      OutputStream openNewOutputStream() throws IOException {
49          // see LOGBACK-765
50          fos = new FileOutputStream(file, true);
51          return new BufferedOutputStream(fos);
52      }
53  
54      @Override
55      public String toString() {
56          return "c.q.l.c.recovery.ResilientFileOutputStream@" + System.identityHashCode(this);
57      }
58  
59  }