-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathEncoder.java
More file actions
375 lines (337 loc) · 11.6 KB
/
Copy pathEncoder.java
File metadata and controls
375 lines (337 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package org.msgpack.jruby;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import org.jruby.Ruby;
import org.jruby.RubyObject;
import org.jruby.RubyNil;
import org.jruby.RubyBoolean;
import org.jruby.RubyBignum;
import org.jruby.RubyInteger;
import org.jruby.RubyFixnum;
import org.jruby.RubyFloat;
import org.jruby.RubyString;
import org.jruby.RubySymbol;
import org.jruby.RubyArray;
import org.jruby.RubyHash;
import org.jruby.RubyEncoding;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.ByteList;
import org.jcodings.Encoding;
import org.jcodings.specific.UTF8Encoding;
import static org.msgpack.jruby.Types.*;
public class Encoder {
private static final int CACHE_LINE_SIZE = 64;
private static final int ARRAY_HEADER_SIZE = 24;
private final Ruby runtime;
private final Encoding binaryEncoding;
private final Encoding utf8Encoding;
private final boolean compatibilityMode;
private final ExtensionRegistry registry;
private ByteBuffer buffer;
public Encoder(Ruby runtime, boolean compatibilityMode, ExtensionRegistry registry) {
this.runtime = runtime;
this.buffer = ByteBuffer.allocate(CACHE_LINE_SIZE - ARRAY_HEADER_SIZE);
this.binaryEncoding = runtime.getEncodingService().getAscii8bitEncoding();
this.utf8Encoding = UTF8Encoding.INSTANCE;
this.compatibilityMode = compatibilityMode;
this.registry = registry;
}
public boolean isCompatibilityMode() {
return compatibilityMode;
}
private void ensureRemainingCapacity(int c) {
if (buffer.remaining() < c) {
int newLength = Math.max(buffer.capacity() + (buffer.capacity() >> 1), buffer.capacity() + c);
newLength += CACHE_LINE_SIZE - ((ARRAY_HEADER_SIZE + newLength) % CACHE_LINE_SIZE);
buffer = ByteBuffer.allocate(newLength).put(buffer.array(), 0, buffer.position());
}
}
private IRubyObject readRubyString() {
IRubyObject str = runtime.newString(new ByteList(buffer.array(), 0, buffer.position(), binaryEncoding, false));
buffer.clear();
return str;
}
public IRubyObject encode(IRubyObject object) {
appendObject(object);
return readRubyString();
}
public IRubyObject encode(IRubyObject object, IRubyObject destination) {
appendObject(object, destination);
return readRubyString();
}
public IRubyObject encodeArrayHeader(int size) {
appendArrayHeader(size);
return readRubyString();
}
public IRubyObject encodeMapHeader(int size) {
appendHashHeader(size);
return readRubyString();
}
private void appendObject(IRubyObject object) {
appendObject(object, null);
}
private void appendObject(IRubyObject object, IRubyObject destination) {
if (object == null || object instanceof RubyNil) {
ensureRemainingCapacity(1);
buffer.put(NIL);
} else if (object instanceof RubyBoolean) {
ensureRemainingCapacity(1);
buffer.put(((RubyBoolean) object).isTrue() ? TRUE : FALSE);
} else if (object instanceof RubyBignum) {
appendBignum((RubyBignum) object);
} else if (object instanceof RubyInteger) {
appendInteger((RubyInteger) object);
} else if (object instanceof RubyFloat) {
appendFloat((RubyFloat) object);
} else if (object instanceof RubyString) {
appendString((RubyString) object);
} else if (object instanceof RubySymbol) {
appendString(((RubySymbol) object).asString());
} else if (object instanceof RubyArray) {
appendArray((RubyArray) object);
} else if (object instanceof RubyHash) {
appendHash((RubyHash) object);
} else if (object instanceof ExtensionValue) {
appendExtensionValue((ExtensionValue) object);
} else {
appendOther(object, destination);
}
}
private void appendBignum(RubyBignum object) {
BigInteger value = ((RubyBignum) object).getBigIntegerValue();
if (value.bitLength() > 64 || (value.bitLength() > 63 && value.signum() < 0)) {
throw runtime.newArgumentError(String.format("Cannot pack big integer: %s", value));
}
ensureRemainingCapacity(9);
buffer.put(value.signum() < 0 ? INT64 : UINT64);
byte[] b = value.toByteArray();
buffer.put(b, b.length - 8, 8);
}
private void appendInteger(RubyInteger object) {
long value = ((RubyInteger) object).getLongValue();
if (value < 0) {
if (value < Short.MIN_VALUE) {
if (value < Integer.MIN_VALUE) {
ensureRemainingCapacity(9);
buffer.put(INT64);
buffer.putLong(value);
} else {
ensureRemainingCapacity(5);
buffer.put(INT32);
buffer.putInt((int) value);
}
} else if (value >= -0x20L) {
ensureRemainingCapacity(1);
byte b = (byte) (value | 0xe0);
buffer.put(b);
} else if (value < Byte.MIN_VALUE) {
ensureRemainingCapacity(3);
buffer.put(INT16);
buffer.putShort((short) value);
} else {
ensureRemainingCapacity(2);
buffer.put(INT8);
buffer.put((byte) value);
}
} else {
if (value < 0x10000L) {
if (value < 128L) {
ensureRemainingCapacity(1);
buffer.put((byte) value);
} else if (value < 0x100L) {
ensureRemainingCapacity(2);
buffer.put(UINT8);
buffer.put((byte) value);
} else {
ensureRemainingCapacity(3);
buffer.put(UINT16);
buffer.putShort((short) value);
}
} else if (value < 0x100000000L) {
ensureRemainingCapacity(5);
buffer.put(UINT32);
buffer.putInt((int) value);
} else {
ensureRemainingCapacity(9);
buffer.put(INT64);
buffer.putLong(value);
}
}
}
private void appendFloat(RubyFloat object) {
double value = object.getDoubleValue();
float f = (float) value;
//TODO: msgpack-ruby original does encode this value as Double, not float
// if (Double.compare(f, value) == 0) {
// ensureRemainingCapacity(5);
// buffer.put(FLOAT32);
// buffer.putFloat(f);
// } else {
ensureRemainingCapacity(9);
buffer.put(FLOAT64);
buffer.putDouble(value);
// }
}
private void appendString(RubyString object) {
Encoding encoding = object.getEncoding();
boolean binary = !compatibilityMode && encoding == binaryEncoding;
if (encoding != utf8Encoding && encoding != binaryEncoding) {
object = (RubyString) ((RubyString) object).encode(runtime.getCurrentContext(), runtime.getEncodingService().getEncoding(utf8Encoding));
}
ByteList bytes = object.getByteList();
int length = bytes.length();
if (length < 32 && !binary) {
ensureRemainingCapacity(1 + length);
buffer.put((byte) (length | FIXSTR));
} else if (length <= 0xff && !compatibilityMode) {
ensureRemainingCapacity(2 + length);
buffer.put(binary ? BIN8 : STR8);
buffer.put((byte) length);
} else if (length <= 0xffff) {
ensureRemainingCapacity(3 + length);
buffer.put(binary ? BIN16 : STR16);
buffer.putShort((short) length);
} else {
ensureRemainingCapacity(5 + length);
buffer.put(binary ? BIN32 : STR32);
buffer.putInt((int) length);
}
buffer.put(bytes.unsafeBytes(), bytes.begin(), length);
}
private void appendArray(RubyArray object) {
appendArrayHeader(object);
appendArrayElements(object);
}
private void appendArrayHeader(RubyArray object) {
appendArrayHeader(object.size());
}
private void appendArrayHeader(int size) {
if (size < 16) {
ensureRemainingCapacity(1);
buffer.put((byte) (size | 0x90));
} else if (size < 0x10000) {
ensureRemainingCapacity(3);
buffer.put(ARY16);
buffer.putShort((short) size);
} else {
ensureRemainingCapacity(5);
buffer.put(ARY32);
buffer.putInt(size);
}
}
private void appendArrayElements(RubyArray object) {
int size = object.size();
for (int i = 0; i < size; i++) {
appendObject(object.eltOk(i));
}
}
private void appendHash(RubyHash object) {
appendHashHeader(object);
appendHashElements(object);
}
private void appendHashHeader(RubyHash object) {
appendHashHeader(object.size());
}
private void appendHashHeader(int size) {
if (size < 16) {
ensureRemainingCapacity(1);
buffer.put((byte) (size | 0x80));
} else if (size < 0x10000) {
ensureRemainingCapacity(3);
buffer.put(MAP16);
buffer.putShort((short) size);
} else {
ensureRemainingCapacity(5);
buffer.put(MAP32);
buffer.putInt(size);
}
}
private void appendHashElements(RubyHash object) {
int size = object.size();
HashVisitor visitor = new HashVisitor(size);
object.visitAll(visitor);
if (visitor.remain != 0) {
object.getRuntime().newConcurrencyError("Hash size changed while packing");
}
}
private class HashVisitor extends RubyHash.Visitor {
public int remain;
public HashVisitor(int size) {
remain = size;
}
public void visit(IRubyObject key, IRubyObject value) {
if (remain-- > 0) {
appendObject(key);
appendObject(value);
}
}
}
private void appendExt(int type, ByteList payloadBytes) {
int payloadSize = payloadBytes.length();
int outputSize = 0;
boolean fixSize = payloadSize == 1 || payloadSize == 2 || payloadSize == 4 || payloadSize == 8 || payloadSize == 16;
if (fixSize) {
outputSize = 2 + payloadSize;
} else if (payloadSize < 0x100) {
outputSize = 3 + payloadSize;
} else if (payloadSize < 0x10000) {
outputSize = 4 + payloadSize;
} else {
outputSize = 6 + payloadSize;
}
ensureRemainingCapacity(outputSize);
if (payloadSize == 1) {
buffer.put(FIXEXT1);
} else if (payloadSize == 2) {
buffer.put(FIXEXT2);
} else if (payloadSize == 4) {
buffer.put(FIXEXT4);
} else if (payloadSize == 8) {
buffer.put(FIXEXT8);
} else if (payloadSize == 16) {
buffer.put(FIXEXT16);
} else if (payloadSize < 0x100) {
buffer.put(VAREXT8);
buffer.put((byte) payloadSize);
} else if (payloadSize < 0x10000) {
buffer.put(VAREXT16);
buffer.putShort((short) payloadSize);
} else {
buffer.put(VAREXT32);
buffer.putInt(payloadSize);
}
buffer.put((byte) type);
buffer.put(payloadBytes.unsafeBytes(), payloadBytes.begin(), payloadSize);
}
private void appendExtensionValue(ExtensionValue object) {
long type = ((RubyFixnum)object.get_type()).getLongValue();
if (type < -128 || type > 127) {
throw object.getRuntime().newRangeError(String.format("integer %d too big to convert to `signed char'", type));
}
ByteList payloadBytes = ((RubyString)object.payload()).getByteList();
appendExt((int) type, payloadBytes);
}
private void appendOther(IRubyObject object, IRubyObject destination) {
if (registry != null) {
IRubyObject[] pair = registry.lookupPackerByClass(object.getType());
if (pair != null) {
RubyString bytes = pair[0].callMethod(runtime.getCurrentContext(), "call", object).asString();
int type = (int) ((RubyFixnum) pair[1]).getLongValue();
appendExt(type, bytes.getByteList());
return;
}
}
appendCustom(object, destination);
}
private void appendCustom(IRubyObject object, IRubyObject destination) {
if (destination == null) {
IRubyObject result = object.callMethod(runtime.getCurrentContext(), "to_msgpack");
ByteList bytes = result.asString().getByteList();
int length = bytes.length();
ensureRemainingCapacity(length);
buffer.put(bytes.unsafeBytes(), bytes.begin(), length);
} else {
object.callMethod(runtime.getCurrentContext(), "to_msgpack", destination);
}
}
}