KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > codecoverage > v2 > logger > DirectoryClassChannelLogReader


1 /*
2  * @(#)DirectoryClassChannelLogReader.java
3  *
4  * Copyright (C) 2002-2003 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  */

26
27 package net.sourceforge.groboutils.codecoverage.v2.logger;
28
29 import java.io.BufferedReader JavaDoc;
30 import java.io.File JavaDoc;
31 import java.io.FileReader JavaDoc;
32 import java.io.IOException JavaDoc;
33
34 import net.sourceforge.groboutils.codecoverage.v2.IChannelLogRecord;
35 import net.sourceforge.groboutils.codecoverage.v2.IClassChannelLogReader;
36 import net.sourceforge.groboutils.codecoverage.v2.util.HexUtil;
37
38 /**
39  * Reads logs written by DirectoryChannelLogger.
40  * <p>
41  * As of 2004-Jul-03, the output format has changed the way the indices
42  * numbers are written.
43  *
44  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
45  * @version $Date: 2004/07/07 09:39:10 $
46  * @since December 17, 2002
47  */

48 public class DirectoryClassChannelLogReader implements IClassChannelLogReader
49 {
50     private File JavaDoc classLogFile;
51     private String JavaDoc classSig;
52     private BufferedReader JavaDoc in;
53     
54     
55     DirectoryClassChannelLogReader( File JavaDoc log, String JavaDoc classSig )
56     {
57         if (log == null || classSig == null)
58         {
59             throw new IllegalArgumentException JavaDoc( "No null args." );
60         }
61         if (!log.exists() || !log.isFile())
62         {
63             throw new IllegalArgumentException JavaDoc( "File "+log+
64                 " is not a file or doesn't exist." );
65         }
66         this.classLogFile = log;
67         this.classSig = classSig;
68     }
69     
70     
71     /**
72      * Reads the next record from the log. If there are no more records, then
73      * <tt>null</tt> is returned. These do not need to be returned in any
74      * specific order.
75      *
76      * @return the next log record.
77      * @exception IOException thrown if there was an underlying problem reading
78      * from the log.
79      */

80     public synchronized IChannelLogRecord nextRecord()
81             throws IOException JavaDoc
82     {
83         if (this.classLogFile == null)
84         {
85             return null;
86         }
87         if (this.in == null)
88         {
89             this.in = new BufferedReader JavaDoc(
90                 new FileReader JavaDoc( this.classLogFile ) );
91         }
92         
93         String JavaDoc s = this.in.readLine();
94         if (s == null)
95         {
96             close();
97             return null;
98         }
99         
100         // method value then mark value
101
HexUtil.TwoShorts ts = new HexUtil.TwoShorts();
102         if (!HexUtil.getInstance().parseTwoHex( s.trim(), ts, ' ', 0))
103         {
104             close();
105             throw new IOException JavaDoc( "Invalid file format in '"+
106                 this.classLogFile+"'." );
107         }
108         
109         return new DefaultChannelLogRecord(
110             this.classSig, ts.a, ts.b );
111         
112     }
113     
114     
115     public void close()
116             throws IOException JavaDoc
117     {
118         if (this.in != null)
119         {
120             this.in.close();
121             this.in = null;
122         }
123         this.classLogFile = null;
124     }
125     
126     
127     protected void finalize() throws Throwable JavaDoc
128     {
129         close();
130         super.finalize();
131     }
132 }
133
134
Popular Tags