2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.bck2brwsr.emul.zip;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.util.zip.ZipEntry;
26 * @author Jaroslav Tulach <jtulach@netbeans.org>
28 final class ZipArchive {
31 public static ZipArchive createZip(InputStream is) throws IOException {
32 ZipArchive a = new ZipArchive();
37 public static ZipArchive createReal(InputStream is) throws IOException {
38 ZipArchive a = new ZipArchive();
44 * Registers entry name and data
46 final void register(String entry, InputStream is) throws IOException {
47 byte[] arr = new byte[12 * 4096];
48 for (int i = 0; i < arr.length; i++) {
51 byte[] tmp = new byte[i];
52 FastJar.arraycopy(arr, 0, tmp, 0, i);
58 first = new Entry (entry, arr, first);
62 public String toString() {
63 StringBuilder sb = new StringBuilder();
66 String string = e.name;
68 sb.append(string).append(" = ").append(toString(bs)).append("\n");
74 public void assertEquals(ZipArchive zip, String msg) {
76 StringBuilder sb = new StringBuilder();
80 String string = e.name;
82 byte[] other = zip.find(string);
87 sb.append("EXTRA ").append(string).append(" = ").append(toString(bs));
91 if (equals(bs, other)) {
92 sb.append("OK ").append(string);
95 sb.append("DIFF ").append(string).append(" = ").append(toString(bs)).append("\n");
96 sb.append(" TO").append(string).append(" = ").append(toString(other)).append("\n");
103 String string = e.name;
104 if (find(string) == null) {
105 sb.append("MISS ").append(string).append(" = ").append(toString(e.arr));
111 assert false : sb.toString();
115 public static void readZip(InputStream is, ZipArchive data) throws IOException {
116 ZipInputStream zip = new org.apidesign.bck2brwsr.emul.zip.ZipInputStream(is);
118 ZipEntry en = zip.getNextEntry();
122 data.register(en.getName(), zip);
126 public static void realZip(InputStream is, ZipArchive data) throws IOException {
127 java.util.zip.ZipInputStream zip = new java.util.zip.ZipInputStream(is);
129 ZipEntry en = zip.getNextEntry();
133 data.register(en.getName(), zip);
137 private byte[] find(String name) {
140 if (e.name.equals(name)) {
148 private boolean equals(byte[] bs, byte[] other) {
149 if (bs.length != other.length) {
152 for (int i = 0; i < bs.length; i++) {
153 if (bs[i] != other[i]) {
160 private Object toString(byte[] arr) {
161 StringBuilder sb = new StringBuilder();
164 for (int i = 0; i < arr.length; i++) {
165 sb.append(sep).append(arr[i]);
169 return sb.toString();
172 private static final class Entry {
177 public Entry(String name, byte[] arr, Entry next) {