-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a JRuby-based ext wrapping the net.jpountz.lz4 library.
rake build:jruby will build the JRuby version of the gem. The 'mvn' command must be present on the build system.
- Loading branch information
Showing
7 changed files
with
168 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
require 'lz4ruby.jar' | ||
|
||
com.headius.jruby.lz4.LZ4Library.new.load(JRuby.runtime, false) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.headius.jruby.lz4</groupId> | ||
<artifactId>lz4ruby</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>lz4ruby</name> | ||
<url>http://maven.apache.org</url> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>3.8.1</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>net.jpountz.lz4</groupId> | ||
<artifactId>lz4</artifactId> | ||
<version>1.1.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.jruby</groupId> | ||
<artifactId>jruby</artifactId> | ||
<version>1.7.0</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-shade-plugin</artifactId> | ||
<version>2.0</version> | ||
<configuration> | ||
<relocations> | ||
<relocation> | ||
<pattern>net.jpountz</pattern> | ||
<shadedPattern>com.headius.jruby.lz4.vendor.net.jpountz</shadedPattern> | ||
</relocation> | ||
</relocations> | ||
<outputFile>${basedir}/lib/lz4ruby.jar</outputFile> | ||
</configuration> | ||
<executions> | ||
<execution> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>shade</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.headius.jruby.lz4; | ||
|
||
import net.jpountz.lz4.LZ4Compressor; | ||
import net.jpountz.lz4.LZ4Factory; | ||
import org.jruby.RubyInteger; | ||
import org.jruby.RubyString; | ||
import org.jruby.anno.JRubyMethod; | ||
import org.jruby.runtime.ThreadContext; | ||
import org.jruby.runtime.builtin.IRubyObject; | ||
import org.jruby.util.ByteList; | ||
|
||
public class LZ4Internal { | ||
private static final LZ4Factory FACTORY = LZ4Factory.fastestInstance(); | ||
|
||
public static IRubyObject compress_internal(ThreadContext context, LZ4Compressor compressor, IRubyObject _header, IRubyObject _input, IRubyObject _in_size) { | ||
RubyString input = _input.convertToString(); | ||
RubyString header = _header.convertToString(); | ||
RubyInteger in_size = _in_size.convertToInteger(); | ||
|
||
ByteList inputBL = input.getByteList(); | ||
int srcSize = (int)in_size.getLongValue(); | ||
|
||
ByteList headerBL = header.getByteList(); | ||
int headerSize = headerBL.getRealSize(); | ||
|
||
int bufSize = compressor.maxCompressedLength(srcSize); | ||
byte[] buf = new byte[bufSize + headerSize]; | ||
|
||
System.arraycopy(headerBL.getUnsafeBytes(), headerBL.getBegin(), buf, 0, headerSize); | ||
|
||
compressor.compress(inputBL.getUnsafeBytes(), inputBL.getBegin(), srcSize, buf, headerSize); | ||
|
||
return RubyString.newStringNoCopy(context.runtime, buf); | ||
} | ||
|
||
@JRubyMethod(module = true) | ||
public static IRubyObject compress(ThreadContext context, IRubyObject self, IRubyObject _header, IRubyObject _input, IRubyObject _in_size) { | ||
return compress_internal(context, FACTORY.fastCompressor(), _header, _input, _in_size); | ||
} | ||
|
||
@JRubyMethod(module = true) | ||
public static IRubyObject compressHC(ThreadContext context, IRubyObject self, IRubyObject _header, IRubyObject _input, IRubyObject _in_size) { | ||
return compress_internal(context, FACTORY.highCompressor(), _header, _input, _in_size); | ||
} | ||
|
||
@JRubyMethod(required = 4, module = true) | ||
public static IRubyObject uncompress(ThreadContext context, IRubyObject self, IRubyObject[] args) { | ||
RubyString input = args[0].convertToString(); | ||
RubyInteger in_size = args[1].convertToInteger(); | ||
RubyInteger header_size = args[2].convertToInteger(); | ||
RubyInteger buf_size = args[3].convertToInteger(); | ||
|
||
ByteList inputBL = input.getByteList(); | ||
int inSize = (int)in_size.getLongValue(); | ||
int headerSize = (int)header_size.getLongValue(); | ||
int bufSize = (int)buf_size.getLongValue(); | ||
|
||
byte[] buf = new byte[bufSize]; | ||
|
||
FACTORY.decompressor().decompress(inputBL.getUnsafeBytes(), inputBL.getBegin() + headerSize, buf, 0, buf.length); | ||
|
||
return RubyString.newStringNoCopy(context.runtime, buf); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.headius.jruby.lz4; | ||
|
||
import java.io.IOException; | ||
import org.jruby.Ruby; | ||
import org.jruby.RubyModule; | ||
import org.jruby.runtime.load.Library; | ||
|
||
public class LZ4Library implements Library { | ||
|
||
public void load(Ruby runtime, boolean wrap) throws IOException { | ||
RubyModule lz4Internal = runtime.defineModule("LZ4Internal"); | ||
|
||
lz4Internal.defineAnnotatedMethods(LZ4Internal.class); | ||
|
||
lz4Internal.defineClassUnder("Error", runtime.getStandardError(), runtime.getStandardError().getAllocator()); | ||
} | ||
|
||
} |