Rebasing the Inflater support on jzlib which, unlike GNU ClassPath, has correct implementation of Huffman code. Making the implementation more easily testable by turning Inflater and ZipInputStream into pure delegates. Current implementation is going to need proper long support.
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.compact.tck;
20 import java.io.ByteArrayOutputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.util.Arrays;
24 import java.util.LinkedHashMap;
26 import java.util.Objects;
27 import java.util.zip.ZipEntry;
28 import org.apidesign.bck2brwsr.emul.zip.ZipInputStream;
32 * @author Jaroslav Tulach <jtulach@netbeans.org>
34 final class ZipArchive {
35 private final Map<String, byte[]> entries = new LinkedHashMap<>();
37 public static ZipArchive createZip(InputStream is) throws IOException {
38 ZipArchive a = new ZipArchive();
43 public static ZipArchive createReal(InputStream is) throws IOException {
44 ZipArchive a = new ZipArchive();
50 * Registers entry name and data
52 final void register(String entry, InputStream is) throws IOException {
53 ByteArrayOutputStream os = new ByteArrayOutputStream();
62 entries.put(entry, os.toByteArray());
66 public int hashCode() {
67 return entries.hashCode();
71 public boolean equals(Object obj) {
75 if (getClass() != obj.getClass()) {
78 final ZipArchive other = (ZipArchive) obj;
79 if (!Objects.deepEquals(this.entries, other.entries)) {
86 public String toString() {
87 StringBuilder sb = new StringBuilder();
88 for (Map.Entry<String, byte[]> en : entries.entrySet()) {
89 String string = en.getKey();
90 byte[] bs = en.getValue();
91 sb.append(string).append(" = ").append(Arrays.toString(bs)).append("\n");
96 public void assertEquals(ZipArchive zip, String msg) {
98 StringBuilder sb = new StringBuilder();
100 for (Map.Entry<String, byte[]> en : entries.entrySet()) {
101 String string = en.getKey();
102 byte[] bs = en.getValue();
103 byte[] other = zip.entries.get(string);
106 sb.append("EXTRA ").append(string).append(" = ").append(Arrays.toString(bs));
110 if (Arrays.equals(bs, other)) {
111 sb.append("OK ").append(string);
114 sb.append("DIFF ").append(string).append(" = ").append(Arrays.toString(bs)).append("\n");
115 sb.append(" TO").append(string).append(" = ").append(Arrays.toString(other)).append("\n");
120 for (Map.Entry<String, byte[]> entry : zip.entries.entrySet()) {
121 String string = entry.getKey();
122 if (entries.get(string) == null) {
123 sb.append("MISS ").append(string).append(" = ").append(Arrays.toString(entry.getValue()));
128 assert false : sb.toString();
132 public static void readZip(InputStream is, ZipArchive data) throws IOException {
133 ZipInputStream zip = new org.apidesign.bck2brwsr.emul.zip.ZipInputStream(is);
135 ZipEntry en = zip.getNextEntry();
139 data.register(en.getName(), zip);
143 public static void realZip(InputStream is, ZipArchive data) throws IOException {
144 java.util.zip.ZipInputStream zip = new java.util.zip.ZipInputStream(is);
146 ZipEntry en = zip.getNextEntry();
150 data.register(en.getName(), zip);