aboutsummaryrefslogtreecommitdiffstats
path: root/java/com/tigervnc/rdr/JavaInStream.java
diff options
context:
space:
mode:
Diffstat (limited to 'java/com/tigervnc/rdr/JavaInStream.java')
-rw-r--r--java/com/tigervnc/rdr/JavaInStream.java107
1 files changed, 42 insertions, 65 deletions
diff --git a/java/com/tigervnc/rdr/JavaInStream.java b/java/com/tigervnc/rdr/JavaInStream.java
index fb6ab8b6..faa968ac 100644
--- a/java/com/tigervnc/rdr/JavaInStream.java
+++ b/java/com/tigervnc/rdr/JavaInStream.java
@@ -21,37 +21,25 @@
//
package com.tigervnc.rdr;
-import java.io.InputStream;
-import java.io.DataInputStream;
public class JavaInStream extends InStream {
static final int defaultBufSize = 8192;
static final int minBulkSize = 1024;
- public JavaInStream(InputStream jis_, int bufSize_)
- {
- jis = new DataInputStream(jis_);
+ public JavaInStream(java.io.InputStream jis_, int bufSize_) {
+ jis = jis_;
bufSize = bufSize_;
b = new byte[bufSize];
- ptr = end = offset = start = 0;
+ ptr = end = offset = 0;
timing = false;
timeWaitedIn100us = 5;
timedKbits = 0;
}
- public JavaInStream(InputStream jis_)
- {
- this(jis_, defaultBufSize);
- }
-
- public int pos()
- {
- return offset + ptr - start;
- }
+ public JavaInStream(java.io.InputStream jis_) { this(jis_, defaultBufSize); }
- public void readBytes(byte[] data, int dataPtr, int length)
- {
+ public void readBytes(byte[] data, int dataPtr, int length) {
if (length < minBulkSize) {
super.readBytes(data, dataPtr, length);
return;
@@ -73,32 +61,56 @@ public class JavaInStream extends InStream {
}
}
+ public int pos() { return offset + ptr; }
+
+ public void startTiming() {
+ timing = true;
+
+ // Carry over up to 1s worth of previous rate for smoothing.
+
+ if (timeWaitedIn100us > 10000) {
+ timedKbits = timedKbits * 10000 / timeWaitedIn100us;
+ timeWaitedIn100us = 10000;
+ }
+ }
+
+ public void stopTiming() {
+ timing = false;
+ if (timeWaitedIn100us < timedKbits/2)
+ timeWaitedIn100us = timedKbits/2; // upper limit 20Mbit/s
+ }
+
+ public long kbitsPerSecond() {
+ return timedKbits * 10000 / timeWaitedIn100us;
+ }
+
+ public long timeWaited() { return timeWaitedIn100us; }
+
protected int overrun(int itemSize, int nItems, boolean wait) {
if (itemSize > bufSize)
throw new Exception("JavaInStream overrun: max itemSize exceeded");
if (end - ptr != 0)
- System.arraycopy(b, ptr, b, start, end - ptr);
+ System.arraycopy(b, ptr, b, 0, end - ptr);
- offset += ptr - start;
- end -= ptr - start;
- ptr = start;
+ offset += ptr;
+ end -= ptr;
+ ptr = 0;
- int bytes_to_read;
- while (end < start + itemSize) {
- bytes_to_read = start + bufSize - end;
+ while (end < itemSize) {
+ 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);
- if (n == 0) return 0;
+
end += n;
}
- if (itemSize * nItems > end - ptr)
- nItems = (end - ptr) / itemSize;
+ if (itemSize * nItems > end)
+ nItems = end / itemSize;
return nItems;
}
@@ -110,17 +122,7 @@ public class JavaInStream extends InStream {
int n = -1;
try {
- //System.out.println("available: "+jis.available());
- //int available = jis.available();
- //if (!wait && (available == 0))
- // return 0;
- //if (available > 0 && available < len) {
- // n = jis.read(buf, bufPtr, available);
- //} else {
- n = jis.read(buf, bufPtr, len);
- //}
- } catch (java.net.SocketTimeoutException e) {
- return Math.max(n,0);
+ n = jis.read(buf, bufPtr, len);
} catch (java.io.IOException e) {
throw new IOException(e);
}
@@ -148,34 +150,9 @@ public class JavaInStream extends InStream {
return n;
}
- private int read(byte[] buf, int bufPtr, int len) {
- return read(buf, bufPtr, len, true);
- }
-
- public void startTiming() {
- timing = true;
-
- // Carry over up to 1s worth of previous rate for smoothing.
-
- if (timeWaitedIn100us > 10000) {
- timedKbits = timedKbits * 10000 / timeWaitedIn100us;
- timeWaitedIn100us = 10000;
- }
- }
-
- public void stopTiming() {
- timing = false;
- if (timeWaitedIn100us < timedKbits/2)
- timeWaitedIn100us = timedKbits/2; // upper limit 20Mbit/s
- }
-
- public long kbitsPerSecond() {
- return timedKbits * 10000 / timeWaitedIn100us;
- }
-
- public long timeWaited() { return timeWaitedIn100us; }
+ private int read(byte[] buf, int bufPtr, int len) { return read(buf, bufPtr, len, true); }
- private DataInputStream jis;
+ private java.io.InputStream jis;
private int offset;
private int bufSize;