-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathByteVisualization.java
357 lines (319 loc) · 11.7 KB
/
ByteVisualization.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
/*
* Copyright © 2015 Lable ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.lable.oss.bitsandbytes;
import java.io.ByteArrayOutputStream;
/**
* Visualize byte arrays in a variety of ways.
*/
public enum ByteVisualization {
/**
* Use the braille Unicode character range (U+2800–U+28FF) to visualize a byte. The braille dots correspond to the
* bits set.
*/
BRAILLE {
@Override
public String visualize(byte input) {
int offset = input < 0 ? 256 - Math.abs(input) : input;
return Character.toString((char) ('\u2800' + offset));
}
@Override
public byte[] parse(String input) {
if (input == null) return new byte[0];
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
input.chars()
// Skip any unparsable characters.
.filter(c -> c >= '\u2800' && c <= '\u28FF')
.map(c -> (byte) c - '\u2800')
.forEach(baos::write);
return baos.toByteArray();
}
@Override
public byte parseByte(String input) {
if (input == null) return 0;
for (char c : input.toCharArray()) {
if (c >= '\u2800' && c <= '\u28FF') {
return (byte) (c - '\u2800');
}
}
return 0;
}
},
/**
* Represent a byte as a string of ones and zeroes, e.g., {@code 11000000} for {@code 192}.
*/
ONES_AND_ZEROES {
@Override
public String visualize(byte input) {
char[] out = new char[8];
for (int i = 7; i > -1; i--) {
out[7 - i] = (input >> i & 0x1) == 1 ? '1' : '0';
}
return new String(out);
}
@Override
public byte[] parse(String input) {
if (input == null) return new byte[0];
StringBuilder cleanInput = new StringBuilder();
for (char c : input.toCharArray()) {
// Strip out anything that isn't a 1 or 0.
if (c == '0' || c == '1') {
cleanInput.append(c);
}
}
char[] chars = cleanInput.toString().toCharArray();
int targetLength = chars.length / 8;
int remainder = chars.length % 8;
if (remainder != 0) {
targetLength += 1;
}
byte[] output = new byte[targetLength];
// If the input was not neatly divisible by 8, pad out the first byte with 0s by starting later in the loop.
int bitCounter = remainder == 0 ? 0 : 8 - remainder;
int byteCounter = 0;
for (char bit : chars) {
if (bit == '1') {
output[byteCounter] |= (byte) (1 << (7 - bitCounter));
}
bitCounter++;
if (bitCounter == 8) {
byteCounter++;
bitCounter = 0;
}
}
return output;
}
@Override
public byte parseByte(String input) {
byte out = 0;
int bitCounter = 7;
char[] in = input.toCharArray();
for (int i = in.length - 1; i >= 0; i--) {
char bit = in[i];
if (bit == '1') {
out |= (byte) (1 << (7 - bitCounter));
bitCounter--;
} else if (bit == '0') {
bitCounter--;
}
}
return out;
}
},
/**
* Represent a byte as a hexadecimal string, e.g., {@code C0} for {@code 192}.
*/
HEXADECIMAL {
@Override
public String visualize(byte input) {
int high = input >> 4 & 0x0F;
int low = input & 0x0F;
return new String(new char[]{
(high > 9 ? (char) (high + 0x37) : (char) (high + 0x30)),
(low > 9 ? (char) (low + 0x37) : (char) (low + 0x30))
});
}
@Override
public byte[] parse(String input) {
if (input == null) return new byte[0];
input = input.trim();
if (input.startsWith("0x") || input.startsWith("0X")) {
input = input.substring(2);
}
StringBuilder cleanInput = new StringBuilder();
for (char c : input.toCharArray()) {
// Strip out anything that isn't one of the sixteen hex chars.
if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')) {
cleanInput.append(c);
} else if (c >= 'A' && c <= 'F') {
// Turn uppercase A–F into lowercase a–f.
cleanInput.append((char) (c + 0x20));
}
}
char[] chars;
if (cleanInput.length() % 2 == 0) {
chars = cleanInput.toString().toCharArray();
} else {
// Prepend a '0' if the input string has an odd number of characters.
chars = new char[cleanInput.length() + 1];
chars[0] = '0';
cleanInput.getChars(0, cleanInput.length(), chars, 1);
}
int targetLength = chars.length / 2;
byte[] output = new byte[targetLength];
for (int i = 0; i < chars.length / 2; i++) {
output[i] = (byte) (value(chars[i * 2]) << 4 ^ value(chars[i * 2 + 1]));
}
return output;
}
@Override
public byte parseByte(String input) {
input = input.trim();
if (input.startsWith("0x") || input.startsWith("0X")) {
input = input.substring(2);
}
boolean doHigh = true;
boolean setLow = false;
int high = 0;
int low = 0;
for (char c : input.toCharArray()) {
// Ignore anything that isn't one of the sixteen hex chars.
if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')) {
if (doHigh) {
high = value(c);
doHigh = false;
} else {
low = value(c);
setLow = true;
}
} else if (c >= 'A' && c <= 'F') {
// Turn uppercase A–F into lowercase a–f.
c = (char) (c + 0x20);
if (doHigh) {
high = value(c);
doHigh = false;
} else {
low = value(c);
setLow = true;
}
}
}
// If only one character was parsed, treat it as the lower four bits.
// This makes '0x0F', '0F', and 'F', all equal.
return setLow
? (byte) (high << 4 ^ low)
: (byte) high;
}
private int value(char c) {
return c >= '0' && c <= '9' ? c - 0x30 : c - 0x57;
}
},
HEXADECIMAL_LOWER {
@Override
public String visualize(byte input) {
int high = input >> 4 & 0x0F;
int low = input & 0x0F;
return new String(new char[]{
(high > 9 ? (char) (high + 0x57) : (char) (high + 0x30)),
(low > 9 ? (char) (low + 0x57) : (char) (low + 0x30))
});
}
@Override
public byte[] parse(String input) {
return HEXADECIMAL.parse(input);
}
@Override
public byte parseByte(String input) {
return HEXADECIMAL.parseByte(input);
}
},
/**
* Use Unicode Block Elements to represent each byte using two glyphs.
*/
SQUARES {
// 0..F mapped to a character from the Block Elements range.
final char[] GLYPHS = new char[]{' ', '▘', '▝', '▀', '▖', '▌', '▞', '▛', '▗', '▚', '▐', '▜', '▄', '▙', '▟', '█'};
// Map each character in the Block Elements range to its byte value (if applicable).
// Invalid characters are mapped to -1. The 0 byte is represented by space (U+0020),
// which lies outside of this block.
final byte[] VALUES = new byte[]{
3, -1, -1, -1, 0xC, -1, -1, -1, 0xF, -1, -1, -1, 5, -1, -1, -1,
0xA, -1, -1, -1, -1, -1, 4, 8, 1, 0xD, 9, 7, 0xB, 2, 6, 0xE
};
@Override
public String visualize(byte input) {
return Character.toString(GLYPHS[(input & 0xff) >> 4]) + Character.toString(GLYPHS[input & 0xf]);
}
@Override
public byte[] parse(String input) {
if (input == null) return new byte[0];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Integer firstOfPair = null;
for (char c : input.toCharArray()) {
int value = isValid(c);
// Skip all unparsable characters.
if (value >= 0) {
if (firstOfPair == null) {
firstOfPair = value;
} else {
baos.write(firstOfPair << 4 | value);
firstOfPair = null;
}
}
}
return baos.toByteArray();
}
@Override
public byte parseByte(String input) {
Integer firstOfPair = null;
for (char c : input.toCharArray()) {
int value = isValid(c);
// Skip all unparsable characters.
if (value >= 0) {
if (firstOfPair == null) {
firstOfPair = value;
} else {
return (byte) (firstOfPair << 4 | value);
}
}
}
return 0;
}
int isValid(char c) {
if (c == ' ') return 0;
if (c >= '\u2580' && c <= '\u259F') return VALUES[c - '\u2580'];
return -1;
}
};
/**
* Visualize a byte with a printable string.
*
* @param input A byte.
* @return A printable string.
*/
public abstract String visualize(byte input);
/**
* Turn a visualization of a byte array into the byte array it represents.
*
* @param input A string visualization.
* @return A byte array.
*/
public abstract byte[] parse(String input);
/**
* Turn a visualization of a byte into the byte it represents.
*
* @param input A string visualization.
* @return A byte.
*/
public abstract byte parseByte(String input);
/**
* Visualize a byte array with a printable string.
*
* @param input A byte array.
* @return A printable string.
*/
public String visualize(byte[] input) {
if (input == null) {
return "NULL";
}
// This method could be overridden if needed, but currently all visualization
// techniques can work on a single byte.
StringBuilder builder = new StringBuilder();
for (byte b : input) {
builder.append(visualize(b));
}
return builder.toString();
}
}