Compression.java

Go to the documentation of this file.
00001 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
00002 /*
00003 Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without
00006 modification, are permitted provided that the following conditions are met:
00007 
00008   1. Redistributions of source code must retain the above copyright notice,
00009      this list of conditions and the following disclaimer.
00010 
00011   2. Redistributions in binary form must reproduce the above copyright 
00012      notice, this list of conditions and the following disclaimer in 
00013      the documentation and/or other materials provided with the distribution.
00014 
00015   3. The names of the authors may not be used to endorse or promote products
00016      derived from this software without specific prior written permission.
00017 
00018 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
00019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
00020 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
00021 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
00022 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00023 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00024 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00025 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00026 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00027 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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; // AES256 + HMACSHA1
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 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1