KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test_flush_sync


1 /* -*-mode:java; c-basic-offset:2; -*- */
2 import java.io.*;
3 import com.jcraft.jzlib.*;
4
5 // Test deflate() with full flush
6
class test_flush_sync{
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     int len = hello.length;
20
21     ZStream c_stream=new ZStream();
22
23     err=c_stream.deflateInit(JZlib.Z_DEFAULT_COMPRESSION);
24     CHECK_ERR(c_stream, err, "deflate");
25
26     c_stream.next_in =hello;
27     c_stream.next_in_index =0;
28     c_stream.next_out = compr;
29     c_stream.next_out_index = 0;
30     c_stream.avail_in = 3;
31     c_stream.avail_out = comprLen;
32
33     err = c_stream.deflate(JZlib.Z_FULL_FLUSH);
34     CHECK_ERR(c_stream, err, "deflate");
35
36     compr[3]++; // force an error in first compressed block
37
c_stream.avail_in=len-3;
38
39     err = c_stream.deflate(JZlib.Z_FINISH);
40     if(err!=JZlib.Z_STREAM_END){
41       CHECK_ERR(c_stream, err, "deflate");
42     }
43     err = c_stream.deflateEnd();
44     CHECK_ERR(c_stream, err, "deflateEnd");
45     comprLen=(int)(c_stream.total_out);
46
47     ZStream d_stream=new ZStream();
48
49     d_stream.next_in=compr;
50     d_stream.next_in_index=0;
51     d_stream.avail_in=2;
52
53     err=d_stream.inflateInit();
54     CHECK_ERR(d_stream, err, "inflateInit");
55     d_stream.next_out=uncompr;
56     d_stream.next_out_index=0;
57     d_stream.avail_out=uncomprLen;
58     
59     err=d_stream.inflate(JZlib.Z_NO_FLUSH);
60     CHECK_ERR(d_stream, err, "inflate");
61
62     d_stream.avail_in=comprLen-2;
63
64     err=d_stream.inflateSync();
65     CHECK_ERR(d_stream, err, "inflateSync");
66
67     err=d_stream.inflate(JZlib.Z_FINISH);
68     if (err != JZlib.Z_DATA_ERROR) {
69       System.out.println("inflate should report DATA_ERROR");
70         /* Because of incorrect adler32 */
71       System.exit(1);
72     }
73
74     err=d_stream.inflateEnd();
75     CHECK_ERR(d_stream, err, "inflateEnd");
76
77     int j=0;
78     for(;j<uncompr.length; j++) if(uncompr[j]==0) break;
79
80     System.out.println("after inflateSync(): hel"+new String JavaDoc(uncompr, 0, j));
81   }
82
83   static void CHECK_ERR(ZStream z, int err, String JavaDoc msg) {
84     if(err!=JZlib.Z_OK){
85       if(z.msg!=null) System.out.print(z.msg+" ");
86       System.out.println(msg+" error: "+err);
87
88       System.exit(1);
89     }
90   }
91 }
92
Popular Tags