Clover coverage report - DrJava Test Coverage (drjava-20110828-r5448)
Coverage timestamp: Sun Aug 28 2011 03:13:33 CDT
file stats: LOC: 199   Methods: 11
NCLOC: 90   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
JarBuilder.java 68.8% 75.5% 72.7% 73.7%
coverage coverage
 1    /*BEGIN_COPYRIGHT_BLOCK
 2    *
 3    * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu)
 4    * All rights reserved.
 5    *
 6    * Redistribution and use in source and binary forms, with or without
 7    * modification, are permitted provided that the following conditions are met:
 8    * * Redistributions of source code must retain the above copyright
 9    * notice, this list of conditions and the following disclaimer.
 10    * * Redistributions in binary form must reproduce the above copyright
 11    * notice, this list of conditions and the following disclaimer in the
 12    * documentation and/or other materials provided with the distribution.
 13    * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
 14    * names of its contributors may be used to endorse or promote products
 15    * derived from this software without specific prior written permission.
 16    *
 17    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18    * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 19    * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 20    * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 21    * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 22    * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 23    * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 24    * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 25    * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 26    * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 27    * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28    *
 29    * This software is Open Source Initiative approved Open Source Software.
 30    * Open Source Initative Approved is a trademark of the Open Source Initiative.
 31    *
 32    * This file is part of DrJava. Download the current version of this project
 33    * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
 34    *
 35    * END_COPYRIGHT_BLOCK*/
 36   
 37    package edu.rice.cs.util.jar;
 38   
 39    import java.io.*;
 40    import java.util.jar.JarEntry;
 41    import java.util.jar.JarOutputStream;
 42    import java.util.jar.Manifest;
 43   
 44    public class JarBuilder {
 45    private JarOutputStream _output;
 46   
 47    /** Creates a file file without a manifest
 48    *
 49    * @param file the file to write the jar to
 50    * @throws IOException thrown if the file cannot be opened for writing
 51    */
 52  4 public JarBuilder(File file) throws IOException {
 53  4 _output = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(file)), ManifestWriter.DEFAULT);
 54    }
 55   
 56    /** Creates an empty jar file with the given manifest
 57    *
 58    * @param jar the file to write the jar to
 59    * @param manifest the file that is the manifest for the archive
 60    * @throws IOException thrown if either file cannot be opened for reading
 61    */
 62  0 public JarBuilder(File jar, File manifest) throws IOException {
 63  0 _output = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(jar)), new Manifest(new FileInputStream(manifest)));
 64    }
 65   
 66    /** Creates an empty jar file with the given manifest
 67    *
 68    * @param jar the file to write the jar to
 69    * @param manifest the manifest file for the jar
 70    * @see ManifestWriter
 71    */
 72  0 public JarBuilder(File jar, Manifest manifest) {
 73  0 try {
 74  0 _output = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(jar)), manifest);
 75    }
 76    catch (IOException e) {
 77  0 e.printStackTrace();
 78    }
 79    }
 80   
 81    /** Takes a parent name and a field name and returns the concatenation of them correctly
 82    *
 83    * @param parent The parent directory
 84    * @param name The name of the file or directory
 85    * @return the string concatenation of the parent and the name
 86    */
 87  18 private String makeName(String parent, String name) {
 88  18 String sep = "/"; // NOTE: This can be a '/' since it is a path in the jar file itself
 89  18 if( parent.equals("") )
 90  9 return name;
 91  9 if (parent.endsWith(sep))
 92  0 return parent + name;
 93  9 return parent + sep + name;
 94    }
 95   
 96    /** Adds the file to the given path and name
 97    *
 98    * @param file the file to be added
 99    * @param parent the directory to the path in which the file is to be added
 100    * @param fileName the name of the file in the archive
 101    */
 102  2 public void addFile(File file, String parent, String fileName) throws IOException {
 103  2 byte data[] = new byte[2048];
 104   
 105  2 FileInputStream fi = new FileInputStream(file.getAbsolutePath());
 106  2 BufferedInputStream origin = new BufferedInputStream(fi, 2048);
 107   
 108  2 JarEntry entry = new JarEntry(makeName(parent, fileName));
 109  2 _output.putNextEntry(entry);
 110   
 111  2 int count = origin.read(data, 0, 2048);
 112  2 while (count != -1) {
 113  2 _output.write(data, 0, count);
 114  2 count = origin.read(data, 0, 2048);
 115    }
 116   
 117  2 origin.close();
 118    }
 119   
 120    /** Add the directory into the directory specified by parent
 121    * @param dir the directory to add
 122    * @param parent the path inside the jar that the directory should be added to
 123    */
 124  1 public void addDirectoryRecursive(File dir, String parent) {
 125  1 addDirectoryRecursiveHelper(dir, parent, new byte[2048], new FileFilter() {
 126  8 public boolean accept(File pathname) { return true; }
 127    });
 128    }
 129   
 130    /** Add the directory into the directory specified by parent
 131    * @param dir the directory to add
 132    * @param parent the path inside the jar that the directory should be added to
 133    * @param filter the filter used to filter the files
 134    */
 135  2 public void addDirectoryRecursive(File dir, String parent, FileFilter filter) {
 136  2 addDirectoryRecursiveHelper(dir, parent, new byte[2048], filter);
 137    }
 138   
 139    /** Add the contents of a directory that match a filter to the archive
 140    * @param dir the directory to add
 141    * @param parent the directory to add into
 142    * @param buffer a buffer that is 2048 bytes
 143    * @param filter the FileFilter to filter the files by
 144    * @return true on success, false on failure
 145    */
 146  6 private boolean addDirectoryRecursiveHelper(File dir, String parent, byte[] buffer, FileFilter filter) {
 147  6 try {
 148  6 File[] files = dir.listFiles(filter);
 149  6 BufferedInputStream origin = null;
 150   
 151  6 if( files == null ) // listFiles may return null if there's an IO error
 152  0 return true;
 153  6 for (int i = 0; i < files.length; i++) {
 154  16 if( files[i].isFile() ) {
 155  13 origin = new BufferedInputStream(new FileInputStream(files[i]), 2048);
 156   
 157  13 JarEntry entry = new JarEntry(makeName(parent, files[i].getName()));
 158  13 _output.putNextEntry(entry);
 159   
 160  13 int count;
 161  ? while((count = origin.read(buffer, 0, 2048)) != -1) {
 162  13 _output.write(buffer, 0, count);
 163    }
 164  13 origin.close();
 165    }
 166  3 else if( files[i].isDirectory() ) {
 167  3 addDirectoryRecursiveHelper(files[i], makeName(parent, files[i].getName()),buffer,filter);
 168    }
 169    }
 170    } catch(Exception e) {
 171  0 e.printStackTrace();
 172    }
 173  6 return true;
 174    }
 175   
 176    /** Makes a directory in the jar file
 177    *
 178    * @param parent The name of the parent that the directory is to be created in
 179    * @param dirName The name of the directory to be created
 180    * @return Returns true on success, false on failure
 181    */
 182  0 public boolean makeDirectory(String parent, String dirName) {
 183  0 JarEntry entry = new JarEntry(makeName(parent, dirName));
 184  0 try {
 185  0 _output.putNextEntry(entry);
 186    }
 187    catch (IOException e) {
 188  0 return false;
 189    }
 190  0 return true;
 191    }
 192   
 193    /** Close writing on the jar file
 194    */
 195  4 public void close() throws IOException {
 196  4 _output.flush();
 197  4 _output.close();
 198    }
 199    }