1 9 package com.sqlmagic.tinysql; 10 11 import java.util.*; 12 import java.lang.*; 13 import java.io.*; 14 import java.sql.Types ; 15 16 17 22 public class Utils 23 { 24 final static String encode = "Cp1252"; 32 36 public final static byte[] intToLittleEndian(int val) 37 { 38 byte[] b = new byte[4]; 39 for (int i = 0; i < 4; i++) { 40 b[i] = (byte)(val % 256); 41 val = val / 256; 42 } 43 return b; 44 } 45 46 47 51 public final static byte[] shortToLittleEndian(short val) 52 { 53 byte[] b = new byte[2]; 54 for (int i = 0; i < 2; i++) { 55 b[i] = (byte)(val % 256); 56 val = (short)(val / 256); 57 } 58 return b; 59 } 60 61 62 72 public final static double vax_to_long(byte[] b) { 73 74 78 long lngTmp = (long)(0x0ffL & b[0]) 81 | ( (0x0ffL & (long)b[1]) << 8 ) 82 | ( (0x0ffL & (long)b[2]) << 16 ) 83 | ( (0x0ffL & (long)b[3]) << 24 ); 84 85 86 return((double)lngTmp); 87 88 } 89 90 91 101 public final static int vax_to_short(byte[] b) { 102 return (int) ( fixByte(b[0]) + ( fixByte(b[1]) * 256)); 103 } 104 105 106 111 public final static short fixByte (byte b) { 112 113 if (b < 0) { 114 return (short) ( b + 256); 115 } 116 return b; 117 } 118 119 126 public final static String forceToSize(String str, int size, String padChar) 127 { 128 if (str != null && str.length() == size) 129 return str; 130 131 StringBuffer tmp; 132 if (str == null) 133 tmp = new StringBuffer (size); 134 else 135 tmp = new StringBuffer (str); 136 137 if (tmp.length() > size) { 138 return tmp.toString().substring(0, size); } 140 else { 141 StringBuffer pad = new StringBuffer (size); 143 int numBlanks = size - tmp.length(); 144 for (int p = 0; p < numBlanks; p++) { 145 pad.append(padChar); 146 } 147 return tmp.append(pad).toString(); 148 } 149 } 150 151 152 159 public final static byte[] forceToSize(String str, int size, byte padByte) throws java.io.UnsupportedEncodingException 160 { 161 if (str != null && str.length() == size) 162 return str.getBytes(encode); 163 164 byte[] result = new byte[size]; 165 166 if (str == null) { 167 for (int ii = 0; ii<size; ii++) result[ii] = padByte; 168 return result; 169 } 170 171 if (str.length() > size) 172 return str.substring(0, size).getBytes(encode); 174 byte[] tmp = str.getBytes(encode); 176 for (int jj = 0; jj < tmp.length; jj++) 177 result[jj] = tmp[jj]; 178 for (int kk = tmp.length; kk < size; kk++) 179 result[kk] = padByte; 180 return result; 181 } 182 183 184 187 public final static void delFile (String fname) throws NullPointerException , IOException 188 { 189 File f = new File( fname ); 190 191 if (f.exists()) 194 { 195 if (!f.delete()) { 198 throw new IOException("Could not delete: " + f.getAbsolutePath() + "."); 199 } 200 } 201 } 202 public final static void delFile (String dataDir, String fname) throws NullPointerException , IOException { 203 204 File f = new File( dataDir + File.separator + fname ); 205 206 if (f.exists()) { 209 if (!f.delete()) { 212 throw new IOException("Could not delete file: " + 213 dataDir + "/" + fname + "."); 214 } 215 } 216 } 217 218 219 223 public final static boolean renameFile(String oldName, String newName) 224 { 225 File f_old = new File(oldName); 226 File f_new = new File(newName); 227 boolean ret = f_old.renameTo(f_new); 228 return ret; 229 } 230 231 232 237 public final static String stripPathAndExtension(final String file) 238 { 239 String sep = File.separator; 240 int begin = file.lastIndexOf(sep); 241 if (begin < 0) begin = 0; 242 else begin++; 243 int end = file.lastIndexOf("."); 244 if (end < 0) end = file.length(); 245 String str = file.substring(begin, end); 246 return str; 247 } 248 249 250 254 public final static Vector getAllFiles(final String path, final String suffix) 255 { 256 Vector vec = (Vector)null; 257 String [] fileNameList; 258 File currentDir,f; 259 String fileName,upperSuffix; 260 int i; 261 upperSuffix = suffix.toUpperCase(); 262 currentDir = new File(path); 263 fileNameList = currentDir.list(); 264 if(fileNameList == null) 265 { 266 System.out.println("*** null for " + path); 267 } else { 268 vec = new Vector(fileNameList.length); 269 for( i = 0; i < fileNameList.length; i++ ) 270 { 271 f = new File(fileNameList[i]); 272 if(!f.isDirectory()) 273 { 274 fileName = f.getPath().toString().toUpperCase(); 275 if (upperSuffix == null | fileName.endsWith(upperSuffix)) 277 { 278 vec.addElement(f); 279 } 280 } 281 } 282 } 283 return vec; 284 } 285 public static boolean isDateColumn(int columnType) 286 { 287 if ( columnType == Types.DATE | columnType == Types.TIMESTAMP ) 288 return true; 289 else return false; 290 } 291 public static boolean isCharColumn(int columnType) 292 { 293 if (columnType == Types.CHAR || columnType == Types.VARCHAR || 294 columnType == Types.LONGVARCHAR) return true; 295 else return false; 296 } 297 public static boolean isNumberColumn(int columnType) 298 { 299 if (columnType == Types.NUMERIC || columnType == Types.INTEGER || 300 columnType == Types.TINYINT || columnType == Types.SMALLINT || 301 columnType == Types.BIGINT || columnType == Types.FLOAT || 302 columnType == Types.DOUBLE || columnType == Types.REAL) return true; 303 else return false; 304 } 305 308 public static void setPriority(Vector inputList,String inputTable) 309 { 310 String tableName; 311 int i; 312 if ( inputList == (Vector)null ) return; 313 for ( i = 0; i < inputList.size(); i++ ) 314 { 315 tableName = (String )inputList.elementAt(i); 316 if ( tableName.equals(inputTable) ) 317 { 318 if ( i > 0 ) 319 { 320 inputList.removeElementAt(i); 321 inputList.insertElementAt(tableName,0); 322 } 323 break; 324 } 325 } 326 } 327 328 332 final static boolean debug=false; 333 final static void log(String id, String str) 334 { 335 if (debug) log(id + ": " + str); 336 } 337 final static void log(String str) 338 { 339 if (debug) System.out.println(str); 340 } 341 342 } | Popular Tags |