Use as little of Java APIs as possible closure
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 11 May 2014 08:10:28 +0200
branchclosure
changeset 155571e68f7ed23f
parent 1554 7ba27baf5f3f
child 1556 a936cbe90474
Use as little of Java APIs as possible
rt/emul/zip/src/main/java/org/apidesign/bck2brwsr/emul/zip/CRC32.java
rt/emul/zip/src/main/java/org/apidesign/bck2brwsr/emul/zip/FastJar.java
rt/emul/zip/src/main/java/org/apidesign/bck2brwsr/emul/zip/GZIPHeader.java
rt/emul/zip/src/main/java/org/apidesign/bck2brwsr/emul/zip/InfBlocks.java
rt/emul/zip/src/main/java/org/apidesign/bck2brwsr/emul/zip/InfCodes.java
rt/emul/zip/src/main/java/org/apidesign/bck2brwsr/emul/zip/InfTree.java
rt/emul/zip/src/main/java/org/apidesign/bck2brwsr/emul/zip/Inflate.java
rt/emul/zip/src/main/java/org/apidesign/bck2brwsr/emul/zip/Inflater.java
rt/emul/zip/src/main/java/org/apidesign/bck2brwsr/emul/zip/ZStream.java
rt/emul/zip/src/main/java/org/apidesign/bck2brwsr/vmzip/ZipResources.java
rt/emul/zip/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/ZipFileTest.java
     1.1 --- a/rt/emul/zip/src/main/java/org/apidesign/bck2brwsr/emul/zip/CRC32.java	Sun May 11 08:08:17 2014 +0200
     1.2 +++ b/rt/emul/zip/src/main/java/org/apidesign/bck2brwsr/emul/zip/CRC32.java	Sun May 11 08:10:28 2014 +0200
     1.3 @@ -175,7 +175,7 @@
     1.4  
     1.5    public static int[] getCRC32Table(){
     1.6      int[] tmp = new int[crc_table.length];
     1.7 -    System.arraycopy(crc_table, 0, tmp, 0, tmp.length);
     1.8 +    FastJar.arraycopy(crc_table, 0, tmp, 0, tmp.length);
     1.9      return tmp;
    1.10    }
    1.11  }
     2.1 --- a/rt/emul/zip/src/main/java/org/apidesign/bck2brwsr/emul/zip/FastJar.java	Sun May 11 08:08:17 2014 +0200
     2.2 +++ b/rt/emul/zip/src/main/java/org/apidesign/bck2brwsr/emul/zip/FastJar.java	Sun May 11 08:10:28 2014 +0200
     2.3 @@ -33,8 +33,10 @@
     2.4  import java.io.ByteArrayInputStream;
     2.5  import java.io.IOException;
     2.6  import java.io.InputStream;
     2.7 +import java.lang.reflect.Method;
     2.8  import java.util.zip.ZipEntry;
     2.9  import java.util.zip.ZipInputStream;
    2.10 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
    2.11  
    2.12  /**
    2.13   *
    2.14 @@ -100,7 +102,7 @@
    2.15          int giveup = 0;
    2.16  
    2.17          do {
    2.18 -            System.arraycopy(arr, at, data, 0, data.length);
    2.19 +            FastJar.arraycopy(arr, at, data, 0, data.length);
    2.20              at--;
    2.21              giveup++;
    2.22              if (giveup > GIVE_UP) {
    2.23 @@ -117,7 +119,7 @@
    2.24          int cenread = 0;
    2.25          data = new byte[ZipInputStream.CENHDR];
    2.26          while (cenread < censize) {
    2.27 -            System.arraycopy(arr, at, data, 0, data.length);
    2.28 +            FastJar.arraycopy(arr, at, data, 0, data.length);
    2.29              at += data.length;
    2.30              if (getsig(data) != ZipInputStream.CENSIG) {
    2.31                  throw new IOException("No central table");          //NOI18N
    2.32 @@ -141,7 +143,7 @@
    2.33      private Entry[] addEntry(Entry[] result, Entry entry) {
    2.34          Entry[] e = new Entry[result.length + 1];
    2.35          e[result.length] = entry;
    2.36 -        System.arraycopy(result, 0, e, 0, result.length);
    2.37 +        FastJar.arraycopy(result, 0, e, 0, result.length);
    2.38          return e;
    2.39      }
    2.40  
    2.41 @@ -171,5 +173,26 @@
    2.42  	final int s2 = get16(b, off+2);
    2.43          return s1 | ((long)s2 << 16);
    2.44      }
    2.45 +    
    2.46 +    @JavaScriptBody(args = {"value", "srcBegin", "dst", "dstBegin", "count"}, body
    2.47 +            = "if (srcBegin < dstBegin) {\n"
    2.48 +            + "    while (count-- > 0) {\n"
    2.49 +            + "        dst[dstBegin + count] = value[srcBegin + count];\n"
    2.50 +            + "    }\n"
    2.51 +            + "} else {\n"
    2.52 +            + "    while (count-- > 0) {\n"
    2.53 +            + "        dst[dstBegin++] = value[srcBegin++];\n"
    2.54 +            + "    }\n"
    2.55 +            + "}"
    2.56 +    )
    2.57 +    static void arraycopy(Object src, int srcBegin, Object dst, int dstBegin, int count) {
    2.58 +        try {
    2.59 +            Class<?> system = Class.forName("java.lang.System");
    2.60 +            Method m = system.getMethod("arraycopy", Object.class, int.class, Object.class, int.class, int.class);
    2.61 +            m.invoke(null, src, srcBegin, dst, dstBegin, count);
    2.62 +        } catch (Exception ex) {
    2.63 +            throw new IllegalStateException(ex);
    2.64 +        }
    2.65 +    }
    2.66  
    2.67  }
     3.1 --- a/rt/emul/zip/src/main/java/org/apidesign/bck2brwsr/emul/zip/GZIPHeader.java	Sun May 11 08:08:17 2014 +0200
     3.2 +++ b/rt/emul/zip/src/main/java/org/apidesign/bck2brwsr/emul/zip/GZIPHeader.java	Sun May 11 08:10:28 2014 +0200
     3.3 @@ -194,19 +194,19 @@
     3.4      byte[] tmp;
     3.5      if(gheader.extra!=null){
     3.6        tmp=new byte[gheader.extra.length];
     3.7 -      System.arraycopy(gheader.extra, 0, tmp, 0, tmp.length);
     3.8 +      FastJar.arraycopy(gheader.extra, 0, tmp, 0, tmp.length);
     3.9        gheader.extra = tmp;
    3.10      }
    3.11  
    3.12      if(gheader.name!=null){
    3.13        tmp=new byte[gheader.name.length];
    3.14 -      System.arraycopy(gheader.name, 0, tmp, 0, tmp.length);
    3.15 +      FastJar.arraycopy(gheader.name, 0, tmp, 0, tmp.length);
    3.16        gheader.name = tmp;
    3.17      }
    3.18  
    3.19      if(gheader.comment!=null){
    3.20        tmp=new byte[gheader.comment.length];
    3.21 -      System.arraycopy(gheader.comment, 0, tmp, 0, tmp.length);
    3.22 +      FastJar.arraycopy(gheader.comment, 0, tmp, 0, tmp.length);
    3.23        gheader.comment = tmp;
    3.24      }
    3.25  
     4.1 --- a/rt/emul/zip/src/main/java/org/apidesign/bck2brwsr/emul/zip/InfBlocks.java	Sun May 11 08:08:17 2014 +0200
     4.2 +++ b/rt/emul/zip/src/main/java/org/apidesign/bck2brwsr/emul/zip/InfBlocks.java	Sun May 11 08:10:28 2014 +0200
     4.3 @@ -270,7 +270,7 @@
     4.4  	t = left;
     4.5  	if(t>n) t = n;
     4.6  	if(t>m) t = m;
     4.7 -	System.arraycopy(z.next_in, p, window, q, t);
     4.8 +	FastJar.arraycopy(z.next_in, p, window, q, t);
     4.9  	p += t;  n -= t;
    4.10  	q += t;  m -= t;
    4.11  	if ((left -= t) != 0)
    4.12 @@ -540,7 +540,7 @@
    4.13    }
    4.14  
    4.15    void set_dictionary(byte[] d, int start, int n){
    4.16 -    System.arraycopy(d, start, window, 0, n);
    4.17 +    FastJar.arraycopy(d, start, window, 0, n);
    4.18      read = write = n;
    4.19    }
    4.20  
    4.21 @@ -575,7 +575,7 @@
    4.22      }
    4.23  
    4.24      // copy as far as end of window
    4.25 -    System.arraycopy(window, q, z.next_out, p, n);
    4.26 +    FastJar.arraycopy(window, q, z.next_out, p, n);
    4.27      p += n;
    4.28      q += n;
    4.29  
    4.30 @@ -601,7 +601,7 @@
    4.31        }
    4.32  
    4.33        // copy
    4.34 -      System.arraycopy(window, q, z.next_out, p, n);
    4.35 +      FastJar.arraycopy(window, q, z.next_out, p, n);
    4.36        p += n;
    4.37        q += n;
    4.38      }
     5.1 --- a/rt/emul/zip/src/main/java/org/apidesign/bck2brwsr/emul/zip/InfCodes.java	Sun May 11 08:08:17 2014 +0200
     5.2 +++ b/rt/emul/zip/src/main/java/org/apidesign/bck2brwsr/emul/zip/InfCodes.java	Sun May 11 08:10:28 2014 +0200
     5.3 @@ -503,7 +503,7 @@
     5.4  		  c-=2;
     5.5  		}
     5.6  		else{
     5.7 -		  System.arraycopy(s.window, r, s.window, q, 2);
     5.8 +		  FastJar.arraycopy(s.window, r, s.window, q, 2);
     5.9  		  q+=2; r+=2; c-=2;
    5.10  		}
    5.11  	      }
    5.12 @@ -520,7 +520,7 @@
    5.13  		    while(--e!=0);
    5.14  		  }
    5.15  		  else{
    5.16 -		    System.arraycopy(s.window, r, s.window, q, e);
    5.17 +		    FastJar.arraycopy(s.window, r, s.window, q, e);
    5.18  		    q+=e; r+=e; e=0;
    5.19  		  }
    5.20  		  r = 0;                  // copy rest from start of window
    5.21 @@ -534,7 +534,7 @@
    5.22  		while(--c!=0);
    5.23  	      }
    5.24  	      else{
    5.25 -		System.arraycopy(s.window, r, s.window, q, c);
    5.26 +		FastJar.arraycopy(s.window, r, s.window, q, c);
    5.27  		q+=c; r+=c; c=0;
    5.28  	      }
    5.29  	      break;
     6.1 --- a/rt/emul/zip/src/main/java/org/apidesign/bck2brwsr/emul/zip/InfTree.java	Sun May 11 08:08:17 2014 +0200
     6.2 +++ b/rt/emul/zip/src/main/java/org/apidesign/bck2brwsr/emul/zip/InfTree.java	Sun May 11 08:10:28 2014 +0200
     6.3 @@ -372,7 +372,7 @@
     6.4              r[1]=(byte)l;     // bits to dump before this table
     6.5              j=i>>>(w - l);
     6.6              r[2] = (int)(q - u[h-1] - j);               // offset to this table
     6.7 -            System.arraycopy(r, 0, hp, (u[h-1]+j)*3, 3); // connect to last table
     6.8 +            FastJar.arraycopy(r, 0, hp, (u[h-1]+j)*3, 3); // connect to last table
     6.9            }
    6.10            else{
    6.11              t[0] = q;               // first table is returned result
    6.12 @@ -396,7 +396,7 @@
    6.13          // fill code-like entries with r
    6.14          f=1<<(k-w);
    6.15          for (j=i>>>w;j<z;j+=f){
    6.16 -          System.arraycopy(r, 0, hp, (q+j)*3, 3);
    6.17 +          FastJar.arraycopy(r, 0, hp, (q+j)*3, 3);
    6.18  	}
    6.19  
    6.20  	// backwards increment the k-bit code i
    6.21 @@ -514,7 +514,7 @@
    6.22      for(int i=0; i<vsize; i++){v[i]=0;}
    6.23      for(int i=0; i<BMAX+1; i++){c[i]=0;}
    6.24      for(int i=0; i<3; i++){r[i]=0;}
    6.25 -    System.arraycopy(c, 0, u, 0, BMAX);
    6.26 -    System.arraycopy(c, 0, x, 0, BMAX+1);
    6.27 +    FastJar.arraycopy(c, 0, u, 0, BMAX);
    6.28 +    FastJar.arraycopy(c, 0, x, 0, BMAX+1);
    6.29    }
    6.30  }
     7.1 --- a/rt/emul/zip/src/main/java/org/apidesign/bck2brwsr/emul/zip/Inflate.java	Sun May 11 08:08:17 2014 +0200
     7.2 +++ b/rt/emul/zip/src/main/java/org/apidesign/bck2brwsr/emul/zip/Inflate.java	Sun May 11 08:10:28 2014 +0200
     7.3 @@ -457,7 +457,7 @@
     7.4                byte[] foo = tmp_array;
     7.5                tmp_array=null;
     7.6                if(foo.length == gheader.extra.length){
     7.7 -                System.arraycopy(foo, 0, gheader.extra, 0, foo.length);
     7.8 +                FastJar.arraycopy(foo, 0, gheader.extra, 0, foo.length);
     7.9  	      }
    7.10                else{
    7.11                  z.msg = "bad extra field length";
    7.12 @@ -684,7 +684,7 @@
    7.13    
    7.14    private static byte[] copy(byte[] arr, int len) {
    7.15        byte[] ret = new byte[len];
    7.16 -      System.arraycopy(arr, 0, ret, 0, len);
    7.17 +      FastJar.arraycopy(arr, 0, ret, 0, len);
    7.18        return ret;
    7.19    }
    7.20    private static byte[] append(byte[] arr, byte b, int index) {
     8.1 --- a/rt/emul/zip/src/main/java/org/apidesign/bck2brwsr/emul/zip/Inflater.java	Sun May 11 08:08:17 2014 +0200
     8.2 +++ b/rt/emul/zip/src/main/java/org/apidesign/bck2brwsr/emul/zip/Inflater.java	Sun May 11 08:10:28 2014 +0200
     8.3 @@ -154,7 +154,7 @@
     8.4              arr = b;
     8.5          } else {
     8.6              arr = new byte[len];
     8.7 -            System.arraycopy(b, off, arr, 0, len);
     8.8 +            FastJar.arraycopy(b, off, arr, 0, len);
     8.9          }
    8.10          impl.setDictionary(arr, len);
    8.11      }
     9.1 --- a/rt/emul/zip/src/main/java/org/apidesign/bck2brwsr/emul/zip/ZStream.java	Sun May 11 08:08:17 2014 +0200
     9.2 +++ b/rt/emul/zip/src/main/java/org/apidesign/bck2brwsr/emul/zip/ZStream.java	Sun May 11 08:10:28 2014 +0200
     9.3 @@ -170,8 +170,8 @@
     9.4  
     9.5      if(avail_in>0 && append){  
     9.6        byte[] tmp = new byte[avail_in+len];
     9.7 -      System.arraycopy(next_in, next_in_index, tmp, 0, avail_in);
     9.8 -      System.arraycopy(buf, off, tmp, avail_in, len);
     9.9 +      FastJar.arraycopy(next_in, next_in_index, tmp, 0, avail_in);
    9.10 +      FastJar.arraycopy(buf, off, tmp, avail_in, len);
    9.11        next_in=tmp;
    9.12        next_in_index=0;
    9.13        avail_in+=len;
    10.1 --- a/rt/emul/zip/src/main/java/org/apidesign/bck2brwsr/vmzip/ZipResources.java	Sun May 11 08:08:17 2014 +0200
    10.2 +++ b/rt/emul/zip/src/main/java/org/apidesign/bck2brwsr/vmzip/ZipResources.java	Sun May 11 08:10:28 2014 +0200
    10.3 @@ -54,7 +54,7 @@
    10.4      private byte[] findRes(String res) throws IOException {
    10.5          Object arr = findResImpl(res);
    10.6          if (arr instanceof FastJar.Entry) {
    10.7 -            long bef = timeNow();
    10.8 +            double bef = timeNow();
    10.9              InputStream zip = fj.getInputStream((FastJar.Entry)arr);
   10.10              arr = readFully(new byte[512], zip);
   10.11              putRes(res, arr);
   10.12 @@ -97,19 +97,12 @@
   10.13          return arr;
   10.14      }
   10.15  
   10.16 -    private static long timeNow() {
   10.17 -        double time = m();
   10.18 -        if (time >= 0) {
   10.19 -            return (long)time;
   10.20 -        }
   10.21 -        return org.apidesign.bck2brwsr.emul.lang.System.currentTimeMillis();
   10.22 -    }
   10.23      @JavaScriptBody(args = {}, body = 
   10.24          "if (typeof window.performance === 'undefined') return -1;\n"
   10.25        + "if (typeof window.performance.now === 'undefined') return -1;\n"
   10.26        + "return window.performance.now();"
   10.27      )
   10.28 -    private static native double m();
   10.29 +    private static native double timeNow();
   10.30  
   10.31      
   10.32  }
    11.1 --- a/rt/emul/zip/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/ZipFileTest.java	Sun May 11 08:08:17 2014 +0200
    11.2 +++ b/rt/emul/zip/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/ZipFileTest.java	Sun May 11 08:10:28 2014 +0200
    11.3 @@ -19,7 +19,6 @@
    11.4  
    11.5  import java.io.IOException;
    11.6  import java.io.InputStream;
    11.7 -import java.util.Objects;
    11.8  import java.util.zip.ZipEntry;
    11.9  import java.util.zip.ZipInputStream;
   11.10  import org.apidesign.bck2brwsr.core.JavaScriptBody;
   11.11 @@ -99,7 +98,16 @@
   11.12      }
   11.13      
   11.14      private static void assertEquals(Object real, Object exp, String msg) {
   11.15 -        assert Objects.equals(exp, real) : msg + " exp: " + exp + " real: " + real;
   11.16 +        if (real == null) {
   11.17 +            if (exp == null) {
   11.18 +                return;
   11.19 +            }
   11.20 +        } else {
   11.21 +            if (real.equals(exp)) {
   11.22 +                return;
   11.23 +            }
   11.24 +        }
   11.25 +        assert false : msg + " exp: " + exp + " real: " + real;
   11.26      }
   11.27      
   11.28      @Factory public static Object[] create() {