1 22 package com.izforge.izpack.compressor; 23 24 import java.io.BufferedOutputStream ; 25 import java.io.File ; 26 import java.io.FileOutputStream ; 27 import java.io.InputStream ; 28 import java.io.OutputStream ; 29 import java.lang.reflect.Constructor ; 30 import java.net.URL ; 31 import java.net.URLClassLoader ; 32 33 import com.izforge.izpack.compiler.Compiler; 34 35 36 44 45 public abstract class PackCompressorBase implements PackCompressor 46 { 47 48 protected String [] formatNames = null; 49 protected String [] containerPaths = null; 50 protected String decoderMapper = null; 51 60 protected String [][] decoderClassNames = null; 61 protected String encoderClassName = null; 62 63 protected Class [] paramsClasses = null; 64 65 private Compiler compiler; 66 private Constructor constructor; 67 private int level = -1; 68 71 public PackCompressorBase() 72 { 73 super(); 74 } 75 76 79 public String [] getContainerPaths() 80 { 81 return(containerPaths); 82 } 83 84 87 public String getEncoderClassName() 88 { 89 return(encoderClassName); 90 } 91 94 public String [][] getDecoderClassNames() 95 { 96 return(decoderClassNames); 97 } 98 99 102 public boolean useStandardCompression() 103 { 104 return( false ); 105 } 106 107 110 public String [] getCompressionFormatSymbols() 111 { 112 return(formatNames); 113 } 114 115 118 public String getDecoderMapperName() 119 { 120 return(decoderMapper); 121 } 122 123 126 public void setCompiler(Compiler compiler) 127 { 128 this.compiler = compiler; 129 } 130 131 134 public void setCompressionLevel(int level) 135 { 136 this.level = level; 137 } 138 139 142 public int getCompressionLevel() 143 { 144 return( level); 145 } 146 147 150 public boolean needsBufferedOutputStream() 151 { 152 return(true); 153 } 154 155 156 161 public void loadClass( String className) throws Exception 162 { 163 if( getEncoderClassName() == null) 164 return; 165 Class encoder = null; 166 if( getContainerPaths() == null ) 167 { encoder = Class.forName(className); 169 } 170 if( encoder == null) 171 { 172 String [] rawPaths = getContainerPaths(); 173 URL [] uRLs = new URL [rawPaths.length]; 174 Object instance = null; 175 int i; 176 int j = 0; 177 178 for(i = 0; i < rawPaths.length; ++i) 179 { 180 if( rawPaths[i] == null ) 181 continue; 182 String jarPath = compiler.replaceProperties(rawPaths[i]); 183 URL url = compiler.findIzPackResource(jarPath, "Pack compressor jar file"); 184 if (url != null) 185 { 186 uRLs[j++] = url; 187 if (getClass().getResource("/" + jarPath) != null) 188 { InputStream in = null; 191 FileOutputStream outFile = null; 192 byte[] buffer = new byte[5120]; 193 File tf = null; 194 try 195 { 196 tf = File.createTempFile("izpj", ".jar"); 197 tf.deleteOnExit(); 198 outFile = new FileOutputStream (tf); 199 in = getClass().getResourceAsStream("/" + jarPath); 200 long bytesCopied = 0; 201 int bytesInBuffer; 202 while ((bytesInBuffer = in.read(buffer)) != -1) 203 { 204 outFile.write(buffer, 0, bytesInBuffer); 205 bytesCopied += bytesInBuffer; 206 } 207 } 208 finally 209 { 210 if (in != null) in.close(); 211 if (outFile != null) outFile.close(); 212 } 213 url = tf.toURL(); 214 215 } 216 } 217 } 218 if( j > 0 ) 219 { 220 if( j < uRLs.length) 221 { 222 URL [] nurl = new URL [j]; 223 for( i = 0; i < j; ++i) 224 nurl[i] = uRLs[i]; 225 uRLs = nurl; 226 } 227 URLClassLoader ucl = new URLClassLoader (uRLs, PackCompressor.class 230 .getClassLoader()); 231 encoder = ucl.loadClass(className); 232 } 233 } 234 235 if (encoder != null) 236 { 237 constructor = encoder.getDeclaredConstructor(paramsClasses); 240 } 241 else 242 compiler.parseError( "Cannot find defined compressor " + className); 243 } 244 245 257 protected OutputStream getOutputInstance(OutputStream slave) 258 throws Exception 259 { 260 if( needsBufferedOutputStream()) 261 { 262 slave = new BufferedOutputStream ( slave); 263 } 264 Object [] params = resolveConstructorParams( slave ); 265 if( constructor == null ) 266 loadClass(getEncoderClassName()); 267 if( constructor == null ) 268 return(null); 269 Object instance = null; 270 instance = constructor.newInstance( params); 271 if (!OutputStream .class.isInstance(instance)) 272 compiler.parseError( "'" + getEncoderClassName() + "' must be derived from " 273 + OutputStream .class.toString()); 274 return((OutputStream ) instance ); 275 276 } 277 278 292 protected Object [] resolveConstructorParams( OutputStream slave) throws Exception 293 { 294 if( level == -1 ) 295 { 296 paramsClasses = new Class [1]; 297 paramsClasses[0] = Class.forName("java.io.OutputStream"); 298 Object [] params = { slave}; 299 return( params ); 300 } 301 paramsClasses = new Class [2]; 302 paramsClasses[0] = Class.forName("java.io.OutputStream"); 303 paramsClasses[1] = java.lang.Integer.TYPE; 304 Object [] params = { slave, new Integer (level)}; 305 return( params ); 306 } 307 308 } 309 | Popular Tags |