1 22 23 package net.sourceforge.cobertura.coveragedata; 24 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.FileOutputStream ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.io.ObjectInputStream ; 31 import java.io.ObjectOutputStream ; 32 import java.io.OutputStream ; 33 34 38 public abstract class CoverageDataFileHandler implements HasBeenInstrumented 39 { 40 41 44 public static final String FILE_NAME = "cobertura.ser"; 45 46 public static File getDefaultDataFile() 47 { 48 String systemProperty = System 49 .getProperty("net.sourceforge.cobertura.datafile"); 50 if (systemProperty != null) 51 return new File (systemProperty); 52 return new File (FILE_NAME); 53 } 54 55 public static ProjectData loadCoverageData(File dataFile) 56 { 57 InputStream is = null; 58 59 try 61 { 62 is = new FileInputStream (dataFile); 63 return loadCoverageData(is); 64 } 65 catch (IOException e) 66 { 67 System.err.println("Cobertura: Error reading file " 68 + dataFile.getAbsolutePath() + ": " 69 + e.getLocalizedMessage()); 70 return null; 71 } 72 finally 73 { 74 if (is != null) 75 try 76 { 77 is.close(); 78 } 79 catch (IOException e) 80 { 81 System.err.println("Cobertura: Error closing file " 82 + dataFile.getAbsolutePath() + ": " 83 + e.getLocalizedMessage()); 84 } 85 } 86 } 87 88 private static ProjectData loadCoverageData(InputStream dataFile) throws IOException 89 { 90 ObjectInputStream objects = null; 91 92 try 93 { 94 objects = new ObjectInputStream (dataFile); 95 ProjectData projectData = (ProjectData)objects.readObject(); 96 System.out.println("Cobertura: Loaded information on " 97 + projectData.getNumberOfClasses() + " classes."); 98 return projectData; 99 } 100 catch (IOException e) { 101 throw e; 102 } 103 catch (Exception e) 104 { 105 System.err.println("Cobertura: Error reading from object stream."); 106 e.printStackTrace(); 107 return null; 108 } 109 finally 110 { 111 if (objects != null) 112 { 113 try 114 { 115 objects.close(); 116 } 117 catch (IOException e) 118 { 119 System.err 120 .println("Cobertura: Error closing object stream."); 121 e.printStackTrace(); 122 } 123 } 124 } 125 } 126 127 public static void saveCoverageData(ProjectData projectData, 128 File dataFile) 129 { 130 FileOutputStream os = null; 131 132 try 134 { 135 os = new FileOutputStream (dataFile); 136 saveCoverageData(projectData, os); 137 } 138 catch (IOException e) 139 { 140 System.err.println("Cobertura: Error writing file " 141 + dataFile.getAbsolutePath()); 142 e.printStackTrace(); 143 } 144 finally 145 { 146 if (os != null) 147 { 148 try 149 { 150 os.close(); 151 } 152 catch (IOException e) 153 { 154 System.err.println("Cobertura: Error closing file " 155 + dataFile.getAbsolutePath()); 156 e.printStackTrace(); 157 } 158 } 159 } 160 } 161 162 private static void saveCoverageData(ProjectData projectData, 163 OutputStream dataFile) 164 { 165 ObjectOutputStream objects = null; 166 167 try 168 { 169 objects = new ObjectOutputStream (dataFile); 170 objects.writeObject(projectData); 171 System.out.println("Cobertura: Saved information on " + projectData.getNumberOfClasses() + " classes."); 172 } 173 catch (IOException e) 174 { 175 System.err.println("Cobertura: Error writing to object stream."); 176 e.printStackTrace(); 177 } 178 finally 179 { 180 if (objects != null) 181 { 182 try 183 { 184 objects.close(); 185 } 186 catch (IOException e) 187 { 188 System.err 189 .println("Cobertura: Error closing object stream."); 190 e.printStackTrace(); 191 } 192 } 193 } 194 } 195 196 } 197 | Popular Tags |