-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathAionBuffer.java
510 lines (465 loc) · 16.7 KB
/
AionBuffer.java
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
package org.aion.avm.userlib;
import java.math.BigInteger;
import java.nio.BufferOverflowException;
import java.nio.BufferUnderflowException;
import avm.Address;
/**
* A buffer, much like an NIO ByteBuffer, which allows the easy encoding/decoding of primitive values.
*/
public class AionBuffer {
private static final int BYTE_MASK = 0xff;
private static final int BYTE_SIZE = Byte.SIZE;
// Note that AVM forces all BigInteger to 32-bytes, so we will serialize them as this fixed-size value.
private static final int BIG_INTEGER_BYTES = 32;
private final byte[] buffer;
private int position;
private int limit;
private AionBuffer(byte[] array) {
this.buffer = array;
this.position = 0;
this.limit = array.length;
}
/**
* Creates a new AionBuffer instance with the given capacity.
* @param capacity The size of the underlying buffer to create, in bytes.
* @return The new AionBuffer instance.
*/
public static AionBuffer allocate(int capacity) {
if (capacity < 1) {
throw new IllegalArgumentException("Illegal capacity: " + capacity);
}
return new AionBuffer(new byte[capacity]);
}
/**
* Creates a new AionBuffer instance wrapping the given byte array.
* @param array The array to wrap.
* @return The new AionBuffer instance.
*/
public static AionBuffer wrap(byte[] array) {
if (array == null) {
throw new NullPointerException();
}
if (array.length < 1) {
throw new IllegalArgumentException("Illegal capacity: " + array.length);
}
return new AionBuffer(array);
}
// ====================
// relative get methods
// ====================
/**
* Populates the given dst buffer with the next bytes in the buffer and advances the position.
* Note that dst MUST not be larger than the rest of the bytes in the receiver.
* @param dst The byte array to populate with the contents of the receiver.
* @return The receiver (for call chaining).
*/
public AionBuffer get(byte[] dst) {
if (dst == null) {
throw new NullPointerException();
}
int remaining = this.limit - this.position;
if (remaining < dst.length) {
throw new BufferUnderflowException();
}
System.arraycopy(this.buffer, this.position, dst, 0, dst.length);
this.position += dst.length;
return this;
}
/**
* Returns the next boolean in the buffer and advances the position.
* Note that we store booleans as a 1-byte quantity (0x1 or 0x0).
* @return The underlying byte, interpreted as a boolean (0x1 is true).
*/
public boolean getBoolean() {
byte value = internalGetByte();
return (0x1 == value);
}
/**
* Returns the next byte in the buffer and advances the position.
* @return The byte.
*/
public byte getByte() {
return internalGetByte();
}
private byte internalGetByte() {
int remaining = this.limit - this.position;
if (remaining < Byte.BYTES) {
throw new BufferUnderflowException();
}
byte b = this.buffer[this.position];
this.position += Byte.BYTES;
return b;
}
/**
* Returns the next char in the buffer and advances the position.
* @return The char.
*/
public char getChar() {
return (char) getShort();
}
/**
* Returns the next double in the buffer and advances the position.
* @return The double.
*/
public double getDouble() {
return Double.longBitsToDouble(getLong());
}
/**
* Returns the next float in the buffer and advances the position.
* @return The float.
*/
public float getFloat() {
return Float.intBitsToFloat(getInt());
}
/**
* Returns the next int in the buffer and advances the position.
* @return The int.
*/
public int getInt() {
int remaining = this.limit - this.position;
if (remaining < Integer.BYTES) {
throw new BufferUnderflowException();
}
int i = this.buffer[this.position] << BYTE_SIZE;
i = (i | (this.buffer[this.position + 1] & BYTE_MASK)) << BYTE_SIZE;
i = (i | (this.buffer[this.position + 2] & BYTE_MASK)) << BYTE_SIZE;
i |= (this.buffer[this.position + 3] & BYTE_MASK);
this.position += Integer.BYTES;
return i;
}
/**
* Returns the next long in the buffer and advances the position.
* @return The long.
*/
public long getLong() {
int remaining = this.limit - this.position;
if (remaining < Long.BYTES) {
throw new BufferUnderflowException();
}
long l = this.buffer[this.position] << BYTE_SIZE;
l = (l | (this.buffer[this.position + 1] & BYTE_MASK)) << BYTE_SIZE;
l = (l | (this.buffer[this.position + 2] & BYTE_MASK)) << BYTE_SIZE;
l = (l | (this.buffer[this.position + 3] & BYTE_MASK)) << BYTE_SIZE;
l = (l | (this.buffer[this.position + 4] & BYTE_MASK)) << BYTE_SIZE;
l = (l | (this.buffer[this.position + 5] & BYTE_MASK)) << BYTE_SIZE;
l = (l | (this.buffer[this.position + 6] & BYTE_MASK)) << BYTE_SIZE;
l |= this.buffer[this.position + 7] & BYTE_MASK;
this.position += Long.BYTES;
return l;
}
/**
* Returns the next short in the buffer and advances the position.
* @return The short.
*/
public short getShort() {
int remaining = this.limit - this.position;
if (remaining < Short.BYTES) {
throw new BufferUnderflowException();
}
short s = (short) (this.buffer[this.position] << BYTE_SIZE);
s |= (this.buffer[this.position + 1] & BYTE_MASK);
this.position += Short.BYTES;
return s;
}
/**
* Returns the next 32-byte Aion address in the buffer and advances the position.
* @return The address.
*/
public Address getAddress() {
int remaining = this.limit - this.position;
if (remaining < Address.LENGTH) {
throw new BufferUnderflowException();
}
byte[] raw = new byte[Address.LENGTH];
System.arraycopy(this.buffer, this.position, raw, 0, raw.length);
this.position += raw.length;
return new Address(raw);
}
/**
* Returns the next 32-byte signed BigInteger in the buffer and advances the position.
* @return The BigInteger.
*/
public BigInteger get32ByteInt() {
int remaining = this.limit - this.position;
if (remaining < BIG_INTEGER_BYTES) {
throw new BufferUnderflowException();
}
byte[] raw = new byte[BIG_INTEGER_BYTES];
System.arraycopy(this.buffer, this.position, raw, 0, raw.length);
this.position += raw.length;
return new BigInteger(raw);
}
// ====================
// relative put methods
// ====================
/**
* Copies the bytes from src into the buffer and advances the position.
* Note that src MUST not be larger than the rest of the bytes in the receiver.
* @param src The bytes to copy.
* @return The receiver (for call chaining).
*/
public AionBuffer put(byte[] src) {
if (src == null) {
throw new NullPointerException();
}
int remaining = this.limit - this.position;
if (remaining < src.length) {
throw new BufferOverflowException();
}
System.arraycopy(src, 0, this.buffer, this.position, src.length);
this.position += src.length;
return this;
}
/**
* Stores a boolean into the buffer and advances the position.
* Note that we store booleans as a 1-byte quantity (0x1 or 0x0).
* @param flag The boolean to store as a byte (0x1 for true, 0x0 for false).
* @return The receiver (for call chaining).
*/
public AionBuffer putBoolean(boolean flag) {
byte b = (byte)(flag ? 0x1 : 0x0);
return internalPutByte(b);
}
/**
* Stores a byte into the buffer and advances the position.
* @param b The byte to write.
* @return The receiver (for call chaining).
*/
public AionBuffer putByte(byte b) {
return internalPutByte(b);
}
private AionBuffer internalPutByte(byte b) {
int remaining = this.limit - this.position;
if (remaining < Byte.BYTES) {
throw new BufferOverflowException();
}
this.buffer[this.position] = b;
this.position += Byte.BYTES;
return this;
}
/**
* Stores a char into the buffer and advances the position.
* @param value The char to write.
* @return The receiver (for call chaining).
*/
public AionBuffer putChar(char value) {
return putShort((short) value);
}
/**
* Stores a double into the buffer and advances the position.
* @param value The double to write.
* @return The receiver (for call chaining).
*/
public AionBuffer putDouble(double value) {
return putLong(Double.doubleToLongBits(value));
}
/**
* Stores a float into the buffer and advances the position.
* @param value The float to write.
* @return The receiver (for call chaining).
*/
public AionBuffer putFloat(float value) {
return putInt(Float.floatToIntBits(value));
}
/**
* Stores a int into the buffer and advances the position.
* @param value The int to write.
* @return The receiver (for call chaining).
*/
public AionBuffer putInt(int value) {
int remaining = this.limit - this.position;
if (remaining < Integer.BYTES) {
throw new BufferOverflowException();
}
this.buffer[this.position] = (byte) ((value >> 24) & BYTE_MASK);
this.buffer[this.position + 1] = (byte) ((value >> 16) & BYTE_MASK);
this.buffer[this.position + 2] = (byte) ((value >> 8) & BYTE_MASK);
this.buffer[this.position + 3] = (byte) (value & BYTE_MASK);
this.position += Integer.BYTES;
return this;
}
/**
* Stores a long into the buffer and advances the position.
* @param value The long to write.
* @return The receiver (for call chaining).
*/
public AionBuffer putLong(long value) {
int remaining = this.limit - this.position;
if (remaining < Long.BYTES) {
throw new BufferOverflowException();
}
this.buffer[this.position] = (byte) ((value >> 56) & BYTE_MASK);
this.buffer[this.position + 1] = (byte) ((value >> 48) & BYTE_MASK);
this.buffer[this.position + 2] = (byte) ((value >> 40) & BYTE_MASK);
this.buffer[this.position + 3] = (byte) ((value >> 32) & BYTE_MASK);
this.buffer[this.position + 4] = (byte) ((value >> 24) & BYTE_MASK);
this.buffer[this.position + 5] = (byte) ((value >> 16) & BYTE_MASK);
this.buffer[this.position + 6] = (byte) ((value >> 8) & BYTE_MASK);
this.buffer[this.position + 7] = (byte) (value & BYTE_MASK);
this.position += Long.BYTES;
return this;
}
/**
* Stores a short into the buffer and advances the position.
* @param value The short to write.
* @return The receiver (for call chaining).
*/
public AionBuffer putShort(short value) {
int remaining = this.limit - this.position;
if (remaining < Short.BYTES) {
throw new BufferOverflowException();
}
this.buffer[this.position] = (byte) ((value >> 8) & BYTE_MASK);
this.buffer[this.position + 1] = (byte) (value & BYTE_MASK);
this.position += Short.BYTES;
return this;
}
/**
* Stores an Aion address into the buffer and advances the position.
* @param value The address to write.
* @return The receiver (for call chaining).
*/
public AionBuffer putAddress(Address value) {
if (value == null) {
throw new NullPointerException();
}
int remaining = this.limit - this.position;
if (remaining < Address.LENGTH) {
throw new BufferOverflowException();
}
byte[] raw = value.toByteArray();
System.arraycopy(raw, 0, this.buffer, this.position, raw.length);
this.position += raw.length;
return this;
}
/**
* Stores a 32-byte signed BigInteger into the buffer and advances the position.
* Note that this BigInteger is always stored as 32 bytes, even if its internal representation may be smaller.
* @param value The BigInteger to write.
* @return The receiver (for call chaining).
*/
public AionBuffer put32ByteInt(BigInteger value) {
if (value == null) {
throw new NullPointerException();
}
int remaining = this.limit - this.position;
if (remaining < BIG_INTEGER_BYTES) {
throw new BufferOverflowException();
}
byte prefixByte = (-1 == value.signum())
? (byte)0xff
: (byte)0x0;
byte[] raw = value.toByteArray();
// BigInteger instances can't be larger than 32-bytes, in AVM.
assert (raw.length <= BIG_INTEGER_BYTES);
// Add additional 0-bytes for any not expressed in the BigInteger (this is big-endian, so they preceed the value).
for (int i = raw.length; i < BIG_INTEGER_BYTES; ++i) {
internalPutByte(prefixByte);
}
System.arraycopy(raw, 0, this.buffer, this.position, raw.length);
this.position += raw.length;
return this;
}
// =====================
// query & misc. methods
// =====================
/**
* Allows access to the byte array under the buffer. Note that this will be a shared instance so changes to one will be observable in the other.
* @return The byte array underneath the receiver.
*/
public byte[] getArray() {
return this.buffer;
}
/**
* @return The total capacity of the receiver.
*/
public int getCapacity() {
return this.buffer.length;
}
/**
* @return The offset into the underlying byte array which the receiver will read/write its next byte.
*/
public int getPosition() {
return this.position;
}
/**
* @return The end of the buffer which will currently be used by a read/write operation.
*/
public int getLimit() {
return this.limit;
}
/**
* Resets the position to 0 and the limit to the full capacity of the buffer.
* Used when discarding state associated with a previous use of the buffer.
*
* @return The receiver (for call chaining).
*/
public AionBuffer clear() {
this.position = 0;
this.limit = this.buffer.length;
return this;
}
/**
* Sets the limit to the current position and resets the position to 0.
* Primarily used when switching between writing and reading modes:
* write(X), write(Y), write(Z), flip(), read(X), read(Y), read(Z).
*
* @return The receiver (for call chaining).
*/
public AionBuffer flip() {
this.limit = this.position;
this.position = 0;
return this;
}
/**
* Sets the position back to 0.
* Useful for cases where the previously processed contents want to be reprocessed.
*
* @return The receiver (for call chaining).
*/
public AionBuffer rewind() {
this.position = 0;
return this;
}
@Override
public boolean equals(Object ob) {
// The standard JCL ByteBuffer derives its equality from its state and internal data so do the same, here.
if (this == ob) {
return true;
}
if (!(ob instanceof AionBuffer)) {
return false;
}
AionBuffer other = (AionBuffer) ob;
if (this.buffer.length != other.buffer.length) {
return false;
}
if (this.position != other.position) {
return false;
}
if (this.limit != other.limit) {
return false;
}
// The comparison is not the full buffer, only up to the limit.
for (int i = 0; i < this.limit; i++) {
if (this.buffer[i] != other.buffer[i]) {
return false;
}
}
return true;
}
@Override
public int hashCode() {
// The standard JCL ByteBuffer derives its hash code from its internal data so do the same, here.
int h = 1;
// The comparison is not the full buffer, only up to the limit.
for (int i = this.limit - 1; i >= 0; i--) {
h = 31 * h + (int) this.buffer[i];
}
return h;
}
@Override
public String toString() {
return "AionBuffer [capacity = " + this.buffer.length + ", position = " + this.position + ", limit = " + this.limit + " ]";
}
}