msgpack 1.2.9 → 1.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ci.yaml +56 -0
  3. data/.gitignore +3 -1
  4. data/.rubocop.yml +4 -1
  5. data/ChangeLog +46 -0
  6. data/Gemfile +3 -0
  7. data/README.md +242 -0
  8. data/Rakefile +1 -9
  9. data/doclib/msgpack/factory.rb +1 -0
  10. data/doclib/msgpack/time.rb +22 -0
  11. data/doclib/msgpack/timestamp.rb +44 -0
  12. data/ext/java/org/msgpack/jruby/Buffer.java +17 -16
  13. data/ext/java/org/msgpack/jruby/Decoder.java +29 -10
  14. data/ext/java/org/msgpack/jruby/Encoder.java +22 -12
  15. data/ext/java/org/msgpack/jruby/ExtensionRegistry.java +9 -9
  16. data/ext/java/org/msgpack/jruby/ExtensionValue.java +5 -8
  17. data/ext/java/org/msgpack/jruby/Factory.java +7 -2
  18. data/ext/java/org/msgpack/jruby/Packer.java +11 -9
  19. data/ext/java/org/msgpack/jruby/Unpacker.java +40 -27
  20. data/ext/msgpack/buffer.c +4 -16
  21. data/ext/msgpack/buffer.h +60 -5
  22. data/ext/msgpack/compat.h +1 -12
  23. data/ext/msgpack/extconf.rb +39 -7
  24. data/ext/msgpack/factory_class.c +10 -5
  25. data/ext/msgpack/packer.c +18 -5
  26. data/ext/msgpack/packer.h +0 -16
  27. data/ext/msgpack/packer_class.c +0 -9
  28. data/ext/msgpack/packer_ext_registry.c +0 -22
  29. data/ext/msgpack/unpacker.c +41 -49
  30. data/ext/msgpack/unpacker.h +8 -0
  31. data/ext/msgpack/unpacker_class.c +23 -13
  32. data/lib/msgpack/symbol.rb +14 -4
  33. data/lib/msgpack/time.rb +29 -0
  34. data/lib/msgpack/timestamp.rb +76 -0
  35. data/lib/msgpack/version.rb +4 -7
  36. data/lib/msgpack.rb +5 -8
  37. data/msgpack.gemspec +3 -7
  38. data/spec/factory_spec.rb +17 -0
  39. data/spec/msgpack_spec.rb +1 -1
  40. data/spec/packer_spec.rb +18 -0
  41. data/spec/spec_helper.rb +27 -0
  42. data/spec/timestamp_spec.rb +161 -0
  43. data/spec/unpacker_spec.rb +113 -1
  44. metadata +19 -51
  45. data/.travis.yml +0 -41
  46. data/README.rdoc +0 -209
