FileDocCategorySizeDatePackage
JarResourceExtractor.javaAPI DocGlassfish v2 API6405Fri May 04 22:34:26 BST 2007com.sun.enterprise.connectors.util

JarResourceExtractor

public final class JarResourceExtractor extends Object
JarResourceExtractor: JarResourceExtractor maps all resources included in a Zip or Jar file. Additionaly, it provides a method to extract one as a blob.
author
Sivakumar Thyagarajan

Fields Summary
static Logger
_logger
private Hashtable
htJarContents
Constructors Summary
public JarResourceExtractor(String jarFileName)
creates a JarResourceExtractor. It extracts all resources from a Jar into an internal hashtable, keyed by resource names.

param
jarFileName a jar or zip file

    
                                              
       
        init(jarFileName);
    
Methods Summary
private voidextractResources(java.util.zip.ZipInputStream zis)

throws
FileNotFoundException
throws
IOException

        ZipEntry ze = null;
        while ((ze = zis.getNextEntry()) != null) {
            _logger.finer("ExtractResources : " + ze.getName());
            extractZipEntryContents(ze, zis);
        }
    
private voidextractZipEntryContents(java.util.zip.ZipEntry ze, java.util.zip.ZipInputStream zis)

param
zis
throws
IOException

            if (ze.isDirectory()) {
                return;
            }
            
            _logger.finer("ze.getName()=" + ze.getName() + ","
                        + "getSize()=" + ze.getSize());

            byte[] b = getZipEntryContents(ze,zis);
            //If it is a jar go RECURSIVE !!
            if(ze.getName().trim().endsWith(".jar")){
                _logger.finer("JAR - going into it !!");
                BufferedInputStream bis = new BufferedInputStream( (new ByteArrayInputStream(b)));
                extractResources(new ZipInputStream(bis));
            } else {
                //add to internal resource hashtable
                htJarContents.put(ze.getName(), b );
                if (ze.getName().trim().endsWith("class")){ 
                    _logger.finer("CLASS added " + ze.getName());
                }
                _logger.finer(ze.getName() + ",size="
                        + b.length + ",csize=" + ze.getCompressedSize());
            }
    
public byte[]getResource(java.lang.String name)
Extracts a jar resource as a blob.

param
name a resource name.

        _logger.finer("getResource: " + name);
        return (byte[]) htJarContents.get(name);
    
private byte[]getZipEntryContents(java.util.zip.ZipEntry ze, java.util.zip.ZipInputStream zis)

        int size = (int) ze.getSize();
        
        byte[] b = null;
        // -1 means unknown size.
        if (size != -1) {
            //got a proper size, read 'size' bytes
            b = new byte[(int) size];
            
            int rb = 0;
            int chunk = 0;
            
            while (((int) size - rb) > 0) {
                chunk = zis.read(b, rb, (int) size - rb);
                if (chunk == -1) {
                    break;
                }
                rb += chunk;
            }
        } else {
            //size of entry unknown .. keep on reading till we hit a -1
            ArrayList al = new ArrayList();
            int c = 0;
            while( (c = zis.read()) != -1) {
                al.add(new Byte((byte)c));
            }
            Byte[] btArr = (Byte[])al.toArray(new Byte[]{});
            b = new byte[btArr.length];
            _logger.finer("ByteArray length" + btArr.length);
            for (int i = 0; i < btArr.length; i++) {
                b[i] = btArr[i].byteValue();
            }
        }
        
        return b;
    
private voidinit(java.lang.String jarFileName)
initializes internal hash tables with Jar file resources.

        try {
            //extract resources and put them into the hashtable.
            FileInputStream fis = new FileInputStream(jarFileName);
            BufferedInputStream bis = new BufferedInputStream(fis);
            ZipInputStream zis = new ZipInputStream(bis);            
            extractResources(zis);
        } catch (Exception ex){
            ex.printStackTrace();
        }