performance improvements

git-svn-id: svn://svn.code.sf.net/p/tigervnc/code/trunk@4733 3789f03b-4d11-0410-bbf8-ca57d06f2519
diff --git a/java/com/tigervnc/rdr/InStream.java b/java/com/tigervnc/rdr/InStream.java
index 1e0d226..803f55f 100644
--- a/java/com/tigervnc/rdr/InStream.java
+++ b/java/com/tigervnc/rdr/InStream.java
@@ -30,11 +30,12 @@
   // maximum of nItems).
 
   public int check(int itemSize, int nItems, boolean wait) {
-    if (ptr + itemSize * nItems > end) {
-      if (ptr + itemSize > end)
+    int available = end - ptr;
+    if (itemSize * nItems > available) {
+      if (itemSize > available)
         return overrun(itemSize, nItems, wait);
 
-      nItems = (end - ptr) / itemSize;
+      nItems = available / itemSize;
     }
     return nItems;
   }
@@ -50,10 +51,10 @@
 
   // readU/SN() methods read unsigned and signed N-bit integers.
 
-  public final int readS8()  { check(1); return b[ptr++]; }
-  public final int readS16() { check(2); int b0 = b[ptr++];
+  public final int readS8()  { check(1,1,true); return b[ptr++]; }
+  public final int readS16() { check(2,1,true); int b0 = b[ptr++];
                                int b1 = b[ptr++] & 0xff; return b0 << 8 | b1; }
-  public final int readS32() { check(4); int b0 = b[ptr++];
+  public final int readS32() { check(4,1,true); int b0 = b[ptr++];
                                int b1 = b[ptr++] & 0xff;
                                int b2 = b[ptr++] & 0xff;
                                int b3 = b[ptr++] & 0xff;
@@ -88,7 +89,7 @@
 
   public final void skip(int bytes) {
     while (bytes > 0) {
-      int n = check(1, bytes);
+      int n = check(1, bytes, true);
       ptr += n;
       bytes -= n;
     }
@@ -99,7 +100,7 @@
   public void readBytes(byte[] data, int dataPtr, int length) {
     int dataEnd = dataPtr + length;
     while (dataPtr < dataEnd) {
-      int n = check(1, dataEnd - dataPtr);
+      int n = check(1, dataEnd - dataPtr, true);
       System.arraycopy(b, ptr, data, dataPtr, n);
       ptr += n;
       dataPtr += n;
@@ -109,7 +110,7 @@
   public void readBytes(int[] data, int dataPtr, int length) {
     int dataEnd = dataPtr + length;
     while (dataPtr < dataEnd) {
-      int n = check(1, dataEnd - dataPtr);
+      int n = check(1, dataEnd - dataPtr, true);
       System.arraycopy(b, ptr, data, dataPtr, n);
       ptr += n;
       dataPtr += n;
@@ -122,10 +123,10 @@
   public final int readOpaque8()  { return readU8(); }
   public final int readOpaque16() { return readU16(); }
   public final int readOpaque32() { return readU32(); }
-  public final int readOpaque24A() { check(3); int b0 = b[ptr++];
+  public final int readOpaque24A() { check(3, 1, true); int b0 = b[ptr++];
                                      int b1 = b[ptr++]; int b2 = b[ptr++];
                                      return b0 << 24 | b1 << 16 | b2 << 8; }
-  public final int readOpaque24B() { check(3); int b0 = b[ptr++];
+  public final int readOpaque24B() { check(3, 1, true); int b0 = b[ptr++];
                                      int b1 = b[ptr++]; int b2 = b[ptr++];
                                      return b0 << 16 | b1 << 8 | b2; }
 
diff --git a/java/com/tigervnc/rdr/JavaInStream.java b/java/com/tigervnc/rdr/JavaInStream.java
index ce8efdd..5c35036 100644
--- a/java/com/tigervnc/rdr/JavaInStream.java
+++ b/java/com/tigervnc/rdr/JavaInStream.java
@@ -111,14 +111,14 @@
     try {
       long before = 0;
       if (timing)
-        before = System.currentTimeMillis();
+        before = System.nanoTime();
 
       int n = jis.read(buf, bufPtr, len);
       if (n < 0) throw new EndOfStream();
 
       if (timing) {
-        long after = System.currentTimeMillis();
-        long newTimeWaited = (after - before) * 10;
+        long after = System.nanoTime();
+        long newTimeWaited = (after - before) / 100000;
         int newKbits = n * 8 / 1000;
 
         // limit rate to between 10kbit/s and 40Mbit/s
diff --git a/java/com/tigervnc/vncviewer/DesktopWindow.java b/java/com/tigervnc/vncviewer/DesktopWindow.java
index 978368d..c0071da 100644
--- a/java/com/tigervnc/vncviewer/DesktopWindow.java
+++ b/java/com/tigervnc/vncviewer/DesktopWindow.java
@@ -163,6 +163,7 @@
       tk.createImage(bitmap).getScaledInstance(cw,ch,hint);
     softCursor = tk.createCustomCursor(cursorImage,
                   new java.awt.Point(hotspot.x,hotspot.y), "Cursor");
+    cursorImage.flush();
 
     if (softCursor != null) {
       setCursor(softCursor); 
@@ -282,7 +283,7 @@
     if (overlapsCursor(x, y, w, h) || overlapsCursor(srcX, srcY, w, h))
       hideLocalCursor();
     synchronized (im) {
-      im.copyRect(x, y, w, h, srcX, srcY);
+      im.copyRect(x, y, w, h, srcX, srcY, graphics);
     }
     if (!cc.viewer.fastCopyRect.getValue()) {
       invalidate(x, y, w, h);
diff --git a/java/com/tigervnc/vncviewer/PixelBufferImage.java b/java/com/tigervnc/vncviewer/PixelBufferImage.java
index ef0eed8..78697d8 100644
--- a/java/com/tigervnc/vncviewer/PixelBufferImage.java
+++ b/java/com/tigervnc/vncviewer/PixelBufferImage.java
@@ -112,10 +112,11 @@
   // copyRect() we also need to tell the ImageConsumer that the pixels have
   // changed (this is done in the put() call for the others).
 
-  public void copyRect(int x, int y, int w, int h, int srcX, int srcY) {
+  public void copyRect(int x, int y, int w, int h, int srcX, int srcY, Graphics g) {
     super.copyRect(x, y, w, h, srcX, srcY);
     if (ic == null) return;
     ic.setPixels(x, y, w, h, cm, data, width() * y + x, width());
+    g.setClip(x, y, w, h);
     ic.imageComplete(ImageConsumer.SINGLEFRAMEDONE);
   }