msgpack 0.5.9 → 0.5.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,78 @@
1
+ package org.msgpack.jruby;
2
+
3
+
4
+ import org.jruby.Ruby;
5
+ import org.jruby.RubyClass;
6
+ import org.jruby.RubyObject;
7
+ import org.jruby.RubyHash;
8
+ import org.jruby.RubyIO;
9
+ import org.jruby.RubyInteger;
10
+ import org.jruby.runtime.builtin.IRubyObject;
11
+ import org.jruby.anno.JRubyClass;
12
+ import org.jruby.anno.JRubyMethod;
13
+ import org.jruby.runtime.ThreadContext;
14
+ import org.jruby.runtime.ObjectAllocator;
15
+ import org.jruby.util.ByteList;
16
+
17
+
18
+ @JRubyClass(name="MessagePack::Packer")
19
+ public class Packer extends RubyObject {
20
+ private Buffer buffer;
21
+ private Encoder encoder;
22
+
23
+ public Packer(Ruby runtime, RubyClass type) {
24
+ super(runtime, type);
25
+ }
26
+
27
+ static class PackerAllocator implements ObjectAllocator {
28
+ public IRubyObject allocate(Ruby runtime, RubyClass type) {
29
+ return new Packer(runtime, type);
30
+ }
31
+ }
32
+
33
+ @JRubyMethod(name = "initialize", optional = 2)
34
+ public IRubyObject initialize(ThreadContext ctx, IRubyObject[] args) {
35
+ this.encoder = new Encoder(ctx.getRuntime());
36
+ this.buffer = new Buffer(ctx.getRuntime(), ctx.getRuntime().getModule("MessagePack").getClass("Buffer"));
37
+ this.buffer.initialize(ctx, args);
38
+ return this;
39
+ }
40
+
41
+ @JRubyMethod(name = "write")
42
+ public IRubyObject write(ThreadContext ctx, IRubyObject obj) {
43
+ return buffer.write(ctx, encoder.encode(obj, this));
44
+ }
45
+
46
+ @JRubyMethod(name = "write_nil")
47
+ public IRubyObject writeNil(ThreadContext ctx) {
48
+ return write(ctx, null);
49
+ }
50
+
51
+ @JRubyMethod(name = "write_array_header")
52
+ public IRubyObject writeArrayHeader(ThreadContext ctx, IRubyObject size) {
53
+ int s = (int) size.convertToInteger().getLongValue();
54
+ return buffer.write(ctx, encoder.encodeArrayHeader(s));
55
+ }
56
+
57
+ @JRubyMethod(name = "write_map_header")
58
+ public IRubyObject writeMapHeader(ThreadContext ctx, IRubyObject size) {
59
+ int s = (int) size.convertToInteger().getLongValue();
60
+ return buffer.write(ctx, encoder.encodeMapHeader(s));
61
+ }
62
+
63
+ @JRubyMethod(name = "to_s")
64
+ public IRubyObject toS(ThreadContext ctx) {
65
+ return buffer.toS(ctx);
66
+ }
67
+
68
+ @JRubyMethod(name = "buffer")
69
+ public IRubyObject buffer(ThreadContext ctx) {
70
+ return buffer;
71
+ }
72
+
73
+ @JRubyMethod(name = "flush")
74
+ public IRubyObject flush(ThreadContext ctx) {
75
+ return buffer.flush(ctx);
76
+ }
77
+
78
+ }
@@ -0,0 +1,37 @@
1
+ package org.msgpack.jruby;
2
+
3
+
4
+ public interface Types {
5
+ public static final byte FIXSTR = (byte) 0xa0; // This is actually not header byte, but prefix bit mask
6
+ public static final byte NIL = (byte) 0xc0;
7
+ public static final byte FALSE = (byte) 0xc2;
8
+ public static final byte TRUE = (byte) 0xc3;
9
+ public static final byte BIN8 = (byte) 0xc4;
10
+ public static final byte BIN16 = (byte) 0xc5;
11
+ public static final byte BIN32 = (byte) 0xc6;
12
+ public static final byte VAREXT8 = (byte) 0xc7;
13
+ public static final byte VAREXT16 = (byte) 0xc8;
14
+ public static final byte VAREXT32 = (byte) 0xc9;
15
+ public static final byte FLOAT32 = (byte) 0xca;
16
+ public static final byte FLOAT64 = (byte) 0xcb;
17
+ public static final byte UINT8 = (byte) 0xcc;
18
+ public static final byte UINT16 = (byte) 0xcd;
19
+ public static final byte UINT32 = (byte) 0xce;
20
+ public static final byte UINT64 = (byte) 0xcf;
21
+ public static final byte INT8 = (byte) 0xd0;
22
+ public static final byte INT16 = (byte) 0xd1;
23
+ public static final byte INT32 = (byte) 0xd2;
24
+ public static final byte INT64 = (byte) 0xd3;
25
+ public static final byte FIXEXT1 = (byte) 0xd4;
26
+ public static final byte FIXEXT2 = (byte) 0xd5;
27
+ public static final byte FIXEXT4 = (byte) 0xd6;
28
+ public static final byte FIXEXT8 = (byte) 0xd7;
29
+ public static final byte FIXEXT16 = (byte) 0xd8;
30
+ public static final byte STR8 = (byte) 0xd9;
31
+ public static final byte STR16 = (byte) 0xda;
32
+ public static final byte STR32 = (byte) 0xdb;
33
+ public static final byte ARY16 = (byte) 0xdc;
34
+ public static final byte ARY32 = (byte) 0xdd;
35
+ public static final byte MAP16 = (byte) 0xde;
36
+ public static final byte MAP32 = (byte) 0xdf;
37
+ }
@@ -0,0 +1,170 @@
1
+ package org.msgpack.jruby;
2
+
3
+
4
+ import org.jruby.Ruby;
5
+ import org.jruby.RubyClass;
6
+ import org.jruby.RubyString;
7
+ import org.jruby.RubyObject;
8
+ import org.jruby.RubyHash;
9
+ import org.jruby.RubyNumeric;
10
+ import org.jruby.RubyIO;
11
+ import org.jruby.exceptions.RaiseException;
12
+ import org.jruby.runtime.builtin.IRubyObject;
13
+ import org.jruby.runtime.Block;
14
+ import org.jruby.runtime.ObjectAllocator;
15
+ import org.jruby.runtime.ThreadContext;
16
+ import org.jruby.anno.JRubyClass;
17
+ import org.jruby.anno.JRubyMethod;
18
+ import org.jruby.util.ByteList;
19
+ import org.jruby.ext.stringio.StringIO;
20
+
21
+ import static org.jruby.runtime.Visibility.PRIVATE;
22
+
23
+
24
+ @JRubyClass(name="MessagePack::Unpacker")
25
+ public class Unpacker extends RubyObject {
26
+ private IRubyObject stream;
27
+ private IRubyObject data;
28
+ private Decoder decoder;
29
+ private final RubyClass underflowErrorClass;
30
+
31
+ public Unpacker(Ruby runtime, RubyClass type) {
32
+ super(runtime, type);
33
+ this.underflowErrorClass = runtime.getModule("MessagePack").getClass("UnderflowError");
34
+ }
35
+
36
+ static class UnpackerAllocator implements ObjectAllocator {
37
+ public IRubyObject allocate(Ruby runtime, RubyClass klass) {
38
+ return new Unpacker(runtime, klass);
39
+ }
40
+ }
41
+
42
+ @JRubyMethod(name = "initialize", optional = 1, visibility = PRIVATE)
43
+ public IRubyObject initialize(ThreadContext ctx, IRubyObject[] args) {
44
+ if (args.length > 0) {
45
+ if (args[args.length - 1] instanceof RubyHash) {
46
+ //TODO: symbolize_keys
47
+ } else if (!(args[0] instanceof RubyHash)) {
48
+ setStream(ctx, args[0]);
49
+ }
50
+ }
51
+ return this;
52
+ }
53
+
54
+ @JRubyMethod(required = 2)
55
+ public IRubyObject execute(ThreadContext ctx, IRubyObject data, IRubyObject offset) {
56
+ return executeLimit(ctx, data, offset, null);
57
+ }
58
+
59
+ @JRubyMethod(name = "execute_limit", required = 3)
60
+ public IRubyObject executeLimit(ThreadContext ctx, IRubyObject str, IRubyObject off, IRubyObject lim) {
61
+ RubyString input = str.asString();
62
+ int offset = RubyNumeric.fix2int(off);
63
+ int limit = lim == null || lim.isNil() ? -1 : RubyNumeric.fix2int(lim);
64
+ ByteList byteList = input.getByteList();
65
+ if (limit == -1) {
66
+ limit = byteList.length() - offset;
67
+ }
68
+ Decoder decoder = new Decoder(ctx.getRuntime(), byteList.unsafeBytes(), byteList.begin() + offset, limit);
69
+ try {
70
+ this.data = null;
71
+ this.data = decoder.next();
72
+ } catch (RaiseException re) {
73
+ if (re.getException().getType() != underflowErrorClass) {
74
+ throw re;
75
+ }
76
+ }
77
+ return ctx.getRuntime().newFixnum(decoder.offset());
78
+ }
79
+
80
+ @JRubyMethod(name = "data")
81
+ public IRubyObject getData(ThreadContext ctx) {
82
+ if (data == null) {
83
+ return ctx.getRuntime().getNil();
84
+ } else {
85
+ return data;
86
+ }
87
+ }
88
+
89
+ @JRubyMethod(name = "finished?")
90
+ public IRubyObject finished_p(ThreadContext ctx) {
91
+ return data == null ? ctx.getRuntime().getFalse() : ctx.getRuntime().getTrue();
92
+ }
93
+
94
+ @JRubyMethod(required = 1)
95
+ public IRubyObject feed(ThreadContext ctx, IRubyObject data) {
96
+ ByteList byteList = data.asString().getByteList();
97
+ if (decoder == null) {
98
+ decoder = new Decoder(ctx.getRuntime(), byteList.unsafeBytes(), byteList.begin(), byteList.length());
99
+ } else {
100
+ decoder.feed(byteList.unsafeBytes(), byteList.begin(), byteList.length());
101
+ }
102
+ return ctx.getRuntime().getNil();
103
+ }
104
+
105
+ @JRubyMethod(name = "feed_each", required = 1)
106
+ public IRubyObject feedEach(ThreadContext ctx, IRubyObject data, Block block) {
107
+ feed(ctx, data);
108
+ each(ctx, block);
109
+ return ctx.getRuntime().getNil();
110
+ }
111
+
112
+ @JRubyMethod
113
+ public IRubyObject each(ThreadContext ctx, Block block) {
114
+ if (block.isGiven()) {
115
+ if (decoder != null) {
116
+ try {
117
+ while (decoder.hasNext()) {
118
+ block.yield(ctx, decoder.next());
119
+ }
120
+ } catch (RaiseException re) {
121
+ if (re.getException().getType() != underflowErrorClass) {
122
+ throw re;
123
+ }
124
+ }
125
+ }
126
+ return this;
127
+ } else {
128
+ return callMethod(ctx, "to_enum");
129
+ }
130
+ }
131
+
132
+ @JRubyMethod
133
+ public IRubyObject fill(ThreadContext ctx) {
134
+ return ctx.getRuntime().getNil();
135
+ }
136
+
137
+ @JRubyMethod
138
+ public IRubyObject reset(ThreadContext ctx) {
139
+ if (decoder != null) {
140
+ decoder.reset();
141
+ }
142
+ return ctx.getRuntime().getNil();
143
+ }
144
+
145
+ @JRubyMethod(name = "stream")
146
+ public IRubyObject getStream(ThreadContext ctx) {
147
+ if (stream == null) {
148
+ return ctx.getRuntime().getNil();
149
+ } else {
150
+ return stream;
151
+ }
152
+ }
153
+
154
+ @JRubyMethod(name = "stream=", required = 1)
155
+ public IRubyObject setStream(ThreadContext ctx, IRubyObject stream) {
156
+ RubyString str;
157
+ if (stream instanceof StringIO) {
158
+ str = stream.callMethod(ctx, "string").asString();
159
+ } else if (stream instanceof RubyIO) {
160
+ str = stream.callMethod(ctx, "read").asString();
161
+ } else {
162
+ throw ctx.getRuntime().newTypeError(stream, "IO");
163
+ }
164
+ ByteList byteList = str.getByteList();
165
+ this.stream = stream;
166
+ this.decoder = null;
167
+ this.decoder = new Decoder(ctx.getRuntime(), byteList.unsafeBytes(), byteList.begin(), byteList.length());
168
+ return getStream(ctx);
169
+ }
170
+ }
@@ -1,6 +1,13 @@
1
1
  require "msgpack/version"
