Sync up java Tight decoder with recent changes to C client as much as possible. These changes should also fix the 16bpp issue reported in bug #3429667.  I think there are probably errors in the FilterGradient* code but I can't get any servers to actually send this type of data to test it.

git-svn-id: svn://svn.code.sf.net/p/tigervnc/code/trunk@4819 3789f03b-4d11-0410-bbf8-ca57d06f2519
diff --git a/java/com/tigervnc/rdr/InStream.java b/java/com/tigervnc/rdr/InStream.java
index 6096799..a18ea4e 100644
--- a/java/com/tigervnc/rdr/InStream.java
+++ b/java/com/tigervnc/rdr/InStream.java
@@ -107,16 +107,6 @@
     }
   }
 
-  public void readBytes(int[] data, int dataPtr, int length) {
-    int dataEnd = dataPtr + length;
-    while (dataPtr < dataEnd) {
-      int n = check(1, dataEnd - dataPtr, true);
-      System.arraycopy(b, ptr, data, dataPtr, n);
-      ptr += n;
-      dataPtr += n;
-    }
-  }
-
   // readOpaqueN() reads a quantity "without byte-swapping".  Because java has
   // no byte-ordering, we just use big-endian.
 
diff --git a/java/com/tigervnc/rdr/JavaInStream.java b/java/com/tigervnc/rdr/JavaInStream.java
index d2eda52..faa968a 100644
--- a/java/com/tigervnc/rdr/JavaInStream.java
+++ b/java/com/tigervnc/rdr/JavaInStream.java
@@ -98,7 +98,14 @@
     ptr = 0;
 
     while (end < itemSize) {
-      int n = read(b, end, bufSize - end, wait);
+      int bytes_to_read = bufSize - end;
+
+      if (!timing) {
+        bytes_to_read = Math.min(bytes_to_read, Math.max(itemSize*nItems, 8));
+      }
+
+      int n = read(b, end, bytes_to_read, wait);
+
       end += n;
     }
 
@@ -121,6 +128,7 @@
       }
 
       if (n < 0) throw new EndOfStream();
+      if (n == 0) return 0;
 
       if (timing) {
         long after = System.nanoTime();