1 2 29 30 package com.jcraft.jsch.jcraft; 31 import com.jcraft.jzlib.*; 32 import com.jcraft.jsch.*; 33 34 public class Compression implements com.jcraft.jsch.Compression { 35 static private final int BUF_SIZE=4096; 36 37 private int type; 38 private ZStream stream; 39 private byte[] tmpbuf=new byte[BUF_SIZE]; 40 41 public Compression(){ 42 stream=new ZStream(); 43 } 44 45 public void init(int type, int level){ 46 if(type==DEFLATER){ 47 stream.deflateInit(level); 48 this.type=DEFLATER; 49 } 50 else if(type==INFLATER){ 51 stream.inflateInit(); 52 inflated_buf=new byte[BUF_SIZE]; 53 this.type=INFLATER; 54 } 55 } 56 64 private byte[] inflated_buf; 65 74 75 public int compress(byte[] buf, int start, int len){ 76 stream.next_in=buf; 77 stream.next_in_index=start; 78 stream.avail_in=len-start; 79 int status; 80 int outputlen=start; 81 82 do{ 83 stream.next_out=tmpbuf; 84 stream.next_out_index=0; 85 stream.avail_out=BUF_SIZE; 86 status=stream.deflate(JZlib.Z_PARTIAL_FLUSH); 87 switch(status){ 88 case JZlib.Z_OK: 89 System.arraycopy(tmpbuf, 0, 90 buf, outputlen, 91 BUF_SIZE-stream.avail_out); 92 outputlen+=(BUF_SIZE-stream.avail_out); 93 break; 94 default: 95 System.err.println("compress: deflate returnd "+status); 96 } 97 } 98 while(stream.avail_out==0); 99 return outputlen; 100 } 101 102 public byte[] uncompress(byte[] buffer, int start, int[] length){ 103 int inflated_end=0; 104 105 stream.next_in=buffer; 106 stream.next_in_index=start; 107 stream.avail_in=length[0]; 108 109 while(true){ 110 stream.next_out=tmpbuf; 111 stream.next_out_index=0; 112 stream.avail_out=BUF_SIZE; 113 int status=stream.inflate(JZlib.Z_PARTIAL_FLUSH); 114 switch(status){ 115 case JZlib.Z_OK: 116 if(inflated_buf.length<inflated_end+BUF_SIZE-stream.avail_out){ 117 byte[] foo=new byte[inflated_end+BUF_SIZE-stream.avail_out]; 118 System.arraycopy(inflated_buf, 0, foo, 0, inflated_end); 119 inflated_buf=foo; 120 } 121 System.arraycopy(tmpbuf, 0, 122 inflated_buf, inflated_end, 123 BUF_SIZE-stream.avail_out); 124 inflated_end+=(BUF_SIZE-stream.avail_out); 125 length[0]=inflated_end; 126 break; 127 case JZlib.Z_BUF_ERROR: 128 if(inflated_end>buffer.length-start){ 129 byte[] foo=new byte[inflated_end+start]; 130 System.arraycopy(buffer, 0, foo, 0, start); 131 System.arraycopy(inflated_buf, 0, foo, start, inflated_end); 132 buffer=foo; 133 } 134 else{ 135 System.arraycopy(inflated_buf, 0, buffer, start, inflated_end); 136 } 137 length[0]=inflated_end; 138 return buffer; 139 default: 140 System.err.println("uncompress: inflate returnd "+status); 141 return null; 142 } 143 } 144 } 145 } 146 | Popular Tags |