1+ package me .bmax .apatch .util .apksign ;
2+
3+ import java .io .Closeable ;
4+ import java .io .File ;
5+ import java .io .IOException ;
6+ import java .io .InputStream ;
7+ import java .io .OutputStream ;
8+ import java .util .Collections ;
9+ import java .util .Enumeration ;
10+ import java .util .LinkedHashMap ;
11+ import java .util .jar .JarEntry ;
12+ import java .util .jar .JarFile ;
13+ import java .util .jar .JarInputStream ;
14+ import java .util .jar .Manifest ;
15+ import java .util .zip .ZipEntry ;
16+ import java .util .zip .ZipFile ;
17+
18+ public abstract class JarMap implements Closeable {
19+
20+ LinkedHashMap <String , JarEntry > entryMap ;
21+
22+ public static JarMap open (File file , boolean verify ) throws IOException {
23+ return new FileMap (file , verify , ZipFile .OPEN_READ );
24+ }
25+
26+ public static JarMap open (InputStream is , boolean verify ) throws IOException {
27+ return new StreamMap (is , verify );
28+ }
29+
30+ public File getFile () {
31+ return null ;
32+ }
33+
34+ public abstract Manifest getManifest () throws IOException ;
35+
36+ public InputStream getInputStream (ZipEntry ze ) throws IOException {
37+ JarMapEntry e = getMapEntry (ze .getName ());
38+ return e != null ? e .data .getInputStream () : null ;
39+ }
40+
41+ public OutputStream getOutputStream (ZipEntry ze ) {
42+ if (entryMap == null )
43+ entryMap = new LinkedHashMap <>();
44+ JarMapEntry e = new JarMapEntry (ze .getName ());
45+ entryMap .put (ze .getName (), e );
46+ return e .data ;
47+ }
48+
49+ public byte [] getRawData (ZipEntry ze ) throws IOException {
50+ JarMapEntry e = getMapEntry (ze .getName ());
51+ return e != null ? e .data .toByteArray () : null ;
52+ }
53+
54+ public abstract Enumeration <JarEntry > entries ();
55+
56+ public final ZipEntry getEntry (String name ) {
57+ return getJarEntry (name );
58+ }
59+
60+ public JarEntry getJarEntry (String name ) {
61+ return getMapEntry (name );
62+ }
63+
64+ JarMapEntry getMapEntry (String name ) {
65+ JarMapEntry e = null ;
66+ if (entryMap != null )
67+ e = (JarMapEntry ) entryMap .get (name );
68+ return e ;
69+ }
70+
71+ private static class FileMap extends JarMap {
72+
73+ private final JarFile jarFile ;
74+
75+ FileMap (File file , boolean verify , int mode ) throws IOException {
76+ jarFile = new JarFile (file , verify , mode );
77+ }
78+
79+ @ Override
80+ public File getFile () {
81+ return new File (jarFile .getName ());
82+ }
83+
84+ @ Override
85+ public Manifest getManifest () throws IOException {
86+ return jarFile .getManifest ();
87+ }
88+
89+ @ Override
90+ public InputStream getInputStream (ZipEntry ze ) throws IOException {
91+ InputStream is = super .getInputStream (ze );
92+ return is != null ? is : jarFile .getInputStream (ze );
93+ }
94+
95+ @ Override
96+ public byte [] getRawData (ZipEntry ze ) throws IOException {
97+ byte [] b = super .getRawData (ze );
98+ if (b != null )
99+ return b ;
100+ ByteArrayStream bytes = new ByteArrayStream ();
101+ bytes .readFrom (jarFile .getInputStream (ze ));
102+ return bytes .toByteArray ();
103+ }
104+
105+ @ Override
106+ public Enumeration <JarEntry > entries () {
107+ return jarFile .entries ();
108+ }
109+
110+ @ Override
111+ public JarEntry getJarEntry (String name ) {
112+ JarEntry e = getMapEntry (name );
113+ return e != null ? e : jarFile .getJarEntry (name );
114+ }
115+
116+ @ Override
117+ public void close () throws IOException {
118+ jarFile .close ();
119+ }
120+ }
121+
122+ private static class StreamMap extends JarMap {
123+
124+ private final JarInputStream jis ;
125+
126+ StreamMap (InputStream is , boolean verify ) throws IOException {
127+ jis = new JarInputStream (is , verify );
128+ entryMap = new LinkedHashMap <>();
129+ JarEntry entry ;
130+ while ((entry = jis .getNextJarEntry ()) != null ) {
131+ entryMap .put (entry .getName (), new JarMapEntry (entry , jis ));
132+ }
133+ }
134+
135+ @ Override
136+ public Manifest getManifest () {
137+ return jis .getManifest ();
138+ }
139+
140+ @ Override
141+ public Enumeration <JarEntry > entries () {
142+ return Collections .enumeration (entryMap .values ());
143+ }
144+
145+ @ Override
146+ public void close () throws IOException {
147+ jis .close ();
148+ }
149+ }
150+
151+ private static class JarMapEntry extends JarEntry {
152+
153+ ByteArrayStream data ;
154+
155+ JarMapEntry (JarEntry je , InputStream is ) {
156+ super (je );
157+ data = new ByteArrayStream ();
158+ data .readFrom (is );
159+ }
160+
161+ JarMapEntry (String s ) {
162+ super (s );
163+ data = new ByteArrayStream ();
164+ }
165+ }
166+ }
0 commit comments