later version of client-side WSDL2Java generated simple Alignment
[jalview.git] / src / ext / jemboss / JembossJarUtil.java
1 /***************************************************************
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License
5 * as published by the Free Software Foundation; either version 2
6 * of the License, or (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
16 *
17 *  @author: Copyright (C) Tim Carver
18 *
19 ***************************************************************/
20
21 package ext.jemboss;
22
23 import java.util.*;
24 import java.util.zip.*;
25 import java.io.*;
26
27
28 /**
29 *
30 * Unpacks a Jar file into a Hashtable
31 *
32 */
33 public class JembossJarUtil
34 {
35
36   /** Hashtable containing the unpacked contents of the jar file */
37   private Hashtable jarStore = new Hashtable();
38
39   /**
40   *
41   * Given the path to a jar file unpack to a hashtable
42   * @param jarFile      path to jar file to unpack
43   * @throws Exception   if it is not possible to read jar file
44   *
45   */
46   public JembossJarUtil(String jarFile) throws Exception
47   {
48
49     try
50     {
51       // extracts just sizes only
52       ClassLoader cl = this.getClass().getClassLoader();
53       ZipInputStream zis= new ZipInputStream(
54                      cl.getResourceAsStream(jarFile));
55       ZipEntry ze=null;
56       Hashtable htSizes = new Hashtable();
57
58       while((ze=zis.getNextEntry())!=null)
59       {
60         int ret=0;
61         int cnt=0;
62         int rb = 0;
63         while(ret != -1)
64         {
65           byte[] b1 = new byte[1];
66           ret=zis.read(b1,rb,1);
67           cnt++;
68         }
69         htSizes.put(ze.getName(),new Integer(cnt));
70       }
71       zis.close();
72
73       // extract resources and put them into the hashtable
74       zis = new ZipInputStream(cl.getResourceAsStream(jarFile));
75       ze=null;
76       while ((ze=zis.getNextEntry())!=null)
77       {
78         if(ze.isDirectory())
79           continue;
80
81         int size=(int)ze.getSize(); // -1 means unknown size
82         if(size==-1)
83           size=((Integer)htSizes.get(ze.getName())).intValue();
84
85         byte[] b=new byte[(int)size];
86         int rb=0;
87         int chunk=0;
88         while (((int)size - rb) > 0)
89         {
90           chunk=zis.read(b,rb,(int)size - rb);
91           if(chunk==-1)
92             break;
93           rb+=chunk;
94         }
95
96         // add to internal resource hashtable
97         jarStore.put(ze.getName(),b);
98
99 //      System.out.println(ze.getName());
100       }
101       zis.close();
102     }
103     catch (Exception e) { throw new Exception();}
104
105 //  catch (NullPointerException e)
106 //  {
107 //    System.out.println("JembossJarUtil Error: jarStore");
108 //  }
109 //  catch (FileNotFoundException e)
110 //  {
111 //    e.printStackTrace();
112 //  }
113 //  catch (IOException e)
114 //  {
115 //    e.printStackTrace();
116 //  }
117   }
118
119
120   /**
121   *
122   * Return the hashtable
123   * @return jarStore    the hashtable containing the contents
124   *                     of the jar
125   *
126   */
127   public Hashtable getHash()
128   {
129     return jarStore;
130   }
131
132   /**
133   *
134   * Return an element of the hashtable
135   * @param el   key of an element in the hashtable
136   * @return     the hashtable containing the contents
137   *             of the jar
138   *
139   */
140   public Object getElement(String el)
141   {
142     return jarStore.get(el);
143   }
144
145   /**
146   *
147   * Write contents of an element in the hashtable
148   * @param el   key of an element in the hashtable
149   * @param f    path of file to write to
150   * @return     true if written file
151   *
152   */
153   public boolean writeByteFile(String el, String f)
154   {
155     try
156     {
157       FileOutputStream out = new FileOutputStream(f);
158       out.write((byte []) jarStore.get(el));
159       out.close();
160     }
161     catch(FileNotFoundException fnfe) {return false;}
162     catch(IOException ioe) {return false;}
163
164     return true;
165   }
166
167 }