001: /*
002: * @(#)DirectoryClassChannelLogReader.java
003: *
004: * Copyright (C) 2002-2003 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a
009: * copy of this software and associated documentation files (the "Software"),
010: * to deal in the Software without restriction, including without limitation
011: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
012: * and/or sell copies of the Software, and to permit persons to whom the
013: * Software is furnished to do so, subject to the following conditions:
014: *
015: * The above copyright notice and this permission notice shall be included in
016: * all copies or substantial portions of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
021: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
023: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
024: * DEALINGS IN THE SOFTWARE.
025: */
026:
027: package net.sourceforge.groboutils.codecoverage.v2.logger;
028:
029: import java.io.BufferedReader;
030: import java.io.File;
031: import java.io.FileReader;
032: import java.io.IOException;
033:
034: import net.sourceforge.groboutils.codecoverage.v2.IChannelLogRecord;
035: import net.sourceforge.groboutils.codecoverage.v2.IClassChannelLogReader;
036: import net.sourceforge.groboutils.codecoverage.v2.util.HexUtil;
037:
038: /**
039: * Reads logs written by DirectoryChannelLogger.
040: * <p>
041: * As of 2004-Jul-03, the output format has changed the way the indices
042: * numbers are written.
043: *
044: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
045: * @version $Date: 2004/07/07 09:39:10 $
046: * @since December 17, 2002
047: */
048: public class DirectoryClassChannelLogReader implements
049: IClassChannelLogReader {
050: private File classLogFile;
051: private String classSig;
052: private BufferedReader in;
053:
054: DirectoryClassChannelLogReader(File log, String classSig) {
055: if (log == null || classSig == null) {
056: throw new IllegalArgumentException("No null args.");
057: }
058: if (!log.exists() || !log.isFile()) {
059: throw new IllegalArgumentException("File " + log
060: + " is not a file or doesn't exist.");
061: }
062: this .classLogFile = log;
063: this .classSig = classSig;
064: }
065:
066: /**
067: * Reads the next record from the log. If there are no more records, then
068: * <tt>null</tt> is returned. These do not need to be returned in any
069: * specific order.
070: *
071: * @return the next log record.
072: * @exception IOException thrown if there was an underlying problem reading
073: * from the log.
074: */
075: public synchronized IChannelLogRecord nextRecord()
076: throws IOException {
077: if (this .classLogFile == null) {
078: return null;
079: }
080: if (this .in == null) {
081: this .in = new BufferedReader(new FileReader(
082: this .classLogFile));
083: }
084:
085: String s = this .in.readLine();
086: if (s == null) {
087: close();
088: return null;
089: }
090:
091: // method value then mark value
092: HexUtil.TwoShorts ts = new HexUtil.TwoShorts();
093: if (!HexUtil.getInstance().parseTwoHex(s.trim(), ts, ' ', 0)) {
094: close();
095: throw new IOException("Invalid file format in '"
096: + this .classLogFile + "'.");
097: }
098:
099: return new DefaultChannelLogRecord(this .classSig, ts.a, ts.b);
100:
101: }
102:
103: public void close() throws IOException {
104: if (this .in != null) {
105: this .in.close();
106: this .in = null;
107: }
108: this .classLogFile = null;
109: }
110:
111: protected void finalize() throws Throwable {
112: close();
113: super.finalize();
114: }
115: }
|