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