-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathDecoder.java
More file actions
201 lines (176 loc) · 6.53 KB
/
Copy pathDecoder.java
File metadata and controls
201 lines (176 loc) · 6.53 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
package org.msgpack.jruby;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.BufferUnderflowException;
import java.util.Iterator;
import java.util.Arrays;
import org.jruby.Ruby;
import org.jruby.RubyObject;
import org.jruby.RubyClass;
import org.jruby.RubyBignum;
import org.jruby.RubyString;
import org.jruby.RubyHash;
import org.jruby.exceptions.RaiseException;
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 Decoder implements Iterator<IRubyObject> {
private final Ruby runtime;
private final Encoding binaryEncoding;
private final Encoding utf8Encoding;
private final RubyClass unpackErrorClass;
private final RubyClass underflowErrorClass;
private ByteBuffer buffer;
private boolean symbolizeKeys;
public Decoder(Ruby runtime) {
this(runtime, new byte[] {}, 0, 0);
}
public Decoder(Ruby runtime, byte[] bytes) {
this(runtime, bytes, 0, bytes.length);
}
public Decoder(Ruby runtime, byte[] bytes, int offset, int length) {
this.runtime = runtime;
this.binaryEncoding = runtime.getEncodingService().getAscii8bitEncoding();
this.utf8Encoding = UTF8Encoding.INSTANCE;
this.unpackErrorClass = runtime.getModule("MessagePack").getClass("UnpackError");
this.underflowErrorClass = runtime.getModule("MessagePack").getClass("UnderflowError");
feed(bytes, offset, length);
}
public void symbolizeKeys(boolean symbolize) {
this.symbolizeKeys = symbolize;
}
public void feed(byte[] bytes) {
feed(bytes, 0, bytes.length);
}
public void feed(byte[] bytes, int offset, int length) {
if (buffer == null) {
buffer = ByteBuffer.wrap(bytes, offset, length);
} else {
ByteBuffer newBuffer = ByteBuffer.allocate(buffer.remaining() + length);
newBuffer.put(buffer);
newBuffer.put(bytes, offset, length);
newBuffer.flip();
buffer = newBuffer;
}
}
public void reset() {
buffer.rewind();
}
public int offset() {
return buffer.position();
}
private IRubyObject consumeUnsignedLong() {
long value = buffer.getLong();
if (value < 0) {
return RubyBignum.newBignum(runtime, BigInteger.valueOf(value & ((1L<<63)-1)).setBit(63));
} else {
return runtime.newFixnum(value);
}
}
private IRubyObject consumeString(int size, Encoding encoding) {
byte[] bytes = readBytes(size);
ByteList byteList = new ByteList(bytes, encoding);
return runtime.newString(byteList);
}
private IRubyObject consumeArray(int size) {
IRubyObject[] elements = new IRubyObject[size];
for (int i = 0; i < size; i++) {
elements[i] = next();
}
return runtime.newArray(elements);
}
private IRubyObject consumeHash(int size) {
RubyHash hash = RubyHash.newHash(runtime);
for (int i = 0; i < size; i++) {
IRubyObject key = next();
if (this.symbolizeKeys && key instanceof RubyString) {
key = ((RubyString) key).intern();
}
hash.fastASet(key, next());
}
return hash;
}
private IRubyObject consumeExtension(int size) {
int type = buffer.get();
byte[] payload = readBytes(size);
return ExtensionValue.newExtensionValue(runtime, type, payload);
}
private byte[] readBytes(int size) {
byte[] payload = new byte[size];
buffer.get(payload);
return payload;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
@Override
public boolean hasNext() {
return buffer.remaining() > 0;
}
@Override
public IRubyObject next() {
int position = buffer.position();
try {
byte b = buffer.get();
outer: switch ((b >> 4) & 0xf) {
case 0x8: return consumeHash(b & 0x0f);
case 0x9: return consumeArray(b & 0x0f);
case 0xa:
case 0xb: return consumeString(b & 0x1f, utf8Encoding);
case 0xc:
switch (b) {
case NIL: return runtime.getNil();
case FALSE: return runtime.getFalse();
case TRUE: return runtime.getTrue();
case BIN8: return consumeString(buffer.get() & 0xff, binaryEncoding);
case BIN16: return consumeString(buffer.getShort() & 0xffff, binaryEncoding);
case BIN32: return consumeString(buffer.getInt(), binaryEncoding);
case VAREXT8: return consumeExtension(buffer.get());
case VAREXT16: return consumeExtension(buffer.getShort());
case VAREXT32: return consumeExtension(buffer.getInt());
case FLOAT32: return runtime.newFloat(buffer.getFloat());
case FLOAT64: return runtime.newFloat(buffer.getDouble());
case UINT8: return runtime.newFixnum(buffer.get() & 0xffL);
case UINT16: return runtime.newFixnum(buffer.getShort() & 0xffffL);
case UINT32: return runtime.newFixnum(buffer.getInt() & 0xffffffffL);
case UINT64: return consumeUnsignedLong();
default: break outer;
}
case 0xd:
switch (b) {
case INT8: return runtime.newFixnum(buffer.get());
case INT16: return runtime.newFixnum(buffer.getShort());
case INT32: return runtime.newFixnum(buffer.getInt());
case INT64: return runtime.newFixnum(buffer.getLong());
case FIXEXT1: return consumeExtension(1);
case FIXEXT2: return consumeExtension(2);
case FIXEXT4: return consumeExtension(4);
case FIXEXT8: return consumeExtension(8);
case FIXEXT16: return consumeExtension(16);
case STR8: return consumeString(buffer.get() & 0xff, utf8Encoding);
case STR16: return consumeString(buffer.getShort() & 0xffff, utf8Encoding);
case STR32: return consumeString(buffer.getInt(), utf8Encoding);
case ARY16: return consumeArray(buffer.getShort() & 0xffff);
case ARY32: return consumeArray(buffer.getInt());
case MAP16: return consumeHash(buffer.getShort() & 0xffff);
case MAP32: return consumeHash(buffer.getInt());
default: break outer;
}
case 0xe:
case 0xf: return runtime.newFixnum((0x1f & b) - 0x20);
default: return runtime.newFixnum(b);
}
buffer.position(position);
throw runtime.newRaiseException(unpackErrorClass, "Illegal byte sequence");
} catch (RaiseException re) {
buffer.position(position);
throw re;
} catch (BufferUnderflowException bue) {
buffer.position(position);
throw runtime.newRaiseException(underflowErrorClass, "Not enough bytes available");
}
}
}