1 2 import java.io.*; 3 import com.jcraft.jzlib.*; 4 5 class test_dict_deflate_inflate{ 7 8 static final byte[] dictionary = "hello ".getBytes(); 9 static final byte[] hello="hello, hello! ".getBytes(); 10 static{ 11 dictionary[dictionary.length-1]=0; 12 hello[hello.length-1]=0; 13 } 14 15 public static void main(String [] arg){ 16 int err; 17 int comprLen=40000; 18 int uncomprLen=comprLen; 19 byte[] uncompr=new byte[uncomprLen]; 20 byte[] compr=new byte[comprLen]; 21 long dictId; 22 23 ZStream c_stream=new ZStream(); 24 err=c_stream.deflateInit(JZlib.Z_BEST_COMPRESSION); 25 CHECK_ERR(c_stream, err, "deflateInit"); 26 27 err = c_stream.deflateSetDictionary(dictionary, dictionary.length); 28 CHECK_ERR(c_stream, err, "deflateSetDictionary"); 29 30 dictId=c_stream.adler; 31 32 c_stream.next_out = compr; 33 c_stream.next_out_index = 0; 34 c_stream.avail_out = comprLen; 35 36 c_stream.next_in = hello; 37 c_stream.next_in_index = 0; 38 c_stream.avail_in = hello.length; 39 40 err = c_stream.deflate(JZlib.Z_FINISH); 41 if (err!=JZlib.Z_STREAM_END) { 42 System.out.println("deflate should report Z_STREAM_END"); 43 System.exit(1); 44 } 45 err = c_stream.deflateEnd(); 46 CHECK_ERR(c_stream, err, "deflateEnd"); 47 48 ZStream d_stream=new ZStream(); 49 50 d_stream.next_in=compr; 51 d_stream.next_in_index=0; 52 d_stream.avail_in=comprLen; 53 54 err=d_stream.inflateInit(); 55 CHECK_ERR(d_stream, err, "inflateInit"); 56 d_stream.next_out=uncompr; 57 d_stream.next_out_index=0; 58 d_stream.avail_out=uncomprLen; 59 60 while(true){ 61 err=d_stream.inflate(JZlib.Z_NO_FLUSH); 62 if(err==JZlib.Z_STREAM_END){ 63 break; 64 } 65 if(err==JZlib.Z_NEED_DICT){ 66 if((int)d_stream.adler != (int)dictId) { 67 System.out.println("unexpected dictionary"); 68 System.exit(1); 69 } 70 err=d_stream.inflateSetDictionary(dictionary, dictionary.length); 71 } 72 CHECK_ERR(d_stream, err, "inflate with dict"); 73 } 74 75 err=d_stream.inflateEnd(); 76 CHECK_ERR(d_stream, err, "inflateEnd"); 77 78 int j=0; 79 for(;j<uncompr.length; j++) if(uncompr[j]==0) break; 80 81 System.out.println("after inflateSync(): hel"+new String (uncompr, 0, j)); 82 } 83 84 static void CHECK_ERR(ZStream z, int err, String msg) { 85 if(err!=JZlib.Z_OK){ 86 if(z.msg!=null) System.out.print(z.msg+" "); 87 System.out.println(msg+" error: "+err); 88 89 System.exit(1); 90 } 91 } 92 } 93 | Popular Tags |