rt/emul/zip/src/test/java/org/apidesign/bck2brwsr/emul/zip/ZipArchive.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 24 Feb 2015 11:12:53 +0100
changeset 1787 ea12a3bb4b33
parent 1571 287b113fa5ac
permissions -rw-r--r--
Using year range 2012-2015 in copyright header
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     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.
     8  *
     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.
    13  *
    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.
    17  */
    18 package org.apidesign.bck2brwsr.emul.zip;
    19 
    20 import java.io.IOException;
    21 import java.io.InputStream;
    22 import java.util.zip.ZipEntry;
    23 
    24 /**
    25  *
    26  * @author Jaroslav Tulach <jtulach@netbeans.org>
    27  */
    28 final class ZipArchive {
    29     private Entry first;
    30 
    31     public static ZipArchive createZip(InputStream is) throws IOException {
    32         ZipArchive a = new ZipArchive();
    33         readZip(is, a);
    34         return a;
    35     }
    36 
    37     public static ZipArchive createReal(InputStream is) throws IOException {
    38         ZipArchive a = new ZipArchive();
    39         realZip(is, a);
    40         return a;
    41     }
    42 
    43     /**
    44      * Registers entry name and data
    45      */
    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++) {
    49             int ch = is.read();
    50             if (ch == -1) {
    51                 byte[] tmp = new byte[i];
    52                 FastJar.arraycopy(arr, 0, tmp, 0, i);
    53                 arr = tmp;
    54                 break;
    55             }
    56             arr[i] = (byte) ch;
    57         }
    58         first = new Entry (entry, arr, first);
    59     }
    60     
    61     @Override
    62     public String toString() {
    63         StringBuilder sb = new StringBuilder();
    64         Entry e = first;
    65         while (e != null) {
    66             String string = e.name;
    67             byte[] bs = e.arr;
    68             sb.append(string).append(" = ").append(toString(bs)).append("\n");
    69             e = e.next;
    70         }
    71         return sb.toString();
    72     }
    73 
    74     public void assertEquals(ZipArchive zip, String msg) {
    75         boolean ok = true;
    76         StringBuilder sb = new StringBuilder();
    77         sb.append(msg);
    78         Entry e = first;
    79         while (e != null) {
    80             String string = e.name;
    81             byte[] bs = e.arr;
    82             byte[] other = zip.find(string);
    83             e = e.next;
    84             
    85             sb.append("\n");
    86             if (other == null) {
    87                 sb.append("EXTRA ").append(string).append(" = ").append(toString(bs));
    88                 ok = false;
    89                 continue;
    90             }
    91             if (equals(bs, other)) {
    92                 sb.append("OK    ").append(string);
    93                 continue;
    94             } else {
    95                 sb.append("DIFF  ").append(string).append(" = ").append(toString(bs)).append("\n");
    96                 sb.append("    TO").append(string).append(" = ").append(toString(other)).append("\n");
    97                 ok = false;
    98                 continue;
    99             }
   100         }
   101         e = zip.first;
   102         while (e != null) {
   103             String string = e.name;
   104             if (find(string) == null) {
   105                 sb.append("MISS  ").append(string).append(" = ").append(toString(e.arr));
   106                 ok = false;
   107             }
   108             e = e.next;
   109         }
   110         if (!ok) {
   111             assert false : sb.toString();
   112         }
   113     }
   114 
   115     public static void readZip(InputStream is, ZipArchive data) throws IOException {
   116         ZipInputStream zip = new org.apidesign.bck2brwsr.emul.zip.ZipInputStream(is);
   117         for (;;) {
   118             ZipEntry en = zip.getNextEntry();
   119             if (en == null) {
   120                 return;
   121             }
   122             data.register(en.getName(), zip);
   123         }
   124     }
   125 
   126     public static void realZip(InputStream is, ZipArchive data) throws IOException {
   127         java.util.zip.ZipInputStream zip = new java.util.zip.ZipInputStream(is);
   128         for (;;) {
   129             ZipEntry en = zip.getNextEntry();
   130             if (en == null) {
   131                 return;
   132             }
   133             data.register(en.getName(), zip);
   134         }
   135     }
   136 
   137     private byte[] find(String name) {
   138         Entry e = first;
   139         while (e != null) {
   140             if (e.name.equals(name)) {
   141                 return e.arr;
   142             }
   143             e = e.next;
   144         }
   145         return null;
   146     }
   147 
   148     private boolean equals(byte[] bs, byte[] other) {
   149         if (bs.length != other.length) {
   150             return false;
   151         }
   152         for (int i = 0; i < bs.length; i++) {
   153             if (bs[i] != other[i]) {
   154                 return false;
   155             }
   156         }
   157         return true;
   158     }
   159 
   160     private Object toString(byte[] arr) {
   161         StringBuilder sb = new StringBuilder();
   162         sb.append("[");
   163         String sep = "";
   164         for (int i = 0; i < arr.length; i++) {
   165             sb.append(sep).append(arr[i]);
   166             sep = ", ";
   167         }
   168         sb.append("]");
   169         return sb.toString();
   170     }
   171 
   172     private static final class Entry {
   173         final String name;
   174         final byte[] arr;
   175         final Entry next;
   176 
   177         public Entry(String name, byte[] arr, Entry next) {
   178             this.name = name;
   179             this.arr = arr;
   180             this.next = next;
   181         }
   182     }
   183 }