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/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();