protobuf_java_helpers 0.1.1-java → 0.2.0-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ext/ruby_protobuf/Int32ProtobufField.java +1 -1
- data/ext/ruby_protobuf/Int64ProtobufField.java +9 -14
- data/ext/ruby_protobuf/IntegerProtobufField.java +90 -0
- data/ext/ruby_protobuf/Sint64ProtobufField.java +1 -1
- data/ext/ruby_protobuf/Uint32ProtobufField.java +1 -1
- data/ext/ruby_protobuf/Uint64ProtobufField.java +1 -1
- data/ext/ruby_protobuf/Varint.java +203 -0
- data/ext/ruby_protobuf/Varinter.java +15 -88
- data/ext/ruby_protobuf/VarinterService.java +3 -0
- data/lib/jars/protobuf_java_helpers.jar +0 -0
- data/lib/protobuf_java_helpers.rb +29 -1
- data/lib/protobuf_java_helpers/version.rb +1 -1
- data/protobuf_java_helpers.gemspec +1 -0
- metadata +18 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5dbe390930dc3f5880b318fae77f1d18783b875e
|
4
|
+
data.tar.gz: d67af2c6dfba079537fbecba804268f57ff7a8ae
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 65fe02b012cdaf484e043e0e282eb9d886e14b8468efbd3a0eb5cac1e84e77b6696d1e6894f26027ea0b2ac0d26345eed3b1f3f7cefe216172c576dfa93c6ccc
|
7
|
+
data.tar.gz: 06cda872d96c48edc94d7809dc2e34beda699adcf4b2c07973375ef987d2e43847d32ab07748880d2b66c5c1926f98ced0dadddeb90094c2ea1102c1cefa2833
|
@@ -15,7 +15,7 @@ import org.jruby.runtime.ThreadContext;
|
|
15
15
|
import org.jruby.runtime.builtin.IRubyObject;
|
16
16
|
|
17
17
|
public class Int32ProtobufField {
|
18
|
-
private static final long MIN =
|
18
|
+
private static final long MIN = (long)java.lang.Math.pow(-2, 31);
|
19
19
|
private static final long MAX = ((long)java.lang.Math.pow(2, 31) - 1);
|
20
20
|
private static String TYPE = "Int32";
|
21
21
|
|
@@ -17,12 +17,15 @@ import org.jruby.runtime.builtin.IRubyObject;
|
|
17
17
|
public class Int64ProtobufField {
|
18
18
|
private static final long MIN = ((long)java.lang.Math.pow(-2, 63));
|
19
19
|
private static final long MAX = ((long)java.lang.Math.pow(2, 63) - 1);
|
20
|
-
private static String TYPE = "
|
20
|
+
private static String TYPE = "Int64";
|
21
|
+
|
22
|
+
private static boolean internal_acceptable(long value) {
|
23
|
+
return value >= MIN && value <= MAX;
|
24
|
+
}
|
21
25
|
|
22
26
|
private static boolean internal_acceptable(IRubyObject recv) {
|
23
|
-
if (recv instanceof RubyInteger || recv instanceof RubyFixnum) {
|
24
|
-
|
25
|
-
return value >= MIN && value <= MAX;
|
27
|
+
if (recv instanceof RubyInteger || recv instanceof RubyFixnum || recv instanceof RubyNumeric || recv instanceof RubyFloat) {
|
28
|
+
return internal_acceptable(((RubyNumeric) recv).getLongValue());
|
26
29
|
}
|
27
30
|
|
28
31
|
return false;
|
@@ -46,15 +49,7 @@ public class Int64ProtobufField {
|
|
46
49
|
if (!internal_acceptable(recv)) {
|
47
50
|
throw runtime.newTypeError("can't convert " + recv.getMetaClass() + " into " + TYPE);
|
48
51
|
}
|
49
|
-
|
50
|
-
|
51
|
-
return ((RubyInteger)recv).to_i();
|
52
|
-
}
|
53
|
-
|
54
|
-
if (recv instanceof RubyFixnum) {
|
55
|
-
return ((RubyFixnum)recv).to_i();
|
56
|
-
}
|
57
|
-
|
58
|
-
return org.jruby.util.TypeConverter.convertToInteger(context, recv, 10);
|
52
|
+
|
53
|
+
return context.getRuntime().newFixnum(((RubyNumeric) recv).getLongValue());
|
59
54
|
}
|
60
55
|
}
|
@@ -0,0 +1,90 @@
|
|
1
|
+
package com.abrandoned.protobuf_java_helpers;
|
2
|
+
|
3
|
+
import java.lang.Math;
|
4
|
+
import org.jruby.Ruby;
|
5
|
+
import org.jruby.RubyBignum;
|
6
|
+
import org.jruby.RubyComplex;
|
7
|
+
import org.jruby.RubyFixnum;
|
8
|
+
import org.jruby.RubyFloat;
|
9
|
+
import org.jruby.RubyInteger;
|
10
|
+
import org.jruby.RubyNumeric;
|
11
|
+
import org.jruby.RubyString;
|
12
|
+
import java.math.BigInteger;
|
13
|
+
import org.jruby.util.TypeConverter;
|
14
|
+
import org.jruby.anno.JRubyMethod;
|
15
|
+
import org.jruby.runtime.ThreadContext;
|
16
|
+
import org.jruby.runtime.builtin.IRubyObject;
|
17
|
+
|
18
|
+
public class IntegerProtobufField {
|
19
|
+
private static final long MIN = ((long)java.lang.Math.pow(-2, 63));
|
20
|
+
private static final long MAX = ((long)java.lang.Math.pow(2, 63) - 1);
|
21
|
+
private static String TYPE = "Integer";
|
22
|
+
|
23
|
+
private static boolean internal_acceptable(long value) {
|
24
|
+
return value >= MIN && value <= MAX;
|
25
|
+
}
|
26
|
+
|
27
|
+
private static boolean internal_acceptable(IRubyObject recv) {
|
28
|
+
if (recv instanceof RubyInteger || recv instanceof RubyFixnum || recv instanceof RubyNumeric || recv instanceof RubyFloat) {
|
29
|
+
return internal_acceptable(((RubyFixnum) recv).getLongValue());
|
30
|
+
}
|
31
|
+
|
32
|
+
return false;
|
33
|
+
}
|
34
|
+
|
35
|
+
@JRubyMethod(name = "acceptable?")
|
36
|
+
public static IRubyObject acceptable_p( ThreadContext context, IRubyObject self, IRubyObject recv ) {
|
37
|
+
org.jruby.Ruby runtime = context.getRuntime();
|
38
|
+
|
39
|
+
if (internal_acceptable(recv)) {
|
40
|
+
return runtime.getTrue();
|
41
|
+
}
|
42
|
+
|
43
|
+
return runtime.getFalse();
|
44
|
+
}
|
45
|
+
|
46
|
+
@JRubyMethod(name = "coerce!")
|
47
|
+
public static IRubyObject coerce_bang( ThreadContext context, IRubyObject self, IRubyObject recv ) {
|
48
|
+
org.jruby.Ruby runtime = context.getRuntime();
|
49
|
+
|
50
|
+
if (!internal_acceptable(recv)) {
|
51
|
+
throw runtime.newTypeError("can't convert " + recv.getMetaClass() + " into " + TYPE);
|
52
|
+
}
|
53
|
+
|
54
|
+
if (recv instanceof RubyInteger) {
|
55
|
+
return ((RubyInteger)recv).to_i();
|
56
|
+
}
|
57
|
+
|
58
|
+
if (recv instanceof RubyFixnum) {
|
59
|
+
return ((RubyFixnum)recv).to_i();
|
60
|
+
}
|
61
|
+
|
62
|
+
return org.jruby.util.TypeConverter.convertToInteger(context, recv, 10);
|
63
|
+
}
|
64
|
+
|
65
|
+
@JRubyMethod
|
66
|
+
public static IRubyObject decode_varint_64( ThreadContext context, IRubyObject self, IRubyObject recv ) {
|
67
|
+
if (!(recv instanceof RubyInteger || recv instanceof RubyFixnum || recv instanceof RubyNumeric || recv instanceof RubyFloat)) {
|
68
|
+
System.out.println("DERP");
|
69
|
+
return context.nil;
|
70
|
+
}
|
71
|
+
|
72
|
+
long value = ((RubyNumeric) recv).getLongValue();
|
73
|
+
org.jruby.Ruby runtime = context.getRuntime();
|
74
|
+
|
75
|
+
if ((value & 0x8000000000000000L) != 0L) {
|
76
|
+
java.math.BigInteger big_value = java.math.BigInteger.valueOf(value);
|
77
|
+
java.math.BigInteger big_minus = java.math.BigInteger.ONE.shiftLeft(16);
|
78
|
+
big_value = big_value.subtract(big_minus);
|
79
|
+
|
80
|
+
value = big_value.longValue();
|
81
|
+
}
|
82
|
+
|
83
|
+
if (internal_acceptable(value)) {
|
84
|
+
return context.getRuntime().newFixnum(value);
|
85
|
+
} else {
|
86
|
+
System.out.println("DERP2");
|
87
|
+
return context.nil;
|
88
|
+
}
|
89
|
+
}
|
90
|
+
}
|
@@ -17,7 +17,7 @@ import org.jruby.runtime.builtin.IRubyObject;
|
|
17
17
|
public class Sint64ProtobufField {
|
18
18
|
private static final long MIN = ((long)java.lang.Math.pow(-2, 63));
|
19
19
|
private static final long MAX = ((long)java.lang.Math.pow(2, 63) - 1);
|
20
|
-
private static String TYPE = "
|
20
|
+
private static String TYPE = "Sint64";
|
21
21
|
|
22
22
|
private static boolean internal_acceptable(IRubyObject recv) {
|
23
23
|
if (recv instanceof RubyInteger || recv instanceof RubyFixnum) {
|
@@ -16,7 +16,7 @@ import org.jruby.runtime.builtin.IRubyObject;
|
|
16
16
|
|
17
17
|
public class Uint32ProtobufField {
|
18
18
|
private static final long MIN = 0L;
|
19
|
-
private static final long MAX = ((long)java.lang.Math.pow(2,
|
19
|
+
private static final long MAX = ((long)java.lang.Math.pow(2, 32) - 1);
|
20
20
|
private static String TYPE = "Uint32";
|
21
21
|
|
22
22
|
private static boolean internal_acceptable(IRubyObject recv) {
|
@@ -17,7 +17,7 @@ import org.jruby.runtime.builtin.IRubyObject;
|
|
17
17
|
public class Uint64ProtobufField {
|
18
18
|
private static final long MIN = 0L;
|
19
19
|
private static final long MAX = ((long)java.lang.Math.pow(2, 64) - 1);
|
20
|
-
private static String TYPE = "
|
20
|
+
private static String TYPE = "Uint64";
|
21
21
|
|
22
22
|
private static boolean internal_acceptable(IRubyObject recv) {
|
23
23
|
if (recv instanceof RubyInteger || recv instanceof RubyFixnum) {
|
@@ -0,0 +1,203 @@
|
|
1
|
+
package com.abrandoned.protobuf_java_helpers;
|
2
|
+
|
3
|
+
/**
|
4
|
+
* Licensed to the Apache Software Foundation (ASF) under one or more
|
5
|
+
* contributor license agreements. See the NOTICE file distributed with
|
6
|
+
* this work for additional information regarding copyright ownership.
|
7
|
+
* The ASF licenses this file to You under the Apache License, Version 2.0
|
8
|
+
* (the "License"); you may not use this file except in compliance with
|
9
|
+
* the License. You may obtain a copy of the License at
|
10
|
+
*
|
11
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
*
|
13
|
+
* Unless required by applicable law or agreed to in writing, software
|
14
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16
|
+
* See the License for the specific language governing permissions and
|
17
|
+
* limitations under the License.
|
18
|
+
*/
|
19
|
+
import java.io.DataInput;
|
20
|
+
import java.io.DataOutput;
|
21
|
+
import java.io.IOException;
|
22
|
+
|
23
|
+
/**
|
24
|
+
* <p>Encodes signed and unsigned values using a common variable-length
|
25
|
+
* scheme, found for example in
|
26
|
+
* <a href="http://code.google.com/apis/protocolbuffers/docs/encoding.html">
|
27
|
+
* Google's Protocol Buffers</a>. It uses fewer bytes to encode smaller values,
|
28
|
+
* but will use slightly more bytes to encode large values.</p>
|
29
|
+
* <p/>
|
30
|
+
* <p>Signed values are further encoded using so-called zig-zag encoding
|
31
|
+
* in order to make them "compatible" with variable-length encoding.</p>
|
32
|
+
*/
|
33
|
+
public final class Varint {
|
34
|
+
|
35
|
+
private Varint() {
|
36
|
+
}
|
37
|
+
|
38
|
+
/**
|
39
|
+
* Encodes a value using the variable-length encoding from
|
40
|
+
* <a href="http://code.google.com/apis/protocolbuffers/docs/encoding.html">
|
41
|
+
* Google Protocol Buffers</a>. It uses zig-zag encoding to efficiently
|
42
|
+
* encode signed values. If values are known to be nonnegative,
|
43
|
+
* {@link #writeUnsignedVarLong(long, DataOutput)} should be used.
|
44
|
+
*
|
45
|
+
* @param value value to encode
|
46
|
+
* @param out to write bytes to
|
47
|
+
* @throws IOException if {@link DataOutput} throws {@link IOException}
|
48
|
+
*/
|
49
|
+
public static void writeSignedVarLong(long value, DataOutput out) throws IOException {
|
50
|
+
// Great trick from http://code.google.com/apis/protocolbuffers/docs/encoding.html#types
|
51
|
+
writeUnsignedVarLong((value << 1) ^ (value >> 63), out);
|
52
|
+
}
|
53
|
+
|
54
|
+
/**
|
55
|
+
* Encodes a value using the variable-length encoding from
|
56
|
+
* <a href="http://code.google.com/apis/protocolbuffers/docs/encoding.html">
|
57
|
+
* Google Protocol Buffers</a>. Zig-zag is not used, so input must not be negative.
|
58
|
+
* If values can be negative, use {@link #writeSignedVarLong(long, DataOutput)}
|
59
|
+
* instead. This method treats negative input as like a large unsigned value.
|
60
|
+
*
|
61
|
+
* @param value value to encode
|
62
|
+
* @param out to write bytes to
|
63
|
+
* @throws IOException if {@link DataOutput} throws {@link IOException}
|
64
|
+
*/
|
65
|
+
public static void writeUnsignedVarLong(long value, DataOutput out) throws IOException {
|
66
|
+
while ((value & 0xFFFFFFFFFFFFFF80L) != 0L) {
|
67
|
+
out.writeByte(((int) value & 0x7F) | 0x80);
|
68
|
+
value >>>= 7;
|
69
|
+
}
|
70
|
+
out.writeByte((int) value & 0x7F);
|
71
|
+
}
|
72
|
+
|
73
|
+
public static void writeUnsignedVarLong2(long value, DataOutput out) throws IOException {
|
74
|
+
if ((value & 0xFFFFFFFFFFFFFF80L) != 0L) {
|
75
|
+
out.writeByte(((int) value & 0x7F) | 0x80);
|
76
|
+
value >>>= 7;
|
77
|
+
if ((value & 0xFFFFFFFFFFFFFF80L) != 0L) {
|
78
|
+
out.writeByte(((int) value & 0x7F) | 0x80);
|
79
|
+
value >>>= 7;
|
80
|
+
if ((value & 0xFFFFFFFFFFFFFF80L) != 0L) {
|
81
|
+
out.writeByte(((int) value & 0x7F) | 0x80);
|
82
|
+
value >>>= 7;
|
83
|
+
if ((value & 0xFFFFFFFFFFFFFF80L) != 0L) {
|
84
|
+
out.writeByte(((int) value & 0x7F) | 0x80);
|
85
|
+
value >>>= 7;
|
86
|
+
if ((value & 0xFFFFFFFFFFFFFF80L) != 0L) {
|
87
|
+
out.writeByte(((int) value & 0x7F) | 0x80);
|
88
|
+
value >>>= 7;
|
89
|
+
if ((value & 0xFFFFFFFFFFFFFF80L) != 0L) {
|
90
|
+
out.writeByte(((int) value & 0x7F) | 0x80);
|
91
|
+
value >>>= 7;
|
92
|
+
if ((value & 0xFFFFFFFFFFFFFF80L) != 0L) {
|
93
|
+
out.writeByte(((int) value & 0x7F) | 0x80);
|
94
|
+
value >>>= 7;
|
95
|
+
if ((value & 0xFFFFFFFFFFFFFF80L) != 0L) {
|
96
|
+
out.writeByte(((int) value & 0x7F) | 0x80);
|
97
|
+
value >>>= 7;
|
98
|
+
if ((value & 0xFFFFFFFFFFFFFF80L) != 0L) {
|
99
|
+
out.writeByte(((int) value & 0x7F) | 0x80);
|
100
|
+
value >>>= 7;
|
101
|
+
}
|
102
|
+
}
|
103
|
+
}
|
104
|
+
}
|
105
|
+
}
|
106
|
+
}
|
107
|
+
}
|
108
|
+
}
|
109
|
+
}
|
110
|
+
|
111
|
+
out.writeByte((int) value & 0x7F);
|
112
|
+
}
|
113
|
+
|
114
|
+
/**
|
115
|
+
* @see #writeSignedVarLong(long, DataOutput)
|
116
|
+
*/
|
117
|
+
public static void writeSignedVarInt(int value, DataOutput out) throws IOException {
|
118
|
+
// Great trick from http://code.google.com/apis/protocolbuffers/docs/encoding.html#types
|
119
|
+
writeUnsignedVarInt((value << 1) ^ (value >> 31), out);
|
120
|
+
}
|
121
|
+
|
122
|
+
/**
|
123
|
+
* @see #writeUnsignedVarLong(long, DataOutput)
|
124
|
+
*/
|
125
|
+
public static void writeUnsignedVarInt(int value, DataOutput out) throws IOException {
|
126
|
+
while ((value & 0xFFFFFF80) != 0L) {
|
127
|
+
out.writeByte((value & 0x7F) | 0x80);
|
128
|
+
value >>>= 7;
|
129
|
+
}
|
130
|
+
out.writeByte(value & 0x7F);
|
131
|
+
}
|
132
|
+
|
133
|
+
/**
|
134
|
+
* @param in to read bytes from
|
135
|
+
* @return decode value
|
136
|
+
* @throws IOException if {@link DataInput} throws {@link IOException}
|
137
|
+
* @throws IllegalArgumentException if variable-length value does not terminate
|
138
|
+
* after 9 bytes have been read
|
139
|
+
* @see #writeSignedVarLong(long, DataOutput)
|
140
|
+
*/
|
141
|
+
public static long readSignedVarLong(DataInput in) throws IOException {
|
142
|
+
long raw = readUnsignedVarLong(in);
|
143
|
+
// This undoes the trick in writeSignedVarLong()
|
144
|
+
long temp = (((raw << 63) >> 63) ^ raw) >> 1;
|
145
|
+
// This extra step lets us deal with the largest signed values by treating
|
146
|
+
// negative results from read unsigned methods as like unsigned values
|
147
|
+
// Must re-flip the top bit if the original read value had it set.
|
148
|
+
return temp ^ (raw & (1L << 63));
|
149
|
+
}
|
150
|
+
|
151
|
+
/**
|
152
|
+
* @param in to read bytes from
|
153
|
+
* @return decode value
|
154
|
+
* @throws IOException if {@link DataInput} throws {@link IOException}
|
155
|
+
* @throws IllegalArgumentException if variable-length value does not terminate
|
156
|
+
* after 9 bytes have been read
|
157
|
+
* @see #writeUnsignedVarLong(long, DataOutput)
|
158
|
+
*/
|
159
|
+
public static long readUnsignedVarLong(DataInput in) throws IOException {
|
160
|
+
long value = 0L;
|
161
|
+
int i = 0;
|
162
|
+
long b;
|
163
|
+
while (((b = in.readByte()) & 0x80L) != 0) {
|
164
|
+
value |= (b & 0x7F) << i;
|
165
|
+
i += 7;
|
166
|
+
}
|
167
|
+
return value | (b << i);
|
168
|
+
}
|
169
|
+
|
170
|
+
/**
|
171
|
+
* @throws IllegalArgumentException if variable-length value does not terminate
|
172
|
+
* after 5 bytes have been read
|
173
|
+
* @throws IOException if {@link DataInput} throws {@link IOException}
|
174
|
+
* @see #readSignedVarLong(DataInput)
|
175
|
+
*/
|
176
|
+
public static int readSignedVarInt(DataInput in) throws IOException {
|
177
|
+
int raw = readUnsignedVarInt(in);
|
178
|
+
// This undoes the trick in writeSignedVarInt()
|
179
|
+
int temp = (((raw << 31) >> 31) ^ raw) >> 1;
|
180
|
+
// This extra step lets us deal with the largest signed values by treating
|
181
|
+
// negative results from read unsigned methods as like unsigned values.
|
182
|
+
// Must re-flip the top bit if the original read value had it set.
|
183
|
+
return temp ^ (raw & (1 << 31));
|
184
|
+
}
|
185
|
+
|
186
|
+
|
187
|
+
/**
|
188
|
+
* @throws IllegalArgumentException if variable-length value does not terminate
|
189
|
+
* after 5 bytes have been read
|
190
|
+
* @throws IOException if {@link DataInput} throws {@link IOException}
|
191
|
+
* @see #readUnsignedVarLong(DataInput)
|
192
|
+
*/
|
193
|
+
public static int readUnsignedVarInt(DataInput in) throws IOException {
|
194
|
+
int value = 0;
|
195
|
+
int i = 0;
|
196
|
+
int b;
|
197
|
+
while (((b = in.readByte()) & 0x80) != 0) {
|
198
|
+
value |= (b & 0x7F) << i;
|
199
|
+
i += 7;
|
200
|
+
}
|
201
|
+
return value | (b << i);
|
202
|
+
}
|
203
|
+
}
|
@@ -2,13 +2,17 @@ package com.abrandoned.protobuf_java_helpers;
|
|
2
2
|
|
3
3
|
import java.io.IOException;
|
4
4
|
import java.io.InputStream;
|
5
|
+
import java.io.OutputStream;;
|
6
|
+
import java.io.DataOutput;
|
7
|
+
import java.io.DataInput;
|
8
|
+
import java.io.DataInputStream;
|
9
|
+
import java.io.DataOutputStream;
|
5
10
|
import java.io.ByteArrayOutputStream;
|
6
11
|
import java.lang.Math;
|
7
|
-
import java.
|
8
|
-
import java.util.concurrent.ConcurrentHashMap;
|
9
|
-
import java.util.function.Function;
|
12
|
+
import java.math.BigInteger;
|
10
13
|
import org.jcodings.specific.ASCIIEncoding;
|
11
14
|
import org.jruby.Ruby;
|
15
|
+
import org.jruby.RubyArray;
|
12
16
|
import org.jruby.RubyBignum;
|
13
17
|
import org.jruby.RubyComplex;
|
14
18
|
import org.jruby.RubyFixnum;
|
@@ -17,104 +21,27 @@ import org.jruby.RubyInteger;
|
|
17
21
|
import org.jruby.RubyNumeric;
|
18
22
|
import org.jruby.RubyString;
|
19
23
|
import org.jruby.RubyIO;
|
20
|
-
import org.jruby.
|
24
|
+
import org.jruby.util.IOInputStream;
|
21
25
|
import org.jruby.util.ByteList;
|
22
26
|
import org.jruby.anno.JRubyMethod;
|
23
27
|
import org.jruby.runtime.ThreadContext;
|
24
28
|
import org.jruby.runtime.builtin.IRubyObject;
|
25
29
|
|
26
30
|
public class Varinter {
|
27
|
-
private static final long INT32_MAX = ((long)java.lang.Math.pow(2, 31) - 1);
|
28
|
-
|
29
|
-
private static byte[] internal_to_varint(long value) {
|
30
|
-
java.io.ByteArrayOutputStream bytes = new java.io.ByteArrayOutputStream();
|
31
|
-
|
32
|
-
while (value > 0x7f) {
|
33
|
-
bytes.write((byte) (0x80 | (value & 0x7f)));
|
34
|
-
value >>= 7;
|
35
|
-
}
|
36
|
-
|
37
|
-
bytes.write((byte) value);
|
38
|
-
return bytes.toByteArray();
|
39
|
-
}
|
40
|
-
|
41
|
-
@JRubyMethod(name = "acceptable?")
|
42
|
-
public static IRubyObject acceptable_p( ThreadContext context, IRubyObject self, IRubyObject recv ) {
|
43
|
-
org.jruby.Ruby runtime = context.getRuntime();
|
44
|
-
|
45
|
-
if (recv instanceof RubyInteger || recv instanceof RubyFixnum) {
|
46
|
-
long value = ((RubyFixnum) recv).getLongValue();
|
47
|
-
|
48
|
-
if (value >= 0 && value <= INT32_MAX) {
|
49
|
-
return runtime.getTrue();
|
50
|
-
} else {
|
51
|
-
return runtime.getFalse();
|
52
|
-
}
|
53
|
-
}
|
54
|
-
|
55
|
-
return runtime.getFalse();
|
56
|
-
}
|
57
|
-
|
58
31
|
@JRubyMethod
|
59
|
-
public static IRubyObject to_varint( ThreadContext context, IRubyObject self, IRubyObject recv ) {
|
60
|
-
if (!(recv instanceof RubyInteger || recv instanceof RubyFixnum)) {
|
32
|
+
public static IRubyObject to_varint( ThreadContext context, IRubyObject self, IRubyObject recv ) throws IOException {
|
33
|
+
if (recv instanceof RubyBignum || !(recv instanceof RubyInteger || recv instanceof RubyFixnum || recv instanceof RubyNumeric || recv instanceof RubyFloat)) {
|
61
34
|
return context.nil;
|
62
35
|
}
|
63
36
|
|
64
|
-
long value = ((
|
37
|
+
long value = ((RubyNumeric) recv).getLongValue();
|
65
38
|
org.jruby.Ruby runtime = context.getRuntime();
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
}
|
39
|
+
ByteArrayOutputStream backing = new ByteArrayOutputStream();
|
40
|
+
DataOutput data_output = new DataOutputStream(backing);
|
41
|
+
com.abrandoned.protobuf_java_helpers.Varint.writeUnsignedVarLong2(value, data_output);
|
70
42
|
|
71
|
-
|
72
|
-
public static IRubyObject to_varint_64( ThreadContext context, IRubyObject self, IRubyObject recv ) {
|
73
|
-
if (!(recv instanceof RubyInteger || recv instanceof RubyFixnum)) {
|
74
|
-
return context.nil;
|
75
|
-
}
|
76
|
-
|
77
|
-
long value = ((RubyFixnum) recv).getLongValue();
|
78
|
-
org.jruby.Ruby runtime = context.getRuntime();
|
79
|
-
RubyString bit_string = runtime.newString(new ByteList(internal_to_varint(value & 0xffffffffffffffffL), true));
|
43
|
+
RubyString bit_string = runtime.newString(new ByteList(backing.toByteArray(), true));
|
80
44
|
bit_string.force_encoding(context, runtime.getEncodingService().getEncoding(org.jcodings.specific.ASCIIEncoding.INSTANCE));
|
81
45
|
return bit_string;
|
82
46
|
}
|
83
|
-
|
84
|
-
@JRubyMethod
|
85
|
-
public static IRubyObject read_varint(ThreadContext context, IRubyObject self, IRubyObject recv ) throws IOException {
|
86
|
-
long value = 0L;
|
87
|
-
int index = 0;
|
88
|
-
long current_byte;
|
89
|
-
|
90
|
-
if (recv instanceof StringIO) {
|
91
|
-
StringIO current_recv = ((StringIO)recv);
|
92
|
-
RubyFixnum fixnum;
|
93
|
-
|
94
|
-
do {
|
95
|
-
fixnum = ((RubyFixnum)(current_recv.getbyte(context)));
|
96
|
-
current_byte = fixnum.getLongValue();
|
97
|
-
if (index == 0 && current_byte < 128) { return context.getRuntime().newFixnum(current_byte); }
|
98
|
-
value = (value | ((current_byte & 0x7f) << (7 * index)));
|
99
|
-
index++;
|
100
|
-
} while ((current_byte & 0x80) != 0);
|
101
|
-
|
102
|
-
return context.getRuntime().newFixnum(value);
|
103
|
-
}
|
104
|
-
|
105
|
-
if (recv instanceof RubyIO) {
|
106
|
-
RubyIO current_recv = ((RubyIO)recv);
|
107
|
-
|
108
|
-
do {
|
109
|
-
current_byte = current_recv.getByte(context);
|
110
|
-
if (index == 0 && current_byte < 128) { return context.getRuntime().newFixnum(current_byte); }
|
111
|
-
value = (value | ((current_byte & 0x7f) << (7 * index)));
|
112
|
-
index++;
|
113
|
-
} while ((current_byte & 0x80) != 0);
|
114
|
-
|
115
|
-
return context.getRuntime().newFixnum(value);
|
116
|
-
}
|
117
|
-
|
118
|
-
return context.nil;
|
119
|
-
}
|
120
47
|
}
|
@@ -15,6 +15,9 @@ public class VarinterService implements BasicLibraryService {
|
|
15
15
|
RubyModule varinter = protobuf_java_helpers.defineModuleUnder(Varinter.class.getSimpleName());
|
16
16
|
varinter.defineAnnotatedMethods(Varinter.class);
|
17
17
|
|
18
|
+
RubyModule integer_protobuf = protobuf_java_helpers.defineModuleUnder(IntegerProtobufField.class.getSimpleName());
|
19
|
+
integer_protobuf.defineAnnotatedMethods(IntegerProtobufField.class);
|
20
|
+
|
18
21
|
RubyModule uint64_protobuf = protobuf_java_helpers.defineModuleUnder(Uint64ProtobufField.class.getSimpleName());
|
19
22
|
uint64_protobuf.defineAnnotatedMethods(Uint64ProtobufField.class);
|
20
23
|
|
Binary file
|
@@ -3,8 +3,36 @@ require "jruby"
|
|
3
3
|
require "protobuf_java_helpers/version"
|
4
4
|
|
5
5
|
module ProtobufJavaHelpers
|
6
|
-
# Your code goes here...
|
7
6
|
end
|
8
7
|
|
9
8
|
require "jars/protobuf_java_helpers.jar"
|
10
9
|
com.abrandoned.protobuf_java_helpers.VarinterService.new.basicLoad(::JRuby.runtime)
|
10
|
+
|
11
|
+
module ProtobufJavaHelpers
|
12
|
+
module EncodeDecode
|
13
|
+
include ::ProtobufJavaHelpers::Varinter
|
14
|
+
|
15
|
+
def pure_encode(value)
|
16
|
+
bytes = []
|
17
|
+
until value < 128
|
18
|
+
bytes << (0x80 | (value & 0x7f))
|
19
|
+
value >>= 7
|
20
|
+
end
|
21
|
+
(bytes << value).pack('C*')
|
22
|
+
end
|
23
|
+
|
24
|
+
def encode(value)
|
25
|
+
to_varint(value) || pure_encode(value)
|
26
|
+
end
|
27
|
+
|
28
|
+
def decode(stream)
|
29
|
+
value = index = 0
|
30
|
+
begin
|
31
|
+
byte = stream.readbyte
|
32
|
+
value |= (byte & 0x7f) << (7 * index)
|
33
|
+
index += 1
|
34
|
+
end while (byte & 0x80).nonzero?
|
35
|
+
value
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -33,6 +33,7 @@ Gem::Specification.new do |spec|
|
|
33
33
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
34
34
|
spec.require_paths = ["lib"]
|
35
35
|
|
36
|
+
spec.add_development_dependency "benchmark-ips"
|
36
37
|
spec.add_development_dependency "bundler", "~> 1.16"
|
37
38
|
spec.add_development_dependency "rake", "~> 10.0"
|
38
39
|
spec.add_development_dependency "minitest", "~> 5.0"
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: protobuf_java_helpers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: java
|
6
6
|
authors:
|
7
7
|
- Brandon Dewitt
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-01-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - ">="
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: '0'
|
19
|
+
name: benchmark-ips
|
20
|
+
prerelease: false
|
21
|
+
type: :development
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
requirement: !ruby/object:Gem::Requirement
|
15
29
|
requirements:
|
@@ -84,10 +98,12 @@ files:
|
|
84
98
|
- bin/setup
|
85
99
|
- ext/ruby_protobuf/Int32ProtobufField.java
|
86
100
|
- ext/ruby_protobuf/Int64ProtobufField.java
|
101
|
+
- ext/ruby_protobuf/IntegerProtobufField.java
|
87
102
|
- ext/ruby_protobuf/Sint32ProtobufField.java
|
88
103
|
- ext/ruby_protobuf/Sint64ProtobufField.java
|
89
104
|
- ext/ruby_protobuf/Uint32ProtobufField.java
|
90
105
|
- ext/ruby_protobuf/Uint64ProtobufField.java
|
106
|
+
- ext/ruby_protobuf/Varint.java
|
91
107
|
- ext/ruby_protobuf/VarintProtobufField.java
|
92
108
|
- ext/ruby_protobuf/Varinter.java
|
93
109
|
- ext/ruby_protobuf/VarinterService.java
|