1 package org.catacomb.dataview.read;
2
3 import org.catacomb.report.E;
4 import org.catacomb.serial.jar.CustomJar;
5
6
7 import java.io.ByteArrayInputStream;
8 import java.io.ByteArrayOutputStream;
9 import java.io.IOException;
10 import java.io.InputStream;
11 import java.util.HashMap;
12 import java.util.zip.ZipEntry;
13 import java.util.zip.ZipInputStream;
14
15
16 public class CustomJarReader {
17
18 HashMap<String, Object> itemHM;
19 HashMap<String, byte[]> rawHM;
20
21 JarImportContext jarImportContext;
22
23
24
25 public CustomJarReader(byte[] ba, JarImportContext jctx) {
26 jarImportContext = jctx;
27 readAll(ba);
28 }
29
30
31
32
33 public void readAll(byte[] bytes) {
34 rawHM = new HashMap<String, byte[]>();
35
36 itemHM = new HashMap<String, Object>();
37
38 CustomJar.naturalize(bytes);
39
40 try {
41 ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
42 ZipInputStream zis = new ZipInputStream(bais);
43 ZipEntry entry = zis.getNextEntry();
44
45 while (entry != null) {
46 String name = entry.getName();
47 byte[] buffer = readBytes(zis);
48
49
50 if (CustomJar.isMetaName(name)) {
51 String s = new String(buffer);
52 itemHM.put(name, s);
53 } else {
54 rawHM.put(name, buffer);
55 }
56
57 entry = zis.getNextEntry();
58 }
59 zis.close();
60
61 } catch (Exception ex) {
62 E.error("zip read exception " + ex);
63 ex.printStackTrace();
64 }
65 }
66
67
68
69 public Object getRelative(String name) {
70 Object ret = null;
71 if (itemHM.containsKey(name)) {
72 ret = itemHM.get(name);
73
74 } else if (rawHM.containsKey(name)) {
75 byte[] bytes = rawHM.get(name);
76 ContentReader cr = Importer.getReader(bytes, jarImportContext);
77
78 ret = cr.getMain();
79
80 itemHM.put(name, ret);
81 }
82 return ret;
83 }
84
85
86
87
88
89
90 private byte[] readBytes(InputStream ins) throws IOException {
91 ByteArrayOutputStream baos = new ByteArrayOutputStream();
92
93 byte[] bb = new byte[4096];
94 int nread = ins.read(bb);
95
96 while (nread > 0) {
97 baos.write(bb, 0, nread);
98 nread = ins.read(bb);
99 }
100 return baos.toByteArray();
101 }
102
103
104
105
106 public Object getMain() {
107 Object ret = null;
108 String sn = CustomJar.getMetaMain();
109
110 if (itemHM.containsKey(sn)) {
111 Object om = itemHM.get(sn);
112
113 if (om instanceof String) {
114 String mainname = (String)om;
115
116 if (hasRelative(mainname)) {
117 ret = getRelative(mainname);
118 }
119 } else {
120 E.error("main name not a string?");
121 }
122
123 }
124 if (ret == null) {
125 E.warning("no main objhect in custom jar - mainname");
126 }
127 return ret;
128 }
129
130
131
132 public boolean hasRelative(String name) {
133 return (itemHM.containsKey(name) || rawHM.containsKey(name));
134 }
135
136
137
138 }