Class RotatingFileOutputStream

java.lang.Object
java.io.OutputStream
com.pnfsoftware.jeb.util.io.RotatingFileOutputStream
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public class RotatingFileOutputStream extends OutputStream
Output stream whose sinks are a series of files. When the output exceeds the maximum file count and maximum size per file, it gets rotated: older output gets deleted to make room for more recent output.
  • Constructor Details

    • RotatingFileOutputStream

      public RotatingFileOutputStream(File folder, String basename, int rotFileCount, long rotFileSize) throws IOException
      Create a rotating file(s) output stream. The stream is buffered, output is appended to existing files (if any).
      Parameters:
      folder -
      basename -
      rotFileCount -
      rotFileSize -
      Throws:
      IOException
    • RotatingFileOutputStream

      public RotatingFileOutputStream(File folder, String basename, int rotFileCount, long rotFileSize, boolean buffered, boolean appendToExisting) throws IOException
      Create a rotating file(s) output stream.
      Parameters:
      folder - output folder; use null to denote the current working directory
      basename - base file name, must be non-empty
      rotFileCount - >=0 (can be 0 for no rotating output); should be >= 1
      rotFileSize - >0 (cannot be negative or "infinite": a maximum file size must be specified to prevent uncontrolled growth)
      buffered - enable or disable buffered-writes (recommended for high-frequency writing)
      appendToExisting - append to a previously existing file, if any
      Throws:
      IOException - on IO error
  • Method Details

    • getRotatingFileCount

      public int getRotatingFileCount()
    • getRotatingFileSize

      public long getRotatingFileSize()
    • isBuffered

      public boolean isBuffered()
    • getRotationCount

      public long getRotationCount()
    • getOutputFile

      public File getOutputFile()
      Retrieve the principal (first) output file.
      Returns:
    • getAdditionalOutputFile

      public File getAdditionalOutputFile(int i) throws IOException
      Retrieve an additional output file by index.
      Parameters:
      i - file index, must be in [1, maximum_filecount]
      Returns:
      Throws:
      IOException
    • generateFileSuffix

      protected String generateFileSuffix(int fileIndex)
      The file suffix appended to the base filename for additional file. Note that the default implementation simply generates a ".N" suffix.
      Parameters:
      fileIndex - file index
      Returns:
    • rotate

      public void rotate() throws IOException
      Throws:
      IOException
    • write

      public void write(int b) throws IOException
      Specified by:
      write in class OutputStream
      Throws:
      IOException
    • write

      public void write(byte[] b, int off, int len) throws IOException
      Overrides:
      write in class OutputStream
      Throws:
      IOException
    • flush

      public void flush() throws IOException
      Specified by:
      flush in interface Flushable
      Overrides:
      flush in class OutputStream
      Throws:
      IOException
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class OutputStream
      Throws:
      IOException