1 29 package org.jruby.util; 30 31 import java.io.UnsupportedEncodingException ; 32 33 import java.util.zip.DataFormatException ; 34 import java.util.zip.Inflater ; 35 36 import org.jruby.Ruby; 37 import org.jruby.RubyString; 38 import org.jruby.runtime.builtin.IRubyObject; 39 40 public class ZlibInflate { 41 private Inflater flater; 42 private ByteList collected; 43 private Ruby runtime; 44 45 public static final int BASE_SIZE = 100; 46 47 public ZlibInflate(IRubyObject caller) { 48 super(); 49 flater = new Inflater (false); 50 collected = new ByteList(BASE_SIZE); 51 runtime = caller.getRuntime(); 52 } 53 54 public static IRubyObject s_inflate(IRubyObject caller, ByteList str) 55 throws DataFormatException { 56 ZlibInflate zstream = new ZlibInflate(caller); 57 IRubyObject result = zstream.inflate(str); 58 zstream.finish(); 59 zstream.close(); 60 return result; 61 } 62 63 public Inflater getInflater() { 64 return flater; 65 } 66 67 public void append(IRubyObject obj) { 68 append(obj.convertToString().getByteList()); 69 } 70 71 public void append(ByteList obj) { 72 collected.append(obj); 73 } 74 75 public IRubyObject sync_point() { 76 return runtime.getFalse(); 77 } 78 79 public IRubyObject set_dictionary(IRubyObject str) throws UnsupportedEncodingException { 80 flater.setDictionary(str.convertToString().getBytes()); 81 return str; 82 } 83 84 public IRubyObject inflate(ByteList str) throws DataFormatException { 85 if (null != str) { 86 append(str); 87 } 88 ByteList result = new ByteList(collected.realSize); 89 byte[] outp = new byte[1024]; 90 ByteList buf = collected; 91 collected = new ByteList(BASE_SIZE); 92 flater.setInput(buf.bytes,0,buf.realSize); 93 int resultLength = -1; 94 while (!flater.finished() && resultLength != 0) { 95 resultLength = flater.inflate(outp); 96 result.append(outp, 0, resultLength); 97 } 98 return RubyString.newString(runtime, result); 99 } 100 101 public IRubyObject sync(IRubyObject str) { 102 append(str); 103 return runtime.getFalse(); 104 } 105 106 public void finish() { 107 flater.end(); 108 } 109 110 public void close() { 111 } 112 } 113 | Popular Tags |