lz4-ruby 0.3.0-java → 0.3.1-java

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e5bd47d046a138d115e1492b2162641880b149aa
4
- data.tar.gz: 9009550b25894654deeffb26019217c0a90b95f8
3
+ metadata.gz: 0b0550566ae8059fe9f3fd846db15c7ee8ad0631
4
+ data.tar.gz: 0c71cc2ea1124c5ec44239b4f9d74923ef4104ce
5
5
  SHA512:
6
- metadata.gz: ab7022f53a5d3af4f15b2f98957ad47556a5ec0d590b91acbf12708180a754c8b2d417455584977ab0a5e9268e9d2ac0c3f98f4bc5bd8e86bbc2dbd555e07492
7
- data.tar.gz: 0814f77b8356d4d1092e5b8c8d373a810b30a67cd355b45fb99e92855136867f299b91a558e48dd055639b890b5d02da2f8fa875b8a29447882870996a6738b3
6
+ metadata.gz: 56e8cf48de27bd2c6a20e4348db88def98775306cae4f2b3c41ea19205d187211245db97018ca4768df444aac6d822dc40c7262d1becea0c366cc7fc1e7733ae
7
+ data.tar.gz: afeb6ced50eb7e54e2aebac74eab64ecb372b1366f146a1f3f547ebf406ed262ce5c045a35e4e5df07b527bfe9e64ed26fff53b59607add26ca6dfe5f68495f8
@@ -33,8 +33,8 @@ Tested on VirtualBox VM : 2-core / 4GB RAM (Host : Core i5-2520M / 8GB RAM).
33
33
 
34
34
  == TODO
35
35
 
36
- * Support raw data handling methods for JRuby.
37
36
  * Support streaming methods.
37
+ * Support compression level (LZ4HC)
38
38
  * Write API documents.
39
39
 
40
40
  == Copyright
data/Rakefile CHANGED
@@ -77,7 +77,7 @@ task :gems do
77
77
  sh "rake clean build"
78
78
  end
79
79
 
80
- task "build:cross" => [:modify_gemspec_for_windows, :cross, :build] do
80
+ task "build:cross" => [:modify_gemspec_for_windows, :build] do
81
81
  file = "pkg/lz4-ruby-#{get_version}.gem"
82
82
  end
83
83
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.3.1
Binary file
@@ -1,7 +1,9 @@
1
1
  package com.headius.jruby.lz4;
2
2
 
3
3
  import net.jpountz.lz4.LZ4Compressor;
4
+ import net.jpountz.lz4.LZ4Exception;
4
5
  import net.jpountz.lz4.LZ4Factory;
6
+ import org.jruby.RubyArray;
5
7
  import org.jruby.RubyInteger;
6
8
  import org.jruby.RubyString;
7
9
  import org.jruby.anno.JRubyMethod;
@@ -9,56 +11,270 @@ import org.jruby.runtime.ThreadContext;
9
11
  import org.jruby.runtime.builtin.IRubyObject;
10
12
  import org.jruby.util.ByteList;
11
13
 
