-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFineBytes.java
More file actions
602 lines (470 loc) · 17.3 KB
/
FineBytes.java
File metadata and controls
602 lines (470 loc) · 17.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
/*
* The MIT License (MIT)
* Copyright (c) 2016 Ethan Gaebel
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import java.lang.IllegalStateException;
/**
* Class to support bit-level byte manipulation in an array of bytes.
* This array of bytes can be accessed by a byte index and a bit index.
*/
public class FineBytes {
//~Constants-----------------------------------
private static final int DEFAULT_CAPACITY = 20;
//~Fields--------------------------------------
/**
* The array of bytes backing FineBytes.
*/
private byte[] bytes;
/**
* The index of the bit in the final byte of the bytes array.
*/
private int bitIndex;
/**
* The index of the last (possibly incomplete) byte.
*/
private int byteIndex;
/**
* Set up the byte array with the DEFAULT_CAPACITY (20) and 0 out everything.
*/
public FineBytes() {
bytes = new byte[DEFAULT_CAPACITY];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = 0x00;
}
bitIndex = 0;
byteIndex = 0;
}
/**
* Set up the byte array with the passed in capacity and 0 out everything.
*/
public FineBytes(int capacity) {
bytes = new byte[capacity];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = 0x00;
}
bitIndex = 0;
byteIndex = 0;
}
/**
* Set up the byte array with the passed in byte array.
*/
public FineBytes(byte[] bytes) {
this.bytes = bytes;
bitIndex = 0;
byteIndex = bytes.length;
}
/**
* Set up the byte array using a string of binary digits.
*/
public FineBytes(String binaryString) {
bytes = new byte[((int) Math.ceil(binaryString.length() / 8))];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = 0x00;
}
byteIndex = 0;
bitIndex = 0;
String byteSizedString;
for (int i = 0; i < binaryString.length(); i += 8) {
if (i + 7 < binaryString.length()) {
byteSizedString = binaryString.substring(i, i + 8);
bytes[byteIndex] = this.parseBinaryString(byteSizedString);
byteIndex++;
} else {
byteSizedString = binaryString.substring(i, binaryString.length());
bytes[byteIndex] = this.parseBinaryString(byteSizedString);
bitIndex += byteSizedString.length();
}
}
}
/**
* Double the size of the byte array.
*/
private void resize() {
byte[] newBytes = new byte[bytes.length * 2];
for (int i = 0; i < bytes.length; i++) {
newBytes[i] = bytes[i];
}
for (int j = bytes.length; j < newBytes.length; j++) {
newBytes[j] = 0x00;
}
bytes = newBytes;
}
/**
* Add an array of bytes to the end.
*/
public void addBytes(byte[] newBytes) {
if ((byteIndex + newBytes.length) >= bytes.length) {
resize();
}
if (this.bitIndex != 0) {
byte bitMask = 0x00;
switch (this.bitIndex) {
case 1:
bitMask = 0x7F;
break;
case 2:
bitMask = 0x3F;
break;
case 3:
bitMask = 0x1F;
break;
case 4:
bitMask = 0x0F;
break;
case 5:
bitMask = 0x07;
break;
case 6:
bitMask = 0x03;
break;
case 7:
bitMask = 0x01;
break;
default:
throw new IllegalStateException("");
}
bytes[this.byteIndex] = (byte) ((bytes[this.byteIndex] << (8 - bitIndex))
^ ((newBytes[0] >>> this.bitIndex) & bitMask));
this.byteIndex++;
int newBytesBitIndex = 8 - this.bitIndex;
this.bitIndex = 0;
//todo: fix this awful mess....
int newByteIndex = 0;
while (newByteIndex < newBytes.length) {
if (this.bitIndex != 0) {
bitMask = 0x00;
switch (this.bitIndex) {
case 1:
bitMask = 0x7F;
break;
case 2:
bitMask = 0x3F;
break;
case 3:
bitMask = 0x1F;
break;
case 4:
bitMask = 0x0F;
break;
case 5:
bitMask = 0x07;
break;
case 6:
bitMask = 0x03;
break;
case 7:
bitMask = 0x01;
break;
default:
throw new IllegalStateException("");
}
bytes[this.byteIndex] = (byte) ((bytes[this.byteIndex] << (8 - bitIndex))
^ ((newBytes[newByteIndex] >>> this.bitIndex) & bitMask));
this.byteIndex++;
newBytesBitIndex = 8 - this.bitIndex;
this.bitIndex = 0;
} else {
bitMask = 0x00;
switch (newBytesBitIndex) {
case 1:
bitMask = 0x7F;
break;
case 2:
bitMask = 0x3F;
break;
case 3:
bitMask = 0x1F;
break;
case 4:
bitMask = 0x0F;
break;
case 5:
bitMask = 0x07;
break;
case 6:
bitMask = 0x03;
break;
case 7:
bitMask = 0x01;
break;
default:
throw new IllegalStateException("");
}
bytes[this.byteIndex] = (byte) ((bytes[this.byteIndex])
^ (newBytes[newByteIndex] & bitMask));
this.bitIndex = 8 - newBytesBitIndex;
newByteIndex++;
}
}
} else {
for (int j = 0; j < newBytes.length; this.byteIndex++, j++) {
bytes[this.byteIndex] = newBytes[j];
}
}
}
/**
* Add a string of bits to the end.
*/
public void addBits(String binaryString) {
//If we need to resize
if ((byteIndex + binaryString.length()) >= bytes.length) {
resize();
}
String byteSizedString;
if (bitIndex != 0) {
int substringEndIndex = ((8 - bitIndex) > binaryString.length())
? binaryString.length() : (8 - bitIndex);
String bitSmallerString = binaryString.substring(0, substringEndIndex);
byte tempByte = this.parseBinaryString(bitSmallerString);
//handle the fucking signed-ness
if ((bitIndex + substringEndIndex) <= 7) {
bytes[byteIndex] = (byte) ((((int) bytes[byteIndex]) << substringEndIndex)
^ tempByte);
bitIndex += substringEndIndex;
} else {
bytes[byteIndex] = (byte) ((((int) bytes[byteIndex]) << substringEndIndex)
^ tempByte);
bitIndex = 0;
byteIndex++;
}
binaryString = binaryString.substring(substringEndIndex, binaryString.length());
if (binaryString.length() == 0) {
return;
}
}
for (int i = 0; i < binaryString.length(); i += 8) {
if (bitIndex != 0) {
int substringEndIndex = ((8 - bitIndex) > binaryString.length())
? binaryString.length() : (8 - bitIndex);
String bitSmallerString = binaryString.substring(0, substringEndIndex);
byte tempByte = this.parseBinaryString(bitSmallerString);
bytes[byteIndex] = (byte) ((bytes[byteIndex] << substringEndIndex) ^ tempByte);
if ((bitIndex + substringEndIndex) <= 7) {
bitIndex += substringEndIndex;
} else {
bitIndex = 0;
byteIndex++;
}
} else if ((i + 7) < binaryString.length()) {
byteSizedString = binaryString.substring(i, i + 8);
bytes[byteIndex] = this.parseBinaryString(byteSizedString);
byteIndex++;
} else {
byteSizedString = binaryString.substring(i, binaryString.length());
bytes[byteIndex] = this.parseBinaryString(byteSizedString);
bitIndex += byteSizedString.length();
}
}
}
/**
* Parse a string of binary digits into bytes.
*/
public static byte parseBinaryString(String binaryString) {
//If we're going to overflow
if (binaryString.length() == 8 && binaryString.charAt(0) == '1') {
char[] binaryCharArray = binaryString.toCharArray();
binaryCharArray[0] = '0';
binaryString = "-" + new String(binaryCharArray);
}
return Byte.parseByte(binaryString, 2);
}
/**
* Get the byte at the end of the array which is not fully used.
*/
public Byte getIncompleteByte() {
if (bitIndex != 0) {
return bytes[byteIndex];
} else {
return null;
}
}
/**
* Check if there is a byte at the end of the array which is not fully used.
*/
public boolean hasIncompleteByte() {
return (bitIndex != 0);
}
/**
* Get all the bytes backing this object.
* The bytes are copied and returned.
*/
public byte[] getCompleteBytes() {
return (bitIndex != 0) ? copyBackingArray(byteIndex) : copyBackingArray(byteIndex + 1);
}
/**
* Copy the backing array of this object.
*/
private byte[] copyBackingArray() {
byte[] copy = new byte[bytes.length];
for (int i = 0; i < bytes.length; i++) {
copy[i] = bytes[i];
}
return copy;
}
/**
* Copy the backing array of this object up until index.
* Index is exclusive.
*/
private byte[] copyBackingArray(int index) {
byte[] copy = new byte[index + 1];
for (int i = 0; i < (index + 1); i++) {
copy[i] = bytes[i];
}
return copy;
}
/**
* Copy the backing array of this object from beginIndex up until endIndex.
* beginIndex is inclusive.
* endIndex is exclusive.
*
* @param beginIndex the index to begin copying at.
* @param endIndex the index to stop copying at.
* @return The copied section of bytes in this object.
*/
private byte[] copyBackingArray(int beginIndex, int endIndex) {
byte[] copy = new byte[endIndex + 1];
for (int i = 0; i < (endIndex + 1); i++) {
copy[i] = bytes[beginIndex + i];
}
return copy;
}
/**
* Get all the fully filled in bytes. Excluding any partially filled in bytes.
*/
public byte[] getBytes() {
int copyIndex = (bitIndex != 0) ? byteIndex : (byteIndex - 1);
return copyBackingArray(copyIndex);
}
/**
* Get the last numBytes from the FineBytes array.
* The last numBytes begins at the last valid bit index.
*
* @param numBytes the number of bytes to get from the byte array.
* @return the final numBytes in the byte array.
*/
public byte[] getLastBytes(int numBytes) {
if (bitIndex == 0) {
return copyBackingArray((bytes.length - numBytes), bytes.length);
}
int beginByteIndex = (bytes.length - numBytes);
byte[] copy = new byte[numBytes];
for (int i = 0; i <= numBytes; i++) {
if (numBytes > i && i > 0) {
copy[i - 1] = (byte) (((int) copy[i])
^ (((int) bytes[beginByteIndex]) >> (8 - bitIndex)));
}
copy[i] = (byte) (((int) bytes[beginByteIndex]) << bitIndex);
}
return copy;
}
/**
* Get the bit string of a passed byte.
*/
public static String getBitString(byte b) {
StringBuilder build = new StringBuilder();
String byteString;
byteString = Integer.toBinaryString(b & 0xFF);
for (int j = 0; j < (8 - byteString.length()); j++) {
build.append("0");
}
build.append(byteString);
return build.toString();
}
/**
* Get the bit string of a passed byte array.
*/
public static String getBitString(byte[] bytes) {
StringBuilder build = new StringBuilder();
String byteString;
for (int i = 0; i < bytes.length; i++) {
build.append(getBitString(bytes[i]));
}
return build.toString();
}
/**
* Get the bit string of this FineBytes object.
*/
public String getBitString() {
StringBuilder build = new StringBuilder();
String byteString;
for (int i = 0; i < byteIndex; i++) {
byteString = Integer.toBinaryString(bytes[i] & 0xFF);
for (int j = 0; j < (8 - byteString.length()); j++) {
build.append("0");
}
build.append(byteString);
}
if (bitIndex != 0) {
String binaryString = Integer.toBinaryString(bytes[byteIndex] & 0xFF);
if (bitIndex == (binaryString.length() % 8)) {
build.append(binaryString.substring(0, bitIndex));
} else {
for (int i = 0; i < bitIndex - (binaryString.length() % 8); i++) {
build.append("0");
}
build.append(binaryString);
}
}
return build.toString();
}
/**
* Get a hex string of the bytes in this FineBytes object.
*/
public String getHexString() {
StringBuilder build = new StringBuilder();
String hexString32Bit;
for (int i = 0; i < byteIndex; i++) {
hexString32Bit = Integer.toHexString(bytes[i]);
int beginIndex = (hexString32Bit.length() >= 2) ? (hexString32Bit.length() - 2) : 0;
if ((hexString32Bit.length() - beginIndex) < 2) {
build.append("0");
}
build.append(hexString32Bit.substring(beginIndex, hexString32Bit.length()));
}
if (bitIndex != 0) {
hexString32Bit = Integer.toHexString(bytes[byteIndex + 1]);
int beginIndex = (hexString32Bit.length() >= 2) ? (hexString32Bit.length() - 2) : 0;
if ((hexString32Bit.length() - beginIndex) < 2) {
build.append("0");
}
build.append(hexString32Bit.substring(beginIndex, hexString32Bit.length()));
}
return build.toString();
}
/**
* Clear out the bytes and bits in this object.
*/
public void clear() {
bytes = new byte[bytes.length];
bitIndex = 0;
byteIndex = 0;
}
/**
* Get the number of bytes in this object (including partials).
*/
public int numBytes() {
return byteIndex;
}
/**
* Get the total number of bits in this object.
*/
public int numBits() {
return (byteIndex * 8) + bitIndex;
}
}