@@ -38,36 +38,38 @@ public class Decoder implements Iterator<IRubyObject> {
38
38
  private ExtensionRegistry registry;
39
39
  private ByteBuffer buffer;
40
40
  private boolean symbolizeKeys;
41
+ private boolean freeze;
41
42
  private boolean allowUnknownExt;
42
43
 
43
44
  public Decoder(Ruby runtime) {
44
- this(runtime, null, new byte[] {}, 0, 0, false, false);
45
+ this(runtime, null, new byte[] {}, 0, 0, false, false, false);
45
46
  }
46
47
 
47
48
  public Decoder(Ruby runtime, ExtensionRegistry registry) {
48
- this(runtime, registry, new byte[] {}, 0, 0, false, false);
49
+ this(runtime, registry, new byte[] {}, 0, 0, false, false, false);
49
50
  }
50
51
 
51
52
  public Decoder(Ruby runtime, byte[] bytes) {
52
- this(runtime, null, bytes, 0, bytes.length, false, false);
53
+ this(runtime, null, bytes, 0, bytes.length, false, false, false);
53
54
  }
54
55
 
55
56
  public Decoder(Ruby runtime, ExtensionRegistry registry, byte[] bytes) {
56
- this(runtime, registry, bytes, 0, bytes.length, false, false);
57
+ this(runtime, registry, bytes, 0, bytes.length, false, false, false);
57
58
  }
58
59
 
59
- public Decoder(Ruby runtime, ExtensionRegistry registry, byte[] bytes, boolean symbolizeKeys, boolean allowUnknownExt) {
60
- this(runtime, registry, bytes, 0, bytes.length, symbolizeKeys, allowUnknownExt);
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
62
  }
62
63
 
63
64
  public Decoder(Ruby runtime, ExtensionRegistry registry, byte[] bytes, int offset, int length) {
64
- this(runtime, registry, bytes, offset, length, false, false);
65
+ this(runtime, registry, bytes, offset, length, false, false, false);
65
66
  }
66
67
 
67
- public Decoder(Ruby runtime, ExtensionRegistry registry, byte[] bytes, int offset, int length, boolean symbolizeKeys, boolean allowUnknownExt) {
68
+ public Decoder(Ruby runtime, ExtensionRegistry registry, byte[] bytes, int offset, int length, boolean symbolizeKeys, boolean freeze, boolean allowUnknownExt) {
68
69
  this.runtime = runtime;
69
70
  this.registry = registry;
70
71
  this.symbolizeKeys = symbolizeKeys;
72
+ this.freeze = freeze;
71
73
  this.allowUnknownExt = allowUnknownExt;
72
74
  this.binaryEncoding = runtime.getEncodingService().getAscii8bitEncoding();
73
75
  this.utf8Encoding = UTF8Encoding.INSTANCE;
@@ -118,7 +120,11 @@ public class Decoder implements Iterator<IRubyObject> {
118
120
  private IRubyObject consumeString(int size, Encoding encoding) {
119
121
  byte[] bytes = readBytes(size);
120
122
  ByteList byteList = new ByteList(bytes, encoding);
121
- return runtime.newString(byteList);
123
+ RubyString string = runtime.newString(byteList);
124
+ if (this.freeze) {
125
+ string = runtime.freezeAndDedupString(string);
126
+ }
127
+ return string;
122
128
  }
123
129
 
124
130
  private IRubyObject consumeArray(int size) {
@@ -133,9 +139,14 @@ public class Decoder implements Iterator<IRubyObject> {
133
139
  RubyHash hash = RubyHash.newHash(runtime);
134
140
  for (int i = 0; i < size; i++) {
135
141
  IRubyObject key = next();
136
- if (this.symbolizeKeys && key instanceof RubyString) {
142
+ if (key instanceof RubyString) {
143
+ if (this.symbolizeKeys) {
137
144
  key = ((RubyString) key).intern();
145
+ } else {
146
+ key = runtime.freezeAndDedupString((RubyString) key);
147
+ }
138
148
  }
149
+
139
150
  hash.fastASet(key, next());
140
151
  }
141
152
  return hash;
@@ -220,6 +231,14 @@ public class Decoder implements Iterator<IRubyObject> {
220
231
 
221
232
  @Override
222
233
  public IRubyObject next() {
234
+ IRubyObject next = consumeNext();
235
+ if (freeze) {
236
+ next.setFrozen(true);
237
+ }
238
+ return next;
239
+ }
240
+
241
+ private IRubyObject consumeNext() {
223
242
  int position = buffer.position();
224
243
  try {
225
244
  byte b = buffer.get();
@@ -119,7 +119,9 @@ public class Encoder {
119
119
  } else if (object instanceof RubyFloat) {
120
120
  appendFloat((RubyFloat) object);
121
121
  } else if (object instanceof RubyString) {
122
- appendString((RubyString) object);
122
+ if (object.getType() == runtime.getString() || !tryAppendWithExtTypeLookup(object)) {
123
+ appendString((RubyString) object);
124
+ }
123
125
  } else if (object instanceof RubySymbol) {
124
126
  if (hasSymbolExtType) {
125
127
  appendOther(object, destination);
@@ -127,9 +129,13 @@ public class Encoder {
127
129
  appendString(((RubySymbol) object).asString());
128
130
  }
129
131
  } else if (object instanceof RubyArray) {
130
- appendArray((RubyArray) object);
132
+ if (object.getType() == runtime.getArray() || !tryAppendWithExtTypeLookup(object)) {
133
+ appendArray((RubyArray) object);
134
+ }
131
135
  } else if (object instanceof RubyHash) {
132
- appendHash((RubyHash) object);
136
+ if (object.getType() == runtime.getHash() || !tryAppendWithExtTypeLookup(object)) {
137
+ appendHash((RubyHash) object);
138
+ }
133
139
  } else if (object instanceof ExtensionValue) {
134
140
  appendExtensionValue((ExtensionValue) object);
135
141
  } else {
@@ -153,7 +159,7 @@ public class Encoder {
153
159
  }
154
160
 
155
161
  private void appendInteger(RubyInteger object) {
156
- long value = ((RubyInteger) object).getLongValue();
162
+ long value = object.getLongValue();
157
163
  if (value < 0) {
158
164
  if (value < Short.MIN_VALUE) {
159
165
  if (value < Integer.MIN_VALUE) {
@@ -241,7 +247,7 @@ public class Encoder {
241
247
  } else {
242
248
  ensureRemainingCapacity(5 + length);
243
249
  buffer.put(binary ? BIN32 : STR32);
244
- buffer.putInt((int) length);
250
+ buffer.putInt(length);
245
251
  }
246
252
  }
247
253
 
@@ -249,7 +255,7 @@ public class Encoder {
249
255
  Encoding encoding = object.getEncoding();
250
256
  boolean binary = !compatibilityMode && encoding == binaryEncoding;
251
257
  if (encoding != utf8Encoding && encoding != binaryEncoding) {
252
- object = (RubyString) ((RubyString) object).encode(runtime.getCurrentContext(), runtime.getEncodingService().getEncoding(utf8Encoding));
258
+ object = (RubyString)(object).encode(runtime.getCurrentContext(), runtime.getEncodingService().getEncoding(utf8Encoding));
253
259
  }
254
260
  ByteList bytes = object.getByteList();
255
261
  int length = bytes.length();
@@ -257,12 +263,12 @@ public class Encoder {
257
263
  buffer.put(bytes.unsafeBytes(), bytes.begin(), length);
258
264
  }
259
265
 
260
- private void appendArray(RubyArray object) {
266
+ private void appendArray(RubyArray<?> object) {
261
267
  appendArrayHeader(object);
262
268
  appendArrayElements(object);
263
269
  }
264
270
 
265
- private void appendArrayHeader(RubyArray object) {
271
+ private void appendArrayHeader(RubyArray<?> object) {
266
272
  appendArrayHeader(object.size());
267
273
  }
268
274
 
@@ -281,7 +287,7 @@ public class Encoder {
281
287
  }
282
288
  }
283
289
 
284
- private void appendArrayElements(RubyArray object) {
290
+ private void appendArrayElements(RubyArray<?> object) {
285
291
  int size = object.size();
286
292
  for (int i = 0; i < size; i++) {
287
293
  appendObject(object.eltOk(i));
@@ -383,7 +389,7 @@ public class Encoder {
383
389
  appendExt((int) type, payloadBytes);
384
390
  }
385
391
 
386
- private void appendOther(IRubyObject object, IRubyObject destination) {
392
+ private boolean tryAppendWithExtTypeLookup(IRubyObject object) {
387
393
  if (registry != null) {
388
394
  RubyModule lookupClass;
389
395
 
@@ -398,10 +404,14 @@ public class Encoder {
398
404
  RubyString bytes = pair[0].callMethod(runtime.getCurrentContext(), "call", object).asString();
399
405
  int type = (int) ((RubyFixnum) pair[1]).getLongValue();
400
406
  appendExt(type, bytes.getByteList());
401
- return;
407
+ return true;
402
408
  }
403
409
  }
404
- appendCustom(object, destination);
410
+ return false;
411
+ }
412
+
413
+ private void appendOther(IRubyObject object, IRubyObject destination) {
414
+ if (!tryAppendWithExtTypeLookup(object)) { appendCustom(object, destination); }
405
415
  }
406
416
 
407
417
  private void appendCustom(IRubyObject object, IRubyObject destination) {
@@ -37,7 +37,7 @@ public class ExtensionRegistry {
37
37
  }
38
38
 
39
39
  public IRubyObject toInternalPackerRegistry(ThreadContext ctx) {
40
- RubyHash hash = RubyHash.newHash(ctx.getRuntime());
40
+ RubyHash hash = RubyHash.newHash(ctx.runtime);
41
41
  for (RubyModule extensionModule : extensionsByModule.keySet()) {
42
42
  ExtensionEntry entry = extensionsByModule.get(extensionModule);
43
43
  if (entry.hasPacker()) {
@@ -48,11 +48,11 @@ public class ExtensionRegistry {
48
48
  }
49
49
 
50
50
  public IRubyObject toInternalUnpackerRegistry(ThreadContext ctx) {
51
- RubyHash hash = RubyHash.newHash(ctx.getRuntime());
51
+ RubyHash hash = RubyHash.newHash(ctx.runtime);
52
52
  for (int typeIdIndex = 0 ; typeIdIndex < 256 ; typeIdIndex++) {
53
53
  ExtensionEntry entry = extensionsByTypeId[typeIdIndex];
54
54
  if (entry != null && entry.hasUnpacker()) {
55
- IRubyObject typeId = RubyFixnum.newFixnum(ctx.getRuntime(), typeIdIndex - 128);
55
+ IRubyObject typeId = RubyFixnum.newFixnum(ctx.runtime, typeIdIndex - 128);
56
56
  hash.put(typeId, entry.toUnpackerTuple(ctx));
57
57
  }
58
58
  }
@@ -124,7 +124,7 @@ public class ExtensionRegistry {
124
124
  private ExtensionEntry findEntryByModuleOrAncestor(final RubyModule mod) {
125
125
  ThreadContext ctx = mod.getRuntime().getCurrentContext();
126
126
  for (RubyModule extensionModule : extensionsByModule.keySet()) {
127
- RubyArray ancestors = (RubyArray) mod.callMethod(ctx, "ancestors");
127
+ RubyArray<?> ancestors = (RubyArray)mod.callMethod(ctx, "ancestors");
128
128
  if (ancestors.callMethod(ctx, "include?", extensionModule).isTrue()) {
129
129
  return extensionsByModule.get(extensionModule);
130
130
  }
@@ -173,16 +173,16 @@ public class ExtensionRegistry {
173
173
  return unpackerProc;
174
174
  }
175
175
 
176
- public RubyArray toPackerTuple(ThreadContext ctx) {
177
- return RubyArray.newArray(ctx.getRuntime(), new IRubyObject[] {RubyFixnum.newFixnum(ctx.getRuntime(), typeId), packerProc, packerArg});
176
+ public RubyArray<?> toPackerTuple(ThreadContext ctx) {
177
+ return ctx.runtime.newArray(new IRubyObject[] {ctx.runtime.newFixnum(typeId), packerProc, packerArg});
178
178
  }
179
179
 
180
- public RubyArray toUnpackerTuple(ThreadContext ctx) {
181
- return RubyArray.newArray(ctx.getRuntime(), new IRubyObject[] {mod, unpackerProc, unpackerArg});
180
+ public RubyArray<?> toUnpackerTuple(ThreadContext ctx) {
181
+ return ctx.runtime.newArray(new IRubyObject[] {mod, unpackerProc, unpackerArg});
182
182
  }
183
183
 
184
184
  public IRubyObject[] toPackerProcTypeIdPair(ThreadContext ctx) {
185
- return new IRubyObject[] {packerProc, RubyFixnum.newFixnum(ctx.getRuntime(), typeId)};
185
+ return new IRubyObject[] {packerProc, ctx.runtime.newFixnum(typeId)};
186
186
  }
187
187
  }
188
188
  }
@@ -25,6 +25,7 @@ import static org.msgpack.jruby.Types.*;
25
25
 
26
26
  @JRubyClass(name="MessagePack::ExtensionValue")
27
27
  public class ExtensionValue extends RubyObject {
28
+ private static final long serialVersionUID = 8451274621449322492L;
28
29
  private final Encoding binaryEncoding;
29
30
 
30
31
  private RubyFixnum type;
@@ -77,12 +78,10 @@ public class ExtensionValue extends RubyObject {
77
78
  }
78
79
  if (o instanceof ExtensionValue) {
79
80
  ExtensionValue other = (ExtensionValue) o;
80
- if (!this.type.eql_p(other.type).isTrue())
81
+ if (!this.type.eql_p(other.type).isTrue()) {
81
82
  return runtime.getFalse();
82
- if (runtime.is1_8()) {
83
- return this.payload.str_eql_p(ctx, other.payload);
84
83
  } else {
85
- return this.payload.str_eql_p19(ctx, other.payload);
84
+ return this.payload.str_eql_p(ctx, other.payload);
86
85
  }
87
86
  }
88
87
  return runtime.getFalse();
@@ -96,12 +95,10 @@ public class ExtensionValue extends RubyObject {
96
95
  }
97
96
  if (o instanceof ExtensionValue) {
98
97
  ExtensionValue other = (ExtensionValue) o;
99
- if (!this.type.op_equal(ctx, other.type).isTrue())
98
+ if (!this.type.op_equal(ctx, other.type).isTrue()) {
100
99
  return runtime.getFalse();
101
- if (runtime.is1_8()) {
102
- return this.payload.op_equal(ctx, other.payload);
103
100
  } else {
104
- return this.payload.op_equal19(ctx, other.payload);
101
+ return this.payload.op_equal(ctx, other.payload);
105
102
  }
106
103
  }
107
104
  return runtime.getFalse();
@@ -23,6 +23,7 @@ import static org.jruby.runtime.Visibility.PRIVATE;
23
23
 
24
24
  @JRubyClass(name="MessagePack::Factory")
25
25
  public class Factory extends RubyObject {
26
+ private static final long serialVersionUID = 8441284623445322492L;
26
27
  private final Ruby runtime;
27
28
  private final ExtensionRegistry extensionRegistry;
28
29
  private boolean hasSymbolExtType;
@@ -61,7 +62,7 @@ public class Factory extends RubyObject {
61
62
 
62
63
  @JRubyMethod(name = "registered_types_internal", visibility = PRIVATE)
63
64
  public IRubyObject registeredTypesInternal(ThreadContext ctx) {
64
- return RubyArray.newArray(ctx.getRuntime(), new IRubyObject[] {
65
+ return RubyArray.newArray(ctx.runtime, new IRubyObject[] {
65
66
  extensionRegistry.toInternalPackerRegistry(ctx),
66
67
  extensionRegistry.toInternalUnpackerRegistry(ctx)
67
68
  });
@@ -69,7 +70,7 @@ public class Factory extends RubyObject {
69
70
 
70
71
  @JRubyMethod(name = "register_type", required = 2, optional = 1)
71
72
  public IRubyObject registerType(ThreadContext ctx, IRubyObject[] args) {
72
- Ruby runtime = ctx.getRuntime();
73
+ Ruby runtime = ctx.runtime;
73
74
  IRubyObject type = args[0];
74
75
  IRubyObject mod = args[1];
75
76
 
@@ -88,6 +89,10 @@ public class Factory extends RubyObject {
88
89
  RubyHash options = (RubyHash) args[args.length - 1];
89
90
  packerArg = options.fastARef(runtime.newSymbol("packer"));
90
91
  unpackerArg = options.fastARef(runtime.newSymbol("unpacker"));
92
+ IRubyObject optimizedSymbolsParsingArg = options.fastARef(runtime.newSymbol("optimized_symbols_parsing"));
93
+ if (optimizedSymbolsParsingArg != null && optimizedSymbolsParsingArg.isTrue()) {
94
+ throw runtime.newArgumentError("JRuby implementation does not support the optimized_symbols_parsing option");
95
+ }
91
96
  } else {
92
97
  throw runtime.newArgumentError(String.format("expected Hash but found %s.", args[args.length - 1].getType().getName()));
93
98
  }
@@ -27,6 +27,7 @@ import static org.jruby.runtime.Visibility.PRIVATE;
27
27
 
28
28
  @JRubyClass(name="MessagePack::Packer")
29
29
  public class Packer extends RubyObject {
30
+ private static final long serialVersionUID = 8451274621499362492L;
30
31
  public ExtensionRegistry registry;
31
32
  private Buffer buffer;
32
33
  private Encoder encoder;
@@ -48,9 +49,10 @@ public class Packer extends RubyObject {
48
49
  @JRubyMethod(name = "initialize", optional = 2)
49
50
  public IRubyObject initialize(ThreadContext ctx, IRubyObject[] args) {
50
51
  boolean compatibilityMode = false;
52
+ Ruby runtime = ctx.runtime;
51
53
  if (args.length > 0 && args[args.length - 1] instanceof RubyHash) {
52
54
  RubyHash options = (RubyHash) args[args.length - 1];
53
- IRubyObject mode = options.fastARef(ctx.getRuntime().newSymbol("compatibility_mode"));
55
+ IRubyObject mode = options.fastARef(runtime.newSymbol("compatibility_mode"));
54
56
  compatibilityMode = (mode != null) && mode.isTrue();
55
57
  }
56
58
  if (registry == null) {
@@ -58,22 +60,22 @@ public class Packer extends RubyObject {
58
60
  // registry is already initialized (and somthing might be registered) when newPacker from Factory
59
61
  this.registry = new ExtensionRegistry();
60
62
  }
61
- this.encoder = new Encoder(ctx.getRuntime(), compatibilityMode, registry, hasSymbolExtType);
62
- this.buffer = new Buffer(ctx.getRuntime(), ctx.getRuntime().getModule("MessagePack").getClass("Buffer"));
63
+ this.encoder = new Encoder(runtime, compatibilityMode, registry, hasSymbolExtType);
64
+ this.buffer = new Buffer(runtime, runtime.getModule("MessagePack").getClass("Buffer"));
63
65
  this.buffer.initialize(ctx, args);
64
- this.binaryEncoding = ctx.getRuntime().getEncodingService().getAscii8bitEncoding();
66
+ this.binaryEncoding = runtime.getEncodingService().getAscii8bitEncoding();
65
67
  return this;
66
68
  }
67
69
 
68
70
  public static Packer newPacker(ThreadContext ctx, ExtensionRegistry extRegistry, boolean hasSymbolExtType, IRubyObject[] args) {
69
- Packer packer = new Packer(ctx.getRuntime(), ctx.getRuntime().getModule("MessagePack").getClass("Packer"), extRegistry, hasSymbolExtType);
71
+ Packer packer = new Packer(ctx.runtime, ctx.runtime.getModule("MessagePack").getClass("Packer"), extRegistry, hasSymbolExtType);
70
72
  packer.initialize(ctx, args);
71
73
  return packer;
72
74
  }
73
75
 
74
76
  @JRubyMethod(name = "compatibility_mode?")
75
77
  public IRubyObject isCompatibilityMode(ThreadContext ctx) {
76
- return encoder.isCompatibilityMode() ? ctx.getRuntime().getTrue() : ctx.getRuntime().getFalse();
78
+ return encoder.isCompatibilityMode() ? ctx.runtime.getTrue() : ctx.runtime.getFalse();
77
79
  }
78
80
 
79
81
  @JRubyMethod(name = "registered_types_internal", visibility = PRIVATE)
@@ -83,7 +85,7 @@ public class Packer extends RubyObject {
83
85
 
84
86
  @JRubyMethod(name = "register_type", required = 2, optional = 1)
85
87
  public IRubyObject registerType(ThreadContext ctx, IRubyObject[] args, final Block block) {
86
- Ruby runtime = ctx.getRuntime();
88
+ Ruby runtime = ctx.runtime;
87
89
  IRubyObject type = args[0];
88
90
  IRubyObject mod = args[1];
89
91
 
@@ -182,12 +184,12 @@ public class Packer extends RubyObject {
182
184
 
183
185
  @JRubyMethod(name = "write_true")
184
186
  public IRubyObject writeTrue(ThreadContext ctx) {
185
- return write(ctx, ctx.getRuntime().getTrue());
187
+ return write(ctx, ctx.runtime.getTrue());
186
188
  }
187
189
 
188
190
  @JRubyMethod(name = "write_false")
189
191
  public IRubyObject writeFalse(ThreadContext ctx) {
190
- return write(ctx, ctx.getRuntime().getFalse());
192
+ return write(ctx, ctx.runtime.getFalse());
191
193
  }
192
194
 
193
195
  @JRubyMethod(name = "write_nil")
@@ -27,6 +27,7 @@ import static org.jruby.runtime.Visibility.PRIVATE;
27
27
 
28
28
  @JRubyClass(name="MessagePack::Unpacker")
29
29
  public class Unpacker extends RubyObject {
30
+ private static final long serialVersionUID = 8451264671199362492L;
30
31
  private final ExtensionRegistry registry;
31
32
 
32
33
  private IRubyObject stream;
@@ -34,6 +35,7 @@ public class Unpacker extends RubyObject {
34
35
  private Decoder decoder;
35
36
  private final RubyClass underflowErrorClass;
36
37
  private boolean symbolizeKeys;
38
+ private boolean freeze;
37
39
  private boolean allowUnknownExt;
38
40
 
39
41
  public Unpacker(Ruby runtime, RubyClass type) {
@@ -56,19 +58,25 @@ public class Unpacker extends RubyObject {
56
58
  public IRubyObject initialize(ThreadContext ctx, IRubyObject[] args) {
57
59
  symbolizeKeys = false;
58
60
  allowUnknownExt = false;
61
+ freeze = false;
59
62
  if (args.length > 0) {
63
+ Ruby runtime = ctx.runtime;
60
64
  if (args[args.length - 1] instanceof RubyHash) {
61
65
  RubyHash options = (RubyHash) args[args.length - 1];
62
- IRubyObject sk = options.fastARef(ctx.getRuntime().newSymbol("symbolize_keys"));
66
+ IRubyObject sk = options.fastARef(runtime.newSymbol("symbolize_keys"));
63
67
  if (sk != null) {
64
68
  symbolizeKeys = sk.isTrue();
65
69
  }
66
- IRubyObject au = options.fastARef(ctx.getRuntime().newSymbol("allow_unknown_ext"));
70
+ IRubyObject f = options.fastARef(runtime.newSymbol("freeze"));
71
+ if (f != null) {
72
+ freeze = f.isTrue();
73
+ }
74
+ IRubyObject au = options.fastARef(runtime.newSymbol("allow_unknown_ext"));
67
75
  if (au != null) {
68
76
  allowUnknownExt = au.isTrue();
69
77
  }
70
78
  }
71
- if (args[0] != ctx.getRuntime().getNil() && !(args[0] instanceof RubyHash)) {
79
+ if (args[0] != runtime.getNil() && !(args[0] instanceof RubyHash)) {
72
80
  setStream(ctx, args[0]);
73
81
  }
74
82
  }
@@ -76,19 +84,24 @@ public class Unpacker extends RubyObject {
76
84
  }
77
85
 
78
86
  public static Unpacker newUnpacker(ThreadContext ctx, ExtensionRegistry extRegistry, IRubyObject[] args) {
79
- Unpacker unpacker = new Unpacker(ctx.getRuntime(), ctx.getRuntime().getModule("MessagePack").getClass("Unpacker"), extRegistry);
87
+ Unpacker unpacker = new Unpacker(ctx.runtime, ctx.runtime.getModule("MessagePack").getClass("Unpacker"), extRegistry);
80
88
  unpacker.initialize(ctx, args);
81
89
  return unpacker;
82
90
  }
83
91
 
84
92
  @JRubyMethod(name = "symbolize_keys?")
85
93
  public IRubyObject isSymbolizeKeys(ThreadContext ctx) {
86
- return symbolizeKeys ? ctx.getRuntime().getTrue() : ctx.getRuntime().getFalse();
94
+ return symbolizeKeys ? ctx.runtime.getTrue() : ctx.runtime.getFalse();
95
+ }
96
+
97
+ @JRubyMethod(name = "freeze?")
98
+ public IRubyObject isFreeze(ThreadContext ctx) {
99
+ return freeze ? ctx.runtime.getTrue() : ctx.runtime.getFalse();
87
100
  }
88
101
 
89
102
  @JRubyMethod(name = "allow_unknown_ext?")
90
103
  public IRubyObject isAllowUnknownExt(ThreadContext ctx) {
91
- return allowUnknownExt ? ctx.getRuntime().getTrue() : ctx.getRuntime().getFalse();
104
+ return allowUnknownExt ? ctx.runtime.getTrue() : ctx.runtime.getFalse();
92
105
  }
93
106
 
94
107
  @JRubyMethod(name = "registered_types_internal", visibility = PRIVATE)
@@ -98,7 +111,7 @@ public class Unpacker extends RubyObject {
98
111
 
99
112
  @JRubyMethod(name = "register_type", required = 1, optional = 2)
100
113
  public IRubyObject registerType(ThreadContext ctx, IRubyObject[] args, final Block block) {
101
- Ruby runtime = ctx.getRuntime();
114
+ Ruby runtime = ctx.runtime;
102
115
  IRubyObject type = args[0];
103
116
 
104
117
  RubyModule extModule;
@@ -144,7 +157,7 @@ public class Unpacker extends RubyObject {
144
157
  if (limit == -1) {
145
158
  limit = byteList.length() - offset;
146
159
  }
147
- Decoder decoder = new Decoder(ctx.getRuntime(), registry, byteList.unsafeBytes(), byteList.begin() + offset, limit, symbolizeKeys, allowUnknownExt);
160
+ Decoder decoder = new Decoder(ctx.runtime, registry, byteList.unsafeBytes(), byteList.begin() + offset, limit, symbolizeKeys, freeze, allowUnknownExt);
148
161
  try {
149
162
  data = null;
150
163
  data = decoder.next();
@@ -153,13 +166,13 @@ public class Unpacker extends RubyObject {
153
166
  throw re;
154
167
  }
155
168
  }
156
- return ctx.getRuntime().newFixnum(decoder.offset());
169
+ return ctx.runtime.newFixnum(decoder.offset());
157
170
  }
158
171
 
159
172
  @JRubyMethod(name = "data")
160
173
  public IRubyObject getData(ThreadContext ctx) {
161
174
  if (data == null) {
162
- return ctx.getRuntime().getNil();
175
+ return ctx.runtime.getNil();
163
176
  } else {
164
177
  return data;
165
178
  }
@@ -167,14 +180,14 @@ public class Unpacker extends RubyObject {
167
180
 
168
181
  @JRubyMethod(name = "finished?")
169
182
  public IRubyObject finished_p(ThreadContext ctx) {
170
- return data == null ? ctx.getRuntime().getFalse() : ctx.getRuntime().getTrue();
183
+ return data == null ? ctx.runtime.getFalse() : ctx.runtime.getTrue();
171
184
  }
172
185
 
173
- @JRubyMethod(required = 1)
186
+ @JRubyMethod(required = 1, name = "feed", alias = { "feed_reference" })
174
187
  public IRubyObject feed(ThreadContext ctx, IRubyObject data) {
175
188
  ByteList byteList = data.asString().getByteList();
176
189
  if (decoder == null) {
177
- decoder = new Decoder(ctx.getRuntime(), registry, byteList.unsafeBytes(), byteList.begin(), byteList.length(), symbolizeKeys, allowUnknownExt);
190
+ decoder = new Decoder(ctx.runtime, registry, byteList.unsafeBytes(), byteList.begin(), byteList.length(), symbolizeKeys, freeze, allowUnknownExt);
178
191
  } else {
179
192
  decoder.feed(byteList.unsafeBytes(), byteList.begin(), byteList.length());
180
193
  }
@@ -191,7 +204,7 @@ public class Unpacker extends RubyObject {
191
204
  feed(ctx, data);
192
205
  if (block.isGiven()) {
193
206
  each(ctx, block);
194
- return ctx.getRuntime().getNil();
207
+ return ctx.runtime.getNil();
195
208
  } else {
196
209
  return callMethod(ctx, "to_enum");
197
210
  }
@@ -219,7 +232,7 @@ public class Unpacker extends RubyObject {
219
232
 
220
233
  @JRubyMethod
221
234
  public IRubyObject fill(ThreadContext ctx) {
222
- return ctx.getRuntime().getNil();
235
+ return ctx.runtime.getNil();
223
236
  }
224
237
 
225
238
  @JRubyMethod
@@ -227,13 +240,13 @@ public class Unpacker extends RubyObject {
227
240
  if (decoder != null) {
228
241
  decoder.reset();
229
242
  }
230
- return ctx.getRuntime().getNil();
243
+ return ctx.runtime.getNil();
231
244
  }
232
245
 
233
246
  @JRubyMethod(name = "read", alias = { "unpack" })
234
247
  public IRubyObject read(ThreadContext ctx) {
235
248
  if (decoder == null) {
236
- throw ctx.getRuntime().newEOFError();
249
+ throw ctx.runtime.newEOFError();
237
250
  }
238
251
  try {
239
252
  return decoder.next();
@@ -241,19 +254,19 @@ public class Unpacker extends RubyObject {
241
254
  if (re.getException().getType() != underflowErrorClass) {
242
255
  throw re;
243
256
  } else {
244
- throw ctx.getRuntime().newEOFError();
257
+ throw ctx.runtime.newEOFError();
245
258
  }
246
259
  }
247
260
  }
248
261
 
249
262
  @JRubyMethod(name = "skip")
250
263
  public IRubyObject skip(ThreadContext ctx) {
251
- throw ctx.getRuntime().newNotImplementedError("Not supported yet in JRuby implementation");
264
+ throw ctx.runtime.newNotImplementedError("Not supported yet in JRuby implementation");
252
265
  }
253
266
 
254
267
  @JRubyMethod(name = "skip_nil")
255
268
  public IRubyObject skipNil(ThreadContext ctx) {
256
- throw ctx.getRuntime().newNotImplementedError("Not supported yet in JRuby implementation");
269
+ throw ctx.runtime.newNotImplementedError("Not supported yet in JRuby implementation");
257
270
  }
258
271
 
259
272
  @JRubyMethod
@@ -265,11 +278,11 @@ public class Unpacker extends RubyObject {
265
278
  if (re.getException().getType() != underflowErrorClass) {
266
279
  throw re;
267
280
  } else {
268
- throw ctx.getRuntime().newEOFError();
281
+ throw ctx.runtime.newEOFError();
269
282
  }
270
283
  }
271
284
  }
272
- return ctx.getRuntime().getNil();
285
+ return ctx.runtime.getNil();
273
286
  }
274
287
 
275
288
  @JRubyMethod
@@ -281,17 +294,17 @@ public class Unpacker extends RubyObject {
281
294
  if (re.getException().getType() != underflowErrorClass) {
282
295
  throw re;
283
296
  } else {
284
- throw ctx.getRuntime().newEOFError();
297
+ throw ctx.runtime.newEOFError();
285
298
  }
286
299
  }
287
300
  }
288
- return ctx.getRuntime().getNil();
301
+ return ctx.runtime.getNil();
289
302
  }
290
303
 
291
304
  @JRubyMethod(name = "stream")
292
305
  public IRubyObject getStream(ThreadContext ctx) {
293
306
  if (stream == null) {
294
- return ctx.getRuntime().getNil();
307
+ return ctx.runtime.getNil();
295
308
  } else {
296
309
  return stream;
297
310
  }
@@ -307,12 +320,12 @@ public class Unpacker extends RubyObject {
307
320
  } else if (stream.respondsTo("read")) {
308
321
  str = stream.callMethod(ctx, "read").asString();
309
322
  } else {
310
- throw ctx.getRuntime().newTypeError(stream, "IO");
323
+ throw ctx.runtime.newTypeError(stream, "IO");
311
324
  }
312
325
  ByteList byteList = str.getByteList();
313
326
  this.stream = stream;
314
327
  this.decoder = null;
315
- this.decoder = new Decoder(ctx.getRuntime(), registry, byteList.unsafeBytes(), byteList.begin(), byteList.length(), symbolizeKeys, allowUnknownExt);
328
+ this.decoder = new Decoder(ctx.runtime, registry, byteList.unsafeBytes(), byteList.begin(), byteList.length(), symbolizeKeys, freeze, allowUnknownExt);
316
329
  return getStream(ctx);
317
330
  }
318
331
  }