1 26 27 package com.opensugar.cube; 28 29 import java.io.File ; 30 import java.io.InputStream ; 31 import java.io.OutputStream ; 32 import java.io.BufferedInputStream ; 33 import java.io.BufferedOutputStream ; 34 import java.io.FileInputStream ; 35 import java.io.FileOutputStream ; 36 import java.io.IOException ; 37 import java.util.Vector ; 38 import java.util.Hashtable ; 39 import java.util.Enumeration ; 40 41 public abstract class Util { 43 44 protected static void checkNullParameter( Object parameter, String className, String methodName, String parameterName ) { 46 if ( parameter == null ) { 47 throw new IllegalArgumentException ( "Cannot call " + className + "." + methodName + " with null " + parameterName ); 48 } 49 } 50 51 protected static void checkNullArrayParameter( Object [] parameter, String className, String methodName, String parameterName ) { 54 if ( parameter == null ) { 55 throw new IllegalArgumentException ( "Cannot call " + className + "." + methodName + " with null " + parameterName ); 56 } 57 for ( int i = 0; i < parameter.length; i++ ) { 58 if ( parameter[ i ] == null ) { 59 throw new IllegalArgumentException ( "Cannot call " + className + "." + methodName + " with a " + parameterName + " parameter that contains null values" ); 60 } 61 } 62 } 63 64 public static boolean recursiveFileDelete( File file ) { 68 if ( !file.exists() ) { 69 return true; 70 } 71 72 if ( file.isDirectory() ) { 73 String [] listing = file.list(); 74 for ( int i = 0; i < listing.length; i++ ) { 75 if ( !recursiveFileDelete( new File ( file, listing[ i ] ) ) ) { 76 return false; 77 } 78 } 79 } 80 return file.delete(); 81 } 82 83 public static boolean recursiveFileCopy( File source, File target ) { 85 if ( !source.exists() ) { 86 return false; 87 } 88 89 if ( source.isDirectory() ) { 90 if ( !target.exists() ) { 91 target.mkdirs(); 92 } 93 String [] listing = source.list(); 94 for ( int i = 0; i < listing.length; i++ ) { 95 if ( !recursiveFileCopy( new File ( source, listing[ i ] ), new File ( target, listing[ i ] ) ) ) { 96 return false; 97 } 98 } 99 } 100 else { 101 try { 102 transferData( new FileInputStream ( source ), new FileOutputStream ( target ) ); 103 } 104 catch ( IOException e ) { 105 return false; 106 } 107 } 108 109 return true; 110 } 111 112 public static void transferData( InputStream source, OutputStream destination ) throws IOException { 115 BufferedInputStream bis = new BufferedInputStream ( source ); 116 BufferedOutputStream bos = new BufferedOutputStream ( destination ); 117 byte[] buffer = new byte[ 4096 ]; 118 int n; 119 IOException exception = null; 120 try { 121 while ( ( n = bis.read( buffer ) ) != -1 ) { 122 bos.write( buffer, 0, n ); 123 } 124 } 125 catch( IOException e ) { 126 exception = e; 127 } 128 finally { 129 bis.close(); 130 bos.close(); 131 } 132 133 if ( exception != null ) { 134 throw exception; 135 } 136 } 137 138 public static Number [] sortNumbers( Number [] unsorted ) { 139 Vector tmp = new Vector (); 140 for ( int i = 0; i < unsorted.length; i++ ) { 141 tmp.addElement( unsorted[ i ] ); 142 } 143 tmp = sortNumbers( tmp ); 144 Number [] sorted = new Number [ unsorted.length ]; 145 tmp.copyInto( sorted ); 146 return sorted; 147 } 148 149 private static Vector sortNumbers( Vector v ) { 150 Vector sorted = new Vector (); 151 int n; 152 while ( v.size() > 0 ) { 153 n = findIndexOfMinNumber( v ); 154 sorted.addElement( v.elementAt( n ) ); 155 v.removeElementAt( n ); 156 } 157 return sorted; 158 } 159 160 private static int findIndexOfMinNumber( Vector v ) { 161 int index = 0; 162 Number minVal; 163 Number val; 164 minVal = (Number )v.elementAt( 0 ); 165 for ( int i = 1; i < v.size(); i++ ) { 166 val = (Number )v.elementAt( i ); 167 if ( val.doubleValue() < minVal.doubleValue() ) { 168 index = i; 169 minVal = val; 170 } 171 } 172 return index; 173 } 174 175 } | Popular Tags |