forked from jruby/jruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZStream.java
More file actions
193 lines (155 loc) · 5.63 KB
/
Copy pathZStream.java
File metadata and controls
193 lines (155 loc) · 5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
*/
package org.jruby.ext.zlib;
import com.jcraft.jzlib.JZlib;
import org.jruby.Ruby;
import org.jruby.RubyClass;
import org.jruby.RubyFixnum;
import org.jruby.RubyObject;
import org.jruby.RubyString;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.runtime.Block;
import org.jruby.runtime.ThreadContext;
import static org.jruby.runtime.Visibility.PRIVATE;
import org.jruby.runtime.builtin.IRubyObject;
/**
*
*/
@JRubyClass(name = "Zlib::ZStream")
public abstract class ZStream extends RubyObject {
protected boolean closed = false;
protected abstract int internalTotalIn();
protected abstract int internalTotalOut();
protected abstract boolean internalStreamEndP();
protected abstract void internalReset();
protected abstract boolean internalFinished();
protected abstract long internalAdler();
// TODO: eliminate?
protected abstract IRubyObject internalFinish();
protected abstract void internalClose();
public ZStream(Ruby runtime, RubyClass type) {
super(runtime, type);
}
@JRubyMethod(visibility = PRIVATE)
public IRubyObject initialize(Block unusedBlock) {
return this;
}
@JRubyMethod
public IRubyObject flush_next_out(ThreadContext context) {
return RubyString.newEmptyString(context.getRuntime());
}
@JRubyMethod
public IRubyObject total_out() {
checkClosed();
return getRuntime().newFixnum(internalTotalOut());
}
@JRubyMethod(name = "stream_end?")
public IRubyObject stream_end_p() {
return internalStreamEndP() ? getRuntime().getTrue() : getRuntime().getFalse();
}
@JRubyMethod(name = "data_type")
public IRubyObject data_type() {
checkClosed();
return getRuntime().getModule("Zlib").getConstant("UNKNOWN");
}
@JRubyMethod(name = {"closed?", "ended?"})
public IRubyObject closed_p() {
return closed ? getRuntime().getTrue() : getRuntime().getFalse();
}
@JRubyMethod(name = "reset")
public IRubyObject reset() {
checkClosed();
internalReset();
return getRuntime().getNil();
}
@JRubyMethod(name = "avail_out")
public IRubyObject avail_out() {
return RubyFixnum.zero(getRuntime());
}
@JRubyMethod(name = "avail_out=", required = 1)
public IRubyObject set_avail_out(IRubyObject p1) {
checkClosed();
return p1;
}
@JRubyMethod(name = "adler")
public IRubyObject adler() {
checkClosed();
return getRuntime().newFixnum(internalAdler());
}
@JRubyMethod(name = "finish")
public IRubyObject finish(ThreadContext context) {
checkClosed();
IRubyObject result = internalFinish();
return result;
}
@JRubyMethod(name = "avail_in")
public IRubyObject avail_in() {
return RubyFixnum.zero(getRuntime());
}
@JRubyMethod(name = "flush_next_in")
public IRubyObject flush_next_in(ThreadContext context) {
return RubyString.newEmptyString(context.getRuntime());
}
@JRubyMethod(name = "total_in")
public IRubyObject total_in() {
checkClosed();
return getRuntime().newFixnum(internalTotalIn());
}
@JRubyMethod(name = "finished?")
public IRubyObject finished_p(ThreadContext context) {
checkClosed();
Ruby runtime = context.getRuntime();
return internalFinished() ? runtime.getTrue() : runtime.getFalse();
}
@JRubyMethod(name = {"close", "end"})
public IRubyObject close() {
checkClosed();
internalClose();
closed = true;
return getRuntime().getNil();
}
void checkClosed() {
if (closed) throw RubyZlib.newZlibError(getRuntime(), "stream is not ready");
}
// TODO: remove when JZlib checks the given level
static void checkLevel(Ruby runtime, int level) {
if ((level < 0 || level > 9) && level != JZlib.Z_DEFAULT_COMPRESSION) {
throw RubyZlib.newStreamError(runtime, "stream error: invalid level");
}
}
/**
* We only do windowBits=15(32K buffer, LZ77 algorithm) since java.util.zip
* only allows it. NOTE: deflateInit2 of zlib.c also accepts MAX_WBITS +
* 16(gzip compression). inflateInit2 also accepts MAX_WBITS + 16(gzip
* decompression) and MAX_WBITS + 32(automatic detection of gzip and LZ77).
*/
// TODO: remove when JZlib checks the given windowBits
static void checkWindowBits(Ruby runtime, int wbits, boolean forInflate) {
wbits = Math.abs(wbits);
if ((wbits & 0xf) < 8) {
throw RubyZlib.newStreamError(runtime, "stream error: invalid window bits");
}
if ((wbits & 0xf) != 0xf) {
// windowBits < 15 for reducing memory is meaningless on Java platform.
runtime.getWarnings().warn("windowBits < 15 is ignored on this platform");
// continue
}
if (forInflate && wbits > JZlib.MAX_WBITS + 32) {
throw RubyZlib.newStreamError(runtime, "stream error: invalid window bits");
} else if (!forInflate && wbits > JZlib.MAX_WBITS + 16) {
throw RubyZlib.newStreamError(runtime, "stream error: invalid window bits");
}
}
// TODO: remove when JZlib checks the given strategy
static void checkStrategy(Ruby runtime, int strategy) {
switch (strategy) {
case JZlib.Z_DEFAULT_STRATEGY:
case JZlib.Z_FILTERED:
case JZlib.Z_HUFFMAN_ONLY:
break;
default:
throw RubyZlib.newStreamError(runtime, "stream error: invalid strategy");
}
}
}