massive performance improvements. zrle measured at 2.5x faster (no measurements for tight, but should be the same or better).

git-svn-id: svn://svn.code.sf.net/p/tigervnc/code/trunk@4742 3789f03b-4d11-0410-bbf8-ca57d06f2519
diff --git a/java/com/tigervnc/rdr/InStream.java b/java/com/tigervnc/rdr/InStream.java
index 803f55f..6096799 100644
--- a/java/com/tigervnc/rdr/InStream.java
+++ b/java/com/tigervnc/rdr/InStream.java
@@ -130,20 +130,30 @@
                                      int b1 = b[ptr++]; int b2 = b[ptr++];
                                      return b0 << 16 | b1 << 8 | b2; }
 
-  public final int readPixel(int bytesPerPixel, boolean e) {
-    int[] pix = new int[4];
-    for (int i=0; i < bytesPerPixel; i++)
-      pix[i] = readU8();
-    if (e) {
-      return pix[0] << 16 | pix[1] << 8 | pix[2] | (0xff << 24);
+  public final int readPixel(int bytesPerPixel, boolean bigEndian) {
+    byte[] pix = new byte[4];
+    readBytes(pix, 0, bytesPerPixel);
+
+    if (bigEndian) {
+      return 0xff000000 | (pix[0] & 0xff)<<16 | (pix[1] & 0xff)<<8 | (pix[2] & 0xff);
     } else {
-      return pix[2] << 16 | pix[1] << 8 | pix[0] | (0xff << 24);
+      return 0xff000000 | (pix[2] & 0xff)<<16 | (pix[1] & 0xff)<<8 | (pix[0] & 0xff);
     }
   }
 
-  public final void readPixels(int[] buf, int length, int bytesPerPixel, boolean e) {
-    for (int i = 0; i < length; i++)
-      buf[i] = readPixel(bytesPerPixel, e);
+  public final void readPixels(int[] buf, int length, int bytesPerPixel, boolean bigEndian) {
+    int npixels = length*bytesPerPixel;
+    byte[] pixels = new byte[npixels];
+    readBytes(pixels, 0, npixels);
+    for (int i = 0; i < length; i++) {
+      byte[] pix = new byte[4];
+      System.arraycopy(pixels, i*bytesPerPixel, pix, 0, bytesPerPixel);
+      if (bigEndian) {
+        buf[i] = 0xff000000 | (pix[0] & 0xff)<<16 | (pix[1] & 0xff)<<8 | (pix[2] & 0xff);
+      } else {
+        buf[i] = 0xff000000 | (pix[2] & 0xff)<<16 | (pix[1] & 0xff)<<8 | (pix[0] & 0xff);
+      }
+    }
   }
 
   public final int readCompactLength() {
diff --git a/java/com/tigervnc/rdr/JavaInStream.java b/java/com/tigervnc/rdr/JavaInStream.java
index 5c35036..d2eda52 100644
--- a/java/com/tigervnc/rdr/JavaInStream.java
+++ b/java/com/tigervnc/rdr/JavaInStream.java
@@ -32,6 +32,7 @@
     bufSize = bufSize_;
     b = new byte[bufSize];
     ptr = end = offset = 0;
+    timing = false;
     timeWaitedIn100us = 5;
     timedKbits = 0;
   }
@@ -108,12 +109,17 @@
   }
 
   private int read(byte[] buf, int bufPtr, int len, boolean wait) {
-    try {
       long before = 0;
       if (timing)
         before = System.nanoTime();
 
-      int n = jis.read(buf, bufPtr, len);
+      int n = -1;
+      try {
+        n = jis.read(buf, bufPtr, len);
+      } catch (java.io.IOException e) {
+        throw new IOException(e);
+      }
+
       if (n < 0) throw new EndOfStream();
 
       if (timing) {
@@ -123,8 +129,11 @@
 
         // limit rate to between 10kbit/s and 40Mbit/s
 
-        if (newTimeWaited > newKbits*1000) newTimeWaited = newKbits*1000;
-        if (newTimeWaited < newKbits/4)    newTimeWaited = newKbits/4;
+        if (newTimeWaited > newKbits*1000) {
+          newTimeWaited = newKbits*1000;
+        } else if (newTimeWaited < newKbits/4) {
+          newTimeWaited = newKbits/4;
+        }
 
         timeWaitedIn100us += newTimeWaited;
         timedKbits += newKbits;
@@ -132,9 +141,6 @@
 
       return n;
 
-    } catch (java.io.IOException e) {
-      throw new IOException(e);
-    }
   }
   private int read(byte[] buf, int bufPtr, int len) { return read(buf, bufPtr, len, true); }