Skip to content

Commit

Permalink
Reformat TypeConversion to Google Java Style guide
Browse files Browse the repository at this point in the history
  • Loading branch information
seanrobbins committed Jan 20, 2018
1 parent c57f219 commit 3fbee8b
Showing 1 changed file with 53 additions and 52 deletions.
105 changes: 53 additions & 52 deletions src/main/java/org/tron/common/utils/TypeConversion.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,73 +21,74 @@
import java.nio.ByteBuffer;

public class TypeConversion {
private static ByteBuffer buffer = ByteBuffer.allocate(8);

public static byte[] longToBytes(long x) {
buffer.putLong(0, x);
return buffer.array();
}
private static ByteBuffer buffer = ByteBuffer.allocate(8);

public static long bytesToLong(byte[] bytes) {
buffer.put(bytes, 0, bytes.length);
buffer.flip();
return buffer.getLong();
}
public static byte[] longToBytes(long x) {
buffer.putLong(0, x);
return buffer.array();
}

public static String bytesToHexString(byte[] src) {
StringBuilder stringBuilder = new StringBuilder("");
public static long bytesToLong(byte[] bytes) {
buffer.put(bytes, 0, bytes.length);
buffer.flip();
return buffer.getLong();
}

if (src == null || src.length <= 0) {
return null;
}
public static String bytesToHexString(byte[] src) {
StringBuilder stringBuilder = new StringBuilder("");

for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if (src == null || src.length <= 0) {
return null;
}

if (hv.length() < 2) {
stringBuilder.append(0);
}
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);

stringBuilder.append(hv);
}
if (hv.length() < 2) {
stringBuilder.append(0);
}

return stringBuilder.toString();
stringBuilder.append(hv);
}

public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;
}
return stringBuilder.toString();
}

hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;
}

for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte
(hexChars[pos + 1]));
}
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];

return d;
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte
(hexChars[pos + 1]));
}

private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
return d;
}

private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}

public static boolean increment(byte[] bytes) {
final int startIndex = 0;
int i;
for (i = bytes.length - 1; i >= startIndex; i--) {
bytes[i]++;
if (bytes[i] != 0) {
break;
}
}

public static boolean increment(byte[] bytes) {
final int startIndex = 0;
int i;
for (i = bytes.length - 1; i >= startIndex; i--) {
bytes[i]++;
if (bytes[i] != 0) {
break;
}
}

return (i >= startIndex || bytes[startIndex] != 0);
}
return (i >= startIndex || bytes[startIndex] != 0);
}
}

0 comments on commit 3fbee8b

Please sign in to comment.