Coverage Summary for Class: Utils (org.ethereum.json)
Class |
Class, %
|
Method, %
|
Line, %
|
Utils |
100%
(1/1)
|
14.3%
(1/7)
|
12.1%
(4/33)
|
1 /*
2 * This file is part of RskJ
3 * Copyright (C) 2017 RSK Labs Ltd.
4 * (derived from ethereumJ library, Copyright (c) 2016 <ether.camp>)
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 package org.ethereum.json;
21
22 import org.ethereum.util.ByteUtil;
23
24 import org.bouncycastle.util.encoders.Hex;
25
26 import java.math.BigInteger;
27
28 import static org.ethereum.util.ByteUtil.EMPTY_BYTE_ARRAY;
29 import static org.ethereum.util.Utils.unifiedNumericToBigInteger;
30
31 /**
32 * @author Roman Mandeleil
33 * @since 15.12.2014
34 */
35 public class Utils {
36
37 public static byte[] parseVarData(String data){
38 if (data == null || data.equals("")) {
39 return EMPTY_BYTE_ARRAY;
40 }
41 if (data.startsWith("0x")) {
42 data = data.substring(2);
43 if (data.equals("")) {
44 return EMPTY_BYTE_ARRAY;
45 }
46
47 if (data.length() % 2 == 1) {
48 data = "0" + data;
49 }
50
51 return Hex.decode(data);
52 }
53
54 return parseNumericData(data);
55 }
56
57
58 public static byte[] parseData(String data) {
59 if (data == null) {
60 return EMPTY_BYTE_ARRAY;
61 }
62 if (data.startsWith("0x")) {
63 data = data.substring(2);
64 }
65 return Hex.decode(data);
66 }
67
68 public static byte[] parseNumericData(String data){
69
70 if (data == null || data.equals("")) {
71 return EMPTY_BYTE_ARRAY;
72 }
73 byte[] dataB = unifiedNumericToBigInteger(data).toByteArray();
74 return ByteUtil.stripLeadingZeroes(dataB);
75 }
76
77 public static long parseLong(String data) {
78 boolean hex = data.startsWith("0x");
79 if (hex) {
80 data = data.substring(2);
81 }
82 if (data.equals("")) {
83 return 0;
84 }
85 return new BigInteger(data, hex ? 16 : 10).longValue();
86 }
87
88 public static byte parseByte(String data) {
89 if (data.startsWith("0x")) {
90 data = data.substring(2);
91 return data.equals("") ? 0 : Byte.parseByte(data, 16);
92 } else {
93 return data.equals("") ? 0 : Byte.parseByte(data);
94 }
95 }
96
97
98 public static String parseUnidentifiedBase(String number) {
99 if (number.startsWith("0x")) {
100 number = new BigInteger(number.substring(2), 16).toString(10);
101 }
102 return number;
103 }
104 }