KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test_large_deflate_inflate


1 /* -*-mode:java; c-basic-offset:2; -*- */
2 import java.io.*;
3 import com.jcraft.jzlib.*;
4
5 // Test deflate() with large buffers and dynamic change of compression level
6
class test_large_deflate_inflate{
7
8   static final byte[] hello="hello, hello! ".getBytes();
9   static{
10     hello[hello.length-1]=0;
11   }
12
13   public static void main(String JavaDoc[] arg){
14     int err;
15     int comprLen=40000;
16     int uncomprLen=comprLen;
17     byte[] compr=new byte[comprLen];
18     byte[] uncompr=new byte[uncomprLen];
19
20     ZStream c_stream=new ZStream();
21
22     err=c_stream.deflateInit(JZlib.Z_BEST_SPEED);
23     CHECK_ERR(c_stream, err, "deflateInit");
24
25     c_stream.next_out=compr;
26     c_stream.next_out_index=0;
27     c_stream.avail_out=comprLen;
28
29     // At this point, uncompr is still mostly zeroes, so it should compress
30
// very well:
31
c_stream.next_in=uncompr;
32     c_stream.avail_in=uncomprLen;
33     err=c_stream.deflate(JZlib.Z_NO_FLUSH);
34     CHECK_ERR(c_stream, err, "deflate");
35     if(c_stream.avail_in!=0){
36       System.out.println("deflate not greedy");
37       System.exit(1);
38     }
39
40     // Feed in already compressed data and switch to no compression:
41
c_stream.deflateParams(JZlib.Z_NO_COMPRESSION, JZlib.Z_DEFAULT_STRATEGY);
42     c_stream.next_in=compr;
43     c_stream.next_in_index=0;
44     c_stream.avail_in=comprLen/2;
45     err=c_stream.deflate(JZlib.Z_NO_FLUSH);
46     CHECK_ERR(c_stream, err, "deflate");
47
48     // Switch back to compressing mode:
49
c_stream.deflateParams(JZlib.Z_BEST_COMPRESSION, JZlib.Z_FILTERED);
50     c_stream.next_in=uncompr;
51     c_stream.next_in_index=0;
52     c_stream.avail_in=uncomprLen;
53     err=c_stream.deflate(JZlib.Z_NO_FLUSH);
54     CHECK_ERR(c_stream, err, "deflate");
55
56     err=c_stream.deflate(JZlib.Z_FINISH);
57     if(err!=JZlib.Z_STREAM_END){
58       System.out.println("deflate should report Z_STREAM_END");
59       System.exit(1);
60     }
61     err=c_stream.deflateEnd();
62     CHECK_ERR(c_stream, err, "deflateEnd");
63
64     ZStream d_stream=new ZStream();
65
66     d_stream.next_in=compr;
67     d_stream.next_in_index=0;
68     d_stream.avail_in=comprLen;
69
70     err=d_stream.inflateInit();
71     CHECK_ERR(d_stream, err, "inflateInit");
72
73     while(true){
74       d_stream.next_out=uncompr;
75       d_stream.next_out_index=0;
76       d_stream.avail_out=uncomprLen;
77       err=d_stream.inflate(JZlib.Z_NO_FLUSH);
78       if(err==JZlib.Z_STREAM_END) break;
79       CHECK_ERR(d_stream, err, "inflate large");
80     }
81
82     err=d_stream.inflateEnd();
83     CHECK_ERR(d_stream, err, "inflateEnd");
84
85     if (d_stream.total_out!= 2*uncomprLen + comprLen/2) {
86        System.out.println("bad large inflate: "+d_stream.total_out);
87        System.exit(1);
88     }
89     else {
90       System.out.println("large_inflate(): OK");
91     }
92   }
93
94   static void CHECK_ERR(ZStream z, int err, String JavaDoc msg) {
95     if(err!=JZlib.Z_OK){
96       if(z.msg!=null) System.out.print(z.msg+" ");
97       System.out.println(msg+" error: "+err);
98
99       System.exit(1);
100     }
101   }
102 }
103
Popular Tags