Class RolloverFileOutputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.AutoCloseable

    public class RolloverFileOutputStream
    extends java.io.OutputStream
    RolloverFileOutputStream.

    This output stream puts content in a file that is rolled over every 24 hours. The filename must include the string "yyyy_mm_dd", which is replaced with the actual date when creating and rolling over the file.

    Old files are retained for a number of days before being deleted.

    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      void flush()  
      java.lang.String getDatedFilename()  
      java.lang.String getFilename()  
      int getRetainDays()  
      protected void rollover​(java.io.File oldFile, java.io.File backupFile, java.io.File newFile)
      This method is called whenever a log file is rolled over
      static java.time.ZonedDateTime toMidnight​(java.time.ZonedDateTime now)
      Get the "start of day" for the provided DateTime at the zone specified.
      void write​(byte[] buf)  
      void write​(byte[] buf, int off, int len)  
      void write​(int b)  
      • Methods inherited from class java.io.OutputStream

        nullOutputStream
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • RolloverFileOutputStream

        public RolloverFileOutputStream​(java.lang.String filename)
                                 throws java.io.IOException
        Parameters:
        filename - The filename must include the string "yyyy_mm_dd", which is replaced with the actual date when creating and rolling over the file.
        Throws:
        java.io.IOException - if unable to create output
      • RolloverFileOutputStream

        public RolloverFileOutputStream​(java.lang.String filename,
                                        boolean append)
                                 throws java.io.IOException
        Parameters:
        filename - The filename must include the string "yyyy_mm_dd", which is replaced with the actual date when creating and rolling over the file.
        append - If true, existing files will be appended to.
        Throws:
        java.io.IOException - if unable to create output
      • RolloverFileOutputStream

        public RolloverFileOutputStream​(java.lang.String filename,
                                        boolean append,
                                        int retainDays)
                                 throws java.io.IOException
        Parameters:
        filename - The filename must include the string "yyyy_mm_dd", which is replaced with the actual date when creating and rolling over the file.
        append - If true, existing files will be appended to.
        retainDays - The number of days to retain files before deleting them. 0 to retain forever.
        Throws:
        java.io.IOException - if unable to create output
      • RolloverFileOutputStream

        public RolloverFileOutputStream​(java.lang.String filename,
                                        boolean append,
                                        int retainDays,
                                        java.util.TimeZone zone)
                                 throws java.io.IOException
        Parameters:
        filename - The filename must include the string "yyyy_mm_dd", which is replaced with the actual date when creating and rolling over the file.
        append - If true, existing files will be appended to.
        retainDays - The number of days to retain files before deleting them. 0 to retain forever.
        zone - the timezone for the output
        Throws:
        java.io.IOException - if unable to create output
      • RolloverFileOutputStream

        public RolloverFileOutputStream​(java.lang.String filename,
                                        boolean append,
                                        int retainDays,
                                        java.util.TimeZone zone,
                                        java.lang.String dateFormat,
                                        java.lang.String backupFormat)
                                 throws java.io.IOException
        Parameters:
        filename - The filename must include the string "yyyy_mm_dd", which is replaced with the actual date when creating and rolling over the file.
        append - If true, existing files will be appended to.
        retainDays - The number of days to retain files before deleting them. 0 to retain forever.
        zone - the timezone for the output
        dateFormat - The format for the date file substitution. The default is "yyyy_MM_dd". If set to the empty string, the file is rolledover to the same filename, with the current file being renamed to the backup filename.
        backupFormat - The format for the file extension of backup files. The default is "HHmmssSSS".
        Throws:
        java.io.IOException - if unable to create output
    • Method Detail

      • toMidnight

        public static java.time.ZonedDateTime toMidnight​(java.time.ZonedDateTime now)
        Get the "start of day" for the provided DateTime at the zone specified.
        Parameters:
        now - the date time to calculate from
        Returns:
        start of the day of the date provided
      • getFilename

        public java.lang.String getFilename()
      • getDatedFilename

        public java.lang.String getDatedFilename()
      • getRetainDays

        public int getRetainDays()
      • rollover

        protected void rollover​(java.io.File oldFile,
                                java.io.File backupFile,
                                java.io.File newFile)
        This method is called whenever a log file is rolled over
        Parameters:
        oldFile - The original filename or null if this is the first creation
        backupFile - The backup filename or null if the filename is dated.
        newFile - The new filename that is now being used for logging
      • write

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

        public void write​(byte[] buf)
                   throws java.io.IOException
        Overrides:
        write in class java.io.OutputStream
        Throws:
        java.io.IOException
      • write

        public void write​(byte[] buf,
                          int off,
                          int len)
                   throws java.io.IOException
        Overrides:
        write in class java.io.OutputStream
        Throws:
        java.io.IOException
      • flush

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

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