Imported JZlib source tree and re-implemented ZlibInStream using JZlib, similar to the way it's implemented in C with zlib. Also set svn:eol-style keyword to 'native' for most of the java files. There are still a handful with inconsistent eols style that need to be corrected.
git-svn-id: svn://svn.code.sf.net/p/tigervnc/code/trunk@4506 3789f03b-4d11-0410-bbf8-ca57d06f2519
diff --git a/java/src/com/tigervnc/rdr/InStream.java b/java/src/com/tigervnc/rdr/InStream.java
index 9b29930..68944b9 100644
--- a/java/src/com/tigervnc/rdr/InStream.java
+++ b/java/src/com/tigervnc/rdr/InStream.java
@@ -29,27 +29,24 @@
// itemSize bytes. Returns the number of items in the buffer (up to a
// maximum of nItems).
- public int check(int itemSize, int nItems) {
+ public int check(int itemSize, int nItems, boolean wait) {
if (ptr + itemSize * nItems > end) {
if (ptr + itemSize > end)
- return overrun(itemSize, nItems);
+ return overrun(itemSize, nItems, wait);
nItems = (end - ptr) / itemSize;
}
return nItems;
}
- public final int check(int itemSize) {
- if (ptr + itemSize > end)
- return overrun(itemSize, 1);
- return 1;
- }
+ public int check(int itemSize, int nItems) { return check(itemSize, nItems, true); }
+ public int check(int itemSize) { return check(itemSize, 1); }
// checkNoWait() tries to make sure that the given number of bytes can
// be read without blocking. It returns true if this is the case, false
// otherwise. The length must be "small" (less than the buffer size).
- public final boolean checkNoWait(int length) { return check(length, 1)!=0; }
+ public final boolean checkNoWait(int length) { return check(length, 1, false)!=0; }
// readU/SN() methods read unsigned and signed N-bit integers.
@@ -100,23 +97,23 @@
// readBytes() reads an exact number of bytes into an array at an offset.
- public void readBytes(byte[] data, int offset, int length) {
- int offsetEnd = offset + length;
- while (offset < offsetEnd) {
- int n = check(1, offsetEnd - offset);
- System.arraycopy(b, ptr, data, offset, n);
+ public void readBytes(byte[] data, int dataPtr, int length) {
+ int dataEnd = dataPtr + length;
+ while (dataPtr < dataEnd) {
+ int n = check(1, dataEnd - dataPtr);
+ System.arraycopy(b, ptr, data, dataPtr, n);
ptr += n;
- offset += n;
+ dataPtr += n;
}
}
- public void readBytes(int[] data, int offset, int length) {
- int offsetEnd = offset + length;
- while (offset < offsetEnd) {
- int n = check(1, offsetEnd - offset);
- System.arraycopy(b, ptr, data, offset, n);
+ public void readBytes(int[] data, int dataPtr, int length) {
+ int dataEnd = dataPtr + length;
+ while (dataPtr < dataEnd) {
+ int n = check(1, dataEnd - dataPtr);
+ System.arraycopy(b, ptr, data, dataPtr, n);
ptr += n;
- offset += n;
+ dataPtr += n;
}
}
@@ -187,7 +184,7 @@
// the number of items in the buffer (up to a maximum of nItems). itemSize
// is supposed to be "small" (a few bytes).
- abstract protected int overrun(int itemSize, int nItems);
+ abstract protected int overrun(int itemSize, int nItems, boolean wait);
protected InStream() {}
protected byte[] b;
diff --git a/java/src/com/tigervnc/rdr/JavaInStream.java b/java/src/com/tigervnc/rdr/JavaInStream.java
index 426a0e7..0f68bc6 100644
--- a/java/src/com/tigervnc/rdr/JavaInStream.java
+++ b/java/src/com/tigervnc/rdr/JavaInStream.java
@@ -85,7 +85,7 @@
public long timeWaited() { return timeWaitedIn100us; }
- protected int overrun(int itemSize, int nItems) {
+ protected int overrun(int itemSize, int nItems, boolean wait) {
if (itemSize > bufSize)
throw new Exception("JavaInStream overrun: max itemSize exceeded");
diff --git a/java/src/com/tigervnc/rdr/MemInStream.java b/java/src/com/tigervnc/rdr/MemInStream.java
index ce4f91e..32911a3 100644
--- a/java/src/com/tigervnc/rdr/MemInStream.java
+++ b/java/src/com/tigervnc/rdr/MemInStream.java
@@ -28,7 +28,7 @@
public int pos() { return ptr; }
- protected int overrun(int itemSize, int nItems) {
+ protected int overrun(int itemSize, int nItems, boolean wait) {
throw new EndOfStream();
}
}
diff --git a/java/src/com/tigervnc/rdr/ZlibInStream.java b/java/src/com/tigervnc/rdr/ZlibInStream.java
index 64de00a..439ddfe 100644
--- a/java/src/com/tigervnc/rdr/ZlibInStream.java
+++ b/java/src/com/tigervnc/rdr/ZlibInStream.java
@@ -21,58 +21,79 @@
//
package com.tigervnc.rdr;
+import com.jcraft.jzlib.*;
public class ZlibInStream extends InStream {
static final int defaultBufSize = 16384;
- public ZlibInStream(int bufSize_) {
+ public ZlibInStream(int bufSize_)
+ {
bufSize = bufSize_;
b = new byte[bufSize];
- ptr = end = ptrOffset = 0;
- inflater = new java.util.zip.Inflater();
+ bytesIn = offset = 0;
+ zs = new ZStream();
+ zs.next_in_index = 0;
+ zs.avail_in = 0;
+ if (zs.inflateInit() != JZlib.Z_OK) {
+ zs = null;
+ throw new Exception("ZlinInStream: inflateInit failed");
+ }
+ ptr = end = start = 0;
}
public ZlibInStream() { this(defaultBufSize); }
- public void setUnderlying(InStream is, int bytesIn_) {
- underlying = is;
- bytesIn = bytesIn_;
- ptr = end = 0;
+ protected void finalize() throws Throwable {
+ b = null;
+ zs.inflateEnd();
}
- public void reset() {
- ptr = end = 0;
+ public void setUnderlying(InStream is, int bytesIn_)
+ {
+ underlying = is;
+ bytesIn = bytesIn_;
+ ptr = end = start;
+ }
+
+ public int pos()
+ {
+ return offset + ptr - start;
+ }
+
+ public void reset()
+ {
+ ptr = end = start;
if (underlying == null) return;
while (bytesIn > 0) {
- decompress();
- end = 0; // throw away any data
+ decompress(true);
+ end = start; // throw away any data
}
underlying = null;
}
- public int pos() { return ptrOffset + ptr; }
-
- protected int overrun(int itemSize, int nItems) {
+ protected int overrun(int itemSize, int nItems, boolean wait)
+ {
if (itemSize > bufSize)
throw new Exception("ZlibInStream overrun: max itemSize exceeded");
if (underlying == null)
throw new Exception("ZlibInStream overrun: no underlying stream");
if (end - ptr != 0)
- System.arraycopy(b, ptr, b, 0, end - ptr);
+ System.arraycopy(b, ptr, b, start, end - ptr);
- ptrOffset += ptr;
- end -= ptr;
- ptr = 0;
+ offset += ptr - start;
+ end -= ptr - start;
+ ptr = start;
- while (end < itemSize) {
- decompress();
+ while (end - ptr < itemSize) {
+ if (!decompress(wait))
+ return 0;
}
- if (itemSize * nItems > end)
- nItems = end / itemSize;
+ if (itemSize * nItems > end - ptr)
+ nItems = (end - ptr) / itemSize;
return nItems;
}
@@ -82,32 +103,34 @@
// data. Returns false if wait is false and we would block on the underlying
// stream.
- private void decompress() {
- try {
- underlying.check(1);
- int avail_in = underlying.getend() - underlying.getptr();
- if (avail_in > bytesIn)
- avail_in = bytesIn;
+ private boolean decompress(boolean wait)
+ {
+ zs.next_out = b;
+ zs.next_out_index = end;
+ zs.avail_out = start + bufSize - end;
- if (inflater.needsInput()) {
- inflater.setInput(underlying.getbuf(), underlying.getptr(), avail_in);
- }
+ int n = underlying.check(1, 1, wait);
+ if (n == 0) return false;
+ zs.next_in = underlying.getbuf();
+ zs.avail_in = underlying.getend() - underlying.getptr();
+ if (zs.avail_in > bytesIn)
+ zs.avail_in = bytesIn;
- int n = inflater.inflate(b, end, bufSize - end);
-
- end += n;
- if (inflater.needsInput()) {
- bytesIn -= avail_in;
- underlying.setptr(underlying.getptr() + avail_in);
- }
- } catch (java.util.zip.DataFormatException e) {
+ int rc = zs.inflate(JZlib.Z_SYNC_FLUSH);
+ if (rc != JZlib.Z_OK) {
throw new Exception("ZlibInStream: inflate failed");
}
+
+ bytesIn -= zs.next_in_index - underlying.getptr();
+ end = zs.next_out_index;
+ underlying.setptr(zs.next_in_index);
+ return true;
}
private InStream underlying;
private int bufSize;
- private int ptrOffset;
- private java.util.zip.Inflater inflater;
+ private int offset;
+ private com.jcraft.jzlib.ZStream zs;
private int bytesIn;
+ private int start;
}