Skip to content

Commit

Permalink
Tweaks and excludes for Zlib gzip/gunzip support.
Browse files Browse the repository at this point in the history
* Add gzip and gunzip (in Ruby)
* Fix GzipReader and GzipWriter constructors to yield block
* Separate gunzip errors test since jzlib errors differently.
headius committed Jul 17, 2017
1 parent f8e478d commit f23f9a0
Showing 8 changed files with 55 additions and 22 deletions.
4 changes: 2 additions & 2 deletions core/src/main/java/org/jruby/ast/util/ArgsUtil.java
Original file line number Diff line number Diff line change
@@ -106,7 +106,7 @@ public static IRubyObject getOptionsArg(Ruby runtime, IRubyObject arg) {
* @param validKeys A list of valid kwargs keys.
* @return an array of objects corresponding to the given keys.
*/
public static IRubyObject[] extractKeywordArgs(ThreadContext context, RubyHash options, String[] validKeys) {
public static IRubyObject[] extractKeywordArgs(ThreadContext context, RubyHash options, String... validKeys) {
IRubyObject[] ret = new IRubyObject[validKeys.length];
int index = 0;
HashSet<RubySymbol> validKeySet = new HashSet<RubySymbol>();
@@ -133,7 +133,7 @@ public static IRubyObject[] extractKeywordArgs(ThreadContext context, RubyHash o
return ret;
}

public static IRubyObject[] extractKeywordArgs(ThreadContext context, IRubyObject[] args, String[] validKeys) {
public static IRubyObject[] extractKeywordArgs(ThreadContext context, IRubyObject[] args, String... validKeys) {
IRubyObject options = ArgsUtil.getOptionsArg(context.runtime, args);
if(options instanceof RubyHash) {
return extractKeywordArgs(context, (RubyHash)options, validKeys);
16 changes: 9 additions & 7 deletions core/src/main/java/org/jruby/ext/zlib/JZlibRubyGzipReader.java
Original file line number Diff line number Diff line change
@@ -78,25 +78,27 @@ public IRubyObject allocate(Ruby runtime, RubyClass klass) {
};

@JRubyMethod(name = "new", rest = true, meta = true)
public static JZlibRubyGzipReader newInstance(IRubyObject recv, IRubyObject[] args, Block block) {
public static IRubyObject newInstance(ThreadContext context, IRubyObject recv, IRubyObject[] args, Block block) {
JZlibRubyGzipReader result = newInstance(recv, args);

return RubyGzipFile.wrapBlock(context, result, block);
}

public static JZlibRubyGzipReader newInstance(IRubyObject recv, IRubyObject[] args) {
RubyClass klass = (RubyClass) recv;
JZlibRubyGzipReader result = (JZlibRubyGzipReader) klass.allocate();

result.callInit(args, block);
result.callInit(args, Block.NULL_BLOCK);

return result;
}

public static IRubyObject open18(final ThreadContext context, IRubyObject recv, IRubyObject[] args, Block block) {
return open19(context, recv, args, block);
}

@JRubyMethod(name = "open", required = 1, optional = 1, meta = true)
public static IRubyObject open19(final ThreadContext context, IRubyObject recv, IRubyObject[] args, Block block) {
Ruby runtime = recv.getRuntime();
args[0] = Helpers.invoke(context, runtime.getFile(), "open", args[0], runtime.newString("rb"));

JZlibRubyGzipReader gzio = newInstance(recv, args, block);
JZlibRubyGzipReader gzio = newInstance(recv, args);

return RubyGzipFile.wrapBlock(context, gzio, block);
}
22 changes: 12 additions & 10 deletions core/src/main/java/org/jruby/ext/zlib/JZlibRubyGzipWriter.java
Original file line number Diff line number Diff line change
@@ -69,17 +69,19 @@ public IRubyObject allocate(Ruby runtime, RubyClass klass) {
};

@JRubyMethod(name = "new", rest = true, meta = true)
public static JZlibRubyGzipWriter newInstance(IRubyObject recv, IRubyObject[] args, Block block) {
public static IRubyObject newInstance(ThreadContext context, IRubyObject recv, IRubyObject[] args, Block block) {
JZlibRubyGzipWriter result = newInstance(recv, args);

return RubyGzipFile.wrapBlock(context, result, block);
}

public static JZlibRubyGzipWriter newInstance(IRubyObject recv, IRubyObject[] args) {
RubyClass klass = (RubyClass) recv;
JZlibRubyGzipWriter result = (JZlibRubyGzipWriter) klass.allocate();

result.callInit(args, block);

return result;
}

public static IRubyObject open18(ThreadContext context, IRubyObject recv, IRubyObject[] args, Block block) {
return open19(context, recv, args, block);
result.callInit(args, Block.NULL_BLOCK);

return result;
}

@JRubyMethod(name = "open", required = 1, optional = 3, meta = true)
@@ -88,7 +90,7 @@ public static IRubyObject open19(ThreadContext context, IRubyObject recv, IRubyO

args[0] = Helpers.invoke(context, runtime.getFile(), "open", args[0], runtime.newString("wb"));

JZlibRubyGzipWriter gzio = newInstance(recv, args, block);
JZlibRubyGzipWriter gzio = newInstance(recv, args);

return RubyGzipFile.wrapBlock(context, gzio, block);
}
@@ -102,7 +104,7 @@ public IRubyObject initialize(IRubyObject[] args) {
}

@JRubyMethod(name = "initialize", rest = true, visibility = PRIVATE)
public IRubyObject initialize19(ThreadContext context, IRubyObject[] args, Block unused) {
public IRubyObject initialize19(ThreadContext context, IRubyObject[] args, Block block) {
Ruby runtime = context.getRuntime();
IRubyObject opt = context.nil;

4 changes: 2 additions & 2 deletions core/src/main/java/org/jruby/ext/zlib/RubyGzipFile.java
Original file line number Diff line number Diff line change
@@ -88,9 +88,9 @@ public static IRubyObject wrap19(ThreadContext context, IRubyObject recv, IRubyO

// TODO: People extending GzipWriter/reader will break. Find better way here.
if (recv == runtime.getModule("Zlib").getClass("GzipWriter")) {
instance = JZlibRubyGzipWriter.newInstance(recv, args, block);
instance = JZlibRubyGzipWriter.newInstance(recv, args);
} else {
instance = JZlibRubyGzipReader.newInstance(recv, args, block);
instance = JZlibRubyGzipReader.newInstance(recv, args);
}

return wrapBlock(context, instance, block);
2 changes: 2 additions & 0 deletions core/src/main/java/org/jruby/ext/zlib/RubyZlib.java
Original file line number Diff line number Diff line change
@@ -52,9 +52,11 @@
import org.jruby.anno.JRubyMethod;
import org.jruby.anno.JRubyModule;

import org.jruby.ast.util.ArgsUtil;
import org.jruby.exceptions.RaiseException;

import org.jruby.runtime.Arity;
import org.jruby.runtime.Block;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
import static org.jruby.runtime.Visibility.*;
24 changes: 24 additions & 0 deletions lib/ruby/stdlib/zlib.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'zlib.jar'
require 'stringio'

module Zlib
def self.gzip(src, opts = nil)
if Hash === opts
level = opts[:level]
strategy = opts[:strategy]
end
io = StringIO.new("".force_encoding("ASCII-8BIT"))
GzipWriter.new(io, level, strategy) do |writer|
writer.write(src)
end
io.string
end

def self.gunzip(src)
io = StringIO.new(src)
reader = GzipReader.new(io)
result = reader.read
reader.close
result
end
end
3 changes: 2 additions & 1 deletion test/mri/excludes/TestZlib.rb
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
exclude :test_deflate_stream, "stream support is new in 2.2 (#2128)"
exclude :test_deflate_stream, "stream support is new in 2.2 (#2128)"
exclude :test_gunzip_errors, "jzlib errors somewhat differently"
2 changes: 2 additions & 0 deletions test/mri/zlib/test_zlib.rb
Original file line number Diff line number Diff line change
@@ -1158,7 +1158,9 @@ def test_gzip
def test_gunzip
src = %w[1f8b08000000000000034bcbcf07002165738c03000000].pack("H*")
assert_equal 'foo', Zlib.gunzip(src.freeze)
end

def test_gunzip_errors
src = %w[1f8b08000000000000034bcbcf07002165738c03000001].pack("H*")
assert_raise(Zlib::GzipFile::LengthError){ Zlib.gunzip(src) }

0 comments on commit f23f9a0

Please sign in to comment.