001/*
002 * Cobertura - http://cobertura.sourceforge.net/
003 *
004 * Copyright (C) 2003 jcoverage ltd.
005 * Copyright (C) 2005 Mark Doliner
006 * Copyright (C) 2007 Joakim Erdfelt
007 * Copyright (C) 2007 Ignat Zapolsky
008 *
009 * Cobertura is free software; you can redistribute it and/or modify
010 * it under the terms of the GNU General Public License as published
011 * by the Free Software Foundation; either version 2 of the License,
012 * or (at your option) any later version.
013 *
014 * Cobertura is distributed in the hope that it will be useful, but
015 * WITHOUT ANY WARRANTY; without even the implied warranty of
016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017 * General Public License for more details.
018 *
019 * You should have received a copy of the GNU General Public License
020 * along with Cobertura; if not, write to the Free Software
021 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
022 * USA
023 */
024
025package net.sourceforge.cobertura.coveragedata;
026
027import net.sourceforge.cobertura.util.ConfigurationUtil;
028
029import java.io.BufferedInputStream;
030import java.io.File;
031import java.io.FileInputStream;
032import java.io.FileOutputStream;
033import java.io.IOException;
034import java.io.InputStream;
035import java.io.ObjectInputStream;
036import java.io.ObjectOutputStream;
037import java.io.OutputStream;
038
039/**
040 * This contains methods used for reading and writing the
041 * "cobertura.ser" file.
042 */
043public abstract class CoverageDataFileHandler implements HasBeenInstrumented
044{
045        private static File defaultFile = null;
046
047        public static File getDefaultDataFile()
048        {
049                // return cached defaultFile
050                if (defaultFile != null) 
051                {
052                        return defaultFile;
053                }
054
055                // load and cache datafile configuration
056                ConfigurationUtil config = new ConfigurationUtil();
057                defaultFile = new File(config.getDatafile());
058        
059                return defaultFile;
060        }
061
062        public static ProjectData loadCoverageData(File dataFile)
063        {
064                InputStream is = null;
065
066                //System.out.println("Cobertura: Loading coverage data from " + dataFile.getAbsolutePath());
067                try
068                {
069                        is = new BufferedInputStream(new FileInputStream(dataFile), 16384);
070                        return loadCoverageData(is);
071                }
072                catch (IOException e)
073                {
074                        System.err.println("Cobertura: Error reading file "
075                                        + dataFile.getAbsolutePath() + ": "
076                                        + e.getLocalizedMessage());
077                        return null;
078                }
079                finally
080                {
081                        if (is != null)
082                                try
083                                {
084                                        is.close();
085                                }
086                                catch (IOException e)
087                                {
088                                        System.err.println("Cobertura: Error closing file "
089                                                        + dataFile.getAbsolutePath() + ": "
090                                                        + e.getLocalizedMessage());
091                                }
092                }
093        }
094
095        private static ProjectData loadCoverageData(InputStream dataFile) throws IOException
096        {
097                ObjectInputStream objects = null;
098
099                try
100                {
101                        objects = new ObjectInputStream(dataFile);
102                        ProjectData projectData = (ProjectData)objects.readObject();
103                        System.out.println("Cobertura: Loaded information on "
104                                        + projectData.getNumberOfClasses() + " classes.");
105                        return projectData;
106                }
107                catch (IOException e) {
108                        throw e;
109                }
110                catch (Exception e)
111                {
112                        System.err.println("Cobertura: Error reading from object stream.");
113                        e.printStackTrace();
114                        return null;
115                }
116                finally
117                {
118                        if (objects != null)
119                        {
120                                try
121                                {
122                                        objects.close();
123                                }
124                                catch (IOException e)
125                                {
126                                        System.err
127                                                        .println("Cobertura: Error closing object stream.");
128                                        e.printStackTrace();
129                                }
130                        }
131                }
132        }
133
134        public static void saveCoverageData(ProjectData projectData,
135                        File dataFile)
136        {
137                FileOutputStream os = null;
138
139                //System.out.println("Cobertura: Saving coverage data to " + dataFile.getAbsolutePath());
140                try
141                {
142                        File dataDir = dataFile.getParentFile();
143                        if( (dataDir != null) && !dataDir.exists() )
144                        {
145                                dataDir.mkdirs();
146                        }
147                        os = new FileOutputStream(dataFile);
148                        saveCoverageData(projectData, os);
149                }
150                catch (IOException e)
151                {
152                        System.err.println("Cobertura: Error writing file "
153                                        + dataFile.getAbsolutePath());
154                        e.printStackTrace();
155                }
156                finally
157                {
158                        if (os != null)
159                        {
160                                try
161                                {
162                                        os.close();
163                                }
164                                catch (IOException e)
165                                {
166                                        System.err.println("Cobertura: Error closing file "
167                                                        + dataFile.getAbsolutePath());
168                                        e.printStackTrace();
169                                }
170                        }
171                }
172        }
173
174        private static void saveCoverageData(ProjectData projectData,
175                        OutputStream dataFile)
176        {
177                ObjectOutputStream objects = null;
178        
179                try
180                {
181                        objects = new ObjectOutputStream(dataFile);
182                        objects.writeObject(projectData);
183                        System.out.println("Cobertura: Saved information on " + projectData.getNumberOfClasses() + " classes.");
184                }
185                catch (IOException e)
186                {
187                        System.err.println("Cobertura: Error writing to object stream.");
188                        e.printStackTrace();
189                }
190                finally
191                {
192                        if (objects != null)
193                        {
194                                try
195                                {
196                                        objects.close();
197                                }
198                                catch (IOException e)
199                                {
200                                        System.err
201                                                        .println("Cobertura: Error closing object stream.");
202                                        e.printStackTrace();
203                                }
204                        }
205                }
206        }
207
208}