Repository navigation
Expand file tree
/
Copy pathDataIO.java
More file actions
1471 lines (1263 loc) · 42.3 KB
/
Copy pathDataIO.java
File metadata and controls
1471 lines (1263 loc) · 42.3 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
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package org.mapdb;
import java.io.*;
import java.lang.ref.WeakReference;
import java.nio.ByteBuffer;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Various IO classes and utilities..
*/
public final class DataIO {
private DataIO(){}
/**
* Unpack int value from the input stream.
*
* @param is The input stream.
* @return The long value.
*
* @throws java.io.IOException in case of IO error
*/
static public int unpackInt(DataInput is) throws IOException {
int ret = 0;
byte v;
do{
v = is.readByte();
ret = (ret<<7 ) | (v & 0x7F);
}while(v<0);
return ret;
}
/**
* Unpack long value from the input stream.
*
* @param in The input stream.
* @return The long value.
*
* @throws java.io.IOException in case of IO error
*/
static public long unpackLong(DataInput in) throws IOException {
long ret = 0;
byte v;
do{
v = in.readByte();
ret = (ret<<7 ) | (v & 0x7F);
}while(v<0);
return ret;
}
/**
* Unpack long value from the input stream.
*
* @param in The input stream.
* @return The long value.
*
* @throws java.io.IOException in case of IO error
*/
static public long unpackLong(InputStream in) throws IOException {
long ret = 0;
int v;
do{
v = in.read();
if(v==-1)
throw new EOFException();
ret = (ret<<7 ) | (v & 0x7F);
}while((v&0x80)!=0);
return ret;
}
/**
* Pack long into output.
* It will occupy 1-10 bytes depending on value (lower values occupy smaller space)
*
* @param out DataOutput to put value into
* @param value to be serialized, must be non-negative
*
* @throws java.io.IOException in case of IO error
*/
static public void packLong(DataOutput out, long value) throws IOException {
//$DELAY$
int shift = 63-Long.numberOfLeadingZeros(value);
shift -= shift%7; // round down to nearest multiple of 7
while(shift!=0){
out.writeByte((byte) (((value>>>shift) & 0x7F) | 0x80));
//$DELAY$
shift-=7;
}
out.writeByte((byte) (value & 0x7F));
}
/**
* Pack long into output.
* It will occupy 1-10 bytes depending on value (lower values occupy smaller space)
*
* @param out OutputStream to put value into
* @param value to be serialized, must be non-negative
*
* @throws java.io.IOException in case of IO error
*/
static public void packLong(OutputStream out, long value) throws IOException {
//$DELAY$
int shift = 63-Long.numberOfLeadingZeros(value);
shift -= shift%7; // round down to nearest multiple of 7
while(shift!=0){
out.write((int) (((value>>>shift) & 0x7F) | 0x80));
//$DELAY$
shift-=7;
}
out.write((int) (value & 0x7F));
}
/**
* Calculate how much bytes packed long consumes.
*
* @param value to calculate
* @return number of bytes used in packed form
*/
public static int packLongSize(long value) {
int shift = 63-Long.numberOfLeadingZeros(value);
shift -= shift%7; // round down to nearest multiple of 7
int ret = 1;
while(shift!=0){
//TODO remove cycle, just count zeroes
shift-=7;
ret++;
}
return ret;
}
/**
* Unpack RECID value from the input stream with 3 bit checksum.
*
* @param in The input stream.
* @return The long value.
* @throws java.io.IOException in case of IO error
*/
static public long unpackRecid(DataInput in) throws IOException {
long val = unpackLong(in);
val = DataIO.parity3Get(val);
return val >>> 3;
}
/**
* Pack RECID into output stream with 3 bit checksum.
* It will occupy 1-10 bytes depending on value (lower values occupy smaller space)
*
* @param out DataOutput to put value into
* @param value to be serialized, must be non-negative
* @throws java.io.IOException in case of IO error
*/
static public void packRecid(DataOutput out, long value) throws IOException {
value = DataIO.parity3Set(value<<3);
packLong(out,value);
}
/**
* Pack int into an output stream.
* It will occupy 1-5 bytes depending on value (lower values occupy smaller space)
*
* @param out DataOutput to put value into
* @param value to be serialized, must be non-negative
* @throws java.io.IOException in case of IO error
*/
static public void packInt(DataOutput out, int value) throws IOException {
// Optimize for the common case where value is small. This is particular important where our caller
// is SerializerBase.SER_STRING.serialize because most chars will be ASCII characters and hence in this range.
// credit Max Bolingbroke https://github.com/jankotek/MapDB/pull/489
int shift = (value & ~0x7F); //reuse variable
if (shift != 0) {
//$DELAY$
shift = 31-Integer.numberOfLeadingZeros(value);
shift -= shift%7; // round down to nearest multiple of 7
while(shift!=0){
out.writeByte((byte) (((value>>>shift) & 0x7F) | 0x80));
//$DELAY$
shift-=7;
}
}
//$DELAY$
out.writeByte((byte) (value & 0x7F));
}
/**
* Pack int into an output stream.
* It will occupy 1-5 bytes depending on value (lower values occupy smaller space)
*
* This method is same as {@link #packInt(DataOutput, int)},
* but is optimized for values larger than 127. Usually it is recids.
*
* @param out DataOutput to put value into
* @param value to be serialized, must be non-negative
* @throws java.io.IOException in case of IO error
*/
static public void packIntBigger(DataOutput out, int value) throws IOException {
//$DELAY$
int shift = 31-Integer.numberOfLeadingZeros(value);
shift -= shift%7; // round down to nearest multiple of 7
while(shift!=0){
out.writeByte((byte) (((value>>>shift) & 0x7F) | 0x80));
//$DELAY$
shift-=7;
}
//$DELAY$
out.writeByte((byte) (value & 0x7F));
}
public static int longHash(long h) {
//$DELAY$
h = h * -7046029254386353131L;
h ^= h >> 32;
return (int)(h ^ h >> 16);
//TODO koloboke credit
}
public static int intHash(int h) {
//$DELAY$
h = h * -1640531527;
return h ^ h >> 16;
//TODO koloboke credit
}
public static final long PACK_LONG_RESULT_MASK = 0xFFFFFFFFFFFFFFL;
public static int packLongBidi(DataOutput out, long value) throws IOException {
out.write((((int) value & 0x7F)) | 0x80);
value >>>= 7;
int counter = 2;
//$DELAY$
while ((value & ~0x7FL) != 0) {
out.write((((int) value & 0x7F)));
value >>>= 7;
//$DELAY$
counter++;
}
//$DELAY$
out.write((byte) value| 0x80);
return counter;
}
public static int packLongBidi(byte[] buf, int pos, long value) {
buf[pos++] = (byte) ((((int) value & 0x7F))| 0x80);
value >>>= 7;
int counter = 2;
//$DELAY$
while ((value & ~0x7FL) != 0) {
buf[pos++] = (byte) (((int) value & 0x7F));
value >>>= 7;
//$DELAY$
counter++;
}
//$DELAY$
buf[pos++] = (byte) ((byte) value| 0x80);
return counter;
}
public static long unpackLongBidi(byte[] bb, int pos){
//$DELAY$
long b = bb[pos++];
if(CC.ASSERT && (b&0x80)==0)
throw new DBException.DataCorruption("long pack bidi wrong header");
long result = (b & 0x7F) ;
int offset = 7;
do {
//$DELAY$
b = bb[pos++];
result |= (b & 0x7F) << offset;
if(CC.ASSERT && offset>64)
throw new DBException.DataCorruption("long pack bidi too long");
offset += 7;
}while((b & 0x80) == 0);
//$DELAY$
return (((long)(offset/7))<<56) | result;
}
public static long unpackLongBidiReverse(byte[] bb, int pos){
//$DELAY$
long b = bb[--pos];
if(CC.ASSERT && (b&0x80)==0)
throw new DBException.DataCorruption("long pack bidi wrong header");
long result = (b & 0x7F) ;
int counter = 1;
do {
//$DELAY$
b = bb[--pos];
result = (b & 0x7F) | (result<<7);
if(CC.ASSERT && counter>8)
throw new DBException.DataCorruption("long pack bidi too long");
counter++;
}while((b & 0x80) == 0);
//$DELAY$
return (((long)counter)<<56) | result;
}
public static long getLong(byte[] buf, int pos) {
return
((((long)buf[pos++]) << 56) |
(((long)buf[pos++] & 0xFF) << 48) |
(((long)buf[pos++] & 0xFF) << 40) |
(((long)buf[pos++] & 0xFF) << 32) |
(((long)buf[pos++] & 0xFF) << 24) |
(((long)buf[pos++] & 0xFF) << 16) |
(((long)buf[pos++] & 0xFF) << 8) |
(((long)buf[pos] & 0xFF)));
}
public static void putLong(byte[] buf, int pos,long v) {
buf[pos++] = (byte) (0xff & (v >> 56));
buf[pos++] = (byte) (0xff & (v >> 48));
buf[pos++] = (byte) (0xff & (v >> 40));
buf[pos++] = (byte) (0xff & (v >> 32));
buf[pos++] = (byte) (0xff & (v >> 24));
buf[pos++] = (byte) (0xff & (v >> 16));
buf[pos++] = (byte) (0xff & (v >> 8));
buf[pos] = (byte) (0xff & (v));
}
public static long getSixLong(byte[] buf, int pos) {
return
((long) (buf[pos++] & 0xff) << 40) |
((long) (buf[pos++] & 0xff) << 32) |
((long) (buf[pos++] & 0xff) << 24) |
((long) (buf[pos++] & 0xff) << 16) |
((long) (buf[pos++] & 0xff) << 8) |
((long) (buf[pos] & 0xff));
}
public static void putSixLong(byte[] buf, int pos, long value) {
if(CC.ASSERT && (value>>>48!=0))
throw new AssertionError();
buf[pos++] = (byte) (0xff & (value >> 40));
buf[pos++] = (byte) (0xff & (value >> 32));
buf[pos++] = (byte) (0xff & (value >> 24));
buf[pos++] = (byte) (0xff & (value >> 16));
buf[pos++] = (byte) (0xff & (value >> 8));
buf[pos] = (byte) (0xff & (value));
}
public static long nextPowTwo(final long a)
{
return 1L << (64 - Long.numberOfLeadingZeros(a - 1L));
}
public static int nextPowTwo(final int a)
{
return 1 << (32 - Integer.numberOfLeadingZeros(a - 1));
}
public static void readFully(InputStream in, byte[] data) throws IOException {
int len = data.length;
for(int read=0; read<len;){
int c = in.read(data, read, len - read);
if(c<0)
throw new EOFException();
read+=c;
}
}
/**
* Give access to internal byte[] or ByteBuffer in DataInput2..
* Should not be used unless you are writing MapDB extension and needs some performance bonus
*/
public interface DataInputInternal extends DataInput,Closeable {
int getPos();
void setPos(int pos);
/** @return underlying {@code byte[]} or null if it does not exist*/
byte[] internalByteArray();
/** @return underlying {@code ByteBuffer} or null if it does not exist*/
ByteBuffer internalByteBuffer();
void close();
long unpackLong() throws IOException;
int unpackInt() throws IOException;
long[] unpackLongArrayDeltaCompression(int size) throws IOException;
void unpackLongArray(long[] ret, int i, int len);
void unpackIntArray(int[] ret, int i, int len);
}
/** DataInput on top of {@code byte[]} */
static public final class DataInputByteArray implements DataInput, DataInputInternal {
protected final byte[] buf;
protected int pos;
public DataInputByteArray(byte[] b) {
this(b, 0);
}
public DataInputByteArray(byte[] bb, int pos) {
//$DELAY$
buf = bb;
this.pos = pos;
}
@Override
public void readFully(byte[] b) throws IOException {
readFully(b, 0, b.length);
}
@Override
public void readFully(byte[] b, int off, int len) throws IOException {
System.arraycopy(buf, pos, b, off, len);
//$DELAY$
pos += len;
}
@Override
public int skipBytes(final int n) throws IOException {
pos += n;
//$DELAY$
return n;
}
@Override
public boolean readBoolean() throws IOException {
//$DELAY$
return buf[pos++] == 1;
}
@Override
public byte readByte() throws IOException {
//$DELAY$
return buf[pos++];
}
@Override
public int readUnsignedByte() throws IOException {
//$DELAY$
return buf[pos++] & 0xff;
}
@Override
public short readShort() throws IOException {
//$DELAY$
return (short)((buf[pos++] << 8) | (buf[pos++] & 0xff));
}
@Override
public int readUnsignedShort() throws IOException {
//$DELAY$
return readChar();
}
@Override
public char readChar() throws IOException {
//$DELAY$
return (char) (
((buf[pos++] & 0xff) << 8) |
(buf[pos++] & 0xff));
}
@Override
public int readInt() throws IOException {
int p = pos;
final byte[] b = buf;
final int ret =
((((int)b[p++]) << 24) |
(((int)b[p++] & 0xFF) << 16) |
(((int)b[p++] & 0xFF) << 8) |
(((int)b[p++] & 0xFF)));
pos = p;
return ret;
}
@Override
public long readLong() throws IOException {
int p = pos;
final byte[] b = buf;
final long ret =
((((long)b[p++]) << 56) |
(((long)b[p++] & 0xFF) << 48) |
(((long)b[p++] & 0xFF) << 40) |
(((long)b[p++] & 0xFF) << 32) |
(((long)b[p++] & 0xFF) << 24) |
(((long)b[p++] & 0xFF) << 16) |
(((long)b[p++] & 0xFF) << 8) |
(((long)b[p++] & 0xFF)));
pos = p;
return ret;
}
@Override
public float readFloat() throws IOException {
return Float.intBitsToFloat(readInt());
}
@Override
public double readDouble() throws IOException {
return Double.longBitsToDouble(readLong());
}
@Override
public String readLine() throws IOException {
return readUTF();
}
@Override
public String readUTF() throws IOException {
final int len = unpackInt();
char[] b = new char[len];
for (int i = 0; i < len; i++)
//$DELAY$
b[i] = (char) unpackInt();
return new String(b);
}
@Override
public int getPos() {
return pos;
}
@Override
public void setPos(int pos) {
this.pos = pos;
}
@Override
public byte[] internalByteArray() {
return buf;
}
@Override
public ByteBuffer internalByteBuffer() {
return null;
}
@Override
public void close() {
}
@Override
public long unpackLong() throws IOException {
byte[] b = buf;
int p = pos;
long ret = 0;
byte v;
do{
//$DELAY$
v = b[p++];
ret = (ret<<7 ) | (v & 0x7F);
}while(v<0);
pos = p;
return ret;
}
@Override
public int unpackInt() throws IOException {
byte[] b = buf;
int p = pos;
int ret = 0;
byte v;
do{
//$DELAY$
v = b[p++];
ret = (ret<<7 ) | (v & 0x7F);
}while(v<0);
pos = p;
return ret;
}
@Override
public long[] unpackLongArrayDeltaCompression(final int size) throws IOException {
long[] ret = new long[size];
int pos2 = pos;
byte[] buf2 = buf;
long prev =0;
byte v;
for(int i=0;i<size;i++){
long r = 0;
do {
//$DELAY$
v = buf2[pos2++];
r = (r << 7) | (v & 0x7F);
} while (v < 0);
prev+=r;
ret[i]=prev;
}
pos = pos2;
return ret;
}
@Override
public void unpackLongArray(long[] array, int start, int end) {
int pos2 = pos;
byte[] buf2 = buf;
long ret;
byte v;
for(;start<end;start++) {
ret = 0;
do {
//$DELAY$
v = buf2[pos2++];
ret = (ret << 7) | (v & 0x7F);
} while (v < 0);
array[start]=ret;
}
pos = pos2;
}
@Override
public void unpackIntArray(int[] array, int start, int end) {
int pos2 = pos;
byte[] buf2 = buf;
int ret;
byte v;
for(;start<end;start++) {
ret = 0;
do {
//$DELAY$
v = buf2[pos2++];
ret = (ret << 7) | (v & 0x7F);
} while (v < 0);
array[start]=ret;
}
pos = pos2;
}
}
/**
* Wraps {@code DataInput} into {@code InputStream}
*/
public static final class DataInputToStream extends InputStream {
protected final DataInput in;
public DataInputToStream(DataInput in) {
this.in = in;
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
in.readFully(b,off,len);
return len;
}
@Override
public long skip(long n) throws IOException {
n = Math.min(n, Integer.MAX_VALUE);
//$DELAY$
return in.skipBytes((int) n);
}
@Override
public void close() throws IOException {
if(in instanceof Closeable)
((Closeable) in).close();
}
@Override
public int read() throws IOException {
return in.readUnsignedByte();
}
}
/**
* Wraps {@link java.nio.ByteBuffer} and provides {@link java.io.DataInput}
*
* @author Jan Kotek
*/
public static final class DataInputByteBuffer implements DataInput, DataInputInternal {
public final ByteBuffer buf;
public int pos;
public DataInputByteBuffer(final ByteBuffer buf, final int pos) {
//$DELAY$
this.buf = buf;
this.pos = pos;
}
/**
* @param b byte buffer
* @deprecated use {@link org.mapdb.DataIO.DataInputByteArray}
*/
public DataInputByteBuffer(byte[] b) {
this(ByteBuffer.wrap(b),0);
}
@Override
public void readFully(byte[] b) throws IOException {
readFully(b, 0, b.length);
}
@Override
public void readFully(byte[] b, int off, int len) throws IOException {
ByteBuffer clone = buf.duplicate();
clone.position(pos);
//$DELAY$
pos+=len;
clone.get(b, off, len);
}
@Override
public int skipBytes(final int n) throws IOException {
pos +=n;
//$DELAY$
return n;
}
@Override
public boolean readBoolean() throws IOException {
//$DELAY$
return buf.get(pos++) ==1;
}
@Override
public byte readByte() throws IOException {
//$DELAY$
return buf.get(pos++);
}
@Override
public int readUnsignedByte() throws IOException {
//$DELAY$
return buf.get(pos++)& 0xff;
}
@Override
public short readShort() throws IOException {
final short ret = buf.getShort(pos);
//$DELAY$
pos+=2;
return ret;
}
@Override
public int readUnsignedShort() throws IOException {
return readChar();
}
@Override
public char readChar() throws IOException {
//$DELAY$
return (char) (
((buf.get(pos++) & 0xff) << 8) |
(buf.get(pos++) & 0xff));
}
@Override
public int readInt() throws IOException {
final int ret = buf.getInt(pos);
//$DELAY$
pos+=4;
return ret;
}
@Override
public long readLong() throws IOException {
final long ret = buf.getLong(pos);
//$DELAY$
pos+=8;
return ret;
}
@Override
public float readFloat() throws IOException {
final float ret = buf.getFloat(pos);
//$DELAY$
pos+=4;
return ret;
}
@Override
public double readDouble() throws IOException {
final double ret = buf.getDouble(pos);
//$DELAY$
pos+=8;
return ret;
}
@Override
public String readLine() throws IOException {
return readUTF();
}
@Override
public String readUTF() throws IOException {
//TODO verify this method accross multiple serializers
final int size = unpackInt();
//$DELAY$
return SerializerBase.deserializeString(this, size);
}
@Override
public int getPos() {
return pos;
}
@Override
public void setPos(int pos) {
this.pos = pos;
}
@Override
public byte[] internalByteArray() {
return null;
}
@Override
public ByteBuffer internalByteBuffer() {
return buf;
}
@Override
public void close() {
}
@Override
public long unpackLong() throws IOException {
long ret = 0;
byte v;
do{
v = buf.get(pos++);
ret = (ret<<7 ) | (v & 0x7F);
}while(v<0);
return ret;
}
@Override
public int unpackInt() throws IOException {
int ret = 0;
byte v;
do{
v = buf.get(pos++);
ret = (ret<<7 ) | (v & 0x7F);
}while(v<0);
return ret;
}
@Override
public long[] unpackLongArrayDeltaCompression(final int size) throws IOException {
long[] ret = new long[size];
int pos2 = pos;
ByteBuffer buf2 = buf;
long prev=0;
byte v;
for(int i=0;i<size;i++){
long r = 0;
do {
//$DELAY$
v = buf2.get(pos2++);
r = (r << 7) | (v & 0x7F);
} while (v < 0);
prev+=r;
ret[i]=prev;
}
pos = pos2;
return ret;
}
@Override
public void unpackLongArray(long[] array, int start, int end) {
int pos2 = pos;
ByteBuffer buf2 = buf;
long ret;
byte v;
for(;start<end;start++) {
ret = 0;
do {
//$DELAY$
v = buf2.get(pos2++);
ret = (ret << 7) | (v & 0x7F);
} while (v < 0);
array[start] = ret;
}
pos = pos2;
}
@Override
public void unpackIntArray(int[] array, int start, int end) {
int pos2 = pos;
ByteBuffer buf2 = buf;
int ret;
byte v;
for(;start<end;start++) {
ret = 0;
do {
//$DELAY$
v = buf2.get(pos2++);
ret = (ret << 7) | (v & 0x7F);
} while (v < 0);
array[start] = ret;
}
pos = pos2;
}
}
/**
* Provides {@link java.io.DataOutput} implementation on top of growable {@code byte[]}
* {@link java.io.ByteArrayOutputStream} is not used as it requires {@code byte[]} copying
*
* @author Jan Kotek
*/
public static final class DataOutputByteArray extends OutputStream implements DataOutput {
public byte[] buf;
public int pos;
public int sizeMask;
public DataOutputByteArray(){
pos = 0;
buf = new byte[128]; //TODO take hint from serializer for initial size
sizeMask = 0xFFFFFFFF-(buf.length-1);
}
public byte[] copyBytes(){
return Arrays.copyOf(buf, pos);
}
/**
* make sure there will be enough space in buffer to write N bytes
* @param n number of bytes which can be safely written after this method returns
*/
public void ensureAvail(int n) {
//$DELAY$
n+=pos;
if ((n&sizeMask)!=0) {
grow(n);
}
}
private void grow(int n) {
//$DELAY$
int newSize = Math.max(nextPowTwo(n),buf.length);
sizeMask = 0xFFFFFFFF-(newSize-1);
buf = Arrays.copyOf(buf, newSize);
}
@Override
public void write(final int b) throws IOException {
ensureAvail(1);
//$DELAY$
buf[pos++] = (byte) b;
}
@Override
public void write(final byte[] b, final int off, final int len) throws IOException {
ensureAvail(len);
//$DELAY$
System.arraycopy(b, off, buf, pos, len);
pos += len;
}
@Override
public void writeBoolean(final boolean v) throws IOException {
ensureAvail(1);
//$DELAY$
buf[pos++] = (byte) (v ? 1 : 0);
}
@Override
public void writeByte(final int v) throws IOException {
ensureAvail(1);
//$DELAY$
buf[pos++] = (byte) (v);
}