1 50 51 package net.sf.just4log; 52 53 import java.io.ByteArrayInputStream ; 54 import java.io.ByteArrayOutputStream ; 55 import java.io.File ; 56 import java.io.FileInputStream ; 57 import java.io.FileOutputStream ; 58 import java.io.IOException ; 59 import java.io.InputStream ; 60 import java.io.OutputStream ; 61 import java.util.zip.ZipEntry ; 62 import java.util.zip.ZipInputStream ; 63 import java.util.zip.ZipOutputStream ; 64 65 import net.sf.just4log.transform.Transform; 66 import net.sf.just4log.util.NotClosingInputStream; 67 68 import org.apache.bcel.Repository; 69 import org.apache.bcel.classfile.ClassFormatException; 70 import org.apache.bcel.classfile.ClassParser; 71 import org.apache.bcel.classfile.JavaClass; 72 import org.apache.bcel.util.ClassPath; 73 import org.apache.bcel.util.SyntheticRepository; 74 import org.apache.commons.logging.Log; 75 import org.apache.commons.logging.LogFactory; 76 77 82 83 public class JustLog { 84 85 private static Log logger = LogFactory.getLog(JustLog.class); 86 private static String CLASSID = 87 "$Id: JustLog.java,v 1.11 2003/07/23 23:59:10 lbruand Exp $"; 88 89 92 private JustLog() { 93 super(); 94 } 95 96 public static void setClasspath(String cp) { 97 String extracp = 98 ClassPath.SYSTEM_CLASS_PATH.toString() 99 + File.pathSeparatorChar 100 + cp; 101 logger.info("using " + extracp + "as classpath."); 102 System.out.println("using " + extracp + "as classpath."); 103 Repository.setRepository( 104 SyntheticRepository.getInstance(new ClassPath(extracp))); 105 } 106 private static NotClosingInputStream input = 107 new NotClosingInputStream(null); 108 public static void speedup( 109 InputStream in, 110 String filename, 111 OutputStream out) 112 throws ClassFormatException, IOException { 113 input.setInput(in); 114 JavaClass source = (new ClassParser(input, filename)).parse(); 115 logger.info("Source size:" + source.getBytes().length); 116 JavaClass target = Transform.speedup(source); 117 logger.info("after optimization: " + target.getBytes().length); 118 byte[] tmp = target.getBytes(); 119 out.write(tmp); 120 } 121 122 130 public static void speedup(File source, File target) 131 throws ClassFormatException, IOException { 132 FileInputStream in = null; 133 FileOutputStream out = null; 134 byte[] buffer = new byte[BUFFER]; 135 int count; 136 try { 137 in = new FileInputStream (source); 138 ByteArrayOutputStream outb = new ByteArrayOutputStream (); 139 while ((count = in.read(buffer, 0, BUFFER)) != -1) { 140 outb.write(buffer, 0, count); 141 } 142 out = new FileOutputStream (target); 143 speedup( 144 new ByteArrayInputStream (outb.toByteArray()), 145 source.getName(), 146 out); 147 } finally { 148 try { 149 in.close(); 150 out.close(); 151 } catch (IOException e) { 152 logger.warn("problem with IOException " + e); 153 } catch (NullPointerException e) { 154 logger.warn("problem with Exception " + e); 155 } 156 } 157 } 158 public static final int BUFFER = 4096; 159 public static void speedupZip(InputStream input, OutputStream output) 160 throws IOException { 161 ZipInputStream in = new ZipInputStream (input); 162 ZipOutputStream out = new ZipOutputStream (output); 163 ZipEntry inentry; 164 ZipEntry outentry; 165 int count; 166 byte[] buffer = new byte[BUFFER]; 167 while ((inentry = in.getNextEntry()) != null) { 168 outentry = new ZipEntry (inentry.getName()); 169 logger.debug("Extra: " + inentry.getExtra()); 170 outentry.setExtra(inentry.getExtra()); 171 logger.debug("time: " + inentry.getTime()); 172 outentry.setTime(inentry.getTime()); 173 logger.debug("method: " + inentry.getMethod()); 174 outentry.setMethod(inentry.getMethod()); 175 logger.debug("comment: " + inentry.getComment()); 176 outentry.setComment(inentry.getComment()); 177 logger.debug("CRC: " + inentry.getCrc()); 178 if (inentry.getCrc() != -1) { 179 outentry.setCrc(inentry.getCrc()); 180 } 181 logger.debug("size: " + inentry.getSize()); 182 if (inentry.getSize() != -1) { 183 outentry.setSize(inentry.getSize()); 184 } 185 out.putNextEntry(outentry); 186 if (!inentry.isDirectory()) { 187 if (inentry.getName().endsWith(".jar") 188 || inentry.getName().endsWith(".zip") 189 || inentry.getName().endsWith(".war") 190 || inentry.getName().endsWith(".ear")) { 191 speedupZip(in, out); 192 } else if (inentry.getName().endsWith(".class")) { 193 speedup(in, inentry.getName(), out); 194 } else { 195 while ((count = in.read(buffer, 0, BUFFER)) != -1) { 196 out.write(buffer, 0, count); 197 } 198 } 199 200 } 201 out.closeEntry(); 202 in.closeEntry(); 203 } 204 out.close(); 205 in.close(); 206 } 207 } 208 | Popular Tags |