2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 package org.apache.harmony.archive.tests.java.util.zip;
19 import java.io.BufferedInputStream;
20 import java.io.FileNotFoundException;
21 import java.io.IOException;
22 import java.util.Arrays;
23 import java.util.zip.Adler32;
24 import java.io.UnsupportedEncodingException;
25 import java.util.zip.DataFormatException;
26 import java.util.zip.Deflater;
27 import java.util.zip.Inflater;
28 import java.util.zip.ZipException;
30 import tests.support.resource.Support_Resources;
32 public class InflaterTest extends junit.framework.TestCase {
33 byte outPutBuff1[] = new byte[500];
35 byte outPutDiction[] = new byte[500];
38 * @tests java.util.zip.Inflater#end()
40 public void test_end() {
41 // test method of java.util.zip.inflater.end()
42 byte byteArray[] = { 5, 2, 3, 7, 8 };
45 Inflater inflate = new Inflater();
46 inflate.setInput(byteArray);
50 inflate.setInput(byteArray);
51 } catch (NullPointerException e) {
54 assertEquals("inflate can still be used after end is called", 1, r);
56 Inflater i = new Inflater();
58 // check for exception
63 * @tests java.util.zip.Inflater#finished()
65 public void test_finished() {
66 // test method of java.util.zip.inflater.finished()
67 byte byteArray[] = { 1, 3, 4, 7, 8, 'e', 'r', 't', 'y', '5' };
68 Inflater inflate = new Inflater(false);
69 byte outPutInf[] = new byte[500];
71 while (!(inflate.finished())) {
72 if (inflate.needsInput()) {
73 inflate.setInput(outPutBuff1);
76 inflate.inflate(outPutInf);
79 "the method finished() returned false when no more data needs to be decompressed",
81 } catch (DataFormatException e) {
82 fail("Invalid input to be decompressed");
84 for (int i = 0; i < byteArray.length; i++) {
86 "Final decompressed data does not equal the original data",
87 byteArray[i] == outPutInf[i]);
89 assertEquals("final decompressed data contained more bytes than original - finished()",
90 0, outPutInf[byteArray.length]);
94 * @tests java.util.zip.Inflater#getAdler()
96 public void test_getAdler() {
97 // test method of java.util.zip.inflater.getAdler()
98 byte dictionaryArray[] = { 'e', 'r', 't', 'a', 'b', 2, 3 };
100 Inflater inflateDiction = new Inflater();
101 inflateDiction.setInput(outPutDiction);
102 if (inflateDiction.needsDictionary() == true) {
103 // getting the checkSum value through the Adler32 class
104 Adler32 adl = new Adler32();
105 adl.update(dictionaryArray);
106 long checkSumR = adl.getValue();
108 "the checksum value returned by getAdler() is not the same as the checksum returned by creating the adler32 instance",
109 checkSumR == inflateDiction.getAdler());
114 * @tests java.util.zip.Inflater#getRemaining()
116 public void test_getRemaining() {
117 // test method of java.util.zip.inflater.getRemaining()
118 byte byteArray[] = { 1, 3, 5, 6, 7 };
119 Inflater inflate = new Inflater();
120 assertEquals("upon creating an instance of inflate, getRemaining returned a non zero value",
121 0, inflate.getRemaining());
122 inflate.setInput(byteArray);
124 "getRemaining returned zero when there is input in the input buffer",
125 inflate.getRemaining() != 0);
129 * @tests java.util.zip.Inflater#getTotalIn()
131 public void test_getTotalIn() {
132 // test method of java.util.zip.inflater.getTotalIn()
133 // creating the decompressed data
134 byte outPutBuf[] = new byte[500];
135 byte byteArray[] = { 1, 3, 4, 7, 8 };
136 byte outPutInf[] = new byte[500];
138 Deflater deflate = new Deflater(1);
139 deflate.setInput(byteArray);
140 while (!(deflate.needsInput())) {
141 x += deflate.deflate(outPutBuf, x, outPutBuf.length - x);
144 while (!(deflate.finished())) {
145 x = x + deflate.deflate(outPutBuf, x, outPutBuf.length - x);
148 Inflater inflate = new Inflater();
150 while (!(inflate.finished())) {
151 if (inflate.needsInput()) {
152 inflate.setInput(outPutBuf);
155 inflate.inflate(outPutInf);
157 } catch (DataFormatException e) {
158 fail("Input to inflate is invalid or corrupted - getTotalIn");
160 // System.out.print(deflate.getTotalOut() + " " + inflate.getTotalIn());
162 "the total byte in outPutBuf did not equal the byte returned in getTotalIn",
163 inflate.getTotalIn() == deflate.getTotalOut());
165 Inflater inflate2 = new Inflater();
166 int offSet = 0;// seems only can start as 0
169 // seems no while loops allowed
170 if (inflate2.needsInput()) {
171 inflate2.setInput(outPutBuff1, offSet, length);
174 inflate2.inflate(outPutInf);
176 } catch (DataFormatException e) {
177 fail("Input to inflate is invalid or corrupted - getTotalIn");
179 // System.out.print(inflate2.getTotalIn() + " " + length);
181 "total byte dictated by length did not equal byte returned in getTotalIn",
182 inflate2.getTotalIn() == length);
186 * @tests java.util.zip.Inflater#getTotalOut()
188 public void test_getTotalOut() {
189 // test method of java.util.zip.inflater.Inflater()
190 // creating the decompressed data
191 byte outPutBuf[] = new byte[500];
192 byte byteArray[] = { 1, 3, 4, 7, 8 };
195 Deflater deflate = new Deflater(1);
196 deflate.setInput(byteArray);
197 while (!(deflate.needsInput())) {
198 x += deflate.deflate(outPutBuf, x, outPutBuf.length - x);
201 while (!(deflate.finished())) {
202 x = x + deflate.deflate(outPutBuf, x, outPutBuf.length - x);
205 Inflater inflate = new Inflater();
206 byte outPutInf[] = new byte[500];
208 while (!(inflate.finished())) {
209 if (inflate.needsInput()) {
210 inflate.setInput(outPutBuf);
213 y += inflate.inflate(outPutInf);
215 } catch (DataFormatException e) {
216 fail("Input to inflate is invalid or corrupted - getTotalIn");
220 "the sum of the bytes returned from inflate does not equal the bytes of getTotalOut()",
221 y == inflate.getTotalOut());
223 "the total number of bytes to be compressed does not equal the total bytes decompressed",
224 inflate.getTotalOut() == deflate.getTotalIn());
226 // testing inflate(byte,int,int)
229 int offSet = 0;// seems only can start as 0
232 while (!(inflate.finished())) {
233 if (inflate.needsInput()) {
234 inflate.setInput(outPutBuf);
237 y += inflate.inflate(outPutInf, offSet, length);
239 } catch (DataFormatException e) {
241 .println("Input to inflate is invalid or corrupted - getTotalIn");
244 "the sum of the bytes returned from inflate does not equal the bytes of getTotalOut()",
245 y == inflate.getTotalOut());
247 "the total number of bytes to be compressed does not equal the total bytes decompressed",
248 inflate.getTotalOut() == deflate.getTotalIn());
252 * @tests java.util.zip.Inflater#inflate(byte[])
254 public void test_inflate$B() {
255 // test method of java.util.zip.inflater.inflate(byte)
257 byte byteArray[] = { 1, 3, 4, 7, 8, 'e', 'r', 't', 'y', '5' };
258 byte outPutInf[] = new byte[500];
259 Inflater inflate = new Inflater();
261 while (!(inflate.finished())) {
262 if (inflate.needsInput()) {
263 inflate.setInput(outPutBuff1);
265 inflate.inflate(outPutInf);
267 } catch (DataFormatException e) {
268 fail("Invalid input to be decompressed");
270 for (int i = 0; i < byteArray.length; i++) {
272 "Final decompressed data does not equal the original data",
273 byteArray[i] == outPutInf[i]);
275 assertEquals("final decompressed data contained more bytes than original - inflateB",
276 0, outPutInf[byteArray.length]);
277 // testing for an empty input array
278 byte outPutBuf[] = new byte[500];
279 byte emptyArray[] = new byte[11];
281 Deflater defEmpty = new Deflater(3);
282 defEmpty.setInput(emptyArray);
283 while (!(defEmpty.needsInput())) {
284 x += defEmpty.deflate(outPutBuf, x, outPutBuf.length - x);
287 while (!(defEmpty.finished())) {
288 x += defEmpty.deflate(outPutBuf, x, outPutBuf.length - x);
291 "the total number of byte from deflate did not equal getTotalOut - inflate(byte)",
292 x == defEmpty.getTotalOut());
294 "the number of input byte from the array did not correspond with getTotalIn - inflate(byte)",
295 defEmpty.getTotalIn() == emptyArray.length);
296 Inflater infEmpty = new Inflater();
298 while (!(infEmpty.finished())) {
299 if (infEmpty.needsInput()) {
300 infEmpty.setInput(outPutBuf);
302 infEmpty.inflate(outPutInf);
304 } catch (DataFormatException e) {
305 fail("Invalid input to be decompressed");
307 for (int i = 0; i < emptyArray.length; i++) {
309 "Final decompressed data does not equal the original data",
310 emptyArray[i] == outPutInf[i]);
311 assertEquals("Final decompressed data does not equal zero",
314 assertEquals("Final decompressed data contains more element than original data",
315 0, outPutInf[emptyArray.length]);
318 public void test_inflate$B1() {
320 120, -38, 75, -54, 73, -52, 80, 40, 46, 41, -54, -52, 75, 87,
321 72, -50, -49, 43, 73, -52, -52, 43, 86, 72, 2, 10, 34, 99,
322 -123, -60, -68, 20, -80, 32, 0, -101, -69, 17, 84};
323 String codedString = "blah string contains blahblahblahblah and blah";
325 Inflater infl1 = new Inflater();
326 Inflater infl2 = new Inflater();
328 byte[] result = new byte[100];
331 infl1.setInput(codedData, 0, codedData.length);
333 decLen = infl1.inflate(result);
334 } catch (DataFormatException e) {
335 fail("Unexpected DataFormatException");
339 assertEquals(codedString, new String(result, 0, decLen));
342 infl2.setInput(codedData, 0, codedData.length);
344 decLen = infl2.inflate(result);
345 fail("Expected DataFormatException");
346 } catch (DataFormatException e) {
354 * @tests java.util.zip.Inflater#inflate(byte[], int, int)
356 public void test_inflate$BII() {
357 // test method of java.util.zip.inflater.inflate(byte,int,int)
359 byte byteArray[] = { 1, 3, 4, 7, 8, 'e', 'r', 't', 'y', '5' };
360 byte outPutInf[] = new byte[100];
362 Inflater inflate = new Inflater();
364 while (!(inflate.finished())) {
365 if (inflate.needsInput()) {
366 inflate.setInput(outPutBuff1);
368 y += inflate.inflate(outPutInf, y, outPutInf.length - y);
370 } catch (DataFormatException e) {
371 fail("Invalid input to be decompressed");
373 for (int i = 0; i < byteArray.length; i++) {
375 "Final decompressed data does not equal the original data",
376 byteArray[i] == outPutInf[i]);
378 assertEquals("final decompressed data contained more bytes than original - inflateB",
379 0, outPutInf[byteArray.length]);
381 // test boundary checks
385 int lengthError = 101;
387 if (inflate.needsInput()) {
388 inflate.setInput(outPutBuff1);
390 inflate.inflate(outPutInf, offSet, lengthError);
392 } catch (DataFormatException e) {
393 fail("Invalid input to be decompressed");
394 } catch (ArrayIndexOutOfBoundsException e) {
397 assertEquals("out of bounds error did not get caught", 1, r);
400 public void test_inflate$BII1() {
402 120, -38, 75, -54, 73, -52, 80, 40, 46, 41, -54, -52, 75, 87,
403 72, -50, -49, 43, 73, -52, -52, 43, 86, 72, 2, 10, 34, 99,
404 -123, -60, -68, 20, -80, 32, 0, -101, -69, 17, 84};
405 String codedString = "blah string";
407 Inflater infl1 = new Inflater();
408 Inflater infl2 = new Inflater();
410 byte[] result = new byte[100];
413 infl1.setInput(codedData, 0, codedData.length);
415 decLen = infl1.inflate(result, 10, 11);
416 } catch (DataFormatException e) {
417 fail("Unexpected DataFormatException");
421 assertEquals(codedString, new String(result, 10, decLen));
424 infl2.setInput(codedData, 0, codedData.length);
426 decLen = infl2.inflate(result, 10, 11);
427 fail("Expected DataFormatException");
428 } catch (DataFormatException e) {
436 * @tests java.util.zip.Inflater#Inflater()
438 public void test_Constructor() {
439 // test method of java.util.zip.inflater.Inflater()
440 Inflater inflate = new Inflater();
441 assertNotNull("failed to create the instance of inflater",
446 * @tests java.util.zip.Inflater#Inflater(boolean)
448 public void test_ConstructorZ() {
449 // test method of java.util.zip.inflater.Inflater(boolean)
450 // note does not throw exception if deflater has a header, but inflater
451 // doesn't or vice versa.
452 byte byteArray[] = { 1, 3, 4, 7, 8, 'e', 'r', 't', 'y', '5' };
453 Inflater inflate = new Inflater(true);
454 assertNotNull("failed to create the instance of inflater", inflate);
455 byte outPutInf[] = new byte[500];
458 while (!(inflate.finished())) {
459 if (inflate.needsInput()) {
460 inflate.setInput(outPutBuff1);
463 inflate.inflate(outPutInf);
465 for (int i = 0; i < byteArray.length; i++) {
466 assertEquals("the output array from inflate should contain 0 because the header of inflate and deflate did not match, but this failed",
469 } catch (DataFormatException e) {
472 assertEquals("Error: exception should be thrown because of header inconsistency",
478 * @tests java.util.zip.Inflater#needsDictionary()
480 public void test_needsDictionary() {
481 // test method of java.util.zip.inflater.needsDictionary()
482 // note: this flag is set after inflate is called
483 byte outPutInf[] = new byte[500];
485 // testing with dictionary set.
486 Inflater inflateDiction = new Inflater();
487 if (inflateDiction.needsInput()) {
488 inflateDiction.setInput(outPutDiction);
491 assertEquals("should return 0 because needs dictionary",
492 0, inflateDiction.inflate(outPutInf));
493 } catch (DataFormatException e) {
494 fail("Should not cause exception");
497 "method needsDictionary returned false when dictionary was used in deflater",
498 inflateDiction.needsDictionary());
500 // testing without dictionary
501 Inflater inflate = new Inflater();
503 inflate.setInput(outPutBuff1);
504 inflate.inflate(outPutInf);
506 "method needsDictionary returned true when dictionary was not used in deflater",
507 inflate.needsDictionary());
508 } catch (DataFormatException e) {
510 "Input to inflate is invalid or corrupted - needsDictionary");
513 // Regression test for HARMONY-86
514 Inflater inf = new Inflater();
515 assertFalse(inf.needsDictionary());
516 assertEquals(0,inf.getTotalIn());
517 assertEquals(0,inf.getTotalOut());
518 assertEquals(0,inf.getBytesRead());
519 assertEquals(0,inf.getBytesWritten());
523 * @tests java.util.zip.Inflater#needsInput()
525 public void test_needsInput() {
526 // test method of java.util.zip.inflater.needsInput()
527 Inflater inflate = new Inflater();
529 "needsInput give the wrong boolean value as a result of no input buffer",
530 inflate.needsInput());
532 byte byteArray[] = { 2, 3, 4, 't', 'y', 'u', 'e', 'w', 7, 6, 5, 9 };
533 inflate.setInput(byteArray);
535 "methodNeedsInput returned true when the input buffer is full",
536 inflate.needsInput());
539 byte byteArrayEmpty[] = new byte[0];
540 inflate.setInput(byteArrayEmpty);
542 "needsInput give wrong boolean value as a result of an empty input buffer",
543 inflate.needsInput());
547 * @tests java.util.zip.Inflater#reset()
549 public void test_reset() {
550 // test method of java.util.zip.inflater.reset()
551 byte byteArray[] = { 1, 3, 4, 7, 8, 'e', 'r', 't', 'y', '5' };
552 byte outPutInf[] = new byte[100];
554 Inflater inflate = new Inflater();
556 while (!(inflate.finished())) {
557 if (inflate.needsInput()) {
558 inflate.setInput(outPutBuff1);
560 y += inflate.inflate(outPutInf, y, outPutInf.length - y);
562 } catch (DataFormatException e) {
563 fail("Invalid input to be decompressed");
565 for (int i = 0; i < byteArray.length; i++) {
567 "Final decompressed data does not equal the original data",
568 byteArray[i] == outPutInf[i]);
570 assertEquals("final decompressed data contained more bytes than original - reset",
571 0, outPutInf[byteArray.length]);
573 // testing that resetting the inflater will also return the correct
578 while (!(inflate.finished())) {
579 if (inflate.needsInput()) {
580 inflate.setInput(outPutBuff1);
582 inflate.inflate(outPutInf);
584 } catch (DataFormatException e) {
585 fail("Invalid input to be decompressed");
587 for (int i = 0; i < byteArray.length; i++) {
589 "Final decompressed data does not equal the original data",
590 byteArray[i] == outPutInf[i]);
592 assertEquals("final decompressed data contained more bytes than original - reset",
593 0, outPutInf[byteArray.length]);
598 * @tests java.util.zip.Inflater#setDictionary(byte[])
600 public void test_setDictionary$B() {
601 //FIXME This test doesn't pass in Harmony classlib or Sun 5.0_7 RI
603 // test method of java.util.zip.inflater.setDictionary(byte)
604 byte dictionaryArray[] = { 'e', 'r', 't', 'a', 'b', 2, 3 };
605 byte byteArray[] = { 4, 5, 3, 2, 'a', 'b', 6, 7, 8, 9, 0, 's', '3',
608 byte outPutInf[] = new byte[100];
610 // trying to inflate without setting a dictionary
612 Inflater inflateWO = new Inflater();
613 byte outPutInf2[] = new byte[100];
616 while (!(inflateWO.finished())) {
617 if (inflateWO.needsInput()) {
618 inflateWO.setInput(outPutDiction);
620 inflateWO.inflate(outPutInf2);
622 } catch (DataFormatException e) {
625 assertEquals("invalid input to be decompressed due to dictionary not set",
627 // now setting the dictionary in inflater
628 Inflater inflate = new Inflater();
630 while (!(inflate.finished())) {
631 if (inflate.needsInput()) {
632 inflate.setInput(outPutDiction);
634 if (inflate.needsDictionary()) {
635 inflate.setDictionary(dictionaryArray);
637 inflate.inflate(outPutInf);
639 } catch (DataFormatException e) {
640 fail("Invalid input to be decompressed");
642 for (int i = 0; i < byteArray.length; i++) {
644 "Final decompressed data does not equal the original data",
645 byteArray[i] == outPutInf[i]);
647 assertEquals("final decompressed data contained more bytes than original - deflateB",
648 0, outPutInf[byteArray.length]);
653 * @tests java.util.zip.Inflater#setInput(byte[])
655 public void test_setInput$B() {
656 // test method of java.util.zip.inflater.setInput(byte)
657 byte byteArray[] = { 2, 3, 4, 't', 'y', 'u', 'e', 'w', 7, 6, 5, 9 };
658 Inflater inflate = new Inflater();
659 inflate.setInput(byteArray);
660 assertTrue("setInputB did not deliver any byte to the input buffer",
661 inflate.getRemaining() != 0);
665 * @tests java.util.zip.Inflater#setInput(byte[], int, int)
667 public void test_setInput$BII() {
668 // test method of java.util.zip.inflater.setInput(byte,int,int)
669 byte byteArray[] = { 2, 3, 4, 't', 'y', 'u', 'e', 'w', 7, 6, 5, 9 };
672 Inflater inflate = new Inflater();
673 inflate.setInput(byteArray, offSet, length);
675 "setInputBII did not deliver the right number of bytes to the input buffer",
676 inflate.getRemaining() == length);
681 inflate.setInput(byteArray, 100, 100);
682 } catch (ArrayIndexOutOfBoundsException e) {
685 assertEquals("boundary check is not present for setInput", 1, r);
689 protected void setUp() {
691 java.io.InputStream infile = Support_Resources
692 .getStream("hyts_compressD.txt");
693 BufferedInputStream inflatIP = new BufferedInputStream(infile);
694 inflatIP.read(outPutBuff1, 0, outPutBuff1.length);
697 java.io.InputStream infile2 = Support_Resources
698 .getStream("hyts_compDiction.txt");
699 BufferedInputStream inflatIP2 = new BufferedInputStream(infile2);
700 inflatIP2.read(outPutDiction, 0, outPutDiction.length);
703 } catch (FileNotFoundException e) {
705 "input file to test InflaterInputStream constructor is not found");
706 } catch (ZipException e) {
708 "read() threw an zip exception while testing constructor");
709 } catch (IOException e) {
710 fail("read() threw an exception while testing constructor");
715 protected void tearDown() {
719 * @tests java.util.zip.Deflater#getBytesRead()
721 public void test_getBytesRead() throws DataFormatException,
722 UnsupportedEncodingException {
723 // Regression test for HARMONY-158
724 Deflater def = new Deflater();
725 Inflater inf = new Inflater();
726 assertEquals(0, def.getTotalIn());
727 assertEquals(0, def.getTotalOut());
728 assertEquals(0, def.getBytesRead());
729 // Encode a String into bytes
730 String inputString = "blahblahblah??";
731 byte[] input = inputString.getBytes("UTF-8");
733 // Compress the bytes
734 byte[] output = new byte[100];
738 inf.setInput(output);
739 int compressedDataLength =inf.inflate(input);
740 assertEquals(16, inf.getTotalIn());
741 assertEquals(compressedDataLength, inf.getTotalOut());
742 assertEquals(16, inf.getBytesRead());
746 * @tests java.util.zip.Deflater#getBytesRead()
748 public void test_getBytesWritten() throws DataFormatException, UnsupportedEncodingException {
749 // Regression test for HARMONY-158
750 Deflater def = new Deflater();
751 Inflater inf = new Inflater();
752 assertEquals(0, def.getTotalIn());
753 assertEquals(0, def.getTotalOut());
754 assertEquals(0, def.getBytesWritten());
755 // Encode a String into bytes
756 String inputString = "blahblahblah??";
757 byte[] input = inputString.getBytes("UTF-8");
759 // Compress the bytes
760 byte[] output = new byte[100];
764 inf.setInput(output);
765 int compressedDataLength =inf.inflate(input);
766 assertEquals(16, inf.getTotalIn());
767 assertEquals(compressedDataLength, inf.getTotalOut());
768 assertEquals(14, inf.getBytesWritten());
772 * @tests java.util.zip.Deflater#inflate(byte[], int, int)
774 public void testInflate() throws Exception {
775 // Regression for HARMONY-81
776 Inflater inf = new Inflater();
777 int res = inf.inflate(new byte[0], 0, 0);
779 assertEquals(0, res);
781 // Regression for HARMONY-2508
782 Inflater inflater = new Inflater();
783 byte[] b = new byte[1024];
784 assertEquals(0, inflater.inflate(b));
787 // Regression for HARMONY-2510
788 inflater = new Inflater();
789 inflater.setInput(new byte[] { -1 });
793 // The RI detects malformed data on the malformed input { -1 }. Both
794 // this implementation and the native zlib API return "need input"
795 // on that data. This is an error if the stream is exhausted, but
796 // not one that results in an exception in the Inflater API.
797 assertTrue(inflater.needsInput());
798 } catch (DataFormatException e) {
802 inflater = new Inflater();
803 inflater.setInput(new byte[] { -1, -1, -1 });
806 } catch (DataFormatException e) {
811 public void testSetDictionary$B() throws Exception {
813 String inputString = "blah string contains blahblahblahblah and blah";
814 String dictionary1 = "blah";
815 String dictionary2 = "1234";
817 byte[] outputNo = new byte[100];
818 byte[] output1 = new byte[100];
819 byte[] output2 = new byte[100];
820 Deflater defDictNo = new Deflater(9);
821 Deflater defDict1 = new Deflater(9);
822 Deflater defDict2 = new Deflater(9);
824 defDict1.setDictionary(dictionary1.getBytes());
825 defDict2.setDictionary(dictionary2.getBytes());
827 defDictNo.setInput(inputString.getBytes());
828 defDict1.setInput(inputString.getBytes());
829 defDict2.setInput(inputString.getBytes());
835 int dataLenNo = defDictNo.deflate(outputNo);
836 int dataLen1 = defDict1.deflate(output1);
837 int dataLen2 = defDict2.deflate(output2);
839 boolean passNo1 = false;
840 boolean passNo2 = false;
841 boolean pass12 = false;
843 for (i = 0; i < (dataLenNo < dataLen1 ? dataLenNo : dataLen1); i++) {
844 if (outputNo[i] != output1[i]) {
849 for (i = 0; i < (dataLenNo < dataLen1 ? dataLenNo : dataLen2); i++) {
850 if (outputNo[i] != output2[i]) {
855 for (i = 0; i < (dataLen1 < dataLen2 ? dataLen1 : dataLen2); i++) {
856 if (output1[i] != output2[i]) {
863 "Compressed data the same for stream with dictionary and without it.",
866 "Compressed data the same for stream with dictionary and without it.",
869 "Compressed data the same for stream with different dictionaries.",
872 Inflater inflNo = new Inflater();
873 Inflater infl1 = new Inflater();
874 Inflater infl2 = new Inflater();
876 byte[] result = new byte[100];
879 inflNo.setInput(outputNo, 0, dataLenNo);
880 decLen = inflNo.inflate(result);
882 assertFalse(inflNo.needsDictionary());
884 assertEquals(inputString, new String(result, 0, decLen));
886 infl1.setInput(output1, 0, dataLen1);
887 decLen = infl1.inflate(result);
889 assertTrue(infl1.needsDictionary());
890 infl1.setDictionary(dictionary1.getBytes());
891 decLen = infl1.inflate(result);
893 assertEquals(inputString, new String(result, 0, decLen));
895 infl2.setInput(output2, 0, dataLen2);
896 decLen = infl2.inflate(result);
898 assertTrue(infl2.needsDictionary());
899 infl2.setDictionary(dictionary2.getBytes());
900 decLen = infl2.inflate(result);
902 assertEquals(inputString, new String(result, 0, decLen));
905 inflNo = new Inflater();
906 infl1 = new Inflater();
907 inflNo.setInput(outputNo, 0, dataLenNo);
909 infl1.setDictionary(dictionary1.getBytes());
910 fail("IllegalArgumentException expected.");
911 } catch (IllegalArgumentException ee) {
916 infl1.setInput(output1, 0, dataLen1);
917 decLen = infl1.inflate(result);
919 assertTrue(infl1.needsDictionary());
921 infl1.setDictionary(dictionary2.getBytes());
922 fail("IllegalArgumentException expected.");
923 } catch (IllegalArgumentException ee) {
929 public void testSetDictionary$BII() throws Exception {
931 String inputString = "blah string contains blahblahblahblah and blah";
932 String dictionary1 = "blah";
933 String dictionary2 = "blahblahblah";
935 byte[] output1 = new byte[100];
936 byte[] output2 = new byte[100];
937 byte[] output3 = new byte[100];
939 Deflater defDict1 = new Deflater(9);
940 Deflater defDict2 = new Deflater(9);
941 Deflater defDict3 = new Deflater(9);
943 defDict1.setDictionary(dictionary1.getBytes());
944 defDict2.setDictionary(dictionary2.getBytes());
945 defDict3.setDictionary(dictionary2.getBytes(), 4, 4);
947 defDict1.setInput(inputString.getBytes());
948 defDict2.setInput(inputString.getBytes());
949 defDict3.setInput(inputString.getBytes());
955 int dataLen1 = defDict1.deflate(output1);
956 int dataLen2 = defDict2.deflate(output2);
957 int dataLen3 = defDict3.deflate(output3);
959 boolean pass12 = false;
960 boolean pass23 = false;
961 boolean pass13 = true;
963 for (i = 0; i < (dataLen1 < dataLen2 ? dataLen1 : dataLen2); i++) {
964 if (output1[i] != output2[i]) {
969 for (i = 0; i < (dataLen2 < dataLen3 ? dataLen2 : dataLen3); i++) {
970 if (output2[i] != output3[i]) {
975 for (i = 0; i < (dataLen1 < dataLen3 ? dataLen1 : dataLen3); i++) {
976 if (output1[i] != output3[i]) {
983 "Compressed data the same for stream with different dictionaries.",
986 "Compressed data the same for stream with different dictionaries.",
989 "Compressed data the differs for stream with the same dictionaries.",
992 Inflater infl1 = new Inflater();
993 Inflater infl2 = new Inflater();
994 Inflater infl3 = new Inflater();
996 byte[] result = new byte[100];
999 infl1.setInput(output1, 0, dataLen1);
1000 decLen = infl1.inflate(result);
1002 assertTrue(infl1.needsDictionary());
1003 infl1.setDictionary(dictionary2.getBytes(), 4, 4);
1004 decLen = infl1.inflate(result);
1006 assertEquals(inputString, new String(result, 0, decLen));
1008 infl2.setInput(output2, 0, dataLen2);
1009 decLen = infl2.inflate(result);
1011 assertTrue(infl2.needsDictionary());
1013 infl2.setDictionary(dictionary1.getBytes());
1014 fail("IllegalArgumentException expected.");
1015 } catch (IllegalArgumentException ee) {
1020 infl3.setInput(output3, 0, dataLen3);
1021 decLen = infl3.inflate(result);
1023 assertTrue(infl3.needsDictionary());
1024 infl3.setDictionary(dictionary1.getBytes());
1025 decLen = infl3.inflate(result);
1027 assertEquals(inputString, new String(result, 0, decLen));