changeset 98:3db7ac2b10f7

JUnit test passed, but VNC stopped.
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 03 Aug 2011 20:55:15 +0900
parents 145506601e0d
children 0c5762c3a8dd
files src/myVncProxy/MyRfbProto.java
diffstat 1 files changed, 37 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/src/myVncProxy/MyRfbProto.java	Wed Aug 03 15:02:37 2011 +0900
+++ b/src/myVncProxy/MyRfbProto.java	Wed Aug 03 20:55:15 2011 +0900
@@ -536,23 +536,30 @@
 	 */
 	public int zip(Deflater deflater,LinkedList<ByteBuffer> inputs, int inputIndex, LinkedList<ByteBuffer> outputs) throws IOException {
 		int len1=0,len = 0;
-		deflater.reset();
-		while(inputIndex < inputs.size()) {
+		ByteBuffer c1= ByteBuffer.allocate(INFLATE_BUFSIZE);
+		while(inputIndex < inputs.size() ) {
 			ByteBuffer b1 = inputs.get(inputIndex++);
-			deflater.setInput(b1.array(),b1.position(),b1.limit());
+			deflater.setInput(b1.array(),b1.position(),b1.remaining());
 			if (inputIndex==inputs.size()) {
 				deflater.finish();
 			} 
 			do {
-				ByteBuffer c1 = ByteBuffer.allocate(INFLATE_BUFSIZE);
-				len1 = deflater.deflate(c1.array(),c1.position(),c1.capacity());
-				c1.limit(len1);
-				if (len1>0) {
+				len1 = deflater.deflate(c1.array(),c1.position(),c1.remaining());
+				if (len1<=0) break;   // get next buffer 
+				len += len1;
+				c1.position(c1.position()+len1); 
+				if (c1.remaining()==0) {
+					c1.flip();
 					outputs.addLast(c1);
-					len += len1;
+					c1 = ByteBuffer.allocate(INFLATE_BUFSIZE);
 				}
-			} while (len1==INFLATE_BUFSIZE);
-		} 
+			} while (!deflater.finished());
+		}
+		if (c1.position()!=0) {
+			c1.flip();
+			outputs.addLast(c1);
+		}
+		deflater.reset();
 		return len;
 	}
 	
@@ -566,24 +573,30 @@
 	 *@return  number of total bytes            
 	 * @throws IOException
 	 */
-	public int unzip(Inflater inflater, LinkedList<ByteBuffer> inputs, LinkedList<ByteBuffer> outputs)
+	public int unzip(Inflater inflater, LinkedList<ByteBuffer> inputs, int inputIndex, LinkedList<ByteBuffer> outputs)
 																	throws DataFormatException {
 		int len=0,len0;
-	    inflater.reset();
-		int inputIndex = 0;
-		do {
+		ByteBuffer buf = ByteBuffer.allocate(INFLATE_BUFSIZE);
+	    // inflater.reset();   // if we uncomment this, test1() will be passed. But it won't work with real connection.
+		while (inputIndex < inputs.size()) {
 			ByteBuffer input = inputs.get(inputIndex++);
 			inflater.setInput(input.array(),0,input.limit());
 			do {
-				ByteBuffer buf = ByteBuffer.allocate(INFLATE_BUFSIZE);
-				len0 = inflater.inflate(buf.array(),0,buf.capacity());
-				buf.limit(len0);
+				len0 = inflater.inflate(buf.array(),buf.position(),buf.remaining());
+				buf.position(buf.position()+len0);
 				len += len0;
-				if (len0>0) {
+				if (buf.remaining()==0) {
+					buf.flip();
 					outputs.addLast(buf);
+					buf = ByteBuffer.allocate(INFLATE_BUFSIZE);
 				}
-			} while (len0>0);
-		} while (inputIndex < inputs.size()) ;
+			} while (!inflater.finished());
+		} 
+		if (buf.position()!=0) {
+			buf.flip();
+			outputs.addLast(buf);
+		}
+		inflater.reset();
 		return len;
 	}
 	
@@ -601,7 +614,7 @@
 				readFully(inputData.array(),0,inputData.capacity()); inputData.limit(dataLen-20);
 				LinkedList<ByteBuffer>inputs = new LinkedList<ByteBuffer>();
 				inputs.add(inputData);
-				unzip(inflater, inputs, bufs);
+				unzip(inflater, inputs, 0, bufs);
 				bufs.addFirst(header);
 				multicastqueue.put(bufs);
 				is.reset();
@@ -631,7 +644,7 @@
 		Runnable sender = new Runnable() {
 			public void run() {
 
-			    Deflater deflater = new Deflater();
+			    Deflater deflater = new Deflater(9,true);
 				try {
 					/**
 					 *  initial connection of RFB protocol
@@ -727,12 +740,12 @@
 			Deflater deflater = new Deflater();
 			zip(deflater,in,0,out);
 			// LinkedList<ByteBuffer> out3 = clone(out);   zipped result is depend on deflator's state
-			unzip(inflater, out, out2);
+			unzip(inflater, out, 0,out2);
 			equalByteBuffers(in1, out2);
 			LinkedList<ByteBuffer> out4 = new LinkedList<ByteBuffer>();
 			zip(deflater,out2,0,out4);
 			LinkedList<ByteBuffer> out5 = new LinkedList<ByteBuffer>();
-			unzip(inflater,out4,out5);
+			unzip(inflater,out4,0, out5);
 			equalByteBuffers(in1,out5);
 			
 			System.out.println("Test Ok.");