msgpack 0.5.9 → 0.5.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- YjA2YWVlMmNiZTg2NGJkNWQ1ZDk5ZGFlM2NlNTVjOTgwNGU4YTgyZA==
4
+ NjBhYjJiYTBhZjk5OGY0ZDdjYmFiOWE3ZDc5ZTQ0MDgzYTU4N2M3ZQ==
5
5
  data.tar.gz: !binary |-
6
- YWYzZGZlNDM5MTEwZmEyMzcyZmU1MmE5ODkzOGM0MTAwOTNhNTM5NQ==
6
+ ODI0N2FmMjRjOTU4ZGUyNDliMTc2OWRkNWE5ZDI0YzYzM2Y4MTQ4Yw==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZmVkYWI4MWRiYWZlYjJjNmQzYzkwNTJiZWMyZTJlN2UzNzJiMGJiZDk3NjRl
10
- NzE1ZWNkMzY3YzQ0NjQxNmQ1NTk1MjBlOTdjMjlkZjA5NTQ1YmYxNjg2MzAw
11
- OGUzN2Q0MzdlMmQxYmYwNmEyZTI5Mjg3Y2Q1ZDkyMzUyYzUwYTY=
9
+ YzBlNTc4N2Y5YjgzNjU2ODVjODY2NDgwYjc3YTViZDlmMGU1YzFiNjExODc2
10
+ YWJhZTNhODU0MjAxMWE1MThjOWU2NGUyZjZhMmVkNDQ0ZWY5NzlmNzE4M2Rl
11
+ NTgyNzVmMDU2MTJlOTM4YzA0ODJhM2JhYWJhYjg3YzNiNGM0ZTM=
12
12
  data.tar.gz: !binary |-
13
- MGJkMzdlODI3ZTQwZWRlNzU2ZmY4YmRkNmUwZDIzYzk5MzY5MTE2NzNjYzQy
14
- ZDNkZWY0NjYyNDQ1ZmU4NDY4NTFhN2EyMzFiZjU2ODJiM2IyNDE5NzY4OTI5
15
- MmE4MWY3ZTEyNjM5ODQ1Y2ZkMjJmM2IyZDU2OGM5NTY5YmUwMzM=
13
+ NzA5MGJjNGM0NGZiMjM2NTU3M2I5Y2I4NzUxYjA3ZDUyNjNlY2JhMmRjOWM1
14
+ MmJjNWNiMGQ0MTUwNjM5ZmVhMjMwYTQ0ZjNiZjg0ZjZjNDAxNTFlNjJkN2Yz
15
+ NjBlMWIxZjY0Yjg5MThlNjgzYzU2OGQyMjljN2E2NGUzN2ViZjE=
data/.gitignore CHANGED
@@ -3,6 +3,7 @@
3
3
  *.bundle
4
4
  *.gem
5
5
  *.class
6
+ *.jar
6
7
  .bundle
7
8
  Gemfile*
8
9
  pkg
@@ -14,5 +15,4 @@ tmp
14
15
  .project
15
16
  .settings
16
17
  /nbproject/private/
17
- ext/java/build
18
- ext/java/msgpack.jar
18
+
@@ -1,14 +1,13 @@
1
1
  language: ruby
2
2
 
3
3
  rvm:
4
- - 1.8.7
4
+ # - 1.8.7
5
5
  - 1.9.3
6
6
  - 2.0.0
7
7
  - 2.1
8
8
  - ruby-head
9
9
  - rbx-2
10
- #- jruby-18mode
11
- #- jruby-19mode
10
+ - jruby-19mode
12
11
 
13
12
  os:
14
13
  - linux
data/ChangeLog CHANGED
@@ -1,4 +1,10 @@
1
1
 
2
+ 2015-01-09 version 0.5.10:
3
+
4
+ * Merged msgpack-jruby by @iconara. JRuby can run `require 'msgpack'` to use
5
+ msgpack-jruby.
6
+
7
+
2
8
  2014-10-05 version 0.5.9:
