comparison src/myVncProxy/MyRfbProto.java @ 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
comparison
equal deleted inserted replaced
97:145506601e0d 98:3db7ac2b10f7
534 * @return byte length in last byte array 534 * @return byte length in last byte array
535 * @throws IOException 535 * @throws IOException
536 */ 536 */
537 public int zip(Deflater deflater,LinkedList<ByteBuffer> inputs, int inputIndex, LinkedList<ByteBuffer> outputs) throws IOException { 537 public int zip(Deflater deflater,LinkedList<ByteBuffer> inputs, int inputIndex, LinkedList<ByteBuffer> outputs) throws IOException {
538 int len1=0,len = 0; 538 int len1=0,len = 0;
539 deflater.reset(); 539 ByteBuffer c1= ByteBuffer.allocate(INFLATE_BUFSIZE);
540 while(inputIndex < inputs.size()) { 540 while(inputIndex < inputs.size() ) {
541 ByteBuffer b1 = inputs.get(inputIndex++); 541 ByteBuffer b1 = inputs.get(inputIndex++);
542 deflater.setInput(b1.array(),b1.position(),b1.limit()); 542 deflater.setInput(b1.array(),b1.position(),b1.remaining());
543 if (inputIndex==inputs.size()) { 543 if (inputIndex==inputs.size()) {
544 deflater.finish(); 544 deflater.finish();
545 } 545 }
546 do { 546 do {
547 ByteBuffer c1 = ByteBuffer.allocate(INFLATE_BUFSIZE); 547 len1 = deflater.deflate(c1.array(),c1.position(),c1.remaining());
548 len1 = deflater.deflate(c1.array(),c1.position(),c1.capacity()); 548 if (len1<=0) break; // get next buffer
549 c1.limit(len1); 549 len += len1;
550 if (len1>0) { 550 c1.position(c1.position()+len1);
551 if (c1.remaining()==0) {
552 c1.flip();
551 outputs.addLast(c1); 553 outputs.addLast(c1);
552 len += len1; 554 c1 = ByteBuffer.allocate(INFLATE_BUFSIZE);
553 } 555 }
554 } while (len1==INFLATE_BUFSIZE); 556 } while (!deflater.finished());
555 } 557 }
558 if (c1.position()!=0) {
559 c1.flip();
560 outputs.addLast(c1);
561 }
562 deflater.reset();
556 return len; 563 return len;
557 } 564 }
558 565
559 /** 566 /**
560 * gunzip byte arrays 567 * gunzip byte arrays
564 * @param outputs 571 * @param outputs
565 * byte data[] 572 * byte data[]
566 *@return number of total bytes 573 *@return number of total bytes
567 * @throws IOException 574 * @throws IOException
568 */ 575 */
569 public int unzip(Inflater inflater, LinkedList<ByteBuffer> inputs, LinkedList<ByteBuffer> outputs) 576 public int unzip(Inflater inflater, LinkedList<ByteBuffer> inputs, int inputIndex, LinkedList<ByteBuffer> outputs)
570 throws DataFormatException { 577 throws DataFormatException {
571 int len=0,len0; 578 int len=0,len0;
572 inflater.reset(); 579 ByteBuffer buf = ByteBuffer.allocate(INFLATE_BUFSIZE);
573 int inputIndex = 0; 580 // inflater.reset(); // if we uncomment this, test1() will be passed. But it won't work with real connection.
574 do { 581 while (inputIndex < inputs.size()) {
575 ByteBuffer input = inputs.get(inputIndex++); 582 ByteBuffer input = inputs.get(inputIndex++);
576 inflater.setInput(input.array(),0,input.limit()); 583 inflater.setInput(input.array(),0,input.limit());
577 do { 584 do {
578 ByteBuffer buf = ByteBuffer.allocate(INFLATE_BUFSIZE); 585 len0 = inflater.inflate(buf.array(),buf.position(),buf.remaining());
579 len0 = inflater.inflate(buf.array(),0,buf.capacity()); 586 buf.position(buf.position()+len0);
580 buf.limit(len0);
581 len += len0; 587 len += len0;
582 if (len0>0) { 588 if (buf.remaining()==0) {
589 buf.flip();
583 outputs.addLast(buf); 590 outputs.addLast(buf);
591 buf = ByteBuffer.allocate(INFLATE_BUFSIZE);
584 } 592 }
585 } while (len0>0); 593 } while (!inflater.finished());
586 } while (inputIndex < inputs.size()) ; 594 }
595 if (buf.position()!=0) {
596 buf.flip();
597 outputs.addLast(buf);
598 }
599 inflater.reset();
587 return len; 600 return len;
588 } 601 }
589 602
590 void readSendData(int dataLen) throws IOException, DataFormatException { 603 void readSendData(int dataLen) throws IOException, DataFormatException {
591 LinkedList<ByteBuffer>bufs = new LinkedList<ByteBuffer>(); 604 LinkedList<ByteBuffer>bufs = new LinkedList<ByteBuffer>();
599 readFully(len.array(),0,4); len.limit(4); 612 readFully(len.array(),0,4); len.limit(4);
600 ByteBuffer inputData = ByteBuffer.allocate(dataLen-20); 613 ByteBuffer inputData = ByteBuffer.allocate(dataLen-20);
601 readFully(inputData.array(),0,inputData.capacity()); inputData.limit(dataLen-20); 614 readFully(inputData.array(),0,inputData.capacity()); inputData.limit(dataLen-20);
602 LinkedList<ByteBuffer>inputs = new LinkedList<ByteBuffer>(); 615 LinkedList<ByteBuffer>inputs = new LinkedList<ByteBuffer>();
603 inputs.add(inputData); 616 inputs.add(inputData);
604 unzip(inflater, inputs, bufs); 617 unzip(inflater, inputs, 0, bufs);
605 bufs.addFirst(header); 618 bufs.addFirst(header);
606 multicastqueue.put(bufs); 619 multicastqueue.put(bufs);
607 is.reset(); 620 is.reset();
608 return ; 621 return ;
609 } 622 }
629 // addSock(newCli); 642 // addSock(newCli);
630 final Client <LinkedList<ByteBuffer>> c = multicastqueue.newClient(); 643 final Client <LinkedList<ByteBuffer>> c = multicastqueue.newClient();
631 Runnable sender = new Runnable() { 644 Runnable sender = new Runnable() {
632 public void run() { 645 public void run() {
633 646
634 Deflater deflater = new Deflater(); 647 Deflater deflater = new Deflater(9,true);
635 try { 648 try {
636 /** 649 /**
637 * initial connection of RFB protocol 650 * initial connection of RFB protocol
638 */ 651 */
639 sendRfbVersion(os); 652 sendRfbVersion(os);
725 LinkedList<ByteBuffer> in1 = clone(in); 738 LinkedList<ByteBuffer> in1 = clone(in);
726 739
727 Deflater deflater = new Deflater(); 740 Deflater deflater = new Deflater();
728 zip(deflater,in,0,out); 741 zip(deflater,in,0,out);
729 // LinkedList<ByteBuffer> out3 = clone(out); zipped result is depend on deflator's state 742 // LinkedList<ByteBuffer> out3 = clone(out); zipped result is depend on deflator's state
730 unzip(inflater, out, out2); 743 unzip(inflater, out, 0,out2);
731 equalByteBuffers(in1, out2); 744 equalByteBuffers(in1, out2);
732 LinkedList<ByteBuffer> out4 = new LinkedList<ByteBuffer>(); 745 LinkedList<ByteBuffer> out4 = new LinkedList<ByteBuffer>();
733 zip(deflater,out2,0,out4); 746 zip(deflater,out2,0,out4);
734 LinkedList<ByteBuffer> out5 = new LinkedList<ByteBuffer>(); 747 LinkedList<ByteBuffer> out5 = new LinkedList<ByteBuffer>();
735 unzip(inflater,out4,out5); 748 unzip(inflater,out4,0, out5);
736 equalByteBuffers(in1,out5); 749 equalByteBuffers(in1,out5);
737 750
738 System.out.println("Test Ok."); 751 System.out.println("Test Ok.");
739 } catch (Exception e) { 752 } catch (Exception e) {
740 assertEquals(0,1); 753 assertEquals(0,1);