emul/mini/src/main/java/java/util/zip/Inflater.java
branchemul
changeset 611 9839e9a75bcf
parent 609 48ef38e9677e
child 640 693745d01b55
     1.1 --- a/emul/mini/src/main/java/java/util/zip/Inflater.java	Wed Jan 30 14:00:17 2013 +0100
     1.2 +++ b/emul/mini/src/main/java/java/util/zip/Inflater.java	Wed Jan 30 14:03:49 2013 +0100
     1.3 @@ -25,6 +25,9 @@
     1.4  
     1.5  package java.util.zip;
     1.6  
     1.7 +import org.apidesign.bck2brwsr.core.ExtraJavaScript;
     1.8 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
     1.9 +
    1.10  /**
    1.11   * This class provides support for general purpose decompression using the
    1.12   * popular ZLIB compression library. The ZLIB compression library was
    1.13 @@ -70,22 +73,19 @@
    1.14   * @author      David Connelly
    1.15   *
    1.16   */
    1.17 +@ExtraJavaScript(
    1.18 +    resource = "/org/apidesign/vm4brwsr/emul/zip/js-inflate.min.js"
    1.19 +)
    1.20  public
    1.21  class Inflater {
    1.22 -
    1.23 -    private final ZStreamRef zsRef;
    1.24 -    private byte[] buf = defaultBuf;
    1.25 -    private int off, len;
    1.26 +    private String data = "";
    1.27 +    private int offset;
    1.28 +    private long counter;
    1.29      private boolean finished;
    1.30      private boolean needDict;
    1.31  
    1.32      private static final byte[] defaultBuf = new byte[0];
    1.33  
    1.34 -    static {
    1.35 -        /* Zip library is loaded from System.initializeSystemClass */
    1.36 -        initIDs();
    1.37 -    }
    1.38 -
    1.39      /**
    1.40       * Creates a new decompressor. If the parameter 'nowrap' is true then
    1.41       * the ZLIB header and checksum fields will not be used. This provides
    1.42 @@ -98,7 +98,6 @@
    1.43       * @param nowrap if true then support GZIP compatible compression
    1.44       */
    1.45      public Inflater(boolean nowrap) {
    1.46 -        zsRef = new ZStreamRef(init(nowrap));
    1.47      }
    1.48  
    1.49      /**
    1.50 @@ -124,11 +123,7 @@
    1.51          if (off < 0 || len < 0 || off > b.length - len) {
    1.52              throw new ArrayIndexOutOfBoundsException();
    1.53          }
    1.54 -        synchronized (zsRef) {
    1.55 -            this.buf = b;
    1.56 -            this.off = off;
    1.57 -            this.len = len;
    1.58 -        }
    1.59 +        data = (String) infl(b, off, len);
    1.60      }
    1.61  
    1.62      /**
    1.63 @@ -160,11 +155,7 @@
    1.64          if (off < 0 || len < 0 || off > b.length - len) {
    1.65              throw new ArrayIndexOutOfBoundsException();
    1.66          }
    1.67 -        synchronized (zsRef) {
    1.68 -            ensureOpen();
    1.69 -            setDictionary(zsRef.address(), b, off, len);
    1.70 -            needDict = false;
    1.71 -        }
    1.72 +        needDict = false;
    1.73      }
    1.74  
    1.75      /**
    1.76 @@ -187,9 +178,7 @@
    1.77       * @return the total number of bytes remaining in the input buffer
    1.78       */
    1.79      public int getRemaining() {
    1.80 -        synchronized (zsRef) {
    1.81 -            return len;
    1.82 -        }
    1.83 +        return data.length() - offset;
    1.84      }
    1.85  
    1.86      /**
    1.87 @@ -199,9 +188,7 @@
    1.88       * @return true if no data remains in the input buffer
    1.89       */
    1.90      public boolean needsInput() {
    1.91 -        synchronized (zsRef) {
    1.92 -            return len <= 0;
    1.93 -        }
    1.94 +        return getRemaining() <= 0;
    1.95      }
    1.96  
    1.97      /**
    1.98 @@ -210,9 +197,7 @@
    1.99       * @see Inflater#setDictionary
   1.100       */
   1.101      public boolean needsDictionary() {
   1.102 -        synchronized (zsRef) {
   1.103 -            return needDict;
   1.104 -        }
   1.105 +        return needDict;
   1.106      }
   1.107  
   1.108      /**
   1.109 @@ -222,9 +207,7 @@
   1.110       * reached
   1.111       */
   1.112      public boolean finished() {
   1.113 -        synchronized (zsRef) {
   1.114 -            return finished;
   1.115 -        }
   1.116 +        return finished;
   1.117      }
   1.118  
   1.119      /**
   1.120 @@ -251,11 +234,21 @@
   1.121          if (off < 0 || len < 0 || off > b.length - len) {
   1.122              throw new ArrayIndexOutOfBoundsException();
   1.123          }
   1.124 -        synchronized (zsRef) {
   1.125 -            ensureOpen();
   1.126 -            return inflateBytes(zsRef.address(), b, off, len);
   1.127 +        int cnt = 0;
   1.128 +        while (offset < data.length()) {
   1.129 +            b[off++] = (byte)data.charAt(offset++);
   1.130 +            cnt++;
   1.131 +            counter++;
   1.132          }
   1.133 +        return cnt;
   1.134      }
   1.135 +    
   1.136 +    @JavaScriptBody(args = { "arr", "offset", "len" }, body = 
   1.137 +          "var r = {};\n"
   1.138 +        + "r.charCodeAt = function(idx) { return arr[offset + idx]; };\n"
   1.139 +        + "return JSInflate.inflate(r);"
   1.140 +    )
   1.141 +    private static native Object infl(byte[] arr, int offset, int len);
   1.142  
   1.143      /**
   1.144       * Uncompresses bytes into specified buffer. Returns actual number
   1.145 @@ -279,10 +272,7 @@
   1.146       * @return the ADLER-32 value of the uncompressed data
   1.147       */
   1.148      public int getAdler() {
   1.149 -        synchronized (zsRef) {
   1.150 -            ensureOpen();
   1.151 -            return getAdler(zsRef.address());
   1.152 -        }
   1.153 +        return 0;
   1.154      }
   1.155  
   1.156      /**
   1.157 @@ -305,10 +295,7 @@
   1.158       * @since 1.5
   1.159       */
   1.160      public long getBytesRead() {
   1.161 -        synchronized (zsRef) {
   1.162 -            ensureOpen();
   1.163 -            return getBytesRead(zsRef.address());
   1.164 -        }
   1.165 +        return counter;
   1.166      }
   1.167  
   1.168      /**
   1.169 @@ -331,24 +318,17 @@
   1.170       * @since 1.5
   1.171       */
   1.172      public long getBytesWritten() {
   1.173 -        synchronized (zsRef) {
   1.174 -            ensureOpen();
   1.175 -            return getBytesWritten(zsRef.address());
   1.176 -        }
   1.177 +        return counter;
   1.178      }
   1.179  
   1.180      /**
   1.181       * Resets inflater so that a new set of input data can be processed.
   1.182       */
   1.183      public void reset() {
   1.184 -        synchronized (zsRef) {
   1.185 -            ensureOpen();
   1.186 -            reset(zsRef.address());
   1.187 -            buf = defaultBuf;
   1.188 -            finished = false;
   1.189 -            needDict = false;
   1.190 -            off = len = 0;
   1.191 -        }
   1.192 +        data = "";
   1.193 +        finished = false;
   1.194 +        needDict = false;
   1.195 +        offset = 0;
   1.196      }
   1.197  
   1.198      /**
   1.199 @@ -359,44 +339,5 @@
   1.200       * object is undefined.
   1.201       */
   1.202      public void end() {
   1.203 -        synchronized (zsRef) {
   1.204 -            long addr = zsRef.address();
   1.205 -            zsRef.clear();
   1.206 -            if (addr != 0) {
   1.207 -                end(addr);
   1.208 -                buf = null;
   1.209 -            }
   1.210 -        }
   1.211      }
   1.212 -
   1.213 -    /**
   1.214 -     * Closes the decompressor when garbage is collected.
   1.215 -     */
   1.216 -    protected void finalize() {
   1.217 -        end();
   1.218 -    }
   1.219 -
   1.220 -    private void ensureOpen () {
   1.221 -        assert Thread.holdsLock(zsRef);
   1.222 -        if (zsRef.address() == 0)
   1.223 -            throw new NullPointerException("Inflater has been closed");
   1.224 -    }
   1.225 -
   1.226 -    boolean ended() {
   1.227 -        synchronized (zsRef) {
   1.228 -            return zsRef.address() == 0;
   1.229 -        }
   1.230 -    }
   1.231 -
   1.232 -    private native static void initIDs();
   1.233 -    private native static long init(boolean nowrap);
   1.234 -    private native static void setDictionary(long addr, byte[] b, int off,
   1.235 -                                             int len);
   1.236 -    private native int inflateBytes(long addr, byte[] b, int off, int len)
   1.237 -            throws DataFormatException;
   1.238 -    private native static int getAdler(long addr);
   1.239 -    private native static long getBytesRead(long addr);
   1.240 -    private native static long getBytesWritten(long addr);
   1.241 -    private native static void reset(long addr);
   1.242 -    private native static void end(long addr);
   1.243  }