2
- begin
3
- require "msgpack/#{RUBY_VERSION[/\d+.\d+/]}/msgpack"
4
- rescue LoadError
5
- require "msgpack/msgpack"
2
+
3
+ if defined?(RUBY_ENGINE) && RUBY_ENGINE == "jruby" # This is same with `/java/ =~ RUBY_VERSION`
4
+ require "java"
5
+ require "msgpack/msgpack.jar"
6
+ org.msgpack.jruby.MessagePackLibrary.new.load(JRuby.runtime, false)
7
+ else
8
+ begin
9
+ require "msgpack/#{RUBY_VERSION[/\d+.\d+/]}/msgpack"
10
+ rescue LoadError
11
+ require "msgpack/msgpack"
12
+ end
6
13
  end
@@ -1,3 +1,3 @@
1
1
  module MessagePack
2
- VERSION = "0.5.9"
2
+ VERSION = "0.5.10"
3
3
  end
@@ -1,4 +1,4 @@
1
- $:.push File.expand_path("../lib", __FILE__)
1
+ $LOAD_PATH.push File.expand_path("../lib", __FILE__)
2
2
  require 'msgpack/version'
3
3
 
4
4
  Gem::Specification.new do |s|
@@ -6,16 +6,21 @@ Gem::Specification.new do |s|
6
6
  s.version = MessagePack::VERSION