14
+ import java.util.Arrays;
15
+
16
+ @SuppressWarnings("UnusedParameters")
12
17
  public class LZ4Internal {
13
18
  private static final LZ4Factory FACTORY = LZ4Factory.fastestInstance();
14
-
15
- public static IRubyObject compress_internal(ThreadContext context, LZ4Compressor compressor, IRubyObject _header, IRubyObject _input, IRubyObject _in_size) {
16
- RubyString input = _input.convertToString();
17
- RubyString header = _header.convertToString();
18
- RubyInteger in_size = _in_size.convertToInteger();
19
-
20
- ByteList inputBL = input.getByteList();
21
- int srcSize = (int)in_size.getLongValue();
22
-
23
- ByteList headerBL = header.getByteList();
24
- int headerSize = headerBL.getRealSize();
25
-
26
- int bufSize = compressor.maxCompressedLength(srcSize);
27
- byte[] buf = new byte[bufSize + headerSize];
28
-
29
- System.arraycopy(headerBL.getUnsafeBytes(), headerBL.getBegin(), buf, 0, headerSize);
30
-
31
- compressor.compress(inputBL.getUnsafeBytes(), inputBL.getBegin(), srcSize, buf, headerSize);
32
-
33
- return RubyString.newStringNoCopy(context.runtime, buf);
19
+
20
+ private static RubyArray compressInternal(
21
+ ThreadContext context,
22
+ LZ4Compressor compressor,
23
+ IRubyObject _header,
24
+ IRubyObject _input,
25
+ IRubyObject _inputSize,
26
+ IRubyObject _outputBuffer,
27
+ IRubyObject _maxOutputSize) {
28
+
29
+ byte[] headerBytes = {};
30
+ int headerSize = 0;
31
+ if (isNotNil(_header)) {
32
+ ByteList header = _header.convertToString().getByteList();
33
+ headerBytes = header.getUnsafeBytes();
34
+ headerSize = header.getRealSize();
35
+ }
36
+
37
+ int inputSize = intFrom(_inputSize);
38
+ ByteOutput output;
39
+ if (isNotNil(_outputBuffer)) {
40
+ output = new ByteOutput(_outputBuffer, _maxOutputSize);
41
+
42
+ } else if (isNotNil(_maxOutputSize)) {
43
+ int maxOutputSize = intFrom(_maxOutputSize) + headerSize;
44
+ output = new ByteOutput(maxOutputSize);
45
+
46
+ } else {
47
+ int maxOutputSize = compressor.maxCompressedLength(inputSize) + headerSize;
48
+ output = new ByteOutput(maxOutputSize);
49
+ }
50
+
51
+ System.arraycopy(
52
+ headerBytes, 0,
53
+ output.unsafeBytes, 0,
54
+ headerSize);
55
+
56
+ ByteInput input = new ByteInput(_input);
57
+ try {
58
+ int compressedSize = compressor.compress(
59
+ input.unsafeBytes, input.offset, inputSize,
60
+ output.unsafeBytes, output.offset + headerSize, output.bufferSize - headerSize);
61
+
62
+ return RubyArray.newArray(context.runtime, Arrays.asList(
63
+ output.toRubyString(context, compressedSize + headerSize),
64
+ RubyInteger.int2fix(context.runtime, compressedSize)));
65
+
66
+ } catch (LZ4Exception ignore) {
67
+ return RubyArray.newArray(context.runtime, Arrays.asList(
68
+ null,
69
+ RubyInteger.int2fix(context.runtime, -1)));
70
+ }
34
71
  }
35
-
72
+
73
+ private static RubyArray decompressInternal(
74
+ ThreadContext context,
75
+ IRubyObject _offset,
76
+ IRubyObject _input,
77
+ IRubyObject _inputSize,
78
+ IRubyObject _outputBuffer,
79
+ IRubyObject _maxOutputSize) {
80
+
81
+ int offset = 0;
82
+ if (isNotNil(_offset)) {
83
+ offset = intFrom(_offset);
84
+ }
85
+
86
+ ByteOutput output;
87
+ if (isNotNil(_outputBuffer)) {
88
+ output = new ByteOutput(_outputBuffer, _maxOutputSize);
89
+
90
+ } else {
91
+ output = new ByteOutput(intFrom(_maxOutputSize));
92
+ }
93
+
94
+ ByteInput input = new ByteInput(_input);
95
+ try {
96
+ int decompressedSize = FACTORY.unknwonSizeDecompressor()
97
+ .decompress(
98
+ input.unsafeBytes, input.offset + offset, intFrom(_inputSize) - offset,
99
+ output.unsafeBytes, output.offset, output.bufferSize);
100
+
101
+ return RubyArray.newArray(context.runtime, Arrays.asList(
102
+ output.toRubyString(context, decompressedSize),
103
+ RubyInteger.int2fix(context.runtime, decompressedSize)));
104
+
105
+ } catch (LZ4Exception ignore) {
106
+ return RubyArray.newArray(context.runtime, Arrays.asList(
107
+ null,
108
+ RubyInteger.int2fix(context.runtime, -1)));
109
+ }
110
+ }
111
+
36
112
  @JRubyMethod(module = true)
37
113
  public static IRubyObject compress(ThreadContext context, IRubyObject self, IRubyObject _header, IRubyObject _input, IRubyObject _in_size) {
38
- return compress_internal(context, FACTORY.fastCompressor(), _header, _input, _in_size);
114
+ RubyArray array = compressInternal(context,
115
+ FACTORY.fastCompressor(),
116
+ _header,
117
+ _input,
118
+ _in_size,
119
+ null,
120
+ null);
121
+ return array.first();
39
122
  }
40
-
123
+
41
124
  @JRubyMethod(module = true)
42
125
  public static IRubyObject compressHC(ThreadContext context, IRubyObject self, IRubyObject _header, IRubyObject _input, IRubyObject _in_size) {
43
- return compress_internal(context, FACTORY.highCompressor(), _header, _input, _in_size);
126
+ RubyArray array = compressInternal(context,
127
+ FACTORY.highCompressor(),
128
+ _header,
129
+ _input,
130
+ _in_size,
131
+ null,
132
+ null);
133
+ return array.first();
44
134
  }
45
-
135
+
46
136
  @JRubyMethod(required = 4, module = true)
47
137
  public static IRubyObject uncompress(ThreadContext context, IRubyObject self, IRubyObject[] args) {
48
138
  RubyString input = args[0].convertToString();
49
139
  RubyInteger in_size = args[1].convertToInteger();
50
140
  RubyInteger header_size = args[2].convertToInteger();
51
141
  RubyInteger buf_size = args[3].convertToInteger();
52
-
53
- ByteList inputBL = input.getByteList();
54
- int inSize = (int)in_size.getLongValue();
55
- int headerSize = (int)header_size.getLongValue();
56
- int bufSize = (int)buf_size.getLongValue();
57
-
58
- byte[] buf = new byte[bufSize];
59
-
60
- FACTORY.decompressor().decompress(inputBL.getUnsafeBytes(), inputBL.getBegin() + headerSize, buf, 0, buf.length);
61
-
62
- return RubyString.newStringNoCopy(context.runtime, buf);
142
+
143
+ RubyArray array = decompressInternal(context,
144
+ header_size,
145
+ input,
146
+ in_size,
147
+ null,
148
+ buf_size);
149
+ return array.first();
150
+ }
151
+
152
+ @JRubyMethod(required = 4, module = true)
153
+ public static IRubyObject compress_raw(
154
+ ThreadContext context,
155
+ IRubyObject self,
156
+ IRubyObject[] args) {
157
+
158
+ IRubyObject _input = args[0];
159
+ IRubyObject _inputSize = args[1];
160
+ IRubyObject _outputBuffer = args[2];
161
+ IRubyObject _maxOutputSize = args[3];
162
+
163
+ return compressInternal(context,
164
+ FACTORY.fastCompressor(),
165
+ null,
166
+ _input,
167
+ _inputSize,
168
+ _outputBuffer,
169
+ _maxOutputSize);
170
+ }
171
+
172
+ @JRubyMethod(required = 4, module = true)
173
+ public static IRubyObject compressHC_raw(
174
+ ThreadContext context,
175
+ IRubyObject self,
176
+ IRubyObject[] args) {
177
+
178
+ IRubyObject _input = args[0];
179
+ IRubyObject _inputSize = args[1];
180
+ IRubyObject _outputBuffer = args[2];
181
+ IRubyObject _maxOutputSize = args[3];
182
+
183
+ return compressInternal(context,
184
+ FACTORY.highCompressor(),
185
+ null,
186
+ _input,
187
+ _inputSize,
188
+ _outputBuffer,
189
+ _maxOutputSize);
190
+ }
191
+
192
+ @JRubyMethod(required = 4, module = true)
193
+ public static IRubyObject decompress_raw(
194
+ ThreadContext context,
195
+ IRubyObject self,
196
+ IRubyObject[] args) {
197
+
198
+ IRubyObject _input = args[0];
199
+ IRubyObject _inputSize = args[1];
200
+ IRubyObject _outputBuffer = args[2];
201
+ IRubyObject _maxOutputSize = args[3];
202
+
203
+ return decompressInternal(
204
+ context,
205
+ null,
206
+ _input,
207
+ _inputSize,
208
+ _outputBuffer,
209
+ _maxOutputSize);
210
+ }
211
+
212
+ static class ByteInput {
213
+ public final RubyString rubyString;
214
+ public final ByteList byteList;
215
+ public final byte[] unsafeBytes;
216
+ public final int offset;
217
+ public final int realSize;
218
+
219
+ public ByteInput(IRubyObject buffer) {
220
+ rubyString = buffer.convertToString();
221
+ byteList = rubyString.getByteList();
222
+ unsafeBytes = byteList.getUnsafeBytes();
223
+ offset = byteList.getBegin();
224
+ realSize = byteList.getRealSize();
225
+ }
226
+ }
227
+
228
+ static class ByteOutput {
229
+ public final RubyString rubyString;
230
+ public final ByteList byteList;
231
+ public final byte[] unsafeBytes;
232
+ public final int offset;
233
+ public final int bufferSize;
234
+
235
+ public ByteOutput(IRubyObject buffer, IRubyObject size) {
236
+ bufferSize = intFrom(size);
237
+ rubyString = buffer.convertToString();
238
+ byteList = rubyString.getByteList();
239
+ unsafeBytes = byteList.getUnsafeBytes();
240
+ offset = byteList.getBegin();
241
+ }
242
+
243
+ public ByteOutput(int size) {
244
+ bufferSize = size;
245
+ rubyString = null;
246
+ byteList = null;
247
+ unsafeBytes = new byte[bufferSize];
248
+ offset = 0;
249
+ }
250
+
251
+ RubyString toRubyString(ThreadContext context, int length) {
252
+ if (rubyString != null) {
253
+ return rubyString;
254
+ }
255
+
256
+ return RubyString.newString(context.runtime, unsafeBytes, 0, length);
257
+ }
258
+ }
259
+
260
+ /**
261
+ * Test if specified IRubyObject is not null / nil.
262
+ *
263
+ * @param obj
264
+ * @return
265
+ */
266
+ private static boolean isNotNil(IRubyObject obj) {
267
+ return obj != null && !obj.isNil();
268
+ }
269
+
270
+ /**
271
+ * Returns a integer value from specified IRubyObject.
272
+ *
273
+ * @param obj
274
+ * @return
275
+ */
276
+ private static int intFrom(IRubyObject obj) {
277
+ RubyInteger rubyInt = obj.convertToInteger();
278
+ return (int) rubyInt.getLongValue();
63
279
  }
64
280
  }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lz4-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: java
6
6
  authors:
7
7
  - KOMIYA Atsushi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-10 00:00:00.000000000 Z
11
+ date: 2014-04-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec