Clover coverage report - Replica - 1.0-Alpha
Coverage timestamp: Dom Fev 1 2004 17:00:58 WET
file stats: LOC: 233   Methods: 12
NCLOC: 169   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
Utilities.java 0% 0% 0% 0%
coverage
 1   
 package nmp.io;
 2   
 
 3   
 import java.io.*;
 4   
 
 5   
 /**
 6   
  * @version 0.045 12-Dec-2000
 7   
  * @author Nuno Pregui�a
 8   
  */
 9   
 public class Utilities
 10   
 {
 11   
     public final static int BUFFER_SIZE = 1024;
 12   
     public final static byte[] globbuf = new byte[BUFFER_SIZE];
 13   
     /**
 14   
     * Produces filename
 15   
     *@param path Pathname
 16   
     *@param filename Filename
 17   
     */
 18  0
     public static String produceFullPath( String path, String filename)
 19   
     {
 20  0
         char ch = java.io.File.separatorChar;
 21  0
         if( path.charAt( path.length() - 1) == ch)
 22   
         {
 23  0
             if( filename.length() == 0 || filename.charAt( 0) != ch)
 24  0
                 return path + filename;
 25   
             else
 26  0
                 return path.substring( 0, path.length() - 2) + filename;
 27   
         }
 28   
         else
 29   
         {
 30  0
             if( filename.length() == 0 || filename.charAt( 0) != ch)
 31  0
                 return path + String.valueOf( ch) + filename;
 32   
             else
 33  0
                 return path + filename;
 34   
         }
 35   
     }
 36   
 
 37   
     /**
 38   
      * Returns the serialized form of the given object
 39   
      */
 40  0
     public static byte []serialize( Object obj) throws IOException {
 41  0
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
 42  0
         ObjectOutputStream oos = new ObjectOutputStream( bos);
 43  0
         oos.writeObject( obj);
 44  0
         oos.flush();
 45  0
         oos.close();
 46  0
         bos.flush();
 47  0
         bos.close();
 48  0
         return bos.toByteArray();
 49   
     }
 50   
     /**
 51   
      * Returns the given object
 52   
      */
 53  0
     public static Object unserialize( byte []arr) throws IOException {
 54  0
         try {
 55  0
             ByteArrayInputStream bis = new ByteArrayInputStream( arr);
 56  0
             ObjectInputStream ois = new ObjectInputStream( bis);
 57  0
             Object obj = ois.readObject();
 58  0
             ois.close();
 59  0
             bis.close();
 60  0
             return obj;
 61   
         } catch( ClassNotFoundException e) {
 62  0
             throw new IOException( "Class not found exception in unserialize");
 63   
         }
 64   
     }
 65   
     
 66   
     /**
 67   
      * Cleanups outpurstream and associated file - used to delete partially downloaded
 68   
      * resources
 69   
      */
 70  0
     public static void cleanupCon( File f, OutputStream out) {
 71  0
         if( out != null)
 72  0
             try {
 73  0
                 out.close();
 74   
             } catch( Exception e0) {
 75   
                 // do nothing
 76   
             }
 77  0
         if( f != null)
 78  0
             f.delete();
 79   
     }
 80   
     
 81   
     /**
 82   
      * Dumps in to out
 83   
      */
 84  0
     public static void copy( InputStream in, OutputStream out) throws IOException {
 85  0
         byte []buffer = new byte[BUFFER_SIZE];
 86   
         
 87  0
         while( true) {
 88  0
             int len = in.read( buffer, 0, BUFFER_SIZE);
 89  0
             if( len <= 0)
 90  0
                 break;
 91  0
             out.write( buffer, 0, len);
 92   
         }
 93   
     }
 94   
 
 95   
     /**
 96   
      * Load the contents of the file f into buf (appending)
 97   
      * 
 98   
      * @return Returns true if the file has been successfully read
 99   
      */
 100  0
     public static boolean loadText( File f, StringBuffer buf) throws IOException {
 101  0
         if( buf == null)
 102  0
             return false;
 103   
 
 104  0
         BufferedReader reader = new BufferedReader( new FileReader( f));
 105  0
         char []buffer = new char[BUFFER_SIZE];
 106   
         
 107  0
         while( true) {
 108  0
             int len = reader.read( buffer, 0, BUFFER_SIZE);
 109  0
             if( len <= 0)
 110  0
                 break;
 111  0
             buf.append( buffer, 0, len);
 112   
         }
 113  0
         reader.close();
 114  0
         return true;
 115   
     }
 116   
 
 117   
     /**
 118   
      * Save the contents of buf in file f
 119   
      * 
 120   
      * @return Returns true if the file has been successfully saved
 121   
      */
 122  0
     public static boolean saveText( File f, String buf) throws IOException {
 123  0
         if( buf == null)
 124  0
             return false;
 125  0
         if( f == null)
 126  0
             return false;
 127   
 
 128  0
         BufferedWriter writer = new BufferedWriter( new FileWriter( f));
 129  0
         char []buffer = new char[BUFFER_SIZE];
 130   
         
 131  0
         int len = buf.length();
 132  0
         int curpos = 0;
 133   
         
 134  0
         while( curpos < len) {
 135  0
             int nextpos = curpos + BUFFER_SIZE;
 136  0
             if( nextpos > len)
 137  0
                 nextpos = len;
 138  0
             buf.getChars( curpos, nextpos, buffer, 0);
 139  0
             writer.write( buffer, 0, nextpos - curpos);
 140  0
             curpos = nextpos;
 141   
         }
 142  0
         writer.flush();
 143  0
         writer.close();
 144  0
         return true;
 145   
     }
 146   
     
 147   
     /**
 148   
      * Opens object output stream for appending
 149   
      */
 150  0
     public static ObjectOutputStream openAppend( File f) throws IOException, ClassNotFoundException {
 151  0
         if( ! f.exists())
 152  0
             return new ObjectOutputStream( new FileOutputStream( f));
 153  0
         File faux;
 154  0
         for( int i = 0; ; ) {
 155  0
             faux = new File( f.getAbsolutePath() + "." + i);
 156  0
             if( ! faux.exists())
 157  0
                 break;
 158   
         }
 159  0
         if( ! f.renameTo( faux))
 160  0
             throw new IOException( "Unable to rename");
 161  0
         ObjectInputStream ois = new ObjectInputStream( new BufferedInputStream( new FileInputStream( faux)));
 162  0
         ObjectOutputStream oos = new ObjectOutputStream( new BufferedOutputStream( new FileOutputStream( f)));
 163  0
         for( ; ; ) {
 164  0
             Object obj = ois.readObject();
 165  0
             if( obj == null)
 166  0
                 break;
 167  0
             oos.writeObject( obj);
 168   
         }
 169  0
         ois.close();
 170  0
         return oos;
 171   
     }
 172   
     
 173   
     /**
 174   
      * Merges all file with name " prefix | int | suffix " into file filename
 175   
      * 
 176   
      * @param delete If true original files are deleted
 177   
      */
 178  0
     public static void merge( int start, String filename, String prefix, String sufix, boolean delete) throws IOException {
 179  0
         File f = new File( filename);
 180  0
         BufferedOutputStream bos = null;
 181  0
         if( f.exists())
 182  0
             bos = new BufferedOutputStream( new FileOutputStream( filename, true));
 183   
         else
 184  0
             bos = new BufferedOutputStream( new FileOutputStream( filename));
 185  0
         for( int i = start; ; i++) {
 186  0
             File f2 = new File( prefix + i + sufix);
 187  0
             if( ! f2.exists())
 188  0
                 break;
 189  0
             BufferedInputStream bis = new BufferedInputStream( new FileInputStream( f2));
 190  0
             copy( bis, bos);
 191  0
             bis.close();
 192  0
             if( delete)
 193  0
                 f2.delete();
 194   
         }
 195  0
         bos.flush();
 196  0
         bos.close();
 197   
     }
 198   
     
 199   
     /**
 200   
      * Copies files from src to dst
 201   
      */
 202  0
     public static void copyFile( String src, String dst) throws IOException{
 203  0
         copyFile( new File(src), new File(dst));
 204   
     }
 205  0
     public static void copyFile( File src, File dst) throws IOException{
 206  0
         InputStream is = new FileInputStream( src);
 207  0
         OutputStream os = new FileOutputStream( dst);
 208  0
         for( ; ; ) {
 209  0
             int count = is.read( globbuf);
 210  0
             if( count <= 0)
 211  0
                 break;
 212  0
             os.write( globbuf, 0, count);
 213   
         }
 214  0
         os.flush();
 215  0
         os.close();
 216  0
         is.close();
 217   
     }
 218   
     /**
 219   
      * clean dir
 220   
      */
 221  0
     public static void cleanDir( String dir) {
 222  0
         File f = new File( dir);
 223  0
         if( ! f.isDirectory())
 224  0
             return;
 225  0
         File[] fs = f.listFiles();
 226  0
         if( fs != null) {
 227  0
             for( int i = 0; i < fs.length; i++) {
 228  0
                 fs[i].delete();
 229   
             }
 230   
         }
 231   
     }
 232   
 }
 233