public class IOHelper extends LogMatcher
Load and save methods are executed in a synchronous way i.e. they do not return
until the I/O is terminated (or an exception occurs).
Intermediate results useful to monitor the progress of the I/O are communicated
to the listeners implementing the IOProgressListener interface.
The load and save methods of this class are synchronized but I would not say
that this class is thread safe because it does not hold and lock the objects it receives
as parameters like for example the BufferReader and the BufferWriter.
So thread safety must be ensured by the owner of such objects.
Loading
The loading is performed through one of the overloaded loadLogs methods.
The bytes read and the number of the logs successfully read are sent to the listeners
implementing the IOProgressListener interface.
If there are filters, an audience or a discard level defined, then each log is checked
before being sent to the listener.
Saving
The saving of logs can be done by passing a Collection of logs or an Iteratorto one of the
overloaded saveLogs methods.
Such methods communicates the progress to the listener implementing the
IOProgressListener interface.
Saving logs by passing the name of the file does not require any extra steps.
Saving logs by passing a BufferedWriter is always a three steps procedure:
prepareSaveFile to write XML header
saveLogs or the saveLog
Save to add the closing XML tags, flush and close the buffer
Load and save can be very long operations. To stop an I/O, the stopIO()
must be executed.
| Modifier and Type | Class and Description |
|---|---|
class |
IOHelper.GZipLogOutStream
GZipLogOutStream subclass GZIPOutputStream to set the compression level. |
| Modifier and Type | Field and Description |
|---|---|
static int |
DEFAULT_COMPRESSION_LEVEL
The default compression level while saving files
|
protected boolean |
stopped
Signal that a load or a save must be stopped
|
actualDiscardLevel| Constructor and Description |
|---|
IOHelper()
Constructor
|
| Modifier and Type | Method and Description |
|---|---|
java.io.BufferedReader |
getBufferedReader(java.lang.String fileName)
Return the reader to get logs from
|
java.io.BufferedWriter |
getBufferedWriter(java.lang.String fileName,
boolean append,
boolean gzip)
Create a
BufferedWriter to save logs into. |
java.io.BufferedWriter |
getBufferedWriter(java.lang.String fileName,
boolean append,
boolean gzip,
int compressionLevel)
Create a
BufferedWriter to save logs into |
void |
loadLogs(java.io.BufferedReader reader,
ACSRemoteLogListener logListener,
ACSRemoteRawLogListener rawLogListener,
ACSRemoteErrorListener errorListener,
IOPorgressListener progressListener)
Load the logs from the given
BufferedReader. |
long |
loadLogs(java.lang.String fileName,
ACSRemoteLogListener logListener,
ACSRemoteRawLogListener rawLogListener,
ACSRemoteErrorListener errorListener,
IOPorgressListener progressListener)
Load the logs from the file with the given name.
|
long |
loadLogs(java.lang.String fileName,
ACSRemoteLogListener logListener,
ACSRemoteRawLogListener rawLogListener,
ACSRemoteErrorListener errorListener,
IOPorgressListener progressListener,
boolean gzip)
Load the logs from the file with the given name.
|
int |
saveLog(java.io.BufferedWriter outBuf,
ILogEntry log)
Save a log in the passed file
|
void |
saveLogs(java.io.BufferedWriter outBuffer,
java.util.Collection<ILogEntry> logs,
IOPorgressListener progressListener)
Save a collection of logs on a
BufferedWriter. |
void |
saveLogs(java.io.BufferedWriter outBuf,
java.util.Iterator<ILogEntry> iterator,
IOPorgressListener progressListener)
Save a collection of logs on a
BufferedWriter. |
void |
saveLogs(java.lang.String fileName,
java.util.Collection<ILogEntry> logs,
IOPorgressListener progressListener,
boolean append,
boolean gzip)
Save a collection of logs on disk
|
void |
saveLogs(java.lang.String fileName,
java.util.Collection<ILogEntry> logs,
IOPorgressListener progressListener,
boolean append,
boolean gzip,
int compressionLevel)
Save a collection of logs on disk
|
void |
saveLogs(java.lang.String fileName,
java.util.Iterator<ILogEntry> iterator,
IOPorgressListener progressListener,
boolean append,
boolean gzip)
Save the logs available through an
Iterator. |
void |
saveLogs(java.lang.String fileName,
java.util.Iterator<ILogEntry> iterator,
IOPorgressListener progressListener,
boolean append,
boolean gzip,
int compressionLevel)
Save the logs available through an
Iterator. |
void |
stopIO()
Call this method if you wish to interrupt a load or a save.
|
void |
terminateSave(java.io.BufferedWriter outBuffer,
boolean close)
Terminate the saving.
|
void |
writeHeader(java.io.BufferedWriter wBuffer)
Write the XML header in the buffered writer
|
getActualDiscardLevel, getAudience, getFilters, match, setAudience, setDiscardLevel, setFilterspublic static final int DEFAULT_COMPRESSION_LEVEL
protected volatile boolean stopped
public IOHelper()
throws java.lang.Exception
java.lang.Exception - In case of errors building the parserpublic long loadLogs(java.lang.String fileName,
ACSRemoteLogListener logListener,
ACSRemoteRawLogListener rawLogListener,
ACSRemoteErrorListener errorListener,
IOPorgressListener progressListener,
boolean gzip)
throws java.io.IOException,
java.lang.Exception
The logs are sent to the ACSRemoteLogListener and /or
to the ACSRemoteRawLogListener.
fileName - The name of the file to read logs fromlogListener - The callback for each new log read from the IOrawLogListener - The callback for each new XML log read from the IOerrorListener - The listener for errorsprogressListener - The listener to be notified about the bytes readgzip - If true the file to read is compressed in GZIP formatjava.io.IOException - In case of an IO error while reading the filejava.lang.Exception - In case of error building the parserpublic long loadLogs(java.lang.String fileName,
ACSRemoteLogListener logListener,
ACSRemoteRawLogListener rawLogListener,
ACSRemoteErrorListener errorListener,
IOPorgressListener progressListener)
throws java.io.IOException,
java.lang.Exception
The logs are sent to the ACSRemoteLogListener and /or
to the ACSRemoteRawLogListener.
The file can be compressed (GZIP) or plain. Compressed file names must terminate with .gz while plain XML file names must end with .xml.
fileName - The name of the file to read logs from.
fileName must terminate with .gz or .xml (case insensitive)logListener - The callback for each new log read from the IOrawLogListener - The callback for each new XML log read from the IOerrorListener - The listener for errorsprogressListener - The listener to be notified about the bytes readjava.io.IOException - In case of an IO error while reading the filejava.lang.Exception - In case of error building the parserpublic void loadLogs(java.io.BufferedReader reader,
ACSRemoteLogListener logListener,
ACSRemoteRawLogListener rawLogListener,
ACSRemoteErrorListener errorListener,
IOPorgressListener progressListener)
throws java.io.IOException
BufferedReader.
The logs are sent to the ACSRemoteLogListener and /or
to the ACSRemoteRawLogListener.
reader - The reader to read logs fromlogListener - The callback for each new log read from the IOrawLogListener - The callback for each new XML log read from the IOerrorListener - The listener for errorsprogressListener - The listener to be notified about the bytes readjava.io.IOException - In case of an IO error while reading the filepublic void writeHeader(java.io.BufferedWriter wBuffer)
throws java.io.IOException
bw - java.io.IOExceptionpublic void terminateSave(java.io.BufferedWriter outBuffer,
boolean close)
throws java.io.IOException
This method must executed when the save terminates. It does the following:
outBuffer - The file to closeclose - If true the BufferedWriter is closedjava.io.IOException - In case of an IO errorpublic int saveLog(java.io.BufferedWriter outBuf,
ILogEntry log)
throws java.io.IOException
outbuf - The buffered writer where the logs have to be storedlog - The log to saveprogressListener - The listener to be notified about the bytes writtenjava.io.IOException - In case of an IO error while writing logs into the filepublic void saveLogs(java.lang.String fileName,
java.util.Collection<ILogEntry> logs,
IOPorgressListener progressListener,
boolean append,
boolean gzip)
throws java.io.IOException
fileName - The name of the file to store logs intologs - The non empty collection of logs to saveprogressListener - The listener to be notified about the number of bytes writtenappend - true if the logs in the collection must be appended to an existing filefalse and the file aredy exists, it is deleted before writing
gzip - If true the file is compressed (GZIP) with the default compression leveljava.io.IOException - In case of error writingpublic void saveLogs(java.lang.String fileName,
java.util.Collection<ILogEntry> logs,
IOPorgressListener progressListener,
boolean append,
boolean gzip,
int compressionLevel)
throws java.io.IOException
fileName - The name of the file to store logs intologs - The non empty collection of logs to saveprogressListener - The listener to be notified about the number of bytes writtenappend - true if the logs in the collection must be appended to an existing filefalse and the file aredy exists, it is deleted before writing
gzip - If true the file is compressed (GZIP)compressionLevel - The compressionLevel for GZIP compression (0..9);
ignored if gzip is is false>/code>java.io.IOException - In case of error writingpublic void saveLogs(java.io.BufferedWriter outBuffer,
java.util.Collection<ILogEntry> logs,
IOPorgressListener progressListener)
throws java.io.IOException
BufferedWriter.
The buffer must be initialized and terminated i.e. the prepareSaveFile
and the terminateSave are not executed by this method.
outBuffer - The writer to write logs intologs - The non empty collection of logs to saveprogressListener - The listener to be notified about the number of bytes writtengzip - If true the file is compressed (GZIP)java.io.IOException - In case of error writingpublic void saveLogs(java.lang.String fileName,
java.util.Iterator<ILogEntry> iterator,
IOPorgressListener progressListener,
boolean append,
boolean gzip)
throws java.io.IOException
Iterator.filename - The name of the file to write logs intologs - The non empty collection of logs to saveprogressListener - The listener to be notified about the number of bytes writtenappend - true if the logs in the collection must be appended to an existing filefalse and the file already exists, it is deleted before writing
gzip - If true the file is compressed (GZIP) with the default compression leveljava.io.IOException - In case of error writingpublic void saveLogs(java.lang.String fileName,
java.util.Iterator<ILogEntry> iterator,
IOPorgressListener progressListener,
boolean append,
boolean gzip,
int compressionLevel)
throws java.io.IOException
Iterator.filename - The name of the file to write logs intologs - The non empty collection of logs to saveprogressListener - The listener to be notified about the number of bytes writtenappend - true if the logs in the collection must be appended to an existing filefalse and the file already exists, it is deleted before writing
gzip - If true the file is compressed (GZIP)compressionLevel - The compressionLevel for GZIP compression (0..9);
ignored if gzip is false>/code>java.io.IOException - In case of error writingpublic java.io.BufferedWriter getBufferedWriter(java.lang.String fileName,
boolean append,
boolean gzip,
int compressionLevel)
throws java.io.FileNotFoundException,
java.io.IOException
BufferedWriter to save logs intofileName - The name of the file to write logs intoappend - true if the logs in the collection must be appended to an existing filefalse and the file already exists, it is deleted before writing
gzip - If true the file is compressed (GZIP)compressionLevel - The compressionLevel for GZIP compression (0..9);
ignored if gzip is false>/code>BufferedWriter to save logs into the file of the given namejava.io.FileNotFoundExceptionjava.io.IOExceptionpublic java.io.BufferedWriter getBufferedWriter(java.lang.String fileName,
boolean append,
boolean gzip)
throws java.io.FileNotFoundException,
java.io.IOException
BufferedWriter to save logs into.
In case of compressed files, the default compression level is used
fileName - The name of the file to write logs intoappend - true if the logs in the collection must be appended to an existing filefalse and the file already exists, it is deleted before writing
gzip - If true the file is compressed (GZIP) with the default levelBufferedWriter to save logs into the file of the given namejava.io.FileNotFoundExceptionjava.io.IOExceptionpublic java.io.BufferedReader getBufferedReader(java.lang.String fileName)
throws java.io.FileNotFoundException,
java.io.IOException
The reader can be compressed (GZIP) or not depending on the extension of the file name (i.e. ".gz" or ".xml").
fileName - The name of the file to readjava.io.FileNotFoundExceptionjava.io.IOExceptionpublic void saveLogs(java.io.BufferedWriter outBuf,
java.util.Iterator<ILogEntry> iterator,
IOPorgressListener progressListener)
throws java.io.IOException
BufferedWriter.
The buffer must be initialized and terminated i.e. the prepareSaveFile
and the terminateSave are not executed by this method.
outBuf - The buffer to write logs intologs - The non empty collection of logs to saveprogressListener - The listener to be notified about the number of bytes writtenjava.io.IOException - In case of error writingpublic void stopIO()
Load and save are executed synchronously so this method has to be called by a separate thread. A typical example is the "Abort" button of a dialog: when the user presses such a button, this method is invoked by the swing thread.