KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > util > ZlibDeflate


1 /***** BEGIN LICENSE BLOCK *****
2  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Common Public
5  * License Version 1.0 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of
7  * the License at http://www.eclipse.org/legal/cpl-v10.html
8  *
9  * Software distributed under the License is distributed on an "AS
10  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * rights and limitations under the License.
13  *
14  * Copyright (C) 2006 Ola Bini <ola.bini@ki.se>
15  * Copyright (C) 2006 Dave Brosius <dbrosius@mebigfatguy.com>
16  * Copyright (C) 2006 Peter K Chan <peter@oaktop.com>
17  *
18  * Alternatively, the contents of this file may be used under the terms of
19  * either of the GNU General Public License Version 2 or later (the "GPL"),
20  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
21  * in which case the provisions of the GPL or the LGPL are applicable instead
22  * of those above. If you wish to allow use of your version of this file only
23  * under the terms of either the GPL or the LGPL, and not to allow others to
24  * use your version of this file under the terms of the CPL, indicate your
25  * decision by deleting the provisions above and replace them with the notice
26  * and other provisions required by the GPL or the LGPL. If you do not delete
27  * the provisions above, a recipient may use your version of this file under
28  * the terms of any one of the CPL, the GPL or the LGPL.
29  ***** END LICENSE BLOCK *****/

30 package org.jruby.util;
31
32 import java.io.IOException JavaDoc;
33 import java.io.UnsupportedEncodingException JavaDoc;
34 import java.util.zip.DataFormatException JavaDoc;
35 import java.util.zip.Deflater JavaDoc;
36
37 import org.jruby.Ruby;
38 import org.jruby.RubyString;
39 import org.jruby.runtime.builtin.IRubyObject;
40
41 public class ZlibDeflate {
42     private Deflater JavaDoc flater;
43     private ByteList collected;
44     private Ruby runtime;
45
46     public static final int BASE_SIZE = 100;
47
48     public final static int DEF_MEM_LEVEL = 8;
49     public final static int MAX_MEM_LEVEL = 9;
50
51     public final static int MAX_WBITS = 15;
52
53     public final static int NO_FLUSH = 0;
54     public final static int SYNC_FLUSH = 2;
55     public final static int FULL_FLUSH = 3;
56     public final static int FINISH = 4;
57
58     public ZlibDeflate(IRubyObject caller, int level, int win_bits, int memlevel, int strategy) {
59         super();
60         flater = new Deflater JavaDoc(level,false);
61         flater.setStrategy(strategy);
62         collected = new ByteList(BASE_SIZE);
63         runtime = caller.getRuntime();
64     }
65
66     public static IRubyObject s_deflate(IRubyObject caller, ByteList str, int level)
67         throws DataFormatException JavaDoc, IOException JavaDoc {
68         ZlibDeflate zstream = new ZlibDeflate(caller, level, MAX_WBITS, DEF_MEM_LEVEL, Deflater.DEFAULT_STRATEGY);
69         IRubyObject result = zstream.deflate(str, FINISH);
70         zstream.close();
71         return result;
72     }
73
74     public Deflater JavaDoc getDeflater() {
75         return flater;
76     }
77
78     public void append(IRubyObject obj) throws IOException JavaDoc, UnsupportedEncodingException JavaDoc {
79         append(obj.convertToString().getByteList());
80     }
81
82     public void append(ByteList obj) throws IOException JavaDoc {
83         collected.append(obj);
84     }
85
86     public void params(int level, int strategy) {
87         flater.setLevel(level);
88         flater.setStrategy(strategy);
89     }
90
91     public IRubyObject set_dictionary(IRubyObject str) throws UnsupportedEncodingException JavaDoc {
92         flater.setDictionary(str.convertToString().getBytes());
93         return str;
94     }
95
96     public IRubyObject flush(int flush) throws IOException JavaDoc {
97         return deflate(new ByteList(0), flush);
98     }
99
100     public IRubyObject deflate(ByteList str, int flush) throws IOException JavaDoc {
101         if (null == str) {
102             return finish();
103         } else {
104             append(str);
105             if (flush == FINISH) {
106                 return finish();
107             }
108             return runtime.newString("");
109         }
110     }
111     
112     public IRubyObject finish() throws IOException JavaDoc {
113         ByteList result = new ByteList(collected.realSize);
114         byte[] outp = new byte[1024];
115         ByteList buf = collected;
116         collected = new ByteList(BASE_SIZE);
117         flater.setInput(buf.bytes,0,buf.realSize);
118         flater.finish();
119         int resultLength = -1;
120         while (!flater.finished() && resultLength != 0) {
121             resultLength = flater.deflate(outp);
122             result.append(outp, 0, resultLength);
123         }
124         return RubyString.newString(runtime, result);
125     }
126     
127     public void close() {
128     }
129 }
130
Popular Tags