2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 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.ByteArrayOutputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.util.zip.ZipEntry;
27 * @author Jaroslav Tulach <jtulach@netbeans.org>
29 final class ZipArchive {
32 public static ZipArchive createZip(InputStream is) throws IOException {
33 ZipArchive a = new ZipArchive();
38 public static ZipArchive createReal(InputStream is) throws IOException {
39 ZipArchive a = new ZipArchive();
45 * Registers entry name and data
47 final void register(String entry, InputStream is) throws IOException {
48 byte[] arr = new byte[12 * 4096];
49 for (int i = 0; i < arr.length; i++) {
52 byte[] tmp = new byte[i];
53 FastJar.arraycopy(arr, 0, tmp, 0, i);
59 first = new Entry (entry, arr, first);
63 public String toString() {
64 StringBuilder sb = new StringBuilder();
67 String string = e.name;
69 sb.append(string).append(" = ").append(toString(bs)).append("\n");
75 public void assertEquals(ZipArchive zip, String msg) {
77 StringBuilder sb = new StringBuilder();
81 String string = e.name;
83 byte[] other = zip.find(string);
88 sb.append("EXTRA ").append(string).append(" = ").append(toString(bs));
92 if (equals(bs, other)) {
93 sb.append("OK ").append(string);
96 sb.append("DIFF ").append(string).append(" = ").append(toString(bs)).append("\n");
97 sb.append(" TO").append(string).append(" = ").append(toString(other)).append("\n");
104 String string = e.name;
105 if (find(string) == null) {
106 sb.append("MISS ").append(string).append(" = ").append(toString(e.arr));
112 assert false : sb.toString();
116 public static void readZip(InputStream is, ZipArchive data) throws IOException {
117 ZipInputStream zip = new org.apidesign.bck2brwsr.emul.zip.ZipInputStream(is);
119 ZipEntry en = zip.getNextEntry();
123 data.register(en.getName(), zip);
127 public static void realZip(InputStream is, ZipArchive data) throws IOException {
128 java.util.zip.ZipInputStream zip = new java.util.zip.ZipInputStream(is);
130 ZipEntry en = zip.getNextEntry();
134 data.register(en.getName(), zip);
138 private byte[] find(String name) {
141 if (e.name.equals(name)) {
149 private boolean equals(byte[] bs, byte[] other) {
150 if (bs.length != other.length) {
153 for (int i = 0; i < bs.length; i++) {
154 if (bs[i] != other[i]) {
161 private Object toString(byte[] arr) {
162 StringBuilder sb = new StringBuilder();
165 for (int i = 0; i < arr.length; i++) {
166 sb.append(sep).append(arr[i]);
170 return sb.toString();
173 private static final class Entry {
178 public Entry(String name, byte[] arr, Entry next) {