msgpack 1.4.2 → 1.7.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. checksums.yaml +4 -4
  2. data/ChangeLog +89 -0
  3. data/README.md +73 -13
  4. data/ext/java/org/msgpack/jruby/Buffer.java +26 -19
  5. data/ext/java/org/msgpack/jruby/Decoder.java +29 -21
  6. data/ext/java/org/msgpack/jruby/Encoder.java +68 -30
  7. data/ext/java/org/msgpack/jruby/ExtensionRegistry.java +43 -64
  8. data/ext/java/org/msgpack/jruby/ExtensionValue.java +6 -9
  9. data/ext/java/org/msgpack/jruby/Factory.java +43 -42
  10. data/ext/java/org/msgpack/jruby/Packer.java +37 -40
  11. data/ext/java/org/msgpack/jruby/Unpacker.java +80 -73
  12. data/ext/msgpack/buffer.c +54 -74
  13. data/ext/msgpack/buffer.h +21 -18
  14. data/ext/msgpack/buffer_class.c +161 -52
  15. data/ext/msgpack/buffer_class.h +1 -0
  16. data/ext/msgpack/compat.h +0 -99
  17. data/ext/msgpack/extconf.rb +25 -46
  18. data/ext/msgpack/factory_class.c +143 -87
  19. data/ext/msgpack/packer.c +66 -43
  20. data/ext/msgpack/packer.h +25 -20
  21. data/ext/msgpack/packer_class.c +102 -130
  22. data/ext/msgpack/packer_class.h +11 -0
  23. data/ext/msgpack/packer_ext_registry.c +35 -40
  24. data/ext/msgpack/packer_ext_registry.h +41 -38
  25. data/ext/msgpack/rbinit.c +1 -1
  26. data/ext/msgpack/rmem.c +3 -4
  27. data/ext/msgpack/sysdep.h +5 -2
  28. data/ext/msgpack/unpacker.c +136 -111
  29. data/ext/msgpack/unpacker.h +16 -13
  30. data/ext/msgpack/unpacker_class.c +86 -126
  31. data/ext/msgpack/unpacker_class.h +11 -0
  32. data/ext/msgpack/unpacker_ext_registry.c +40 -28
  33. data/ext/msgpack/unpacker_ext_registry.h +21 -18
  34. data/lib/msgpack/bigint.rb +69 -0
  35. data/lib/msgpack/buffer.rb +9 -0
  36. data/lib/msgpack/factory.rb +140 -10
  37. data/lib/msgpack/packer.rb +10 -1
  38. data/lib/msgpack/symbol.rb +21 -4
  39. data/lib/msgpack/time.rb +1 -1
  40. data/lib/msgpack/unpacker.rb +14 -1
  41. data/lib/msgpack/version.rb +1 -1
  42. data/lib/msgpack.rb +6 -7
  43. data/msgpack.gemspec +8 -5
  44. metadata +37 -82
  45. data/.gitignore +0 -23
  46. data/.rubocop.yml +0 -36
  47. data/.travis.yml +0 -39
  48. data/Gemfile +0 -9
  49. data/Rakefile +0 -71
  50. data/appveyor.yml +0 -18
  51. data/bench/pack.rb +0 -23
  52. data/bench/pack_log.rb +0 -33
  53. data/bench/pack_log_long.rb +0 -65
  54. data/bench/pack_symbols.rb +0 -28
  55. data/bench/run.sh +0 -14
  56. data/bench/run_long.sh +0 -35
  57. data/bench/run_symbols.sh +0 -26
  58. data/bench/unpack.rb +0 -21
  59. data/bench/unpack_log.rb +0 -34
  60. data/bench/unpack_log_long.rb +0 -67
  61. data/doclib/msgpack/buffer.rb +0 -193
  62. data/doclib/msgpack/core_ext.rb +0 -101
  63. data/doclib/msgpack/error.rb +0 -19
  64. data/doclib/msgpack/extension_value.rb +0 -9
  65. data/doclib/msgpack/factory.rb +0 -101
  66. data/doclib/msgpack/packer.rb +0 -208
  67. data/doclib/msgpack/time.rb +0 -22
  68. data/doclib/msgpack/timestamp.rb +0 -44
  69. data/doclib/msgpack/unpacker.rb +0 -183
  70. data/doclib/msgpack.rb +0 -87
  71. data/msgpack.org.md +0 -46
  72. data/spec/cases.json +0 -1
  73. data/spec/cases.msg +0 -0
  74. data/spec/cases_compact.msg +0 -0
  75. data/spec/cases_spec.rb +0 -39
  76. data/spec/cruby/buffer_io_spec.rb +0 -255
  77. data/spec/cruby/buffer_packer.rb +0 -29
  78. data/spec/cruby/buffer_spec.rb +0 -575
  79. data/spec/cruby/buffer_unpacker.rb +0 -19
  80. data/spec/cruby/unpacker_spec.rb +0 -70
  81. data/spec/ext_value_spec.rb +0 -99
  82. data/spec/exttypes.rb +0 -51
  83. data/spec/factory_spec.rb +0 -367
  84. data/spec/format_spec.rb +0 -301
  85. data/spec/jruby/benchmarks/shootout_bm.rb +0 -73
  86. data/spec/jruby/benchmarks/symbolize_keys_bm.rb +0 -25
  87. data/spec/jruby/unpacker_spec.rb +0 -186
  88. data/spec/msgpack_spec.rb +0 -214
  89. data/spec/pack_spec.rb +0 -61
  90. data/spec/packer_spec.rb +0 -557
  91. data/spec/random_compat.rb +0 -24
  92. data/spec/spec_helper.rb +0 -55
  93. data/spec/timestamp_spec.rb +0 -121
  94. data/spec/unpack_spec.rb +0 -57
  95. data/spec/unpacker_spec.rb +0 -819
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 451ed743fe37bd3726f897f4daae9e7fd9b2cb065a34cf438ca786350060e0ec
4
- data.tar.gz: 87335c4b1d35f9ac88af648963b1fe60caa72059ec7e6c1b6b507027da9118d8
3
+ metadata.gz: fe6adcd787be2f8c3e9459d46c3baa4ad81c5b08bdcaeb9bfec9a80c9a4540c6
4
+ data.tar.gz: 76d6be5a37a2b6e225ad995e6b0f670d0489839a98b678c9332837bd082e7d7e
5
5
  SHA512:
