Package com.pnfsoftware.jeb.util.io
Class RotatingFileOutputStream
java.lang.Object
java.io.OutputStream
com.pnfsoftware.jeb.util.io.RotatingFileOutputStream
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
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 Summary
ConstructorsConstructorDescriptionRotatingFileOutputStream(File folder, String basename, int rotFileCount, long rotFileSize) Create a rotating file(s) output stream.RotatingFileOutputStream(File folder, String basename, int rotFileCount, long rotFileSize, boolean buffered, boolean appendToExisting) Create a rotating file(s) output stream. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()voidflush()protected StringgenerateFileSuffix(int fileIndex) The file suffix appended to the base filename for additional file.getAdditionalOutputFile(int i) Retrieve an additional output file by index.Retrieve the principal (first) output file.intGet the configured number of rotated files to keep.longGet the configured maximum size of the active output file.longGet the number of completed rotations.booleanDetermine whether writes are buffered.voidrotate()Rotate output files immediately.voidwrite(byte[] b, int off, int len) voidwrite(int b) Methods inherited from class java.io.OutputStream
nullOutputStream, write
-
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- output folder; use null to denote the current working directorybasename- base file namerotFileCount- number of rotated files to keeprotFileSize- maximum size of the active output file- Throws:
IOException- if the initial output file cannot be opened
-
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 directorybasename- base file name, must be non-emptyrotFileCount- >=0 (can be 0 for no rotating output); should be >= 1rotFileSize- >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()Get the configured number of rotated files to keep.- Returns:
- rotated file count
-
getRotatingFileSize
public long getRotatingFileSize()Get the configured maximum size of the active output file.- Returns:
- maximum file size in bytes
-
isBuffered
public boolean isBuffered()Determine whether writes are buffered.- Returns:
- true if buffering is enabled
-
getRotationCount
public long getRotationCount()Get the number of completed rotations.- Returns:
- rotation count
-
getOutputFile
Retrieve the principal (first) output file.- Returns:
- active output file
-
getAdditionalOutputFile
Retrieve an additional output file by index.- Parameters:
i- file index, must be in [1, maximum_filecount]- Returns:
- rotated output file
- Throws:
IOException- if the index is invalid
-
generateFileSuffix
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:
- generated file suffix
-
rotate
Rotate output files immediately.- Throws:
IOException- if a file cannot be closed, renamed, created, or opened
-
write
- Specified by:
writein classOutputStream- Throws:
IOException
-
write
- Overrides:
writein classOutputStream- Throws:
IOException
-
flush
- Specified by:
flushin interfaceFlushable- Overrides:
flushin classOutputStream- Throws:
IOException
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classOutputStream- Throws:
IOException
-