1 23 24 26 27 29 package com.sun.jdo.api.persistence.enhancer; 30 31 32 34 import java.io.IOException ; 35 import java.io.InputStream ; 36 import java.io.OutputStream ; 37 import java.io.ByteArrayInputStream ; 38 import java.io.ByteArrayOutputStream ; 39 40 import java.util.zip.CRC32 ; 41 import java.util.zip.ZipEntry ; 42 import java.util.zip.ZipInputStream ; 43 import java.util.zip.ZipOutputStream ; 44 45 46 51 53 public class ByteCodeEnhancerHelper 54 { 55 56 57 71 72 public static final boolean enhanceClassFile (ByteCodeEnhancer enhancer, 73 InputStream in, 74 OutputStream out) 75 throws EnhancerUserException, 76 EnhancerFatalError 77 { 78 79 return enhancer.enhanceClassFile (in, new OutputStreamWrapper (out)); 80 81 } 83 84 102 103 public static final boolean enhanceZipFile (ByteCodeEnhancer enhancer, 104 ZipInputStream zip_in, 105 ZipOutputStream zip_out) 106 throws EnhancerUserException, 107 EnhancerFatalError 108 { 109 110 boolean enhanced = false; 111 try 112 { 113 CRC32 crc32 = new CRC32 (); 114 ZipEntry entry; 115 while ((entry = zip_in.getNextEntry ()) != null) 116 { 117 InputStream in = zip_in; 118 ZipEntry out_entry = new ZipEntry (entry); 119 120 if (isClassFileEntry (entry)) { 123 in = openZipEntry (zip_in); 127 in.mark (Integer.MAX_VALUE); 128 ByteArrayOutputStream tmp = new ByteArrayOutputStream (); 129 if (enhancer.enhanceClassFile (in, tmp)) 130 { 131 enhanced = true; 132 byte [] bytes = tmp.toByteArray (); 133 tmp.close (); 134 in.close (); 135 modifyZipEntry (out_entry, bytes, crc32); 136 in = new ByteArrayInputStream (bytes); 137 } 138 else 139 { 140 in.reset (); 142 } 143 } 144 145 zip_out.putNextEntry (out_entry); 147 copyZipEntry (in, zip_out); 148 zip_out.closeEntry (); 149 150 if (in != zip_in) 151 { 152 in.close (); 153 } 154 } 155 } 156 catch (IOException ex) 157 { 158 throw new EnhancerFatalError (ex); 159 } 160 161 return enhanced; 162 163 } 165 166 174 175 private static final void copyZipEntry (InputStream in, 176 OutputStream out) 177 throws IOException 178 { 179 180 int b; 181 while ((in.available () > 0) && (b = in.read ()) > -1) 182 { 183 out.write (b); 184 } 185 186 } 188 189 201 202 private static final InputStream openZipEntry (ZipInputStream in) 203 throws IOException 204 { 205 206 ByteArrayOutputStream out = new ByteArrayOutputStream (); 207 copyZipEntry (in, out); 208 209 return new ByteArrayInputStream (out.toByteArray ()); 210 211 } 213 214 224 225 private static final void modifyZipEntry (ZipEntry entry, 226 byte [] bytes, 227 CRC32 crc32) 228 { 229 230 entry.setSize (bytes.length); 231 if (entry.getMethod () == 0) { 233 crc32.reset (); 234 crc32.update (bytes); 235 entry.setCrc (crc32.getValue ()); 236 entry.setCompressedSize (bytes.length); 237 } 238 239 } 241 242 247 248 private static final boolean isClassFileEntry (ZipEntry entry) 249 { 250 251 return entry.getName ().endsWith (".class"); 252 253 } 255 256 } 258 259 | Popular Tags |