6
- metadata.gz: eb519049e9d8e4b36682e4b2c2625f10172745ce74ad1cfc8e6d93468b219071ed579bcfd36b7f0e1210eeb177197b435da2a028f46d2c71c73a3bcf2b53dcf8
7
- data.tar.gz: e8c4765ac836226f5a2ce68a62194f112fc77bcdd9744d39040ff2db5a2c27d876348fb6002110c00b122bccdff2d539223d82ff73ea0bcb4cf1a3d9fb62defe
6
+ metadata.gz: 36b0e748e7b54baad2ecef8c1c071a82cf3e6bd26540fd433a3c71f1e77afe341188594a26bfed817f84af10ef4f613ded14fc0a8448a77dc4978683cf7c35d1
7
+ data.tar.gz: 45ce3770a12b3580945ba3dd42d40ab8ba24a167f675f71d57bb42af6cf4ab204d7cc7960925ea78556988f523886fd92b431789a174e0529143ab0908d52418
data/ChangeLog CHANGED
@@ -1,3 +1,92 @@
1
+ 2024-10-03 1.7.3
2
+
3
+ * Limit initial containers pre-allocation to `SHRT_MAX` (32k) entries.
4
+
5
+ 2023-07-18 1.7.2:
6
+
7
+ * Fix a potential GC bug when packing data using recursive extensions and buffers containing over 512KkiB of data (See #341).
8
+ * Fix a regression where feeding an empty string to an Unpacker would be considered like the end of the buffer.
9
+
10
+ 2023-05-19 1.7.1:
11
+
12
+ * Fix JRuby 9.4 compatibility.
13
+ * Fix compilation on older compilers (gcc 4.x).
14
+ * Fix an infinite recursion issue when registering a Symbol type with a `nil` packer.
15
+
16
+ 2023-03-29 1.7.0:
17
+
18
+ * Fix a possible double-free issue when GC triggers inside `_msgpack_rmem_alloc2`.
19
+ * `Unpacker#feed` now always directly read in provided strings instead of copying content in its buffer.
20
+ * `Unpacker#feed` is now an alias of `Unpacker#feed_reference`.
21
+ * Implement `Factory::Pool#unpacker` and `Factory::Pool#packer` to allow for more precise serialization.
22
+ * Require Ruby 2.5+.
23
+
24
+ 2023-03-03 1.6.1:
25
+
26
+ * Undefine `#clone` and `#dup` on `MessagePack::Buffer`, `MessagePack::Packer` and `MessagePack::Unpacker`.
27
+ These methods were never intended, and using them could cause leaks or crashes or worse.
28
+ * Fix a possible GC crash when GC trigger inside `MessagePack::Buffer.new` (#314).
29
+
30
+ 2022-09-30 1.6.0:
31
+
32
+ * Fix a potential use-after-free bug in Buffer_free when accessing a packer or unpacker buffer.
33
+ * `old-style-definition` compilation warnings.
34
+ * Restore zero-copy buffer feed when provided a Ruby string. This was accidentally broken in 1.5.4.
35
+ * Provide implementations for `ObjectSpace.memsize`. Message pack objects now properly report their size to Ruby.
36
+ * Fix an endianess bug on Windows platform.
37
+
38
+ 2022-08-23 1.5.6:
39
+
40
+ * No actual code change, just re-release the `java` version properly.
41
+
42
+ 2022-08-22 1.5.5:
43
+
44
+ * Fix a segfault when GC triggers inside a recursive extension.
45
+
46
+ 2022-07-25 1.5.4:
47
+
48
+ * Fix a segfault when deserializing empty symbol (`:""`).
49
+ * Improve compilation flags to not strip debug symbols.
50
+
51
+ 2022-05-30 version 1.5.3:
52
+
53
+ * Fix deduplication of empty strings when using the `freeze: true` option.
54
+ * Use `rb_hash_new_capa` when available (Ruby 3.2) for improved performance when parsing large hashes.
55
+
56
+ 2022-05-27 version 1.5.2:
57
+
58
+ * Fix bug about unpacking ext type objects with the recursive option
59
+
60
+ 2022-04-07 version 1.5.1:
61
+
62
+ * Fix bug about packing/unpacking ext type objects with the recursive option
63
+
64
+ 2022-04-06 version 1.5.0:
65
+
66
+ * Add recursive option on Factory#register_type to operate Packer/Unpacker manually
67
+ * Add oversized_integer_extension option on Factory#register_type to pack/unpack bigint using ext types
68
+ * Add Factory#pool method and Factory::Pool class to provide pooled Packer and Unpacker instances
69
+
70
+ 2022-02-15 version 1.4.5:
71
+
72
+ * Fix to create UTF-8 Symbol keys when symbolize_keys: true
73
+ * Fix to assume Symbols as US-ASCII or UTF-8
74
+ * Optimize Packer/Unpacker initialization
75
+ * Optimize extension class lookup
76
+ * Rename Packer#clear as Packer#reset (#clear is still available as an alias)
77
+
78
+ 2022-01-22 version 1.4.4:
79
+
80
+ * Specify the build option --platform=8 for older Java platforms
81
+
82
+ 2022-01-20 version 1.4.3:
83
+
84
+ * Optimize serialization/deserialization of Symbols
85
+ * Support registering ext types for objects of subclasses of primitive types (like Hash)
86
+ * Add optimized_symbols_parsing option to Factory#register_type on MRI implementation
87
+ * Optimize to deduplicate Hash keys on JRuby
88
+ * Support JRuby 9.3 (and drop 9.1)
89
+
1
90
  2021-02-01 version 1.4.2:
2
91
 
3
92
  * Add the required Ruby version (>= 2.4) to avoid compilation errors on older Ruby runtimes
data/README.md CHANGED
@@ -8,15 +8,24 @@ and typical short strings only require an extra byte in addition to the strings
8
8
  If you ever wished to use JSON for convenience (storing an image with metadata) but could
9
9
  not for technical reasons (binary data, size, speed...), MessagePack is a perfect replacement.
10
10
 
11
- require 'msgpack'
12
- msg = [1,2,3].to_msgpack #=> "\x93\x01\x02\x03"
13
- MessagePack.unpack(msg) #=> [1,2,3]
11
+ ```ruby
12
+ require 'msgpack'
13
+ msg = [1,2,3].to_msgpack #=> "\x93\x01\x02\x03"
14
+ MessagePack.unpack(msg) #=> [1,2,3]
15
+ ```
14
16
 
15
- Use RubyGems to install:
17
+ Add msgpack to your Gemfile to install with Bundler:
18
+
19
+ ```ruby
20
+ # Gemfile
21
+ gem 'msgpack'
22
+ ```
23
+
24
+ Or, use RubyGems to install:
16
25
 
17
26
  gem install msgpack
18
27
 
19
- or build msgpack-ruby and install:
28
+ Or, build msgpack-ruby and install from a checked-out msgpack-ruby repository:
20
29
 
21
30
  bundle
22
31
  rake
@@ -27,11 +36,11 @@ or build msgpack-ruby and install:
27
36
 
28
37
  * Create REST API returing MessagePack using Rails + [RABL](https://github.com/nesquena/rabl)
29
38
  * Store objects efficiently serialized by msgpack on memcached or Redis
30
- * In fact Redis supports msgpack in [EVAL-scripts](http://redis.io/commands/eval)
39
+ * In fact Redis supports msgpack in [EVAL-scripts](https://redis.io/docs/latest/commands/eval/)
31
40
  * Upload data in efficient format from mobile devices such as smartphones
32
41
  * MessagePack works on iPhone/iPad and Android. See also [Objective-C](https://github.com/msgpack/msgpack-objectivec) and [Java](https://github.com/msgpack/msgpack-java) implementations
33
42
  * Design a portable protocol to communicate with embedded devices
34
- * Check also [Fluentd](http://fluentd.org/) which is a log collector which uses msgpack for the log format (they say it uses JSON but actually it's msgpack, which is compatible with JSON)
43
+ * Check also [Fluentd](https://www.fluentd.org) which is a log collector which uses msgpack for the log format (they say it uses JSON but actually it's msgpack, which is compatible with JSON)
35
44
  * Exchange objects between software components written in different languages
36
45
  * You'll need a flexible but efficient format so that components exchange objects while keeping compatibility
37
46
 
@@ -40,7 +49,7 @@ or build msgpack-ruby and install:
40
49
  MessagePack for Ruby should run on x86, ARM, PowerPC, SPARC and other CPU architectures.
41
50
 
42
51
  And it works with MRI (CRuby) and Rubinius.
43
- Patches to improve portability is highly welcomed.
52
+ Patches to improve portability are highly welcomed.
44
53
 
45
54
 
46
55
  ## Serializing objects
@@ -51,6 +60,7 @@ Use `MessagePack.pack` or `to_msgpack`:
51
60
  require 'msgpack'
52
61
  msg = MessagePack.pack(obj) # or
53
62
  msg = obj.to_msgpack
63
+ File.binwrite('mydata.msgpack', msg)
54
64
  ```
55
65
 
56
66
  ### Streaming serialization
@@ -71,6 +81,7 @@ Use `MessagePack.unpack`:
71
81
 
72
82
  ```ruby
73
83
  require 'msgpack'
84
+ msg = File.binread('mydata.msgpack')
74
85
  obj = MessagePack.unpack(msg)
75
86
  ```
76
87
 
@@ -126,9 +137,9 @@ being serialized altogether by throwing an exception:
126
137
 
127
138
  ```ruby
128
139
  class Symbol
129
- def to_msgpack_ext
130
- raise "Serialization of symbols prohibited"
131
- end
140
+ def to_msgpack_ext
141
+ raise "Serialization of symbols prohibited"
142
+ end
132
143
  end
133
144
 
134
145
  MessagePack::DefaultFactory.register_type(0x00, Symbol)
@@ -187,6 +198,55 @@ MessagePack::DefaultFactory.register_type(0x03, MyClass3)
187
198
  MessagePack.unpack(data_with_ext_typeid_03) #=> MyClass3 instance
188
199
  ```
189
200
 
201
+ Alternatively, extension types can call the packer or unpacker recursively to generate the extension data:
202
+
203
+ ```ruby
204
+ Point = Struct.new(:x, :y)
205
+ factory = MessagePack::Factory.new
206
+ factory.register_type(
207
+ 0x01,
208
+ Point,
209
+ packer: ->(point, packer) {
210
+ packer.write(point.x)
211
+ packer.write(point.y)
212
+ },
213
+ unpacker: ->(unpacker) {
214
+ x = unpacker.read
215
+ y = unpacker.read
216
+ Point.new(x, y)
217
+ },
218
+ recursive: true,
219
+ )
220
+ factory.load(factory.dump(Point.new(12, 34))) # => #<struct Point x=12, y=34>
221
+ ```
222
+
223
+ ## Pooling
224
+
225
+ Creating `Packer` and `Unpacker` objects is expensive. For best performance it is preferable to re-use these objects.
226
+
227
+ `MessagePack::Factory#pool` makes that easier:
228
+
229
+ ```ruby
230
+ factory = MessagePack::Factory.new
231
+ factory.register_type(
232
+ 0x01,
233
+ Point,
234
+ packer: ->(point, packer) {
235
+ packer.write(point.x)
236
+ packer.write(point.y)
237
+ },
238
+ unpacker: ->(unpacker) {
239
+ x = unpacker.read
240
+ y = unpacker.read
241
+ Point.new(x, y)
242
+ },
243
+ recursive: true,
244
+ )
245
+ pool = factory.pool(5) # The pool size should match the number of threads expected to use the factory concurrently.
246
+
247
+ pool.load(pool.dump(Point.new(12, 34))) # => #<struct Point x=12, y=34>
248
+ ```
249
+
190
250
  ## Buffer API
191
251
 
192
252
  MessagePack for Ruby provides a buffer API so that you can read or write data by hand, not via Packer or Unpacker API.
@@ -225,8 +285,8 @@ If this directory has Gemfile.lock (generated with MRI), remove it beforehand.
225
285
 
226
286
  ## Updating documents
227
287
 
228
- Online documents (http://ruby.msgpack.org) is generated from gh-pages branch.
229
- Following commands update documents in gh-pages branch:
288
+ Online documentation (https://ruby.msgpack.org) is generated from the gh-pages branch.
289
+ To update documents in gh-pages branch:
230
290
 
231
291
  bundle exec rake doc
232
292
  git checkout gh-pages
@@ -21,10 +21,11 @@ import org.jcodings.Encoding;
21
21
 
22
22
  @JRubyClass(name="MessagePack::Buffer")
23
23
  public class Buffer extends RubyObject {
24
- private IRubyObject io;
25
- private ByteBuffer buffer;
24
+ private static final long serialVersionUID = 8441244627425629412L;
25
+ private transient IRubyObject io;
26
+ private transient ByteBuffer buffer;
26
27
  private boolean writeMode;
27
- private Encoding binaryEncoding;
28
+ private transient Encoding binaryEncoding;
28
29
 
29
30
  private static final int CACHE_LINE_SIZE = 64;
30
31
  private static final int ARRAY_HEADER_SIZE = 24;
@@ -49,7 +50,7 @@ public class Buffer extends RubyObject {
49
50
  }
50
51
  this.buffer = ByteBuffer.allocate(CACHE_LINE_SIZE - ARRAY_HEADER_SIZE);
51
52
  this.writeMode = true;
52
- this.binaryEncoding = ctx.getRuntime().getEncodingService().getAscii8bitEncoding();
53
+ this.binaryEncoding = ctx.runtime.getEncodingService().getAscii8bitEncoding();
53
54
  return this;
54
55
  }
55
56
 
@@ -87,17 +88,17 @@ public class Buffer extends RubyObject {
87
88
  writeMode = true;
88
89
  }
89
90
  buffer.clear();
90
- return ctx.getRuntime().getNil();
91
+ return ctx.runtime.getNil();
91
92
  }
92
93
 
93
94
  @JRubyMethod(name = "size")
94
95
  public IRubyObject size(ThreadContext ctx) {
95
- return ctx.getRuntime().newFixnum(rawSize());
96
+ return ctx.runtime.newFixnum(rawSize());
96
97
  }
97
98
 
98
99
  @JRubyMethod(name = "empty?")
99
100
  public IRubyObject isEmpty(ThreadContext ctx) {
100
- return rawSize() == 0 ? ctx.getRuntime().getTrue() : ctx.getRuntime().getFalse();
101
+ return rawSize() == 0 ? ctx.runtime.getTrue() : ctx.runtime.getFalse();
101
102
  }
102
103
 
103
104
  private IRubyObject bufferWrite(ThreadContext ctx, IRubyObject str) {
@@ -105,7 +106,7 @@ public class Buffer extends RubyObject {
105
106
  int length = bytes.length();
106
107
  ensureRemainingCapacity(length);
107
108
  buffer.put(bytes.unsafeBytes(), bytes.begin(), length);
108
- return ctx.getRuntime().newFixnum(length);
109
+ return ctx.runtime.newFixnum(length);
109
110
 
110
111
  }
111
112
 
@@ -131,19 +132,19 @@ public class Buffer extends RubyObject {
131
132
  length = (int) args[0].convertToInteger().getLongValue();
132
133
  }
133
134
  if (raiseOnUnderflow && rawSize() < length) {
134
- throw ctx.getRuntime().newEOFError();
135
+ throw ctx.runtime.newEOFError();
135
136
  }
136
137
  int readLength = Math.min(length, rawSize());
137
138
  if (readLength == 0 && length > 0) {
138
- return ctx.getRuntime().getNil();
139
+ return ctx.runtime.getNil();
139
140
  } else if (readLength == 0) {
140
- return ctx.getRuntime().newString();
141
+ return ctx.runtime.newString();
141
142
  } else {
142
143
  ensureReadMode();
143
144
  byte[] bytes = new byte[readLength];
144
145
  buffer.get(bytes);
145
146
  ByteList byteList = new ByteList(bytes, binaryEncoding);
146
- return ctx.getRuntime().newString(byteList);
147
+ return ctx.runtime.newString(byteList);
147
148
  }
148
149
  }
149
150
 
@@ -161,12 +162,12 @@ public class Buffer extends RubyObject {
161
162
  feed(ctx);
162
163
  int length = (int) _length.convertToInteger().getLongValue();
163
164
  if (raiseOnUnderflow && rawSize() < length) {
164
- throw ctx.getRuntime().newEOFError();
165
+ throw ctx.runtime.newEOFError();
165
166
  }
166
167
  ensureReadMode();
167
168
  int skipLength = Math.min(length, rawSize());
168
169
  buffer.position(buffer.position() + skipLength);
169
- return ctx.getRuntime().newFixnum(skipLength);
170
+ return ctx.runtime.newFixnum(skipLength);
170
171
  }
171
172
 
172
173
  @JRubyMethod(name = "skip")
@@ -188,23 +189,23 @@ public class Buffer extends RubyObject {
188
189
  ensureReadMode();
189
190
  int length = buffer.limit() - buffer.position();
190
191
  ByteList str = new ByteList(buffer.array(), buffer.position(), length, binaryEncoding, true);
191
- return ctx.getRuntime().newString(str);
192
+ return ctx.runtime.newString(str);
192
193
  }
193
194
 
194
195
  @JRubyMethod(name = "to_a")
195
196
  public IRubyObject toA(ThreadContext ctx) {
196
- return ctx.getRuntime().newArray(toS(ctx));
197
+ return ctx.runtime.newArray(toS(ctx));
197
198
  }
198
199
 
199
200
  @JRubyMethod(name = "io")
200
201
  public IRubyObject getIo(ThreadContext ctx) {
201
- return io == null ? ctx.getRuntime().getNil() : io;
202
+ return io == null ? ctx.runtime.getNil() : io;
202
203
  }
203
204
 
204
205
  @JRubyMethod(name = "flush")
205
206
  public IRubyObject flush(ThreadContext ctx) {
206
207
  if (io == null) {
207
- return ctx.getRuntime().getNil();
208
+ return ctx.runtime.getNil();
208
209
  } else {
209
210
  return io.callMethod(ctx, "flush");
210
211
  }
@@ -213,7 +214,7 @@ public class Buffer extends RubyObject {
213
214
  @JRubyMethod(name = "close")
214
215
  public IRubyObject close(ThreadContext ctx) {
215
216
  if (io == null) {
216
- return ctx.getRuntime().getNil();
217
+ return ctx.runtime.getNil();
217
218
  } else {
218
219
  return io.callMethod(ctx, "close");
219
220
  }
@@ -223,4 +224,10 @@ public class Buffer extends RubyObject {
223
224
  public IRubyObject writeTo(ThreadContext ctx, IRubyObject io) {
224
225
  return io.callMethod(ctx, "write", readCommon(ctx, null, false));
225
226
  }
227
+
228
+ public ByteList getBytes() {
229
+ byte[] bytes = new byte[rawSize()];
230
+ buffer.get(bytes);
231
+ return new ByteList(bytes, binaryEncoding);
232
+ }
226
233
  }
@@ -14,6 +14,7 @@ import org.jruby.RubyBignum;
14
14
  import org.jruby.RubyString;
15
15
  import org.jruby.RubyArray;
16
16
  import org.jruby.RubyHash;
17
+ import org.jruby.RubyInteger;
17
18
  import org.jruby.exceptions.RaiseException;
18
19
  import org.jruby.runtime.builtin.IRubyObject;
19
20
  import org.jruby.util.ByteList;
@@ -35,7 +36,7 @@ public class Decoder implements Iterator<IRubyObject> {
35
36
  private final RubyClass unexpectedTypeErrorClass;
36
37
  private final RubyClass unknownExtTypeErrorClass;
37
38
 
38
- private ExtensionRegistry registry;
39
+ private Unpacker unpacker;
39
40
  private ByteBuffer buffer;
40
41
  private boolean symbolizeKeys;
41
42
  private boolean freeze;
@@ -45,29 +46,29 @@ public class Decoder implements Iterator<IRubyObject> {
45
46
  this(runtime, null, new byte[] {}, 0, 0, false, false, false);
46
47
  }
47
48
 
48
- public Decoder(Ruby runtime, ExtensionRegistry registry) {
49
- this(runtime, registry, new byte[] {}, 0, 0, false, false, false);
49
+ public Decoder(Ruby runtime, Unpacker unpacker) {
50
+ this(runtime, unpacker, new byte[] {}, 0, 0, false, false, false);
50
51
  }
51
52
 
52
53
  public Decoder(Ruby runtime, byte[] bytes) {
53
54
  this(runtime, null, bytes, 0, bytes.length, false, false, false);
54
55
  }
55
56
 
56
- public Decoder(Ruby runtime, ExtensionRegistry registry, byte[] bytes) {
57
- this(runtime, registry, bytes, 0, bytes.length, false, false, false);
57
+ public Decoder(Ruby runtime, Unpacker unpacker, byte[] bytes) {
58
+ this(runtime, unpacker, bytes, 0, bytes.length, false, false, false);
58
59
  }
59
60
 
60
- public Decoder(Ruby runtime, ExtensionRegistry registry, byte[] bytes, boolean symbolizeKeys, boolean freeze, boolean allowUnknownExt) {
61
- this(runtime, registry, bytes, 0, bytes.length, symbolizeKeys, freeze, allowUnknownExt);
61
+ public Decoder(Ruby runtime, Unpacker unpacker, byte[] bytes, boolean symbolizeKeys, boolean freeze, boolean allowUnknownExt) {
62
+ this(runtime, unpacker, bytes, 0, bytes.length, symbolizeKeys, freeze, allowUnknownExt);
62
63
  }
63
64
 
64
- public Decoder(Ruby runtime, ExtensionRegistry registry, byte[] bytes, int offset, int length) {
65
- this(runtime, registry, bytes, offset, length, false, false, false);
65
+ public Decoder(Ruby runtime, Unpacker unpacker, byte[] bytes, int offset, int length) {
66
+ this(runtime, unpacker, bytes, offset, length, false, false, false);
66
67
  }
67
68
 
68
- public Decoder(Ruby runtime, ExtensionRegistry registry, byte[] bytes, int offset, int length, boolean symbolizeKeys, boolean freeze, boolean allowUnknownExt) {
69
+ public Decoder(Ruby runtime, Unpacker unpacker, byte[] bytes, int offset, int length, boolean symbolizeKeys, boolean freeze, boolean allowUnknownExt) {
69
70
  this.runtime = runtime;
70
- this.registry = registry;
71
+ this.unpacker = unpacker;
71
72
  this.symbolizeKeys = symbolizeKeys;
72
73
  this.freeze = freeze;
73
74
  this.allowUnknownExt = allowUnknownExt;
@@ -122,7 +123,6 @@ public class Decoder implements Iterator<IRubyObject> {
122
123
  ByteList byteList = new ByteList(bytes, encoding);
123
124
  RubyString string = runtime.newString(byteList);
124
125
  if (this.freeze) {
125
- string.setFrozen(true);
126
126
  string = runtime.freezeAndDedupString(string);
127
127
  }
128
128
  return string;
@@ -140,9 +140,14 @@ public class Decoder implements Iterator<IRubyObject> {
140
140
  RubyHash hash = RubyHash.newHash(runtime);
141
141
  for (int i = 0; i < size; i++) {
142
142
  IRubyObject key = next();
143
- if (this.symbolizeKeys && key instanceof RubyString) {
143
+ if (key instanceof RubyString) {
144
+ if (this.symbolizeKeys) {
144
145
  key = ((RubyString) key).intern();
146
+ } else {
147
+ key = runtime.freezeAndDedupString((RubyString) key);
148
+ }
145
149
  }
150
+
146
151
  hash.fastASet(key, next());
147
152
  }
148
153
  return hash;
@@ -150,18 +155,21 @@ public class Decoder implements Iterator<IRubyObject> {
150
155
 
151
156
  private IRubyObject consumeExtension(int size) {
152
157
  int type = buffer.get();
153
- byte[] payload = readBytes(size);
154
-
155
- if (registry != null) {
156
- IRubyObject proc = registry.lookupUnpackerByTypeId(type);
157
- if (proc != null) {
158
- ByteList byteList = new ByteList(payload, runtime.getEncodingService().getAscii8bitEncoding());
159
- return proc.callMethod(runtime.getCurrentContext(), "call", runtime.newString(byteList));
158
+ if (unpacker != null) {
159
+ ExtensionRegistry.ExtensionEntry entry = unpacker.lookupExtensionByTypeId(type);
160
+ if (entry != null) {
161
+ IRubyObject proc = entry.getUnpackerProc();
162
+ if (entry.isRecursive()) {
163
+ return proc.callMethod(runtime.getCurrentContext(), "call", unpacker);
164
+ } else {
165
+ ByteList byteList = new ByteList(readBytes(size), runtime.getEncodingService().getAscii8bitEncoding());
166
+ return proc.callMethod(runtime.getCurrentContext(), "call", runtime.newString(byteList));
167
+ }
160
168
  }
161
169
  }
162
170
 
163
171
  if (this.allowUnknownExt) {
164
- return ExtensionValue.newExtensionValue(runtime, type, payload);
172
+ return ExtensionValue.newExtensionValue(runtime, type, readBytes(size));
165
173
  }
166
174
 
167
175
  throw runtime.newRaiseException(unknownExtTypeErrorClass, "unexpected extension type");