OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / test / java / org / apache / harmony / archive / tests / java / util / zip / InflaterTest.java
1 /*
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17 package org.apache.harmony.archive.tests.java.util.zip;
18
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;
29
30 import tests.support.resource.Support_Resources;
31
32 public class InflaterTest extends junit.framework.TestCase {
33         byte outPutBuff1[] = new byte[500];
34
35         byte outPutDiction[] = new byte[500];
36
37         /**
38          * @tests java.util.zip.Inflater#end()
39          */
40         public void test_end() {
41                 // test method of java.util.zip.inflater.end()
42                 byte byteArray[] = { 5, 2, 3, 7, 8 };
43
44                 int r = 0;
45                 Inflater inflate = new Inflater();
46                 inflate.setInput(byteArray);
47                 inflate.end();
48                 try {
49                         inflate.reset();
50                         inflate.setInput(byteArray);
51                 } catch (NullPointerException e) {
52                         r = 1;
53                 }
54                 assertEquals("inflate can still be used after end is called", 1, r);
55
56                 Inflater i = new Inflater();
57                 i.end();
58                 // check for exception
59                 i.end();
60         }
61
62         /**
63          * @tests java.util.zip.Inflater#finished()
64          */
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];
70                 try {
71                         while (!(inflate.finished())) {
72                                 if (inflate.needsInput()) {
73                                         inflate.setInput(outPutBuff1);
74                                 }
75
76                                 inflate.inflate(outPutInf);
77                         }
78                         assertTrue(
79                                         "the method finished() returned false when no more data needs to be decompressed",
80                                         inflate.finished());
81                 } catch (DataFormatException e) {
82                         fail("Invalid input to be decompressed");
83                 }
84                 for (int i = 0; i < byteArray.length; i++) {
85                         assertTrue(
86                                         "Final decompressed data does not equal the original data",
87                                         byteArray[i] == outPutInf[i]);
88                 }
89                 assertEquals("final decompressed data contained more bytes than original - finished()",
90                                 0, outPutInf[byteArray.length]);
91         }
92
93         /**
94          * @tests java.util.zip.Inflater#getAdler()
95          */
96         public void test_getAdler() {
97                 // test method of java.util.zip.inflater.getAdler()
98                 byte dictionaryArray[] = { 'e', 'r', 't', 'a', 'b', 2, 3 };
99
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();
107                         assertTrue(
108                                         "the checksum value returned by getAdler() is not the same as the checksum returned by creating the adler32 instance",
109                                         checkSumR == inflateDiction.getAdler());
110                 }
111         }
112
113         /**
114          * @tests java.util.zip.Inflater#getRemaining()
115          */
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);
123                 assertTrue(
124                                 "getRemaining returned zero when there is input in the input buffer",
125                                 inflate.getRemaining() != 0);
126         }
127
128         /**
129          * @tests java.util.zip.Inflater#getTotalIn()
130          */
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];
137                 int x = 0;
138                 Deflater deflate = new Deflater(1);
139                 deflate.setInput(byteArray);
140                 while (!(deflate.needsInput())) {
141                         x += deflate.deflate(outPutBuf, x, outPutBuf.length - x);
142                 }
143                 deflate.finish();
144                 while (!(deflate.finished())) {
145                         x = x + deflate.deflate(outPutBuf, x, outPutBuf.length - x);
146                 }
147
148                 Inflater inflate = new Inflater();
149                 try {
150                         while (!(inflate.finished())) {
151                                 if (inflate.needsInput()) {
152                                         inflate.setInput(outPutBuf);
153                                 }
154
155                                 inflate.inflate(outPutInf);
156                         }
157                 } catch (DataFormatException e) {
158                         fail("Input to inflate is invalid or corrupted - getTotalIn");
159                 }
160                 // System.out.print(deflate.getTotalOut() + " " + inflate.getTotalIn());
161                 assertTrue(
162                                 "the total byte in outPutBuf did not equal the byte returned in getTotalIn",
163                                 inflate.getTotalIn() == deflate.getTotalOut());
164
165                 Inflater inflate2 = new Inflater();
166                 int offSet = 0;// seems only can start as 0
167                 int length = 4;
168                 try {
169                         // seems no while loops allowed
170                         if (inflate2.needsInput()) {
171                                 inflate2.setInput(outPutBuff1, offSet, length);
172                         }
173
174                         inflate2.inflate(outPutInf);
175
176                 } catch (DataFormatException e) {
177                         fail("Input to inflate is invalid or corrupted - getTotalIn");
178                 }
179                 // System.out.print(inflate2.getTotalIn() + " " + length);
180                 assertTrue(
181                                 "total byte dictated by length did not equal byte returned in getTotalIn",
182                                 inflate2.getTotalIn() == length);
183         }
184
185         /**
186          * @tests java.util.zip.Inflater#getTotalOut()
187          */
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 };
193                 int y = 0;
194                 int x = 0;
195                 Deflater deflate = new Deflater(1);
196                 deflate.setInput(byteArray);
197                 while (!(deflate.needsInput())) {
198                         x += deflate.deflate(outPutBuf, x, outPutBuf.length - x);
199                 }
200                 deflate.finish();
201                 while (!(deflate.finished())) {
202                         x = x + deflate.deflate(outPutBuf, x, outPutBuf.length - x);
203                 }
204
205                 Inflater inflate = new Inflater();
206                 byte outPutInf[] = new byte[500];
207                 try {
208                         while (!(inflate.finished())) {
209                                 if (inflate.needsInput()) {
210                                         inflate.setInput(outPutBuf);
211                                 }
212
213                                 y += inflate.inflate(outPutInf);
214                         }
215                 } catch (DataFormatException e) {
216                         fail("Input to inflate is invalid or corrupted - getTotalIn");
217                 }
218
219                 assertTrue(
220                                 "the sum of the bytes returned from inflate does not equal the bytes of getTotalOut()",
221                                 y == inflate.getTotalOut());
222                 assertTrue(
223                                 "the total number of bytes to be compressed does not equal the total bytes decompressed",
224                                 inflate.getTotalOut() == deflate.getTotalIn());
225
226                 // testing inflate(byte,int,int)
227                 inflate.reset();
228                 y = 0;
229                 int offSet = 0;// seems only can start as 0
230                 int length = 4;
231                 try {
232                         while (!(inflate.finished())) {
233                                 if (inflate.needsInput()) {
234                                         inflate.setInput(outPutBuf);
235                                 }
236
237                                 y += inflate.inflate(outPutInf, offSet, length);
238                         }
239                 } catch (DataFormatException e) {
240                         System.out
241                                         .println("Input to inflate is invalid or corrupted - getTotalIn");
242                 }
243                 assertTrue(
244                                 "the sum of the bytes returned from inflate does not equal the bytes of getTotalOut()",
245                                 y == inflate.getTotalOut());
246                 assertTrue(
247                                 "the total number of bytes to be compressed does not equal the total bytes decompressed",
248                                 inflate.getTotalOut() == deflate.getTotalIn());
249         }
250
251         /**
252          * @tests java.util.zip.Inflater#inflate(byte[])
253          */
254         public void test_inflate$B() {
255                 // test method of java.util.zip.inflater.inflate(byte)
256
257                 byte byteArray[] = { 1, 3, 4, 7, 8, 'e', 'r', 't', 'y', '5' };
258                 byte outPutInf[] = new byte[500];
259                 Inflater inflate = new Inflater();
260                 try {
261                         while (!(inflate.finished())) {
262                                 if (inflate.needsInput()) {
263                                         inflate.setInput(outPutBuff1);
264                                 }
265                                 inflate.inflate(outPutInf);
266                         }
267                 } catch (DataFormatException e) {
268                         fail("Invalid input to be decompressed");
269                 }
270                 for (int i = 0; i < byteArray.length; i++) {
271                         assertTrue(
272                                         "Final decompressed data does not equal the original data",
273                                         byteArray[i] == outPutInf[i]);
274                 }
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];
280                 int x = 0;
281                 Deflater defEmpty = new Deflater(3);
282                 defEmpty.setInput(emptyArray);
283                 while (!(defEmpty.needsInput())) {
284                         x += defEmpty.deflate(outPutBuf, x, outPutBuf.length - x);
285                 }
286                 defEmpty.finish();
287                 while (!(defEmpty.finished())) {
288                         x += defEmpty.deflate(outPutBuf, x, outPutBuf.length - x);
289                 }
290                 assertTrue(
291                                 "the total number of byte from deflate did not equal getTotalOut - inflate(byte)",
292                                 x == defEmpty.getTotalOut());
293                 assertTrue(
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();
297                 try {
298                         while (!(infEmpty.finished())) {
299                                 if (infEmpty.needsInput()) {
300                                         infEmpty.setInput(outPutBuf);
301                                 }
302                                 infEmpty.inflate(outPutInf);
303                         }
304                 } catch (DataFormatException e) {
305                         fail("Invalid input to be decompressed");
306                 }
307                 for (int i = 0; i < emptyArray.length; i++) {
308                         assertTrue(
309                                         "Final decompressed data does not equal the original data",
310                                         emptyArray[i] == outPutInf[i]);
311                         assertEquals("Final decompressed data does not equal zero",
312                                         0, outPutInf[i]);
313                 }
314                 assertEquals("Final decompressed data contains more element than original data",
315                                 0, outPutInf[emptyArray.length]);
316         }
317
318     public void test_inflate$B1() {
319         byte codedData[] = {
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";
324
325         Inflater infl1 = new Inflater();
326         Inflater infl2 = new Inflater();
327
328         byte[] result = new byte[100];
329         int decLen = 0;
330
331         infl1.setInput(codedData, 0, codedData.length);
332         try {
333             decLen = infl1.inflate(result);
334         } catch (DataFormatException e) {
335             fail("Unexpected DataFormatException");
336         }
337
338         infl1.end();
339         assertEquals(codedString, new String(result, 0, decLen));
340         codedData[5] = 0;
341
342         infl2.setInput(codedData, 0, codedData.length);
343         try {
344             decLen = infl2.inflate(result);
345             fail("Expected DataFormatException");
346         } catch (DataFormatException e) {
347             // expected
348         }
349
350         infl2.end();
351     }
352
353         /**
354          * @tests java.util.zip.Inflater#inflate(byte[], int, int)
355          */
356         public void test_inflate$BII() {
357                 // test method of java.util.zip.inflater.inflate(byte,int,int)
358
359                 byte byteArray[] = { 1, 3, 4, 7, 8, 'e', 'r', 't', 'y', '5' };
360                 byte outPutInf[] = new byte[100];
361                 int y = 0;
362                 Inflater inflate = new Inflater();
363                 try {
364                         while (!(inflate.finished())) {
365                                 if (inflate.needsInput()) {
366                                         inflate.setInput(outPutBuff1);
367                                 }
368                                 y += inflate.inflate(outPutInf, y, outPutInf.length - y);
369                         }
370                 } catch (DataFormatException e) {
371                         fail("Invalid input to be decompressed");
372                 }
373                 for (int i = 0; i < byteArray.length; i++) {
374                         assertTrue(
375                                         "Final decompressed data does not equal the original data",
376                                         byteArray[i] == outPutInf[i]);
377                 }
378                 assertEquals("final decompressed data contained more bytes than original - inflateB",
379                                 0, outPutInf[byteArray.length]);
380
381                 // test boundary checks
382                 inflate.reset();
383                 int r = 0;
384                 int offSet = 0;
385                 int lengthError = 101;
386                 try {
387                         if (inflate.needsInput()) {
388                                 inflate.setInput(outPutBuff1);
389                         }
390                         inflate.inflate(outPutInf, offSet, lengthError);
391
392                 } catch (DataFormatException e) {
393                         fail("Invalid input to be decompressed");
394                 } catch (ArrayIndexOutOfBoundsException e) {
395                         r = 1;
396                 }
397                 assertEquals("out of bounds error did not get caught", 1, r);
398         }
399
400     public void test_inflate$BII1() {
401         byte codedData[] = {
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";
406
407         Inflater infl1 = new Inflater();
408         Inflater infl2 = new Inflater();
409
410         byte[] result = new byte[100];
411         int decLen = 0;
412
413         infl1.setInput(codedData, 0, codedData.length);
414         try {
415             decLen = infl1.inflate(result, 10, 11);
416         } catch (DataFormatException e) {
417             fail("Unexpected DataFormatException");
418         }
419
420         infl1.end();
421         assertEquals(codedString, new String(result, 10, decLen));
422         codedData[5] = 0;
423
424         infl2.setInput(codedData, 0, codedData.length);
425         try {
426             decLen = infl2.inflate(result, 10, 11);
427             fail("Expected DataFormatException");
428         } catch (DataFormatException e) {
429             // expected
430         }
431
432         infl2.end();
433     }
434
435         /**
436          * @tests java.util.zip.Inflater#Inflater()
437          */
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",
442                                 inflate);
443         }
444
445         /**
446          * @tests java.util.zip.Inflater#Inflater(boolean)
447          */
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];
456                 int r = 0;
457                 try {
458                         while (!(inflate.finished())) {
459                                 if (inflate.needsInput()) {
460                                         inflate.setInput(outPutBuff1);
461                                 }
462
463                                 inflate.inflate(outPutInf);
464                         }
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",
467                                                 0, outPutBuff1[i]);
468                         }
469                 } catch (DataFormatException e) {
470                         r = 1;
471                 }
472                 assertEquals("Error: exception should be thrown because of header inconsistency",
473                                 1, r);
474
475         }
476
477         /**
478          * @tests java.util.zip.Inflater#needsDictionary()
479          */
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];
484
485                 // testing with dictionary set.
486                 Inflater inflateDiction = new Inflater();
487                 if (inflateDiction.needsInput()) {
488                         inflateDiction.setInput(outPutDiction);
489                 }
490                 try {
491                         assertEquals("should return 0 because needs dictionary",
492                                         0, inflateDiction.inflate(outPutInf));
493                 } catch (DataFormatException e) {
494                         fail("Should not cause exception");
495                 }
496                 assertTrue(
497                                 "method needsDictionary returned false when dictionary was used in deflater",
498                                 inflateDiction.needsDictionary());
499
500                 // testing without dictionary
501                 Inflater inflate = new Inflater();
502                 try {
503                         inflate.setInput(outPutBuff1);
504                         inflate.inflate(outPutInf);
505                         assertFalse(
506                                         "method needsDictionary returned true when dictionary was not used in deflater",
507                                         inflate.needsDictionary());
508                 } catch (DataFormatException e) {
509                         fail(
510                                         "Input to inflate is invalid or corrupted - needsDictionary");
511                 }
512
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());
520         }
521
522         /**
523          * @tests java.util.zip.Inflater#needsInput()
524          */
525         public void test_needsInput() {
526                 // test method of java.util.zip.inflater.needsInput()
527                 Inflater inflate = new Inflater();
528                 assertTrue(
529                                 "needsInput give the wrong boolean value as a result of no input buffer",
530                                 inflate.needsInput());
531
532                 byte byteArray[] = { 2, 3, 4, 't', 'y', 'u', 'e', 'w', 7, 6, 5, 9 };
533                 inflate.setInput(byteArray);
534                 assertFalse(
535                                 "methodNeedsInput returned true when the input buffer is full",
536                                 inflate.needsInput());
537
538                 inflate.reset();
539                 byte byteArrayEmpty[] = new byte[0];
540                 inflate.setInput(byteArrayEmpty);
541                 assertTrue(
542                                 "needsInput give wrong boolean value as a result of an empty input buffer",
543                                 inflate.needsInput());
544         }
545
546         /**
547          * @tests java.util.zip.Inflater#reset()
548          */
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];
553                 int y = 0;
554                 Inflater inflate = new Inflater();
555                 try {
556                         while (!(inflate.finished())) {
557                                 if (inflate.needsInput()) {
558                                         inflate.setInput(outPutBuff1);
559                                 }
560                                 y += inflate.inflate(outPutInf, y, outPutInf.length - y);
561                         }
562                 } catch (DataFormatException e) {
563                         fail("Invalid input to be decompressed");
564                 }
565                 for (int i = 0; i < byteArray.length; i++) {
566                         assertTrue(
567                                         "Final decompressed data does not equal the original data",
568                                         byteArray[i] == outPutInf[i]);
569                 }
570                 assertEquals("final decompressed data contained more bytes than original - reset",
571                                 0, outPutInf[byteArray.length]);
572
573                 // testing that resetting the inflater will also return the correct
574                 // decompressed data
575
576                 inflate.reset();
577                 try {
578                         while (!(inflate.finished())) {
579                                 if (inflate.needsInput()) {
580                                         inflate.setInput(outPutBuff1);
581                                 }
582                                 inflate.inflate(outPutInf);
583                         }
584                 } catch (DataFormatException e) {
585                         fail("Invalid input to be decompressed");
586                 }
587                 for (int i = 0; i < byteArray.length; i++) {
588                         assertTrue(
589                                         "Final decompressed data does not equal the original data",
590                                         byteArray[i] == outPutInf[i]);
591                 }
592                 assertEquals("final decompressed data contained more bytes than original - reset",
593                                 0, outPutInf[byteArray.length]);
594
595         }
596
597         /**
598          * @tests java.util.zip.Inflater#setDictionary(byte[])
599          */
600         public void test_setDictionary$B() {
601         //FIXME This test doesn't pass in Harmony classlib or Sun 5.0_7 RI
602         /*
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',
606                                 'w', 'r' };
607
608                 byte outPutInf[] = new byte[100];
609
610                 // trying to inflate without setting a dictionary
611
612                 Inflater inflateWO = new Inflater();
613                 byte outPutInf2[] = new byte[100];
614                 int r = 0;
615                 try {
616                         while (!(inflateWO.finished())) {
617                                 if (inflateWO.needsInput()) {
618                                         inflateWO.setInput(outPutDiction);
619                                 }
620                                 inflateWO.inflate(outPutInf2);
621                         }
622                 } catch (DataFormatException e) {
623                         r = 1;
624                 }
625                 assertEquals("invalid input to be decompressed due to dictionary not set",
626                                 1, r);
627                 // now setting the dictionary in inflater
628                 Inflater inflate = new Inflater();
629                 try {
630                         while (!(inflate.finished())) {
631                                 if (inflate.needsInput()) {
632                                         inflate.setInput(outPutDiction);
633                                 }
634                                 if (inflate.needsDictionary()) {
635                                         inflate.setDictionary(dictionaryArray);
636                                 }
637                                 inflate.inflate(outPutInf);
638                         }
639                 } catch (DataFormatException e) {
640                         fail("Invalid input to be decompressed");
641                 }
642                 for (int i = 0; i < byteArray.length; i++) {
643                         assertTrue(
644                                         "Final decompressed data does not equal the original data",
645                                         byteArray[i] == outPutInf[i]);
646                 }
647                 assertEquals("final decompressed data contained more bytes than original - deflateB",
648                                 0, outPutInf[byteArray.length]);
649                 */
650         }
651
652         /**
653          * @tests java.util.zip.Inflater#setInput(byte[])
654          */
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);
662         }
663
664         /**
665          * @tests java.util.zip.Inflater#setInput(byte[], int, int)
666          */
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 };
670                 int offSet = 6;
671                 int length = 6;
672                 Inflater inflate = new Inflater();
673                 inflate.setInput(byteArray, offSet, length);
674                 assertTrue(
675                                 "setInputBII did not deliver the right number of bytes to the input buffer",
676                                 inflate.getRemaining() == length);
677                 // boundary check
678                 inflate.reset();
679                 int r = 0;
680                 try {
681                         inflate.setInput(byteArray, 100, 100);
682                 } catch (ArrayIndexOutOfBoundsException e) {
683                         r = 1;
684                 }
685                 assertEquals("boundary check is not present for setInput", 1, r);
686         }
687
688         @Override
689     protected void setUp() {
690                 try {
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);
695                         inflatIP.close();
696
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);
701                         inflatIP2.close();
702
703                 } catch (FileNotFoundException e) {
704                         fail(
705                                         "input file to test InflaterInputStream constructor is not found");
706                 } catch (ZipException e) {
707                         fail(
708                                         "read() threw an zip exception while testing constructor");
709                 } catch (IOException e) {
710                         fail("read() threw an exception while testing constructor");
711                 }
712         }
713
714         @Override
715     protected void tearDown() {
716         }
717
718     /**
719      * @tests java.util.zip.Deflater#getBytesRead()
720      */
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");
732
733         // Compress the bytes
734         byte[] output = new byte[100];
735         def.setInput(input);
736         def.finish();
737         def.deflate(output);
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());
743     }
744
745     /**
746      * @tests java.util.zip.Deflater#getBytesRead()
747      */
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");
758
759         // Compress the bytes
760         byte[] output = new byte[100];
761         def.setInput(input);
762         def.finish();
763         def.deflate(output);
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());
769     }
770
771     /**
772      * @tests java.util.zip.Deflater#inflate(byte[], int, int)
773      */
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);
778
779         assertEquals(0, res);
780
781         // Regression for HARMONY-2508
782         Inflater inflater = new Inflater();
783         byte[] b = new byte[1024];
784         assertEquals(0, inflater.inflate(b));
785         inflater.end();
786
787         // Regression for HARMONY-2510
788         inflater = new Inflater();
789         inflater.setInput(new byte[] { -1 });
790         try {
791             inflater.inflate(b);
792
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) {
799             // expected
800         }
801
802         inflater = new Inflater();
803         inflater.setInput(new byte[] { -1, -1, -1 });
804         try {
805             inflater.inflate(b);
806         } catch (DataFormatException e) {
807             // expected
808         }
809     }
810
811     public void testSetDictionary$B() throws Exception {
812         int i = 0;
813         String inputString = "blah string contains blahblahblahblah and blah";
814         String dictionary1 = "blah";
815         String dictionary2 = "1234";
816
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);
823
824         defDict1.setDictionary(dictionary1.getBytes());
825         defDict2.setDictionary(dictionary2.getBytes());
826
827         defDictNo.setInput(inputString.getBytes());
828         defDict1.setInput(inputString.getBytes());
829         defDict2.setInput(inputString.getBytes());
830
831         defDictNo.finish();
832         defDict1.finish();
833         defDict2.finish();
834
835         int dataLenNo = defDictNo.deflate(outputNo);
836         int dataLen1 = defDict1.deflate(output1);
837         int dataLen2 = defDict2.deflate(output2);
838
839         boolean passNo1 = false;
840         boolean passNo2 = false;
841         boolean pass12 = false;
842
843         for (i = 0; i < (dataLenNo < dataLen1 ? dataLenNo : dataLen1); i++) {
844             if (outputNo[i] != output1[i]) {
845                 passNo1 = true;
846                 break;
847             }
848         }
849         for (i = 0; i < (dataLenNo < dataLen1 ? dataLenNo : dataLen2); i++) {
850             if (outputNo[i] != output2[i]) {
851                 passNo2 = true;
852                 break;
853             }
854         }
855         for (i = 0; i < (dataLen1 < dataLen2 ? dataLen1 : dataLen2); i++) {
856             if (output1[i] != output2[i]) {
857                 pass12 = true;
858                 break;
859             }
860         }
861
862         assertTrue(
863                 "Compressed data the same for stream with dictionary and without it.",
864                 passNo1);
865         assertTrue(
866                 "Compressed data the same for stream with dictionary and without it.",
867                 passNo2);
868         assertTrue(
869                 "Compressed data the same for stream with different dictionaries.",
870                 pass12);
871
872         Inflater inflNo = new Inflater();
873         Inflater infl1 = new Inflater();
874         Inflater infl2 = new Inflater();
875
876         byte[] result = new byte[100];
877         int decLen;
878
879         inflNo.setInput(outputNo, 0, dataLenNo);
880         decLen = inflNo.inflate(result);
881
882         assertFalse(inflNo.needsDictionary());
883         inflNo.end();
884         assertEquals(inputString, new String(result, 0, decLen));
885
886         infl1.setInput(output1, 0, dataLen1);
887         decLen = infl1.inflate(result);
888
889         assertTrue(infl1.needsDictionary());
890         infl1.setDictionary(dictionary1.getBytes());
891         decLen = infl1.inflate(result);
892         infl1.end();
893         assertEquals(inputString, new String(result, 0, decLen));
894
895         infl2.setInput(output2, 0, dataLen2);
896         decLen = infl2.inflate(result);
897
898         assertTrue(infl2.needsDictionary());
899         infl2.setDictionary(dictionary2.getBytes());
900         decLen = infl2.inflate(result);
901         infl2.end();
902         assertEquals(inputString, new String(result, 0, decLen));
903
904
905         inflNo = new Inflater();
906         infl1 = new Inflater();
907         inflNo.setInput(outputNo, 0, dataLenNo);
908         try {
909             infl1.setDictionary(dictionary1.getBytes());
910             fail("IllegalArgumentException expected.");
911         } catch (IllegalArgumentException ee) {
912             // expected.
913         }
914         inflNo.end();
915
916         infl1.setInput(output1, 0, dataLen1);
917         decLen = infl1.inflate(result);
918
919         assertTrue(infl1.needsDictionary());
920         try {
921             infl1.setDictionary(dictionary2.getBytes());
922             fail("IllegalArgumentException expected.");
923         } catch (IllegalArgumentException ee) {
924             // expected.
925         }
926         infl1.end();
927     }
928
929     public void testSetDictionary$BII() throws Exception {
930         int i = 0;
931         String inputString = "blah string contains blahblahblahblah and blah";
932         String dictionary1 = "blah";
933         String dictionary2 = "blahblahblah";
934
935         byte[] output1 = new byte[100];
936         byte[] output2 = new byte[100];
937         byte[] output3 = new byte[100];
938
939         Deflater defDict1 = new Deflater(9);
940         Deflater defDict2 = new Deflater(9);
941         Deflater defDict3 = new Deflater(9);
942
943         defDict1.setDictionary(dictionary1.getBytes());
944         defDict2.setDictionary(dictionary2.getBytes());
945         defDict3.setDictionary(dictionary2.getBytes(), 4, 4);
946
947         defDict1.setInput(inputString.getBytes());
948         defDict2.setInput(inputString.getBytes());
949         defDict3.setInput(inputString.getBytes());
950
951         defDict1.finish();
952         defDict2.finish();
953         defDict3.finish();
954
955         int dataLen1 = defDict1.deflate(output1);
956         int dataLen2 = defDict2.deflate(output2);
957         int dataLen3 = defDict3.deflate(output3);
958
959         boolean pass12 = false;
960         boolean pass23 = false;
961         boolean pass13 = true;
962
963         for (i = 0; i < (dataLen1 < dataLen2 ? dataLen1 : dataLen2); i++) {
964             if (output1[i] != output2[i]) {
965                 pass12 = true;
966                 break;
967             }
968         }
969         for (i = 0; i < (dataLen2 < dataLen3 ? dataLen2 : dataLen3); i++) {
970             if (output2[i] != output3[i]) {
971                 pass23 = true;
972                 break;
973             }
974         }
975         for (i = 0; i < (dataLen1 < dataLen3 ? dataLen1 : dataLen3); i++) {
976             if (output1[i] != output3[i]) {
977                 pass13 = false;
978                 break;
979             }
980         }
981
982         assertTrue(
983                 "Compressed data the same for stream with different dictionaries.",
984                 pass12);
985         assertTrue(
986                 "Compressed data the same for stream with different dictionaries.",
987                 pass23);
988         assertTrue(
989                 "Compressed data the differs for stream with the same dictionaries.",
990                 pass13);
991
992         Inflater infl1 = new Inflater();
993         Inflater infl2 = new Inflater();
994         Inflater infl3 = new Inflater();
995
996         byte[] result = new byte[100];
997         int decLen;
998
999         infl1.setInput(output1, 0, dataLen1);
1000         decLen = infl1.inflate(result);
1001
1002         assertTrue(infl1.needsDictionary());
1003         infl1.setDictionary(dictionary2.getBytes(), 4, 4);
1004         decLen = infl1.inflate(result);
1005         infl1.end();
1006         assertEquals(inputString, new String(result, 0, decLen));
1007
1008         infl2.setInput(output2, 0, dataLen2);
1009         decLen = infl2.inflate(result);
1010
1011         assertTrue(infl2.needsDictionary());
1012         try {
1013             infl2.setDictionary(dictionary1.getBytes());
1014             fail("IllegalArgumentException expected.");
1015         } catch (IllegalArgumentException ee) {
1016             // expected
1017         }
1018         infl2.end();
1019
1020         infl3.setInput(output3, 0, dataLen3);
1021         decLen = infl3.inflate(result);
1022
1023         assertTrue(infl3.needsDictionary());
1024         infl3.setDictionary(dictionary1.getBytes());
1025         decLen = infl3.inflate(result);
1026         infl3.end();
1027         assertEquals(inputString, new String(result, 0, decLen));
1028
1029     }
1030
1031 }