001 package log;
002
003 import java.io.*;
004
005 /**
006 * A stream that can be used to read and process log files.
007 *
008 * <p>The LogInputStream will take an {@link InputStream} and try to interpret the data from that stream as
009 * a log file. You can then obtain the {@link LogEntry LogEntries} in the order they were put into the log
010 * by calling {@link #readEntry}.</p>
011 *
012 * <p><code>LogInputStreams</code> can be filtered in which case you will only see the LogEntries that are
013 * {@link LogEntryFilter#accept accepted} by the {@link LogEntryFilter filter}.</p>
014 *
015 * @author Steffen Zschaler
016 * @version 2.0 14/07/1999
017 * @since v2.0
018 */
019 public class LogInputStream extends Object implements Serializable {
020
021 /**
022 * The input stream that backs this stream.
023 */
024 protected InputStream m_isSource;
025
026 /**
027 * The object input stream that is build on top of the input stream. This objects in this stream are
028 * expected to be {@link LogEntry LogEntries}.
029 */
030 protected ObjectInputStream m_oisSource;
031
032 /**
033 * The filter to be applied on the stream, if any.
034 */
035 protected LogEntryFilter m_lefFilter;
036
037 /**
038 * Create a new LogInputStream. There will be no filter.
039 *
040 * @param isSource the InputStream that is the base for this stream.
041 *
042 * @exception IOException if an error occured or the stream is not a valid log file.
043 */
044 public LogInputStream(InputStream isSource) throws IOException {
045 this(isSource, null);
046 }
047
048 /**
049 * Create a new LogInputStream.
050 *
051 * @param isSource the InputStream that is the base for this stream.
052 * @param lef the filter to be applied on the stream.
053 *
054 * @exception IOException if an error occured or the stream is not a valid log file.
055 */
056 public LogInputStream(InputStream isSource, LogEntryFilter lef) throws IOException {
057
058 super();
059
060 m_isSource = isSource;
061
062 // skip the leading zero
063 m_isSource.skip(1);
064
065 m_oisSource = new ObjectInputStream(m_isSource);
066
067 m_lefFilter = lef;
068 }
069
070 /**
071 * Close the stream and all streams that it relies on.
072 *
073 * @exception IOException if there was an error on closing.
074 *
075 * @override Never
076 */
077 public void close() throws IOException {
078 m_oisSource.close();
079 m_isSource.close();
080
081 m_oisSource = null;
082 m_isSource = null;
083 }
084
085 /**
086 * Read the next log entry that is {@link LogEntryFilter#accept accepted} by the filter from the stream.
087 *
088 * @exception IOException if an <code>IOException</code> occurred while reading from the underlying stream.
089 * Especially, this may be an <code>EOFException</code> if the end of the stream has been reached. This is
090 * the only way to find out whether there are more entries in the stream.
091 * @exception ClassNotFoundException if the exception was thrown by the underlying stream.
092 *
093 * @override Never
094 */
095 public LogEntry readEntry() throws IOException, ClassNotFoundException {
096
097 while (true) {
098 try {
099 LogEntry le = (LogEntry)m_oisSource.readObject();
100
101 if ((m_lefFilter == null) || (m_lefFilter.accept(le))) {
102 return le;
103 }
104 }
105 catch (StreamCorruptedException sce) {
106 m_oisSource = new ObjectInputStream(m_isSource);
107 }
108 }
109 }
110 }