View Javadoc

1   package org.catacomb.serial.jar;
2   
3   import org.catacomb.interlish.structure.Binariable;
4   import org.catacomb.report.E;
5   import org.catacomb.serial.Serializer;
6   
7   
8   import java.io.*;
9   import java.util.HashMap;
10  import java.util.zip.ZipEntry;
11  import java.util.zip.ZipOutputStream;
12  
13  
14  public class CustomJarWriter {
15  
16      HashMap<String, Object> itemHM;
17  
18  
19      public CustomJarWriter() {
20          itemHM = new HashMap<String, Object>();
21      }
22  
23      public CustomJarWriter(HashMap<String, Object> hm) {
24          itemHM = hm;
25      }
26  
27  
28  
29  
30      public void addMain(String sdata) {
31          add(CustomJar.getMetaMain(), "main");
32          add("main", sdata);
33      }
34  
35  
36      public void addMain(File f) {
37          add(CustomJar.getMetaMain(), f.getName());
38          add(f);
39      }
40  
41      public void addMimetype(String mt) {
42          add(CustomJar.getMetaMime(), mt);
43      }
44  
45  
46  
47      public void add(File f) {
48          add(f.getName(), f);
49      }
50  
51  
52      public void add(String name, Object value) {
53          itemHM.put(name, value);
54      }
55  
56  
57  
58  
59      public void write(File fout) {
60          try {
61              ByteArrayOutputStream baos = new ByteArrayOutputStream();
62              ZipOutputStream zos = new ZipOutputStream(baos);
63  
64              for (String name : itemHM.keySet()) {
65                  Object value = itemHM.get(name);
66                  zos.putNextEntry(new ZipEntry(name));
67  
68                  if (value instanceof File) {
69                      FileInputStream fis  = new FileInputStream((File)value);
70                      byte[] buf = new byte[4096];
71                      int nread = 0;
72                      while ((nread = fis.read(buf)) > 0) {
73                          zos.write(buf, 0, nread);
74                      }
75                      fis.close();
76  
77  
78                  } else if (value instanceof Binariable) {
79                      E.error("data jar binarizable object but code is missing ");
80  
81                  } else {
82                      String sdata = "";
83                      if (value instanceof String) {
84                          sdata = (String)value;
85  
86                      } else {
87                          sdata = Serializer.serialize(value);
88                      }
89  
90                      OutputStreamWriter osw = new OutputStreamWriter(zos);
91                      BufferedWriter bw = new BufferedWriter(osw);
92                      bw.write(sdata, 0, sdata.length());
93                      bw.flush();
94                  }
95  
96                  zos.closeEntry();
97              }
98  
99              zos.flush();
100             zos.close();
101 
102             byte[] ba= baos.toByteArray();
103 
104 
105             // EXTEND - could hack magic number 	 CustomJar.customize();
106 
107             writeByteArrayToFile(ba, fout);
108         } catch (Exception ex) {
109             E.error("custom jar writing error " + ex);
110             ex.printStackTrace();
111         }
112     }
113 
114 
115 
116 
117     private void writeByteArrayToFile(byte[] ba, File fout) throws IOException {
118         OutputStream os = new BufferedOutputStream(new FileOutputStream(fout));
119         os.write(ba);
120         os.flush();
121         os.close();
122     }
123 
124 
125 
126 }