# HG changeset patch # User Shinji KONO # Date 1312335045 -32400 # Node ID 462bca4c8cecdf37f61cd0135c197767cbf51a11 # Parent 43822a70978c3d977decfa4ecaecc033cdd29cf9 ByteBuffer diff -r 43822a70978c -r 462bca4c8cec src/myVncProxy/MyRfbProto.java --- 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> multicastqueue = new MostRecentMultiCast>(10); + private MulticastQueue> multicastqueue = new MostRecentMultiCast>(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); - LinkedListbufs = new LinkedList(); + LinkedListbufs = new LinkedList(); 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 inputs, LinkedList outputs) throws IOException { - int clen = u32(inputs.poll(),0); - int len = 0, count = 0; - int len2=0; + public int zip(Deflater deflater,LinkedList inputs, LinkedList 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 inputs, LinkedList outputs) + public int unzip(Inflater inflater, LinkedList inputs, LinkedList 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 { - LinkedListbufs = new LinkedList(); - byte header[] = new byte[16]; - readFully(header,0,16); - if (header[0]==RfbProto.FramebufferUpdate) { - int encoding = u32(header,12); + LinkedListbufs = new LinkedList(); + 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); - LinkedListinputs = new LinkedList(); - 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); + LinkedListinputs = new LinkedList(); 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 > c = multicastqueue.newClient(); + final Client > c = multicastqueue.newClient(); Runnable sender = new Runnable() { public void run() { @@ -649,28 +643,27 @@ sendInitData(os); for (;;) { - LinkedList bufs = c.poll(); - byte[] header = bufs.poll(); - if (header[0]==RfbProto.FramebufferUpdate) { - int encoding = u32(header,12); + LinkedList 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 outs = new LinkedList(); + LinkedList outs = new LinkedList(); 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 in = new LinkedList(); - LinkedList out = new LinkedList(); - LinkedList out2 = new LinkedList(); + LinkedList in = new LinkedList(); + LinkedList out = new LinkedList(); + LinkedList out2 = new LinkedList(); 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.");