summaryrefslogtreecommitdiffstats
path: root/jack-launcher/src/com/android/jack/launcher/ZipLoader.java
blob: f7b13057cab6af38d602418d8ff04674e91e27e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.jack.launcher;

import com.android.jack.launcher.util.BytesStreamSucker;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.util.Enumeration;
import java.util.Vector;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

class ZipLoader extends ClassLoader {

  private static final char BANG = '!';
  static {
    // ensure class is loaded early, don't wait for first usage
    ZipURLConnection.class.getName();
  }

  private static class ZipURLConnection extends URLConnection {
    @Nonnull
    private final ZipFile zip;
    @Nonnull
    private final ZipEntry entry;

    ZipURLConnection(@Nonnull URL url, @Nonnull ZipFile zip, @Nonnull ZipEntry entry) {
      super(url);
      this.zip = zip;
      this.entry = entry;
    }

    @Override
    public void connect() {
      // nothing to do
    }

    @Override
    @Nonnull
    public InputStream getInputStream() throws IOException {
      return zip.getInputStream(entry);
    }

  }

  private static class ZipURLStreamHandler extends URLStreamHandler {
    @Nonnull
    private final ZipFile zip;

    ZipURLStreamHandler(@Nonnull ZipFile zip) {
      this.zip = zip;
    }

    @Override
    protected URLConnection openConnection(@Nonnull URL url) throws IOException {
      ZipEntry entry = zip.getEntry(getZipEntry(url));
      if (entry == null) {
        throw new FileNotFoundException(url.toString());
      }
      return new ZipURLConnection(url, zip, entry);
    }
  }

  @Nonnull
  private static URL makeURL(@Nonnull ZipEntry entry, @Nonnull ZipURLStreamHandler handler) {
    try {
      assert entry.getName().indexOf(BANG) == -1;
      return new URL("launcherzip", "", -1, handler.zip.getName() + BANG + entry.getName(),
          handler);
    } catch (MalformedURLException e) {
      throw new AssertionError();
    }
  }

  @Nonnull
  private static String getZipEntry(@Nonnull URL url) {
    String file = url.getFile();
    int dashIndex = file.lastIndexOf(BANG);
    return file.substring(dashIndex + 1);
  }

  @Nonnull
  private final ZipURLStreamHandler[] handlers;


  public ZipLoader(@Nonnull ZipFile[] entries) {
    handlers = new ZipURLStreamHandler[entries.length];
    for (int i = 0; i < entries.length; i++) {
      handlers[i] = new ZipURLStreamHandler(entries[i]);
    }
  }

  @Nonnull
  @Override
  protected Class<?> findClass(@Nonnull String name) throws ClassNotFoundException {

    ZipEntry foundEntry = null;
    ZipFile foundZip = null;
    for (ZipURLStreamHandler handler : handlers) {
      ZipFile zip = handler.zip;
      foundEntry = zip.getEntry(name.replace('.', '/') + ".class");
      if (foundEntry != null) {
        foundZip = zip;
        break;
      }
    }
    if (foundEntry == null) {
      throw new ClassNotFoundException(name);
    }

    InputStream in = null;
    try {
      // FINDBUGS
      assert foundZip != null;
      in = foundZip.getInputStream(foundEntry);
      long size = foundEntry.getSize();
      assert size >= -1 && size <= Integer.MAX_VALUE;
      byte[] classData;
      if (size == -1) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        new BytesStreamSucker(in, out, /* toBeClose = */ true).suck();
        classData = out.toByteArray();
      } else {
        classData = new byte[(int) size];
        // FINDBUGS
        in = new DataInputStream(in);
        ((DataInputStream) in).readFully(classData);
      }
      return defineClass(name, classData, 0, classData.length);
    } catch (IOException e) {
      throw new ClassNotFoundException(name);
    } finally {
      if (in != null) {
        try {
          in.close();
        } catch (IOException e) {
          // ignore
        }
      }
    }
  }

  @CheckForNull
  @Override
  public InputStream getResourceAsStream(@Nonnull String name) {
    InputStream found = getParent().getResourceAsStream(name);
    if (found != null) {
      return found;
    } else {
      for (ZipURLStreamHandler handler : handlers) {
        ZipFile zip = handler.zip;
        ZipEntry foundEntry = zip.getEntry(name);
        if (foundEntry != null) {
          try {
            return zip.getInputStream(foundEntry);
          } catch (IOException e) {
            AssertionError error = new AssertionError();
            error.initCause(e);
            throw error;
          }
        }
      }
      return null;
    }
  }

  @CheckForNull
  @Override
  protected URL findResource(@Nonnull String name) {
    for (ZipURLStreamHandler handler : handlers) {
      ZipFile zip = handler.zip;
      ZipEntry foundEntry = zip.getEntry(name);
      if (foundEntry != null) {
        return makeURL(foundEntry, handler);
      }
    }
    return null;
  }

  @Nonnull
  @Override
  protected Enumeration<URL> findResources(@Nonnull String name) {
    Vector<URL> vector = new Vector<URL>();
    for (ZipURLStreamHandler handler : handlers) {
      ZipFile zip = handler.zip;
      ZipEntry foundEntry = zip.getEntry(name);
      if (foundEntry != null) {
        vector.add(makeURL(foundEntry, handler));
      }
    }
    return vector.elements();
  }
}