changeset 90:462bca4c8cec

ByteBuffer
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 03 Aug 2011 10:30:45 +0900
parents 43822a70978c
children 4116c19cd76e
files src/myVncProxy/MyRfbProto.java
diffstat 1 files changed, 62 insertions(+), 80 deletions(-) [+]
line wrap: on
line diff
--- a/src/myVncProxy/MyRfbProto.java	Wed Aug 03 09:15:25 2011 +0900
+++ b/src/myVncProxy/MyRfbProto.java	Wed Aug 03 10:30:45 2011 +0900
@@ -15,6 +15,7 @@
 import java.net.BindException;
 import java.net.ServerSocket;
 import java.net.Socket;
+import java.nio.ByteBuffer;
 import java.util.LinkedList;
 
 import javax.imageio.ImageIO;
@@ -36,7 +37,7 @@
 	/**
 	 * CheckMillis is one of new msgType for RFB 3.998. 
 	 */
-	final static int SpeedCheckMillis = 4;
+	final static byte SpeedCheckMillis = 4;
 	private static final int INFLATE_BUFSIZE = 1024*100;
 	boolean printStatusFlag = false;
 	long startCheckTime;
@@ -62,7 +63,7 @@
 
 	byte[] pngBytes;
 
-	private MulticastQueue<LinkedList<byte[]>> multicastqueue = new MostRecentMultiCast<LinkedList<byte[]>>(10);
+	private MulticastQueue<LinkedList<ByteBuffer>> multicastqueue = new MostRecentMultiCast<LinkedList<ByteBuffer>>(10);
 	private int clients = 0;
 	private Inflater inflater = new Inflater();
 
@@ -464,12 +465,12 @@
 	}
 	
 	void startSpeedCheck() {
-		byte[] b = new byte[2];
-		b[0] = (byte) SpeedCheckMillis;
-		b[1] = (byte) 0;
+		ByteBuffer b = ByteBuffer.allocate(10);
+		b.put((byte)SpeedCheckMillis);
+		b.flip();
 		startCheckTime = System.currentTimeMillis();
 		System.out.println("startChckTime = "+ startCheckTime);
-		LinkedList<byte[]>bufs = new LinkedList<byte[]>();
+		LinkedList<ByteBuffer>bufs = new LinkedList<ByteBuffer>();
 		bufs.add(b);
 		multicastqueue.put(bufs);
 	}
@@ -532,35 +533,28 @@
 	 * @return  byte length in last byte array
 	 * @throws IOException
 	 */
-	public int zip(Deflater deflater,LinkedList<byte[]> inputs, LinkedList<byte[]> outputs) throws IOException {
-		int clen = u32(inputs.poll(),0);
-		int len = 0, count = 0;
-		int len2=0;
+	public int zip(Deflater deflater,LinkedList<ByteBuffer> inputs, LinkedList<ByteBuffer> outputs) throws IOException {
+		int len1=0,len = 0;
 		deflater.reset();
 		do {
-			byte[] b1 = inputs.poll();
+			ByteBuffer b1 = inputs.poll();
+			deflater.setInput(b1.array(),b1.position(),b1.limit());
 			if (inputs.size()==0) {
-				deflater.setInput(b1,0,clen);
 				deflater.finish();
-			} else {
-				deflater.setInput(b1);
-				clen -= b1.length;
-			}
-			int len1=0; 
+			} 
 			do {
-				byte[] c1 = new byte[INFLATE_BUFSIZE];
-				len2 = len1;
-				len1 = deflater.deflate(c1);
+				ByteBuffer c1 = ByteBuffer.allocate(INFLATE_BUFSIZE);
+				len1 = deflater.deflate(c1.array(),c1.position(),c1.capacity());
+				c1.limit(len1);
 				if (len1>0) {
 					outputs.addLast(c1);
-					count ++;
 					len += len1;
 				}
 			} while (len1 > 0);
 		} while(inputs.size()>0);
-		byte[] blen = castIntByte(len);
+		ByteBuffer blen = ByteBuffer.wrap(castIntByte(len));
 		outputs.addFirst(blen);
-		return len2;
+		return len;
 	}
 	
 	/**
@@ -574,36 +568,36 @@
 	 *            byte data[]
 	 * @throws IOException
 	 */
