java.io.Closeable
, java.io.Flushable
, java.lang.AutoCloseable
public class RollingFileStream
extends java.io.OutputStream
This class borrows extensively from the java.util.logger.FileHandler class for its file handling ability and instead of handling logger messages it extends java.io.OutputStream.
A pattern consists of a string that includes the following special components that will be replaced at runtime:
Thus for example a pattern of "%t/java%g.log" with a count of 2 would typically cause files to be written on Solaris to /var/tmp/java0.log and /var/tmp/java1.log whereas on Windows 95 they would be typically written to C:\TEMP\java0.log and C:\TEMP\java1.log
Generation numbers follow the sequence 0, 1, 2, etc.
Normally the "%u" unique field is set to 0. However, if the FileHandler tries to open the filename and finds the file is currently in use by another process it will increment the unique number field and try again. This will be repeated until FileHandler finds a file name that is not currently in use. If there is a conflict and no "%u" field has been specified, it will be added at the end of the filename after a dot. (This will be after any automatically added generation number.)
Thus if three processes were all trying to output to fred%u.%g.txt then they might end up using fred0.0.txt, fred1.0.txt, fred2.0.txt as the first file in their rotating sequences.
Note that the use of unique ids to avoid conflicts is only guaranteed to work reliably when using a local disk file system.
Modifier and Type | Class | Description |
---|---|---|
private class |
RollingFileStream.MeteredStream |
Modifier and Type | Field | Description |
---|---|---|
private boolean |
append |
The append flag which indicates at creation time to append to an existing
file or to always create a new one
|
private int |
count |
The rolling file count.
|
private java.io.File[] |
files |
The array of File instance representing the rolling files
|
private int |
limit |
|
private java.lang.String |
lockFileName |
The lockfile name
|
private static java.util.HashMap<java.lang.String,java.lang.String> |
locks |
|
private java.io.FileOutputStream |
lockStream |
The output stream that is used as a lock
|
private static int |
MAX_LOCKS |
|
private RollingFileStream.MeteredStream |
meter |
The underlying stream being written to that keeps track of how much
has been written
|
private java.lang.String |
pattern |
The filename pattern.
|
Constructor | Description |
---|---|
RollingFileStream() |
Construct a default RollingFileStream.
|
RollingFileStream(java.lang.String pattern,
int limit,
int count,
boolean append) |
Initialize a RollingFileStream to write to a set of files with optional append.
|
Modifier and Type | Method | Description |
---|---|---|
private void |
checkMeter() |
Invoked by the metered OutputStream
|
void |
close() |
Close all the files.
|
private void |
fileDelete(java.io.File file) |
Delete a file in a privilege block
|
private boolean |
fileExists(java.io.File file) |
Check to see if a file exists in a privilege block
|
private long |
fileLength(java.io.File file) |
Get the length of a file in a privilege block
|
private boolean |
fileRename(java.io.File file1,
java.io.File file2) |
Rename a file in a privilege block
|
private java.io.File |
generate(java.lang.String pattern,
int generation,
int unique) |
Generates and returns File from a pattern
|
private java.lang.String |
getSystemProperty(java.lang.String property) |
Gets a system property in a privileged block
|
private void |
open(java.io.File fname,
boolean append) |
Opens a new file that and delegates it to a MeteredStream
|
private java.io.FileOutputStream |
openFile(java.lang.String filename,
boolean append) |
Opens a file in the privileged block
|
private void |
openFiles() |
Opens the output files files based on the configured pattern, limit, count,
and append mode.
|
private void |
rotate() |
Rotates the log files.
|
void |
write(int b) |
Implements the write method of the OutputStream.
|
private RollingFileStream.MeteredStream meter
private boolean append
private int limit
private int count
private java.lang.String pattern
private java.lang.String lockFileName
private java.io.FileOutputStream lockStream
private java.io.File[] files
private static final int MAX_LOCKS
private static java.util.HashMap<java.lang.String,java.lang.String> locks
public RollingFileStream() throws java.io.IOException, java.lang.SecurityException
java.io.IOException
- if there are IO problems opening the files.java.lang.SecurityException
- if a security manager exists and if the caller does not have
LoggingPermission("control")).java.lang.NullPointerException
- if pattern property is an empty String.public RollingFileStream(java.lang.String pattern, int limit, int count, boolean append) throws java.io.IOException, java.lang.SecurityException
pattern
- the pattern for naming the output filelimit
- the maximum number of bytes to write to any one filecount
- the number of files to useappend
- specifies append modejava.io.IOException
- if there are IO problems opening the files.java.lang.SecurityException
- if a security manager exists and if the caller does not have
LoggingPermission("control").java.lang.IllegalArgumentException
- if limit < 0, or count < 1.java.lang.IllegalArgumentException
- if pattern is an empty stringpublic void write(int b) throws java.io.IOException
write
in class java.io.OutputStream
b
- The value to writejava.io.IOException
private void openFiles() throws java.io.IOException
java.io.IOException
private java.io.File generate(java.lang.String pattern, int generation, int unique) throws java.io.IOException
pattern
- The filename patterngeneration
- The generation number used if there is a conflictunique
- The unique number to append to the filenamejava.io.IOException
private void rotate() throws java.io.IOException
java.io.IOException
public void close() throws java.lang.SecurityException
close
in interface java.lang.AutoCloseable
close
in interface java.io.Closeable
close
in class java.io.OutputStream
java.lang.SecurityException
- if a security manager exists and if the caller does not have
LoggingPermission("control").private java.lang.String getSystemProperty(java.lang.String property)
property
- The propety to getprivate java.io.FileOutputStream openFile(java.lang.String filename, boolean append) throws java.io.IOException
filename
- The name of the file to openappend
- if true
open the file in append modejava.io.IOException
private boolean fileExists(java.io.File file)
file
- The file to checktrue
if the file exists or false
otherwiseprivate void fileDelete(java.io.File file)
file
- The file to deleteprivate boolean fileRename(java.io.File file1, java.io.File file2)
file1
- The file to renamefile2
- The file to rename it totrue
if the file was renamed or false otherwiseprivate long fileLength(java.io.File file)
file
- The file to get the length ofprivate void open(java.io.File fname, boolean append) throws java.io.IOException
fname
- The name of the fileappend
- If true
append to the existing filejava.io.IOException
private void checkMeter() throws java.io.IOException
java.io.IOException
Apache Derby V10.14 Internals - Copyright © 2004,2018 The Apache Software Foundation. All Rights Reserved.