001: /***** BEGIN LICENSE BLOCK *****
002: * Version: CPL 1.0/GPL 2.0/LGPL 2.1
003: *
004: * The contents of this file are subject to the Common Public
005: * License Version 1.0 (the "License"); you may not use this file
006: * except in compliance with the License. You may obtain a copy of
007: * the License at http://www.eclipse.org/legal/cpl-v10.html
008: *
009: * Software distributed under the License is distributed on an "AS
010: * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
011: * implied. See the License for the specific language governing
012: * rights and limitations under the License.
013: *
014: * Copyright (C) 2006 Ola Bini <ola.bini@ki.se>
015: * Copyright (C) 2006 Dave Brosius <dbrosius@mebigfatguy.com>
016: * Copyright (C) 2006 Peter K Chan <peter@oaktop.com>
017: *
018: * Alternatively, the contents of this file may be used under the terms of
019: * either of the GNU General Public License Version 2 or later (the "GPL"),
020: * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
021: * in which case the provisions of the GPL or the LGPL are applicable instead
022: * of those above. If you wish to allow use of your version of this file only
023: * under the terms of either the GPL or the LGPL, and not to allow others to
024: * use your version of this file under the terms of the CPL, indicate your
025: * decision by deleting the provisions above and replace them with the notice
026: * and other provisions required by the GPL or the LGPL. If you do not delete
027: * the provisions above, a recipient may use your version of this file under
028: * the terms of any one of the CPL, the GPL or the LGPL.
029: ***** END LICENSE BLOCK *****/package org.jruby.util;
030:
031: import java.io.IOException;
032: import java.io.UnsupportedEncodingException;
033: import java.util.zip.DataFormatException;
034: import java.util.zip.Deflater;
035:
036: import org.jruby.Ruby;
037: import org.jruby.RubyString;
038: import org.jruby.runtime.builtin.IRubyObject;
039:
040: public class ZlibDeflate {
041: private Deflater flater;
042: private ByteList collected;
043: private Ruby runtime;
044:
045: public static final int BASE_SIZE = 100;
046:
047: public final static int DEF_MEM_LEVEL = 8;
048: public final static int MAX_MEM_LEVEL = 9;
049:
050: public final static int MAX_WBITS = 15;
051:
052: public final static int NO_FLUSH = 0;
053: public final static int SYNC_FLUSH = 2;
054: public final static int FULL_FLUSH = 3;
055: public final static int FINISH = 4;
056:
057: public ZlibDeflate(IRubyObject caller, int level, int win_bits,
058: int memlevel, int strategy) {
059: super ();
060: flater = new Deflater(level, false);
061: flater.setStrategy(strategy);
062: collected = new ByteList(BASE_SIZE);
063: runtime = caller.getRuntime();
064: }
065:
066: public static IRubyObject s_deflate(IRubyObject caller,
067: ByteList str, int level) throws DataFormatException,
068: IOException {
069: ZlibDeflate zstream = new ZlibDeflate(caller, level, MAX_WBITS,
070: DEF_MEM_LEVEL, Deflater.DEFAULT_STRATEGY);
071: IRubyObject result = zstream.deflate(str, FINISH);
072: zstream.close();
073: return result;
074: }
075:
076: public Deflater getDeflater() {
077: return flater;
078: }
079:
080: public void append(IRubyObject obj) throws IOException,
081: UnsupportedEncodingException {
082: append(obj.convertToString().getByteList());
083: }
084:
085: public void append(ByteList obj) throws IOException {
086: collected.append(obj);
087: }
088:
089: public void params(int level, int strategy) {
090: flater.setLevel(level);
091: flater.setStrategy(strategy);
092: }
093:
094: public IRubyObject set_dictionary(IRubyObject str)
095: throws UnsupportedEncodingException {
096: flater.setDictionary(str.convertToString().getBytes());
097: return str;
098: }
099:
100: public IRubyObject flush(int flush) throws IOException {
101: return deflate(new ByteList(0), flush);
102: }
103:
104: public IRubyObject deflate(ByteList str, int flush)
105: throws IOException {
106: if (null == str) {
107: return finish();
108: } else {
109: append(str);
110: if (flush == FINISH) {
111: return finish();
112: }
113: return runtime.newString("");
114: }
115: }
116:
117: public IRubyObject finish() throws IOException {
118: ByteList result = new ByteList(collected.realSize);
119: byte[] outp = new byte[1024];
120: ByteList buf = collected;
121: collected = new ByteList(BASE_SIZE);
122: flater.setInput(buf.bytes, buf.begin, buf.realSize);
123: flater.finish();
124: int resultLength = -1;
125: while (!flater.finished() && resultLength != 0) {
126: resultLength = flater.deflate(outp);
127: result.append(outp, 0, resultLength);
128: }
129: return RubyString.newString(runtime, result);
130: }
131:
132: public void close() {
133: }
134: }
|