7
7
  s.summary = "MessagePack, a binary-based efficient data interchange format."
8
8
  s.description = %q{MessagePack is a binary-based efficient object serialization library. It enables to exchange structured objects between many languages like JSON. But unlike JSON, it is very fast and small.}
9
- s.author = "Sadayuki Furuhashi"
10
- s.email = "frsyuki@gmail.com"
9
+ s.authors = ["Sadayuki Furuhashi", "Theo Hultberg"]
10
+ s.email = ["frsyuki@gmail.com", "theo@iconara.net"]
11
11
  s.license = "Apache 2.0"
12
12
  s.homepage = "http://msgpack.org/"
13
13
  s.rubyforge_project = "msgpack"
14
14
  s.has_rdoc = false
15
- s.files = `git ls-files`.split("\n")
16
- s.test_files = `git ls-files -- {test,spec}/*`.split("\n")
17
15
  s.require_paths = ["lib"]
18
- s.extensions = ["ext/msgpack/extconf.rb"]
16
+ if /java/ =~ RUBY_PLATFORM
17
+ s.files = Dir['lib/**/*.rb', 'lib/**/*.jar']
18
+ s.platform = Gem::Platform.new('java')
19
+ else
20
+ s.files = `git ls-files`.split("\n")
21
+ s.extensions = ["ext/msgpack/extconf.rb"]
22
+ end
23
+ s.test_files = `git ls-files -- {test,spec}/*`.split("\n")
19
24
 
