From b898b888a3d83ed082c020f79283ecb42162a9d7 Mon Sep 17 00:00:00 2001 From: Brian Hinz Date: Sun, 2 Oct 2011 02:40:37 +0000 Subject: [PATCH] cleanup of rdr code, uniquify variable names, etc. git-svn-id: svn://svn.code.sf.net/p/tigervnc/code/trunk@4688 3789f03b-4d11-0410-bbf8-ca57d06f2519 --- java/src/com/tigervnc/rdr/InStream.java | 3 +- java/src/com/tigervnc/rdr/JavaInStream.java | 29 +++++------ java/src/com/tigervnc/rdr/JavaOutStream.java | 51 +++++++++----------- java/src/com/tigervnc/rdr/OutStream.java | 31 +++++++++--- 4 files changed, 64 insertions(+), 50 deletions(-) diff --git a/java/src/com/tigervnc/rdr/InStream.java b/java/src/com/tigervnc/rdr/InStream.java index 68944b9f..1e0d226e 100644 --- a/java/src/com/tigervnc/rdr/InStream.java +++ b/java/src/com/tigervnc/rdr/InStream.java @@ -70,9 +70,8 @@ abstract public class InStream { if (len > maxStringLength) throw new Exception("InStream max string length exceeded"); - byte[] str = new byte[len+1]; + byte[] str = new byte[len]; readBytes(str, 0, len); - str[len] = 0; String utf8string = new String(); try { utf8string = new String(str,"UTF8"); diff --git a/java/src/com/tigervnc/rdr/JavaInStream.java b/java/src/com/tigervnc/rdr/JavaInStream.java index 0f68bc6e..ce8efddc 100644 --- a/java/src/com/tigervnc/rdr/JavaInStream.java +++ b/java/src/com/tigervnc/rdr/JavaInStream.java @@ -31,36 +31,36 @@ public class JavaInStream extends InStream { jis = jis_; bufSize = bufSize_; b = new byte[bufSize]; - ptr = end = ptrOffset = 0; + ptr = end = offset = 0; timeWaitedIn100us = 5; timedKbits = 0; } public JavaInStream(java.io.InputStream jis_) { this(jis_, defaultBufSize); } - public void readBytes(byte[] data, int offset, int length) { + public void readBytes(byte[] data, int dataPtr, int length) { if (length < minBulkSize) { - super.readBytes(data, offset, length); + super.readBytes(data, dataPtr, length); return; } int n = end - ptr; if (n > length) n = length; - System.arraycopy(b, ptr, data, offset, n); - offset += n; + System.arraycopy(b, ptr, data, dataPtr, n); + dataPtr += n; length -= n; ptr += n; while (length > 0) { - n = read(data, offset, length); - offset += n; + n = read(data, dataPtr, length); + dataPtr += n; length -= n; - ptrOffset += n; + offset += n; } } - public int pos() { return ptrOffset + ptr; } + public int pos() { return offset + ptr; } public void startTiming() { timing = true; @@ -92,12 +92,12 @@ public class JavaInStream extends InStream { if (end - ptr != 0) System.arraycopy(b, ptr, b, 0, end - ptr); - ptrOffset += ptr; + offset += ptr; end -= ptr; ptr = 0; while (end < itemSize) { - int n = read(b, end, bufSize - end); + int n = read(b, end, bufSize - end, wait); end += n; } @@ -107,13 +107,13 @@ public class JavaInStream extends InStream { return nItems; } - private int read(byte[] buf, int offset, int len) { + private int read(byte[] buf, int bufPtr, int len, boolean wait) { try { long before = 0; if (timing) before = System.currentTimeMillis(); - int n = jis.read(buf, offset, len); + int n = jis.read(buf, bufPtr, len); if (n < 0) throw new EndOfStream(); if (timing) { @@ -136,9 +136,10 @@ public class JavaInStream extends InStream { throw new IOException(e); } } + private int read(byte[] buf, int bufPtr, int len) { return read(buf, bufPtr, len, true); } private java.io.InputStream jis; - private int ptrOffset; + private int offset; private int bufSize; boolean timing; diff --git a/java/src/com/tigervnc/rdr/JavaOutStream.java b/java/src/com/tigervnc/rdr/JavaOutStream.java index 94791b9d..015f81f5 100644 --- a/java/src/com/tigervnc/rdr/JavaOutStream.java +++ b/java/src/com/tigervnc/rdr/JavaOutStream.java @@ -31,52 +31,47 @@ public class JavaOutStream extends OutStream { jos = jos_; bufSize = bufSize_; b = new byte[bufSize]; - ptr = 0; - end = bufSize; + ptr = offset = start = 0; + end = start + bufSize; } public JavaOutStream(java.io.OutputStream jos) { this(jos, defaultBufSize); } - public void writeBytes(byte[] data, int offset, int length) { - if (length < minBulkSize) { - super.writeBytes(data, offset, length); - return; - } - - flush(); - try { - jos.write(data, offset, length); - } catch (java.io.IOException e) { - throw new IOException(e); - } - ptrOffset += length; + public int length() + { + return offset + ptr - start; } - public void flush() { - try { - jos.write(b, 0, ptr); - } catch (java.io.IOException e) { - throw new IOException(e); + public void flush() + { + int sentUpTo = start; + while (sentUpTo < ptr) { + try { + jos.write(b, sentUpTo, ptr - sentUpTo); + sentUpTo += ptr - sentUpTo; + offset += ptr - sentUpTo; + } catch (java.io.IOException e) { + throw new IOException(e); + } } - ptrOffset += ptr; - ptr = 0; + ptr = start; } - public int length() { return ptrOffset + ptr; } - - protected int overrun(int itemSize, int nItems) { + protected int overrun(int itemSize, int nItems) + { if (itemSize > bufSize) throw new Exception("JavaOutStream overrun: max itemSize exceeded"); flush(); - if (itemSize * nItems > end) - nItems = end / itemSize; + if (itemSize * nItems > end - ptr) + nItems = (end - ptr) / itemSize; return nItems; } private java.io.OutputStream jos; - private int ptrOffset; + private int start; + private int offset; private int bufSize; } diff --git a/java/src/com/tigervnc/rdr/OutStream.java b/java/src/com/tigervnc/rdr/OutStream.java index 06f832d9..0919453e 100644 --- a/java/src/com/tigervnc/rdr/OutStream.java +++ b/java/src/com/tigervnc/rdr/OutStream.java @@ -58,6 +58,25 @@ abstract public class OutStream { public final void writeS16(int s) { writeU16(s); } public final void writeS32(int s) { writeU32(s); } + // writeCompactLength() writes 1..3 bytes representing length of the data + // following. This method is used by the Tight encoder. + + public final void writeCompactLength(int len) { + byte b = (byte)(len & 0x7F); + if (len <= 0x7F) { + writeU8(b); + } else { + writeU8(b | 0x80); + b = (byte)(len >> 7 & 0x7F); + if (len <= 0x3FFF) { + writeU8(b); + } else { + writeU8(b | 0x80); + writeU8(len >> 14 & 0xFF); + } + } + } + // writeString() writes a string - a U32 length followed by the data. public final void writeString(String str) { @@ -85,13 +104,13 @@ abstract public class OutStream { // writeBytes() writes an exact number of bytes from an array at an offset. - public void writeBytes(byte[] data, int offset, int length) { - int offsetEnd = offset + length; - while (offset < offsetEnd) { - int n = check(1, offsetEnd - offset); - System.arraycopy(data, offset, b, ptr, n); + public void writeBytes(byte[] data, int dataPtr, int length) { + int dataEnd = dataPtr + length; + while (dataPtr < dataEnd) { + int n = check(1, dataEnd - dataPtr); + System.arraycopy(data, dataPtr, b, ptr, n); ptr += n; - offset += n; + dataPtr += n; } } -- 2.39.5