Compression.java
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 package com.jcraft.jsch.jcraft;
00031 import com.jcraft.jzlib.*;
00032 import com.jcraft.jsch.*;
00033
00034 public class Compression implements com.jcraft.jsch.Compression {
00035 static private final int BUF_SIZE=4096;
00036 private final int buffer_margin=32+20;
00037 private int type;
00038 private ZStream stream;
00039 private byte[] tmpbuf=new byte[BUF_SIZE];
00040
00041 public Compression(){
00042 stream=new ZStream();
00043 }
00044
00045 public void init(int type, int level){
00046 if(type==DEFLATER){
00047 stream.deflateInit(level);
00048 this.type=DEFLATER;
00049 }
00050 else if(type==INFLATER){
00051 stream.inflateInit();
00052 inflated_buf=new byte[BUF_SIZE];
00053 this.type=INFLATER;
00054 }
00055 }
00056
00057 private byte[] inflated_buf;
00058
00059 public byte[] compress(byte[] buf, int start, int[] len){
00060 stream.next_in=buf;
00061 stream.next_in_index=start;
00062 stream.avail_in=len[0]-start;
00063 int status;
00064 int outputlen=start;
00065 byte[] outputbuf=buf;
00066 int tmp=0;
00067
00068 do{
00069 stream.next_out=tmpbuf;
00070 stream.next_out_index=0;
00071 stream.avail_out=BUF_SIZE;
00072 status=stream.deflate(JZlib.Z_PARTIAL_FLUSH);
00073 switch(status){
00074 case JZlib.Z_OK:
00075 tmp=BUF_SIZE-stream.avail_out;
00076 if(outputbuf.length<outputlen+tmp+buffer_margin){
00077 byte[] foo=new byte[(outputlen+tmp+buffer_margin)*2];
00078 System.arraycopy(outputbuf, 0, foo, 0, outputbuf.length);
00079 outputbuf=foo;
00080 }
00081 System.arraycopy(tmpbuf, 0, outputbuf, outputlen, tmp);
00082 outputlen+=tmp;
00083 break;
00084 default:
00085 System.err.println("compress: deflate returnd "+status);
00086 }
00087 }
00088 while(stream.avail_out==0);
00089
00090 len[0]=outputlen;
00091 return outputbuf;
00092 }
00093
00094 public byte[] uncompress(byte[] buffer, int start, int[] length){
00095 int inflated_end=0;
00096
00097 stream.next_in=buffer;
00098 stream.next_in_index=start;
00099 stream.avail_in=length[0];
00100
00101 while(true){
00102 stream.next_out=tmpbuf;
00103 stream.next_out_index=0;
00104 stream.avail_out=BUF_SIZE;
00105 int status=stream.inflate(JZlib.Z_PARTIAL_FLUSH);
00106 switch(status){
00107 case JZlib.Z_OK:
00108 if(inflated_buf.length<inflated_end+BUF_SIZE-stream.avail_out){
00109 int len=inflated_buf.length*2;
00110 if(len<inflated_end+BUF_SIZE-stream.avail_out)
00111 len=inflated_end+BUF_SIZE-stream.avail_out;
00112 byte[] foo=new byte[len];
00113 System.arraycopy(inflated_buf, 0, foo, 0, inflated_end);
00114 inflated_buf=foo;
00115 }
00116 System.arraycopy(tmpbuf, 0,
00117 inflated_buf, inflated_end,
00118 BUF_SIZE-stream.avail_out);
00119 inflated_end+=(BUF_SIZE-stream.avail_out);
00120 length[0]=inflated_end;
00121 break;
00122 case JZlib.Z_BUF_ERROR:
00123 if(inflated_end>buffer.length-start){
00124 byte[] foo=new byte[inflated_end+start];
00125 System.arraycopy(buffer, 0, foo, 0, start);
00126 System.arraycopy(inflated_buf, 0, foo, start, inflated_end);
00127 buffer=foo;
00128 }
00129 else{
00130 System.arraycopy(inflated_buf, 0, buffer, start, inflated_end);
00131 }
00132 length[0]=inflated_end;
00133 return buffer;
00134 default:
00135 System.err.println("uncompress: inflate returnd "+status);
00136 return null;
00137 }
00138 }
00139 }
00140 }