Repository navigation
Expand file tree
/
Copy pathConverter.java
More file actions
306 lines (254 loc) · 9.2 KB
/
Copy pathConverter.java
File metadata and controls
306 lines (254 loc) · 9.2 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
package jota.utils;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
/**
* This class provides a set of utility methods to are used to convert between different formats.
*/
public class Converter {
/**
* The radix
*/
private static final int RADIX = 3;
/**
* The maximum trit value
*/
private static final int MAX_TRIT_VALUE = (RADIX - 1) / 2, MIN_TRIT_VALUE = -MAX_TRIT_VALUE;
/**
* The number of trits in a byte
*/
private static final int NUMBER_OF_TRITS_IN_A_BYTE = 5;
/**
* The number of trits in a tryte
*/
private static final int NUMBER_OF_TRITS_IN_A_TRYTE = 3;
private static final int[][] BYTE_TO_TRITS_MAPPINGS = new int[243][];
private static final int[][] TRYTE_TO_TRITS_MAPPINGS = new int[27][];
public static final int HIGH_INTEGER_BITS = 0xFFFFFFFF;
public static final long HIGH_LONG_BITS = 0xFFFFFFFFFFFFFFFFL;
static {
final int[] trits = new int[NUMBER_OF_TRITS_IN_A_BYTE];
for (int i = 0; i < 243; i++) {
BYTE_TO_TRITS_MAPPINGS[i] = Arrays.copyOf(trits, NUMBER_OF_TRITS_IN_A_BYTE);
increment(trits, NUMBER_OF_TRITS_IN_A_BYTE);
}
for (int i = 0; i < 27; i++) {
TRYTE_TO_TRITS_MAPPINGS[i] = Arrays.copyOf(trits, NUMBER_OF_TRITS_IN_A_TRYTE);
increment(trits, NUMBER_OF_TRITS_IN_A_TRYTE);
}
}
/**
* Converts the specified trits array to bytes.
*
* @param trits The trits.
* @param offset The offset to start from.
* @param size The size.
* @return The bytes.
*/
public static byte[] bytes(final int[] trits, final int offset, final int size) {
final byte[] bytes = new byte[(size + NUMBER_OF_TRITS_IN_A_BYTE - 1) / NUMBER_OF_TRITS_IN_A_BYTE];
for (int i = 0; i < bytes.length; i++) {
int value = 0;
for (int j = (size - i * NUMBER_OF_TRITS_IN_A_BYTE) < 5 ? (size - i * NUMBER_OF_TRITS_IN_A_BYTE) : NUMBER_OF_TRITS_IN_A_BYTE; j-- > 0; ) {
value = value * RADIX + trits[offset + i * NUMBER_OF_TRITS_IN_A_BYTE + j];
}
bytes[i] = (byte) value;
}
return bytes;
}
public static byte[] bytes(final int[] trits) {
return bytes(trits, 0, trits.length);
}
/**
* Gets the trits from the specified bytes and stores it into the provided trits array.
*
* @param bytes The bytes.
* @param trits The trits.
*/
public static void getTrits(final byte[] bytes, final int[] trits) {
int offset = 0;
for (int i = 0; i < bytes.length && offset < trits.length; i++) {
System.arraycopy(BYTE_TO_TRITS_MAPPINGS[bytes[i] < 0 ? (bytes[i] + BYTE_TO_TRITS_MAPPINGS.length) : bytes[i]], 0, trits, offset, trits.length - offset < NUMBER_OF_TRITS_IN_A_BYTE ? (trits.length - offset) : NUMBER_OF_TRITS_IN_A_BYTE);
offset += NUMBER_OF_TRITS_IN_A_BYTE;
}
while (offset < trits.length) {
trits[offset++] = 0;
}
}
public static int[] convertToIntArray(List<Integer> integers) {
int[] ret = new int[integers.size()];
for (int i = 0; i < ret.length; i++) {
ret[i] = integers.get(i);
}
return ret;
}
/**
* Converts the specified trinary encoded string into a trits array of the specified length.
*
* @param trytes The trytes.
* @param length The length
* @return A trits array.
*/
public static int[] trits(final String trytes, int length) {
int[] trits = trits(trytes);
List<Integer> tritsList = new LinkedList<>();
for (int i : trits)
tritsList.add(i);
while (tritsList.size() < length)
tritsList.add(0);
return convertToIntArray(tritsList);
}
/**
* Converts the specified trinary encoded string into a trits array of the specified length.
*
* @param trytes The trytes.
* @param length The length.
* @return A trits array.
*/
public static int[] trits(final long trytes, int length) {
int[] trits = trits(String.valueOf(trytes));
List<Integer> tritsList = new LinkedList<>();
for (int i : trits)
tritsList.add(i);
while (tritsList.size() < length)
tritsList.add(0);
return convertToIntArray(tritsList);
}
/**
* Converts the specified trinary encoded trytes string to trits.
*
* @param trytes The trytes.
* @return A trits array.
*/
public static int[] tritsString(final String trytes) {
int[] d = new int[3 * trytes.length()];
for (int i = 0; i < trytes.length(); i++) {
System.arraycopy(TRYTE_TO_TRITS_MAPPINGS[Constants.TRYTE_ALPHABET.indexOf(trytes.charAt(i))], 0, d, i * NUMBER_OF_TRITS_IN_A_TRYTE, NUMBER_OF_TRITS_IN_A_TRYTE);
}
return d;
}
/**
* Converts trytes into trits.
*
* @param trytes The trytes to be converted.
* @return Array of trits.
**/
public static int[] trits(final String trytes) {
final List<Integer> trits = new LinkedList<>();
if (InputValidator.isValue(trytes)) {
long value = Long.parseLong(trytes);
long absoluteValue = value < 0 ? -value : value;
int position = 0;
while (absoluteValue > 0) {
int remainder = (int) (absoluteValue % RADIX);
absoluteValue /= RADIX;
if (remainder > MAX_TRIT_VALUE) {
remainder = MIN_TRIT_VALUE;
absoluteValue++;
}
trits.add(position++, remainder);
}
if (value < 0) {
for (int i = 0; i < trits.size(); i++) {
trits.set(i, -trits.get(i));
}
}
} else {
int[] d = new int[3 * trytes.length()];
for (int i = 0; i < trytes.length(); i++) {
System.arraycopy(TRYTE_TO_TRITS_MAPPINGS[Constants.TRYTE_ALPHABET.indexOf(trytes.charAt(i))], 0, d, i * NUMBER_OF_TRITS_IN_A_TRYTE, NUMBER_OF_TRITS_IN_A_TRYTE);
}
return d;
}
return convertToIntArray(trits);
}
/**
* Copies the trits from the input string into the destination array
*
* @param input The input String.
* @param destination The destination array.
* @return The destination.
*/
public static int[] copyTrits(final String input, final int[] destination) {
for (int i = 0; i < input.length(); i++) {
int index = Constants.TRYTE_ALPHABET.indexOf(input.charAt(i));
destination[i * 3] = TRYTE_TO_TRITS_MAPPINGS[index][0];
destination[i * 3 + 1] = TRYTE_TO_TRITS_MAPPINGS[index][1];
destination[i * 3 + 2] = TRYTE_TO_TRITS_MAPPINGS[index][2];
}
return destination;
}
/**
* Converts trites to trytes.
*
* @param trits Teh trits to be converted.
* @param offset The offset to start from.
* @param size The size.
* @return The trytes.
**/
public static String trytes(final int[] trits, final int offset, final int size) {
StringBuilder trytes = new StringBuilder();
for (int i = 0; i < (size + NUMBER_OF_TRITS_IN_A_TRYTE - 1) / NUMBER_OF_TRITS_IN_A_TRYTE; i++) {
int j = trits[offset + i * 3] + trits[offset + i * 3 + 1] * 3 + trits[offset + i * 3 + 2] * 9;
if (j < 0) {
j += Constants.TRYTE_ALPHABET.length();
}
trytes.append(Constants.TRYTE_ALPHABET.charAt(j));
}
return trytes.toString();
}
public static String trytes(final int[] trits) {
return trytes(trits, 0, trits.length);
}
/**
* Converts the specified trits array to trytes in integer representation.
*
* @param trits The trits.
* @param offset The offset to start from.
* @return The value.
*/
public static int tryteValue(final int[] trits, final int offset) {
return trits[offset] + trits[offset + 1] * 3 + trits[offset + 2] * 9;
}
/**
* Converts the specified trits to its corresponding integer value.
*
* @param trits The trits.
* @return The value.
*/
public static int value(final int[] trits) {
int value = 0;
for (int i = trits.length; i-- > 0; ) {
value = value * 3 + trits[i];
}
return value;
}
/**
* Converts the specified trits to its corresponding integer value.
*
* @param trits The trits.
* @return The value.
*/
public static long longValue(final int[] trits) {
long value = 0;
for (int i = trits.length; i-- > 0; ) {
value = value * 3 + trits[i];
}
return value;
}
/**
* Increments the specified trits.
*
* @param trits The trits.
* @param size The size.
*/
public static void increment(final int[] trits, final int size) {
for (int i = 0; i < size; i++) {
if (++trits[i] > Converter.MAX_TRIT_VALUE) {
trits[i] = Converter.MIN_TRIT_VALUE;
} else {
break;
}
}
}
}