20
25
  s.add_development_dependency 'bundler', ['~> 1.0']
21
26
  s.add_development_dependency 'rake', ['~> 0.9.2']
@@ -20,55 +20,55 @@ describe MessagePack do
20
20
 
21
21
  it "positive fixnum" do
22
22
  check 1, 1
23
- check 1, (1<<6)
24
- check 1, (1<<7)-1
23
+ check 1, (1 << 6)
24
+ check 1, (1 << 7)-1
25
25
  end
26
26
 
27
27
  it "positive int 8" do
28
28
  check 1, -1
29
- check 2, (1<<7)
30
- check 2, (1<<8)-1
29
+ check 2, (1 << 7)
30
+ check 2, (1 << 8) - 1
31
31
  end
32
32
 
33
33
  it "positive int 16" do
34
- check 3, (1<<8)
35
- check 3, (1<<16)-1
34
+ check 3, (1 << 8)
35
+ check 3, (1 << 16) - 1
36
36
  end
37
37
 
38
38
  it "positive int 32" do
39
- check 5, (1<<16)
40
- check 5, (1<<32)-1
39
+ check 5, (1 << 16)
40
+ check 5, (1 << 32) - 1
41
41
  end
42
42
 
43
43
  it "positive int 64" do
44
- check 9, (1<<32)
44
+ check 9, (1 << 32)
45
45
  #check 9, (1<<64)-1