-	public void unzip(Inflater inflater, LinkedList<byte[]> inputs, LinkedList<byte[]> outputs)
+	public int unzip(Inflater inflater, LinkedList<ByteBuffer> inputs, LinkedList<ByteBuffer> outputs)
 																	throws DataFormatException {
 		int len=0,len0;
-		inputs.poll();
-		for(byte [] input:inputs) {
-			inflater.setInput(input);
+		do {
+			ByteBuffer input = inputs.poll();
+			inflater.setInput(input.array(),0,input.limit());
 			do {
-				byte buf[] = new byte[INFLATE_BUFSIZE];
-				len0 = inflater.inflate(buf);
+				ByteBuffer buf = ByteBuffer.allocate(INFLATE_BUFSIZE);
+				len0 = inflater.inflate(buf.array(),0,buf.capacity());
+				buf.limit(len0);
 				len += len0;
 				outputs.addLast(buf);
 			} while (len0 ==INFLATE_BUFSIZE);
-		}
-		byte [] blen = castIntByte(len);
-		outputs.addFirst(blen);
+		} while (!inputs.isEmpty());
+		return len;
 	}
 	
 	void readSendData(int dataLen) throws IOException, DataFormatException {
-		LinkedList<byte[]>bufs = new LinkedList<byte[]>();
-		byte header[] = new byte[16];
-		readFully(header,0,16);
-		if (header[0]==RfbProto.FramebufferUpdate) {
-			int encoding = u32(header,12);
+		LinkedList<ByteBuffer>bufs = new LinkedList<ByteBuffer>();
+		ByteBuffer header = ByteBuffer.allocate(16);
+		readFully(header.array(),0,16); 
+		header.limit(16);
+		if (header.get(0)==RfbProto.FramebufferUpdate) {
+			int encoding = header.getInt(12);
 			if (encoding==RfbProto.EncodingZlib||encoding==RfbProto.EncodingZRLE) {
-				byte[] len = new byte[4];
-				readFully(len,0,4);
-				byte inputData[] = new byte[dataLen-20];
-				readFully(inputData);
-				LinkedList<byte[]>inputs = new LinkedList<byte[]>();
-				inputs.add(len);
+				ByteBuffer len = ByteBuffer.allocate(4);
+				readFully(len.array(),0,4); len.limit(4);
+				ByteBuffer inputData = ByteBuffer.allocate(dataLen-20);
+				readFully(inputData.array(),0,inputData.capacity()); inputData.limit(dataLen-20);
+				LinkedList<ByteBuffer>inputs = new LinkedList<ByteBuffer>();
 				inputs.add(inputData);
 				unzip(inflater, inputs, bufs);
 				bufs.addFirst(header);
@@ -614,8 +608,8 @@
 		} 
 		bufs.add(header);
 		if (dataLen>16) {
-			byte b[] = new byte[dataLen-16];
-			readFully(b);
+			ByteBuffer b = ByteBuffer.allocate(dataLen-16);
+			readFully(b.array(),0,dataLen-16); b.limit(dataLen-16);
 			bufs.add(b);
 		}
 		multicastqueue.put(bufs);
@@ -631,7 +625,7 @@
 		// createBimgFlag = true;
 		// rfb.addSockTmp(newCli);
 		//		addSock(newCli);
-		final Client <LinkedList<byte[]>> c = multicastqueue.newClient();
+		final Client <LinkedList<ByteBuffer>> c = multicastqueue.newClient();
 		Runnable sender = new Runnable() {
 			public void run() {
 
@@ -649,28 +643,27 @@
 					sendInitData(os);
 
 					for (;;) {
-						LinkedList<byte[]> bufs = c.poll();
-						byte[] header = bufs.poll();
-						if (header[0]==RfbProto.FramebufferUpdate) {
-							int encoding = u32(header,12);
+						LinkedList<ByteBuffer> bufs = c.poll();
+						ByteBuffer header = bufs.poll();
+						if (header.get(0)==RfbProto.FramebufferUpdate) {
+							int encoding = header.getInt(12);
 							if (encoding==RfbProto.EncodingZlib||encoding==RfbProto.EncodingZRLE) {
-								LinkedList<byte[]> outs = new LinkedList<byte[]>();
+								LinkedList<ByteBuffer> outs = new LinkedList<ByteBuffer>();
 								int len2 = zip(deflater, bufs, outs);
+								ByteBuffer blen = ByteBuffer.allocate(4); blen.putInt(len2); blen.flip();
+								outs.addFirst(blen);
 								outs.addFirst(header);
 								while(!outs.isEmpty()) {
-								   byte [] out=  outs.poll();
-									if (outs.isEmpty()) 
-										os.write(out,0,len2);
-									else
-										os.write(out);
+								   ByteBuffer out=  outs.poll();
+								   os.write(out.array(),out.position(),out.limit());
 								}
 							}
 							os.flush();
 							return;
 						}
-						os.write(header);
-						for(byte [] b : bufs) {
-							os.write(b, 0, b.length);
+						os.write(header.array(),header.position(),header.limit());
+						for(ByteBuffer b : bufs) {
+							os.write(b.array(), b.position(), b.limit());
 						}
 						os.flush();
 					}
@@ -684,37 +677,26 @@
 
 	}
 
-	private int u32(byte[] b, int i) {
-		int ret = 0;
-		for(int j = 0;j<4;j++) {
-			ret *= 256;
-			ret += b[i+j] & 0xff;
-		}
-		return ret;
-	}
 
 
 	@Test
 	public void test1() {
 		try {
-			LinkedList<byte[]> in = new LinkedList<byte[]>();
-			LinkedList<byte[]> out = new LinkedList<byte[]>();
-			LinkedList<byte[]> out2 = new LinkedList<byte[]>();
+			LinkedList<ByteBuffer> in = new LinkedList<ByteBuffer>();
+			LinkedList<ByteBuffer> out = new LinkedList<ByteBuffer>();
+			LinkedList<ByteBuffer> out2 = new LinkedList<ByteBuffer>();
 			for(int i=0;i<10;i++) {
-				in.add("test1".getBytes());
-				in.add("test1".getBytes());
-				in.add("test1".getBytes());
-				in.add("test1".getBytes());
+				in.add(ByteBuffer.wrap("test1".getBytes()));
+				in.add(ByteBuffer.wrap("test2".getBytes()));
+				in.add(ByteBuffer.wrap("test3".getBytes()));
+				in.add(ByteBuffer.wrap("test4".getBytes()));
 			}
-			int len = 0;
-			for(byte[] b: in) 	len += b.length;
-			in.addFirst(castIntByte(len));
-			
+
 			Deflater deflater = new Deflater();
 			zip(deflater, in,out);
 			unzip(inflater, out, out2);
-			for(byte[] b:out) {
-				byte []c = out2.poll();
+			for(ByteBuffer b:out) {
+				ByteBuffer c = out2.poll();
 				assertEquals(b,c);
 			}
 			System.out.println("Test Ok.");