1 package com.ca.commons.cbutil; 2 3 import javax.naming.NamingException ; 4 import javax.naming.directory.Attribute ; 5 import javax.naming.directory.Attributes ; 6 import java.io.*; 7 import java.util.Arrays ; 8 import java.util.Comparator ; 9 import java.util.logging.Level ; 10 import java.util.logging.Logger ; 11 12 13 22 23 public class CBCache 24 { 25 private static int counter = 0; private static int CACHEMAX = 100; private static int CACHEMIN = 50; private static File fileDir = null; private static File audioFileDir = null; private static String allFiles[]; private static String allAudioFiles[]; private static String extension = ".jpg"; private static String dirPath = "temp"; 34 35 private static Logger log = Logger.getLogger(CBCache.class.getName()); 36 37 38 47 48 public static void createCache(String currentDN, Attributes entry, String type, int size) 49 { 50 if (type.equalsIgnoreCase("audio")) 51 { 52 createAudioCache(currentDN, entry, type, size); 53 return; 54 } 55 56 fileDir = makeDir(); 57 allFiles = fileDir.list(); 58 59 currentDN = Integer.toString(currentDN.hashCode()); 60 61 for (int i = 0; i < allFiles.length; i++) { 63 if (allFiles[i].startsWith(currentDN) && type.equalsIgnoreCase("jpegPhoto") && allFiles[i].endsWith(".jpg")) 64 return; 65 else if (allFiles[i].startsWith(currentDN) && type.equalsIgnoreCase("odDocumentDOC") && allFiles[i].endsWith(".doc")) 66 return; 67 else if (allFiles[i].startsWith(currentDN) && type.equalsIgnoreCase("odSpreadSheetXLS") && allFiles[i].endsWith(".xls")) 68 return; 69 else if (allFiles[i].startsWith(currentDN) && type.equalsIgnoreCase("odMovieAVI") && allFiles[i].endsWith(".avi")) 70 return; 71 else if (allFiles[i].startsWith(currentDN) && type.equalsIgnoreCase("odSoundWAV") && allFiles[i].endsWith(".wav")) 72 return; 73 else if (allFiles[i].startsWith(currentDN) && type.equalsIgnoreCase("odMusicMID") && allFiles[i].endsWith(".mid")) 74 return; 75 } 76 77 for (int i = 0; i < size; i++) 78 { 79 byte[] bytes = getAttByteValue(type, entry, i); 81 if (type.equalsIgnoreCase("odDocumentDOC")) 82 { 83 doNormalCache(currentDN, bytes, ".doc"); 84 } 85 else if (type.equalsIgnoreCase("odSpreadSheetXLS")) 86 { 87 doNormalCache(currentDN, bytes, ".xls"); 88 } 89 else if (type.equalsIgnoreCase("odMovieAVI")) 90 { 91 doNormalCache(currentDN, bytes, ".avi"); 92 } 93 else if (type.equalsIgnoreCase("odMusicMID")) 94 { 95 doNormalCache(currentDN, bytes, ".mid"); 96 } 97 else if (type.equalsIgnoreCase("odSoundWAV")) 98 { 99 doNormalCache(currentDN, bytes, ".wav"); 100 } 101 else if (type.equalsIgnoreCase("jpegPhoto")) 102 { 103 doNormalCache(currentDN, bytes, ".jpg"); 104 } 105 } 106 if (allFiles.length > CACHEMAX) decreaseCacheSize(); 108 } 109 110 111 120 121 public static void createAudioCache(String currentDN, Attributes entry, String type, int size) 122 { 123 if (!type.equalsIgnoreCase("audio")) 124 { 125 log.warning("Error - trying to create a audio temporary cache with incorrect data in 'CBCache.createAudioCache'."); 126 return; 127 } 128 129 audioFileDir = makeAudioDir(); 130 allAudioFiles = audioFileDir.list(); 131 132 currentDN = Integer.toString(currentDN.hashCode()); 133 134 for (int i = 0; i < allAudioFiles.length; i++) { 136 if (allAudioFiles[i].startsWith(currentDN)) 137 return; 138 } 139 140 for (int i = 0; i < size; i++) 141 { 142 byte[] bytes = getAttByteValue(type, entry, i); 144 if (type.equalsIgnoreCase("audio")) 145 { 146 doAudioCache(currentDN, bytes); 147 } 148 } 149 if (allAudioFiles.length > CACHEMAX) decreaseAudioCacheSize(); 151 } 152 153 154 161 162 public static void doNormalCache(String currentDN, byte[] bytes, String extension) 163 { 164 String name = currentDN + counter + extension; 165 File file = new File(fileDir, name); 166 167 counter++; 168 169 try 170 { 171 file.deleteOnExit(); 173 FileOutputStream output = new FileOutputStream(file); 174 output.write(bytes); 175 output.close(); 176 } 177 catch (Exception e) 178 { 179 CBUtility.error(CBIntText.get("Problem writing the temporary file: ") + file.toString() + "\n" + e); 180 } 181 } 182 183 184 213 214 public static void doAudioCache(String currentDN, byte[] bytes) 215 { 216 byte[] testBytes = new byte[100]; 217 if (bytes.length > 100) 218 System.arraycopy(bytes, 0, testBytes, 0, 100); 219 else 220 testBytes = bytes; 221 222 String hexSub = CBParse.bytes2Hex(testBytes); 223 224 if (hexSub.indexOf("574156") > -1) 226 extension = ".wav"; 227 else if (hexSub.indexOf("494433") > -1) 228 extension = ".mp3"; 229 else if ((hexSub.toLowerCase()).indexOf("fffb9044") > -1) 230 extension = ".mp3"; 231 else if ((hexSub.toLowerCase()).indexOf("fffb300c") > -1) 232 extension = ".mp3"; 233 else if ((hexSub.toLowerCase()).indexOf("fff330c0") > -1) 234 extension = ".mp3"; 235 else if (hexSub.indexOf("524946") > -1) 236 extension = ".rmi"; 237 else if ((hexSub.toLowerCase()).indexOf("524d46") > -1) 238 extension = ".ram"; 239 else if (hexSub.indexOf("41494646") > -1) 240 extension = ".aiff"; 241 else if ((hexSub.toLowerCase()).indexOf("4d546864") > -1) 242 extension = ".mid"; 243 else if ((hexSub.toLowerCase()).indexOf("2e736e64") > -1) 244 extension = ".au"; 245 else if ((hexSub.toLowerCase()).indexOf("636f6f6c") > -1) 246 extension = ".stm"; else if ((hexSub.toLowerCase()).indexOf("437265617469766520566f6963652046696c65") > -1) 248 extension = ".voc"; else if ((hexSub.toLowerCase()).indexOf("457874656e646564204d6f64756c65") > -1) 250 extension = ".xm"; else if ((hexSub.toLowerCase()).indexOf("5343524d") > -1) 252 extension = ".s3m"; 253 else if ((hexSub.toLowerCase()).indexOf("494d50") > -1) 254 extension = ".it"; 255 else 256 extension = ".xxx"; 257 258 File file = new File(audioFileDir, currentDN + counter + extension); 259 counter++; 260 261 file.deleteOnExit(); try 263 { 264 FileOutputStream output = new FileOutputStream(file); 265 output.write(bytes); 266 output.close(); 267 } 268 catch (IOException e) 269 { 270 CBUtility.error(CBIntText.get("Problem writing the audio temporary file: ") + file.toString() + "\n" + e); 271 } 272 } 273 274 275 278 279 public static class FileComparator implements Comparator 280 { 281 287 288 public int compare(Object o1, Object o2) 289 { 290 long a = ((File) o1).lastModified(); 291 long b = ((File) o2).lastModified(); 292 293 return (a == b) ? 0 : ((a < b) ? -1 : 1); 294 } 295 } 296 297 298 304 305 public static File[] sortFiles(File[] files) 306 { 307 Arrays.sort(files, new FileComparator()); 308 return files; 309 } 310 311 312 317 318 public static File getCacheDirectory() 319 { 320 return fileDir; 321 } 322 323 324 329 330 public static File getAudioCacheDirectory() 331 { 332 return audioFileDir; 333 } 334 335 336 343 344 public static void decreaseCacheSize() 345 { 346 File[] fileArray = fileDir.listFiles(); 347 348 fileArray = sortFiles(fileArray); 350 for (int i = 0; i < (fileArray.length - CACHEMIN); i++) { 352 String fileName = fileArray[i].getName(); 353 354 for (int x = 0; x < allFiles.length; x++) { 356 if (allFiles[x].startsWith(fileName.substring(0, ((fileArray[i].toString()).length()) - 15))) 357 { 358 362 File tempFile = new File(makeDir(), allFiles[x].toString()); 363 tempFile.delete(); 364 } 365 } 366 } 367 decreaseAudioCacheSize(); 368 } 369 370 371 378 379 public static void decreaseAudioCacheSize() 380 { 381 File[] fileArray = audioFileDir.listFiles(); 382 383 fileArray = sortFiles(fileArray); 385 for (int i = 0; i < (fileArray.length - CACHEMIN); i++) { 387 String fileName = fileArray[i].getName(); 388 389 for (int x = 0; x < allAudioFiles.length; x++) { 391 if (allAudioFiles[x].startsWith(fileName.substring(0, ((fileArray[i].toString()).length()) - 15))) 392 { 393 397 File tempFile = new File(makeAudioDir(), allAudioFiles[x].toString()); 398 tempFile.delete(); 399 } 400 } 401 } 402 } 403 404 405 410 411 private static File makeDir() 412 { 413 File dir = new File(dirPath); 414 dir.mkdir(); 415 dir.deleteOnExit(); 416 417 return dir; 418 } 419 420 421 426 427 private static File makeAudioDir() 428 { 429 File dir = new File(dirPath + File.separator + "audio"); 430 dir.mkdir(); 431 dir.deleteOnExit(); 432 433 return dir; 434 } 435 436 437 442 443 public static String getDirPath() 444 { 445 return makeDir().getAbsolutePath(); 446 } 447 448 449 454 455 public static String setDirPath(String path) 456 { 457 return dirPath = path; 458 } 459 460 461 466 467 public static String getAudioDirPath() 468 { 469 return makeAudioDir().getAbsolutePath(); 470 } 471 472 473 481 482 private static byte[] getAttByteValue(String name, Attributes entry, int entries) 483 { 484 try 485 { 486 Attribute a = entry.get(name); 487 if (a == null) return null; if (a.size() == 0 || a.get() == null) return null; 490 Object o = a.get(entries); 492 if (o instanceof byte[]) 493 return (byte[]) o; 494 495 return null; 496 } 497 catch (NamingException e) 498 { 499 log.log(Level.WARNING, "Form Value Error getting value for " + name + " value :\n ", e); return null; 501 } 502 } 503 504 505 510 511 public static void cleanCache(String currentDN) 512 { 513 currentDN = Integer.toString(currentDN.hashCode()); 514 515 allFiles = makeDir().list(); 516 517 for (int i = 0; i < allFiles.length; i++) 518 { 519 if (allFiles[i].startsWith(currentDN.toString())) { 521 File tempFile = new File(fileDir, allFiles[i].toString()); 522 tempFile.delete(); 523 } 524 } 525 526 allAudioFiles = makeAudioDir().list(); 527 528 for (int i = 0; i < allAudioFiles.length; i++) 529 { 530 if (allAudioFiles[i].startsWith(currentDN.toString())) { 532 File tempFile = new File(audioFileDir, allAudioFiles[i].toString()); 533 tempFile.delete(); 534 } 535 } 536 } 537 538 539 542 543 public static void cleanCache() 544 { 545 allFiles = makeDir().list(); 546 547 if (allFiles == null) 548 return; 549 550 for (int i = 0; i < allFiles.length; i++) 551 { 552 File tempFile = new File(makeDir(), allFiles[i].toString()); 553 tempFile.delete(); 554 } 555 556 allAudioFiles = makeAudioDir().list(); 557 558 for (int i = 0; i < allAudioFiles.length; i++) 559 { 560 File tempFile = new File(makeAudioDir(), allAudioFiles[i].toString()); 561 tempFile.delete(); 562 } 563 } 564 565 566 571 572 public static void setMaxCacheSize(int size) 573 { 574 CACHEMAX = size; 575 } 576 577 578 583 584 public static void setMinCacheSize(int size) 585 { 586 CACHEMIN = size; 587 } 588 589 } | Popular Tags |