3
9
 
4
10
  * Fixed Unpacker#read_map_header and #read_array_header
data/Rakefile CHANGED
@@ -30,8 +30,12 @@ if RUBY_PLATFORM =~ /java/
30
30
 
31
31
  Rake::JavaExtensionTask.new('msgpack', spec) do |ext|
32
32
  ext.ext_dir = 'ext/java'
33
+ jruby_home = RbConfig::CONFIG['prefix']
34
+ jars = ["#{jruby_home}/lib/jruby.jar"]
35
+ ext.classpath = jars.map { |x| File.expand_path(x) }.join(':')
33
36
  ext.lib_dir = File.join(*['lib', 'msgpack', ENV['FAT_DIR']].compact)
34
- ext.classpath = Dir['lib/msgpack/java/*.jar'].map { |x| File.expand_path x }.join ':'
37
+ ext.source_version = '1.6'
38
+ ext.target_version = '1.6'
35
39
  end
36
40
 
37
41
  RSpec::Core::RakeTask.new(:spec) do |t|
@@ -0,0 +1,221 @@
1
+ package org.msgpack.jruby;
2
+
3
+
4
+ import java.nio.ByteBuffer;
5
+
6
+ import org.jruby.Ruby;
7
+ import org.jruby.RubyClass;
8
+ import org.jruby.RubyObject;
9
+ import org.jruby.RubyHash;
10
+ import org.jruby.RubyIO;
11
+ import org.jruby.RubyInteger;
12
+ import org.jruby.runtime.builtin.IRubyObject;
13
+ import org.jruby.anno.JRubyClass;
14
+ import org.jruby.anno.JRubyMethod;
15
+ import org.jruby.runtime.ThreadContext;
16
+ import org.jruby.runtime.ObjectAllocator;
17
+ import org.jruby.util.ByteList;
18
+
19
+ import org.jcodings.Encoding;
20
+
21
+
22
+ @JRubyClass(name="MessagePack::Buffer")
23
+ public class Buffer extends RubyObject {
24
+ private RubyHash options;
25
+ private IRubyObject io;
26
+ private ByteBuffer buffer;
27
+ private boolean writeMode;
28
+ private Encoding binaryEncoding;
29
+
30
+ private static final int CACHE_LINE_SIZE = 64;
31
+ private static final int ARRAY_HEADER_SIZE = 24;
32
+
33
+ public Buffer(Ruby runtime, RubyClass type) {
34
+ super(runtime, type);
35
+ }
36
+
37
+ static class BufferAllocator implements ObjectAllocator {
38
+ public IRubyObject allocate(Ruby runtime, RubyClass type) {
39
+ return new Buffer(runtime, type);
40
+ }
41
+ }
42
+
43
+ @JRubyMethod(name = "initialize", optional = 2)
44
+ public IRubyObject initialize(ThreadContext ctx, IRubyObject[] args) {
45
+ if (args.length > 0) {
46
+ if (args[0].respondsTo("read") && args[0].respondsTo("write")) {
47
+ this.io = args[0];
48
+ }
49
+ if (args[args.length - 1] instanceof RubyHash) {
50
+ this.options = (RubyHash) args[args.length - 1];
51
+ }
52
+ }
53
+ this.buffer = ByteBuffer.allocate(CACHE_LINE_SIZE - ARRAY_HEADER_SIZE);
54
+ this.writeMode = true;
55
+ this.binaryEncoding = ctx.getRuntime().getEncodingService().getAscii8bitEncoding();
56
+ return this;
57
+ }
58
+
59
+ private void ensureRemainingCapacity(int c) {
60
+ if (!writeMode) {
61
+ buffer.compact();
62
+ writeMode = true;
63
+ }
64
+ if (buffer.remaining() < c) {
65
+ int newLength = Math.max(buffer.capacity() + (buffer.capacity() >> 1), buffer.capacity() + c);
66
+ newLength += CACHE_LINE_SIZE - ((ARRAY_HEADER_SIZE + newLength) % CACHE_LINE_SIZE);
67
+ buffer = ByteBuffer.allocate(newLength).put(buffer.array(), 0, buffer.position());
68
+ }
69
+ }
70
+
71
+ private void ensureReadMode() {
72
+ if (writeMode) {
73
+ buffer.flip();
74
+ writeMode = false;
75
+ }
76
+ }
77
+
78
+ private int rawSize() {
79
+ if (writeMode) {
80
+ return buffer.position();
81
+ } else {
82
+ return buffer.limit() - buffer.position();
83
+ }
84
+ }
85
+
86
+ @JRubyMethod(name = "clear")
87
+ public IRubyObject clear(ThreadContext ctx) {
88
+ buffer.clear();
89
+ return ctx.getRuntime().getNil();
90
+ }
91
+
92
+ @JRubyMethod(name = "size")
93
+ public IRubyObject size(ThreadContext ctx) {
94
+ return ctx.getRuntime().newFixnum(rawSize());
95
+ }
96
+
97
+ @JRubyMethod(name = "empty?")
98
+ public IRubyObject isEmpty(ThreadContext ctx) {
99
+ return rawSize() == 0 ? ctx.getRuntime().getTrue() : ctx.getRuntime().getFalse();
100
+ }
101
+
102
+ private IRubyObject bufferWrite(ThreadContext ctx, IRubyObject str) {
103
+ ByteList bytes = str.asString().getByteList();
104
+ int length = bytes.length();
105
+ ensureRemainingCapacity(length);
106
+ buffer.put(bytes.unsafeBytes(), bytes.begin(), length);
107
+ return ctx.getRuntime().newFixnum(length);
108
+
109
+ }
110
+
111
+ @JRubyMethod(name = "write", alias = {"<<"})
112
+ public IRubyObject write(ThreadContext ctx, IRubyObject str) {
113
+ if (io == null) {
114
+ return bufferWrite(ctx, str);
115
+ } else {
116
+ return io.callMethod(ctx, "write", str);
117
+ }
118
+ }
119
+
120
+ private void feed(ThreadContext ctx) {
121
+ if (io != null) {
122
+ bufferWrite(ctx, io.callMethod(ctx, "read"));
123
+ }
124
+ }
125
+
126
+ private IRubyObject readCommon(ThreadContext ctx, IRubyObject[] args, boolean raiseOnUnderflow) {
127
+ feed(ctx);
128
+ int length = rawSize();
129
+ if (args != null && args.length == 1) {
130
+ length = (int) args[0].convertToInteger().getLongValue();
131
+ }
132
+ if (raiseOnUnderflow && rawSize() < length) {
133
+ throw ctx.getRuntime().newEOFError();
134
+ }
135
+ int readLength = Math.min(length, rawSize());
136
+ if (readLength == 0 && length > 0) {
137
+ return ctx.getRuntime().getNil();
138
+ } else if (readLength == 0) {
139
+ return ctx.getRuntime().newString();
140
+ } else {
141
+ ensureReadMode();
142
+ byte[] bytes = new byte[readLength];
143
+ buffer.get(bytes);
144
+ ByteList byteList = new ByteList(bytes, binaryEncoding);
145
+ return ctx.getRuntime().newString(byteList);
146
+ }
147
+ }
148
+
149
+ @JRubyMethod(name = "read", optional = 1)
150
+ public IRubyObject read(ThreadContext ctx, IRubyObject[] args) {
151
+ return readCommon(ctx, args, false);
152
+ }
153
+
154
+ @JRubyMethod(name = "read_all", optional = 1)
155
+ public IRubyObject readAll(ThreadContext ctx, IRubyObject[] args) {
156
+ return readCommon(ctx, args, true);
157
+ }
158
+
159
+ private IRubyObject skipCommon(ThreadContext ctx, IRubyObject _length, boolean raiseOnUnderflow) {
160
+ feed(ctx);
161
+ int length = (int) _length.convertToInteger().getLongValue();
162
+ if (raiseOnUnderflow && rawSize() < length) {
163
+ throw ctx.getRuntime().newEOFError();
164
+ }
165
+ ensureReadMode();
166
+ int skipLength = Math.min(length, rawSize());
167
+ buffer.position(buffer.position() + skipLength);
168
+ return ctx.getRuntime().newFixnum(skipLength);
169
+ }
170
+
171
+ @JRubyMethod(name = "skip")
172
+ public IRubyObject skip(ThreadContext ctx, IRubyObject length) {
173
+ return skipCommon(ctx, length, false);
174
+ }
175
+
176
+ @JRubyMethod(name = "skip_all")
177
+ public IRubyObject skipAll(ThreadContext ctx, IRubyObject length) {
178
+ return skipCommon(ctx, length, true);
179
+ }
180
+
181
+ @JRubyMethod(name = "to_s", alias = {"to_str"})
182
+ public IRubyObject toS(ThreadContext ctx) {
183
+ ensureReadMode();
184
+ int length = buffer.limit() - buffer.position();
185
+ ByteList str = new ByteList(buffer.array(), buffer.position(), length, binaryEncoding, true);
186
+ return ctx.getRuntime().newString(str);
187
+ }
188
+
189
+ @JRubyMethod(name = "to_a")
190
+ public IRubyObject toA(ThreadContext ctx) {
191
+ return ctx.getRuntime().newArray(toS(ctx));
192
+ }
193
+
194
+ @JRubyMethod(name = "io")
195
+ public IRubyObject getIo(ThreadContext ctx) {
196
+ return io == null ? ctx.getRuntime().getNil() : io;
197
+ }
198
+
199
+ @JRubyMethod(name = "flush")
200
+ public IRubyObject flush(ThreadContext ctx) {
201
+ if (io == null) {
202
+ return ctx.getRuntime().getNil();
203
+ } else {
204
+ return io.callMethod(ctx, "flush");
205
+ }
206
+ }
207
+
208
+ @JRubyMethod(name = "close")
209
+ public IRubyObject close(ThreadContext ctx) {
210
+ if (io == null) {
211
+ return ctx.getRuntime().getNil();
212
+ } else {
213
+ return io.callMethod(ctx, "close");
214
+ }
215
+ }
216
+
217
+ @JRubyMethod(name = "write_to")
218
+ public IRubyObject writeTo(ThreadContext ctx, IRubyObject io) {
219
+ return io.callMethod(ctx, "write", readCommon(ctx, null, false));
220
+ }
221
+ }
@@ -0,0 +1,201 @@
1
+ package org.msgpack.jruby;
2
+
3
+
4
+ import java.math.BigInteger;
5
+ import java.nio.ByteBuffer;
6
+ import java.nio.BufferUnderflowException;
7
+ import java.util.Iterator;
8
+ import java.util.Arrays;
9
+
10
+ import org.jruby.Ruby;
11
+ import org.jruby.RubyObject;
12
+ import org.jruby.RubyClass;
13
+ import org.jruby.RubyBignum;
14
+ import org.jruby.RubyString;
15
+ import org.jruby.RubyHash;
16
+ import org.jruby.exceptions.RaiseException;
17
+ import org.jruby.runtime.builtin.IRubyObject;
18
+ import org.jruby.util.ByteList;
19
+
20
+ import org.jcodings.Encoding;
21
+ import org.jcodings.specific.UTF8Encoding;
22
+
23
+ import static org.msgpack.jruby.Types.*;
24
+
25
+
26
+ public class Decoder implements Iterator<IRubyObject> {
27
+ private final Ruby runtime;
28
+ private final Encoding binaryEncoding;
29
+ private final Encoding utf8Encoding;
30
+ private final RubyClass unpackErrorClass;
31
+ private final RubyClass underflowErrorClass;
32
+
33
+ private ByteBuffer buffer;
34
+ private boolean symbolizeKeys;
35
+
36
+ public Decoder(Ruby runtime) {
37
+ this(runtime, new byte[] {}, 0, 0);
38
+ }
39
+
40
+ public Decoder(Ruby runtime, byte[] bytes) {
41
+ this(runtime, bytes, 0, bytes.length);
42
+ }
43
+
44
+ public Decoder(Ruby runtime, byte[] bytes, int offset, int length) {
45
+ this.runtime = runtime;
46
+ this.binaryEncoding = runtime.getEncodingService().getAscii8bitEncoding();
47
+ this.utf8Encoding = UTF8Encoding.INSTANCE;
48
+ this.unpackErrorClass = runtime.getModule("MessagePack").getClass("UnpackError");
49
+ this.underflowErrorClass = runtime.getModule("MessagePack").getClass("UnderflowError");
50
+ feed(bytes, offset, length);
51
+ }
52
+
53
+ public void symbolizeKeys(boolean symbolize) {
54
+ this.symbolizeKeys = symbolize;
55
+ }
56
+
57
+ public void feed(byte[] bytes) {
58
+ feed(bytes, 0, bytes.length);
59
+ }
60
+
61
+ public void feed(byte[] bytes, int offset, int length) {
62
+ if (buffer == null) {
63
+ buffer = ByteBuffer.wrap(bytes, offset, length);
64
+ } else {
65
+ ByteBuffer newBuffer = ByteBuffer.allocate(buffer.remaining() + length);
66
+ newBuffer.put(buffer);
67
+ newBuffer.put(bytes, offset, length);
68
+ newBuffer.flip();
69
+ buffer = newBuffer;
70
+ }
71
+ }
72
+
73
+ public void reset() {
74
+ buffer.rewind();
75
+ }
76
+
77
+ public int offset() {
78
+ return buffer.position();
79
+ }
80
+
81
+ private IRubyObject consumeUnsignedLong() {
82
+ long value = buffer.getLong();
83
+ if (value < 0) {
84
+ return RubyBignum.newBignum(runtime, BigInteger.valueOf(value & ((1L<<63)-1)).setBit(63));
85
+ } else {
86
+ return runtime.newFixnum(value);
87
+ }
88
+ }
89
+
90
+ private IRubyObject consumeString(int size, Encoding encoding) {
91
+ byte[] bytes = readBytes(size);
92
+ ByteList byteList = new ByteList(bytes, encoding);
93
+ return runtime.newString(byteList);
94
+ }
95
+
96
+ private IRubyObject consumeArray(int size) {
97
+ IRubyObject[] elements = new IRubyObject[size];
98
+ for (int i = 0; i < size; i++) {
99
+ elements[i] = next();
100
+ }
101
+ return runtime.newArray(elements);
102
+ }
103
+
104
+ private IRubyObject consumeHash(int size) {
105
+ RubyHash hash = RubyHash.newHash(runtime);
106
+ for (int i = 0; i < size; i++) {
107
+ IRubyObject key = next();
108
+ if (this.symbolizeKeys && key instanceof RubyString) {
109
+ key = ((RubyString) key).intern();
110
+ }
111
+ hash.fastASet(key, next());
112
+ }
113
+ return hash;
114
+ }
115
+
116
+ private IRubyObject consumeExtension(int size) {
117
+ int type = buffer.get();
118
+ byte[] payload = readBytes(size);
119
+ return ExtensionValue.newExtensionValue(runtime, type, payload);
120
+ }
121
+
122
+ private byte[] readBytes(int size) {
123
+ byte[] payload = new byte[size];
124
+ buffer.get(payload);
125
+ return payload;
126
+ }
127
+
128
+ @Override
129
+ public void remove() {
130
+ throw new UnsupportedOperationException();
131
+ }
132
+
133
+ @Override
134
+ public boolean hasNext() {
135
+ return buffer.remaining() > 0;
136
+ }
137
+
138
+ @Override
139
+ public IRubyObject next() {
140
+ int position = buffer.position();
141
+ try {
142
+ byte b = buffer.get();
143
+ outer: switch ((b >> 4) & 0xf) {
144
+ case 0x8: return consumeHash(b & 0x0f);
145
+ case 0x9: return consumeArray(b & 0x0f);
146
+ case 0xa:
147
+ case 0xb: return consumeString(b & 0x1f, utf8Encoding);
148
+ case 0xc:
149
+ switch (b) {
150
+ case NIL: return runtime.getNil();
151
+ case FALSE: return runtime.getFalse();
152
+ case TRUE: return runtime.getTrue();
153
+ case BIN8: return consumeString(buffer.get() & 0xff, binaryEncoding);
154
+ case BIN16: return consumeString(buffer.getShort() & 0xffff, binaryEncoding);
155
+ case BIN32: return consumeString(buffer.getInt(), binaryEncoding);
156
+ case VAREXT8: return consumeExtension(buffer.get());
157
+ case VAREXT16: return consumeExtension(buffer.getShort());
158
+ case VAREXT32: return consumeExtension(buffer.getInt());
159
+ case FLOAT32: return runtime.newFloat(buffer.getFloat());
160
+ case FLOAT64: return runtime.newFloat(buffer.getDouble());
161
+ case UINT8: return runtime.newFixnum(buffer.get() & 0xffL);
162
+ case UINT16: return runtime.newFixnum(buffer.getShort() & 0xffffL);
163
+ case UINT32: return runtime.newFixnum(buffer.getInt() & 0xffffffffL);
164
+ case UINT64: return consumeUnsignedLong();
165
+ default: break outer;
166
+ }
167
+ case 0xd:
168
+ switch (b) {
169
+ case INT8: return runtime.newFixnum(buffer.get());
170
+ case INT16: return runtime.newFixnum(buffer.getShort());
171
+ case INT32: return runtime.newFixnum(buffer.getInt());
172
+ case INT64: return runtime.newFixnum(buffer.getLong());
173
+ case FIXEXT1: return consumeExtension(1);
174
+ case FIXEXT2: return consumeExtension(2);
175
+ case FIXEXT4: return consumeExtension(4);
176
+ case FIXEXT8: return consumeExtension(8);
177
+ case FIXEXT16: return consumeExtension(16);
178
+ case STR8: return consumeString(buffer.get() & 0xff, utf8Encoding);
179
+ case STR16: return consumeString(buffer.getShort() & 0xffff, utf8Encoding);
180
+ case STR32: return consumeString(buffer.getInt(), utf8Encoding);
181
+ case ARY16: return consumeArray(buffer.getShort() & 0xffff);
182
+ case ARY32: return consumeArray(buffer.getInt());
183
+ case MAP16: return consumeHash(buffer.getShort() & 0xffff);
184
+ case MAP32: return consumeHash(buffer.getInt());
185
+ default: break outer;
186
+ }
187
+ case 0xe:
188
+ case 0xf: return runtime.newFixnum((0x1f & b) - 0x20);
189
+ default: return runtime.newFixnum(b);
190
+ }
191
+ buffer.position(position);
192
+ throw runtime.newRaiseException(unpackErrorClass, "Illegal byte sequence");
193
+ } catch (RaiseException re) {
194
+ buffer.position(position);
195
+ throw re;
196
+ } catch (BufferUnderflowException bue) {
197
+ buffer.position(position);
198
+ throw runtime.newRaiseException(underflowErrorClass, "Not enough bytes available");
199
+ }
200
+ }
201
+ }