forked from tronprotocol/java-tron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.java
More file actions
executable file
·88 lines (74 loc) · 2.71 KB
/
Utils.java
File metadata and controls
executable file
·88 lines (74 loc) · 2.71 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
/*
* Copyright (c) [2016] [ <ether.camp> ]
* This file is part of the ethereumJ library.
*
* The ethereumJ library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ethereumJ library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the ethereumJ library. If not, see <http://www.gnu.org/licenses/>.
*/
package org.tron.common.utils;
import java.security.SecureRandom;
import java.nio.*;
import java.nio.charset.Charset;
import java.util.Arrays;
public interface Utils {
SecureRandom random = new SecureRandom();
static SecureRandom getRandom() {
return random;
}
static byte[] getBytes(char[] chars) {
Charset cs = Charset.forName("UTF-8");
CharBuffer cb = CharBuffer.allocate(chars.length);
cb.put(chars);
cb.flip();
ByteBuffer bb = cs.encode(cb);
return bb.array();
}
static String getIdShort(String Id) {
return Id == null ? "<null>" : Id.substring(0, 8);
}
static char[] getChars(byte[] bytes) {
Charset cs = Charset.forName("UTF-8");
ByteBuffer bb = ByteBuffer.allocate(bytes.length);
bb.put(bytes);
bb.flip();
CharBuffer cb = cs.decode(bb);
return cb.array();
}
static byte[] clone(byte[] value) {
byte[] clone = new byte[value.length];
System.arraycopy(value, 0, clone, 0, value.length);
return clone;
}
static String sizeToStr(long size) {
if (size < 2 * (1L << 10)) return size + "b";
if (size < 2 * (1L << 20)) return String.format("%dKb", size / (1L << 10));
if (size < 2 * (1L << 30)) return String.format("%dMb", size / (1L << 20));
return String.format("%dGb", size / (1L << 30));
}
static String align(String s, char fillChar, int targetLen, boolean alignRight) {
if (targetLen <= s.length()) return s;
String alignString = repeat("" + fillChar, targetLen - s.length());
return alignRight ? alignString + s : s + alignString;
}
static String repeat(String s, int n) {
if (s.length() == 1) {
byte[] bb = new byte[n];
Arrays.fill(bb, s.getBytes()[0]);
return new String(bb);
} else {
StringBuilder ret = new StringBuilder();
for (int i = 0; i < n; i++) ret.append(s);
return ret.toString();
}
}
}