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 and Description |
|---|---|
| private class |
| Modifier and Type | Field and 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 File[] | files
The array of File instance representing the rolling files |
| private int | |
| private String | lockFileName
The lockfile name |
| private static HashMap | |
| private FileOutputStream | lockStream
The output stream that is used as a lock |
| private static final int | |
| private RollingFileStream. | meter
The underlying stream being written to that keeps track of how much has been written |
| private String | pattern
The filename pattern. |
| Access | Constructor and Description |
|---|---|
| public | |
| public | RollingFileStream(String
the pattern for naming the output file pattern, int the maximum number of bytes to write to any one file limit, int the number of files to use count, boolean specifies append mode append)Initialize a RollingFileStream to write to a set of files with optional append. |
| Modifier and Type | Method and Description |
|---|---|
| private void | |
| public synchronized void | close()
Overrides java. Implements java. Close all the files. |
| private void | |
| private boolean | Returns: true if the file exists or false otherwiseThe file to check file)Check to see if a file exists. |
| private long | Returns: The length of the fileThe file to get the length of file)Get the length of a file. |
| private boolean | Returns: true if the file was renamed or false otherwiseThe file to rename file1, final File The file to rename it to file2)Rename a file. |
| private File | |
| private String | Returns: The property valueThe propety to get property)Gets a system property. |
| private void | |
| private FileOutputStream | |
| private void | openFiles()
Opens the output files files based on the configured pattern, limit, count, and append mode. |
| private synchronized void | |
| public void | write(int
The value to write b)Implements abstract java. Implements the write method of the OutputStream. |
| append | back to summary |
|---|---|
| private boolean append The append flag which indicates at creation time to append to an existing file or to always create a new one | |
| count | back to summary |
|---|---|
| private int count The rolling file count. This many files will be created before the oldest is removed and the files rolled. | |
| files | back to summary |
|---|---|
| private File[] files The array of File instance representing the rolling files | |
| limit | back to summary |
|---|---|
| private int limit | |
| lockFileName | back to summary |
|---|---|
| private String lockFileName The lockfile name | |
| locks | back to summary |
|---|---|
| private static HashMap<String, String> locks | |
| lockStream | back to summary |
|---|---|
| private FileOutputStream lockStream The output stream that is used as a lock | |
| MAX_LOCKS | back to summary |
|---|---|
| private static final int MAX_LOCKS | |
| meter | back to summary |
|---|---|
| private RollingFileStream. The underlying stream being written to that keeps track of how much has been written | |
| pattern | back to summary |
|---|---|
| private String pattern The filename pattern. | |
| RollingFileStream | back to summary |
|---|---|
| public RollingFileStream() throws IOException Construct a default RollingFileStream. This will be configured entirely with default values:
| |
| RollingFileStream | back to summary |
|---|---|
| public RollingFileStream(String pattern, int limit, int count, boolean append) throws IOException Initialize a RollingFileStream to write to a set of files with optional append. When (approximately) the given limit has been written to one file, another file will be opened. The output will cycle through a set of count files.
| |
| checkMeter | back to summary |
|---|---|
| private void checkMeter() throws IOException Invoked by the metered OutputStream | |
| close | back to summary |
|---|---|
| public synchronized void close() Overrides java. Implements java. Close all the files. | |
| fileDelete | back to summary |
|---|---|
| private void fileDelete(final File file) Delete a file.
| |
| fileExists | back to summary |
|---|---|
| private boolean fileExists(final File file) Check to see if a file exists.
| |
| fileLength | back to summary |
|---|---|
| private long fileLength(final File file) Get the length of a file.
| |
| fileRename | back to summary |
|---|---|
| private boolean fileRename(final File file1, final File file2) Rename a file. | |
| generate | back to summary |
|---|---|
| private File generate(String pattern, int generation, int unique) throws IOException Generates and returns File from a pattern | |
| getSystemProperty | back to summary |
|---|---|
| private String getSystemProperty(final String property) Gets a system property. | |
| open | back to summary |
|---|---|
| private void open(File fname, boolean append) throws IOException Opens a new file that and delegates it to a MeteredStream
| |
| openFile | back to summary |
|---|---|
| private FileOutputStream openFile(final String filename, final boolean append) throws IOException Opens a file.
| |
| openFiles | back to summary |
|---|---|
| private void openFiles() throws IOException Opens the output files files based on the configured pattern, limit, count, and append mode. | |
| rotate | back to summary |
|---|---|
| private synchronized void rotate() throws IOException Rotates the log files. The metered OutputStream is closed,the log files are rotated and then a new metered OutputStream is created. | |
| write | back to summary |
|---|---|
| public void write(int b) throws IOException Implements abstract java. Implements the write method of the OutputStream. This writes the value to the metered stream.
| |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv OutputStream | out
The real OutputStream to delegate to |
| pack-priv int | written
The number of bytes written so far to the OutputStream |
| Access | Constructor and Description |
|---|---|
| pack-priv | MeteredStream(OutputStream
The OutputStream to delegate to out, int The number of bytes currently written to the OuptutStream written)Creates a new instance of MeteredStream |
| Modifier and Type | Method and Description |
|---|---|
| public void | close()
Overrides java. Implements java. Closes this output stream and releases any system resources associated with this stream. |
| public int | |
| public void | write(int
the b)byte.Implements abstract java. Writes the specified byte to this output stream. |
| out | back to summary |
|---|---|
| pack-priv OutputStream out The real OutputStream to delegate to | |
| written | back to summary |
|---|---|
| pack-priv int written The number of bytes written so far to the OutputStream | |
| MeteredStream | back to summary |
|---|---|
| pack-priv MeteredStream(OutputStream out, int written) Creates a new instance of MeteredStream
| |
| close | back to summary |
|---|---|
| public void close() throws IOException Overrides java. Implements java. Doc from java. Closes this output stream and releases any system resources
associated with this stream. The general contract of
| |
| getWritten | back to summary |
|---|---|
| public int getWritten() | |
| write | back to summary |
|---|---|
| public void write(int b) throws IOException Implements abstract java. Doc from java. Writes the specified byte to this output stream. The general
contract for
| |