Coverage report

  %line %branch
net.sf.infrared.tools.util.JarHelper
0% 
0% 

 1  
 /*
 2  
  *
 3  
  * Copyright 2005 Tavant Technologies and Contributors
 4  
  * 
 5  
  * Licensed under the Apache License, Version 2.0 (the "License")
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  *     http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  *
 17  
  *
 18  
  *
 19  
  * Original Author:  Patrick Calahan
 20  
  * Contributor(s):   kaushal.kumar (Tavant Technologies);
 21  
  *
 22  
  */
 23  
 package net.sf.infrared.tools.util;
 24  
 
 25  
 import java.io.*;
 26  
 import java.util.jar.JarOutputStream;
 27  
 import java.util.jar.JarEntry;
 28  
 import java.util.jar.JarInputStream;
 29  
 
 30  
 /**
 31  
  * Provides utility services for jarring and unjarring files and directories.
 32  
  * Note that a given instance of JarHelper is not threadsafe with respect to
 33  
  * multiple jar operations.
 34  
  * 
 35  
  * @author Patrick Calahan <pcal@bea.com>
 36  
  */
 37  
 public class JarHelper {
 38  
     // ========================================================================
 39  
     // Constants
 40  
 
 41  
     private static final int BUFFER_SIZE = 64;
 42  
 
 43  
     // ========================================================================
 44  
     // Variables
 45  
 
 46  0
     private byte[] mBuffer = new byte[BUFFER_SIZE];
 47  
 
 48  0
     private int mByteCount = 0;
 49  
 
 50  0
     private boolean mVerbose = false;
 51  
 
 52  
     // ========================================================================
 53  
     // Constructor
 54  
 
 55  
     /**
 56  
      * Instantiates a new JarHelper.
 57  
      */
 58  0
     public JarHelper() {
 59  0
     }
 60  
 
 61  
     // ========================================================================
 62  
     // Public methods
 63  
 
 64  
     /**
 65  
      * Jars a given directory or single file into a JarOutputStream.
 66  
      */
 67  
     public void jarDir(File dirOrFile2Jar, File destJar) throws IOException {
 68  0
         FileOutputStream fout = new FileOutputStream(destJar);
 69  0
         JarOutputStream jout = new JarOutputStream(fout);
 70  
         // jout.setLevel(0);
 71  
         try {
 72  0
             jarDir(dirOrFile2Jar, jout, null);
 73  0
         } catch (IOException ioe) {
 74  0
             throw ioe;
 75  
         } finally {
 76  0
             jout.close();
 77  0
             fout.close();
 78  0
         }
 79  0
     }
 80  
     
 81  
     /**
 82  
      * Jars a given array of directories or files into a JarOutputStream.
 83  
      */
 84  
     public void jarDirs(File[] dirsOrFiles2Jar, File destJar) throws IOException {
 85  0
         FileOutputStream fout = new FileOutputStream(destJar);
 86  0
         JarOutputStream jout = new JarOutputStream(fout);
 87  
         try {
 88  0
             for (int i = 0; i < dirsOrFiles2Jar.length; i++) {
 89  0
                 jarDir(dirsOrFiles2Jar[i], jout, null);
 90  
             }
 91  0
         } catch (IOException ioe) {
 92  0
             throw ioe;
 93  
         } finally {
 94  0
             jout.close();
 95  0
             fout.close();
 96  0
         }
 97  0
     }
 98  
 
 99  
     /**
 100  
      * Unjars a given jar file into a given directory.
 101  
      */
 102  
     public void unjarDir(File jarFile, File destDir) throws IOException {
 103  0
         BufferedOutputStream dest = null;
 104  0
         FileInputStream fis = new FileInputStream(jarFile);
 105  0
         unjar(fis, destDir);
 106  0
     }
 107  
 
 108  
     /**
 109  
      * Given an InputStream on a jar file, unjars the contents into the given
 110  
      * directory.
 111  
      */
 112  
     public void unjar(InputStream in, File destDir) throws IOException {
 113  0
         BufferedOutputStream dest = null;
 114  0
         JarInputStream jis = new JarInputStream(in);
 115  
         JarEntry entry;
 116  0
         while ((entry = jis.getNextJarEntry()) != null) {
 117  0
             if (entry.isDirectory()) {
 118  0
                 File dir = new File(destDir, entry.getName());
 119  0
                 dir.mkdir();
 120  0
                 if (entry.getTime() != -1)
 121  0
                     dir.setLastModified(entry.getTime());
 122  
                 continue;
 123  
             }
 124  
             int count;
 125  0
             byte data[] = new byte[BUFFER_SIZE];
 126  0
             File destFile = new File(destDir, entry.getName());
 127  0
             if (!destFile.exists()) {
 128  0
                 String tempStr = destFile.getParent();
 129  0
                 File tempFile = new File(tempStr);
 130  0
                 tempFile.mkdirs();
 131  
             }
 132  0
             if (mVerbose)
 133  0
                 System.out.println("unjarring " + destFile + " from " + entry.getName());
 134  0
             FileOutputStream fos = new FileOutputStream(destFile);
 135  0
             dest = new BufferedOutputStream(fos, BUFFER_SIZE);
 136  0
             while ((count = jis.read(data, 0, BUFFER_SIZE)) != -1) {
 137  0
                 dest.write(data, 0, count);
 138  
             }
 139  0
             dest.flush();
 140  0
             dest.close();
 141  0
             if (entry.getTime() != -1)
 142  0
                 destFile.setLastModified(entry.getTime());
 143  
         }
 144  0
         jis.close();
 145  0
     }
 146  
 
 147  
     public void setVerbose(boolean b) {
 148  0
         mVerbose = b;
 149  0
     }
 150  
 
 151  
     // ========================================================================
 152  
     // Private methods
 153  
 
 154  
     private static final char SEP = '/';
 155  
 
 156  
     /**
 157  
      * Recursively jars up the given path under the given directory.
 158  
      */
 159  
     private void jarDir(File dirOrFile2jar, JarOutputStream jos, String path) throws IOException {
 160  0
         if (mVerbose)
 161  0
             System.out.println("checking " + dirOrFile2jar);
 162  0
         if (dirOrFile2jar.isDirectory()) {
 163  0
             String[] dirList = dirOrFile2jar.list();
 164  0
             String subPath = (path == null) ? "" : (path + dirOrFile2jar.getName() + SEP);
 165  0
             if (path != null) {
 166  0
                 JarEntry je = new JarEntry(subPath);
 167  0
                 je.setTime(dirOrFile2jar.lastModified());
 168  0
                 jos.putNextEntry(je);
 169  0
                 jos.flush();
 170  0
                 jos.closeEntry();
 171  
             }
 172  0
             for (int i = 0; i < dirList.length; i++) {
 173  0
                 File f = new File(dirOrFile2jar, dirList[i]);
 174  0
                 jarDir(f, jos, subPath);
 175  
             }
 176  
         } else {
 177  0
             if (mVerbose)
 178  0
                 System.out.println("adding " + dirOrFile2jar);
 179  0
             FileInputStream fis = new FileInputStream(dirOrFile2jar);
 180  
             try {
 181  0
                 JarEntry entry = new JarEntry(path + dirOrFile2jar.getName());
 182  0
                 entry.setTime(dirOrFile2jar.lastModified());
 183  0
                 jos.putNextEntry(entry);
 184  0
                 while ((mByteCount = fis.read(mBuffer)) != -1) {
 185  0
                     jos.write(mBuffer, 0, mByteCount);
 186  0
                     if (mVerbose)
 187  0
                         System.out.println("wrote " + mByteCount + " bytes");
 188  
                 }
 189  0
                 jos.flush();
 190  0
                 jos.closeEntry();
 191  0
             } catch (IOException ioe) {
 192  0
                 throw ioe;
 193  
             } finally {
 194  0
                 fis.close();
 195  0
             }
 196  
         }
 197  0
     }
 198  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.