1 4 package com.tc.util; 5 6 import java.io.File ; 7 import java.io.FileOutputStream ; 8 9 12 public class AdaptedClassDumper { 13 14 private final static File adaptedRoot = getFileRoot(); 15 16 private AdaptedClassDumper() { 17 } 19 20 public synchronized static void write(String name, byte[] b) { 21 if (adaptedRoot == null) { return; } 22 23 name = name.replace('.', '/') + ".class"; 24 FileOutputStream fos = null; 25 26 try { 27 try { 28 String pattern = File.separator.replaceAll("\\\\", "\\\\\\\\"); 29 String [] strings = new File (adaptedRoot, name).getAbsolutePath().split(pattern); 30 31 final StringBuffer sb; 32 if (adaptedRoot.getAbsolutePath().startsWith("/")) { 33 sb = new StringBuffer ("/"); 34 } else { 35 sb = new StringBuffer (); 36 } 37 38 for (int i = 0; i < strings.length - 1; i++) { 39 sb.append(strings[i]); 40 sb.append(File.separatorChar); 41 } 42 43 File dir = new File (sb.toString()); 44 if (!dir.exists()) { 45 dir.mkdirs(); 46 } 47 48 File outFile = new File (adaptedRoot, name); 49 System.out.println("Writing resource: " + outFile); 50 fos = new FileOutputStream (outFile); 51 fos.write(b); 52 } finally { 53 if (fos != null) { 54 fos.close(); 55 } 56 } 57 } catch (Exception e) { 58 e.printStackTrace(); 59 } 60 } 61 62 private static File getFileRoot() { 63 try { 64 boolean writeToDisk = (System.getProperty("tc.classloader.writeToDisk") != null); 65 if (!writeToDisk) { return null; } 66 67 String userHome = System.getProperty("user.home"); 68 69 if (userHome != null) { 70 File homeDir = new File (userHome); 71 if (homeDir.isDirectory() && homeDir.canWrite()) { return new File (homeDir, "adapted"); } 72 } 73 74 return null; 75 } catch (Exception e) { 76 e.printStackTrace(); 78 return null; 79 } 80 } 81 82 } 83 | Popular Tags |