46
46
  end
47
47
 
48
48
  it "negative fixnum" do
49
49
  check 1, -1
50
- check 1, -((1<<5)-1)
51
- check 1, -(1<<5)
50
+ check 1, -((1 << 5)-1)
51
+ check 1, -(1 << 5)
52
52
  end
53
53
 
54
54
  it "negative int 8" do
55
- check 2, -((1<<5)+1)
56
- check 2, -(1<<7)
55
+ check 2, -((1 << 5)+1)
56
+ check 2, -(1 << 7)
57
57
  end
58
58
 
59
59
  it "negative int 16" do
60
- check 3, -((1<<7)+1)
61
- check 3, -(1<<15)
60
+ check 3, -((1 << 7)+1)
61
+ check 3, -(1 << 15)
62
62
  end
63
63
 
64
64
  it "negative int 32" do
65
- check 5, -((1<<15)+1)
66
- check 5, -(1<<31)
65
+ check 5, -((1 << 15)+1)
66
+ check 5, -(1 << 31)
67
67
  end
68
68
 
69
69
  it "negative int 64" do
70
- check 9, -((1<<31)+1)
71
- check 9, -(1<<63)
70
+ check 9, -((1 << 31)+1)
71
+ check 9, -(1 << 63)
72
72
  end
73
73
 
74
74
  it "double" do
@@ -80,32 +80,32 @@ describe MessagePack do
80
80
 
81
81
  it "fixraw" do
82
82
  check_raw 1, 0
83
- check_raw 1, (1<<5)-1
83
+ check_raw 1, (1 << 5)-1
84
84
  end
85
85
 
86
86
  it "raw 16" do
87
- check_raw 3, (1<<5)
88
- check_raw 3, (1<<16)-1
87
+ check_raw 3, (1 << 5)
88
+ check_raw 3, (1 << 16)-1
89
89
  end
90
90
 
91
91
  it "raw 32" do
92
- check_raw 5, (1<<16)
93
- #check_raw 5, (1<<32)-1 # memory error
92
+ check_raw 5, (1 << 16)
93
+ #check_raw 5, (1 << 32)-1 # memory error
94
94
  end
95
95
 
96
96
  it "fixarray" do
97
97
  check_array 1, 0
98
- check_array 1, (1<<4)-1
98
+ check_array 1, (1 << 4)-1
99
99
  end
100
100
 
101
101
  it "array 16" do
102
- check_array 3, (1<<4)
103
- #check_array 3, (1<<16)-1
102
+ check_array 3, (1 << 4)
103
+ #check_array 3, (1 << 16)-1
104
104
  end
105
105
 
106
106
  it "array 32" do
107
- #check_array 5, (1<<16)
108
- #check_array 5, (1<<32)-1 # memory error
107
+ #check_array 5, (1 << 16)
108
+ #check_array 5, (1 << 32)-1 # memory error
109
109
  end
110
110
 
111
111
  it "nil" do
@@ -210,7 +210,9 @@ describe MessagePack do
210
210
  end
211
211
 
212
212
  def check_raw(overhead, num)
213
- check num+overhead, " "*num
213
+ rawstr = " "*num
214
+ rawstr.force_encoding("UTF-8")
215
+ check num+overhead, rawstr
214
216
  end
215
217
 
216
218
  def check_array(overhead, num)