forked from TrueCloudLab/frostfs-sdk-java
[#14] add lombok and refactor exceptions. Provide validator.
Signed-off-by: Ori Bruk <o.bruk@yadro.com>
This commit is contained in:
parent
15cf0893c7
commit
388428af76
87 changed files with 819 additions and 970 deletions
|
@ -1,11 +1,14 @@
|
|||
package info.frostfs.sdk;
|
||||
|
||||
import info.frostfs.sdk.exceptions.ProcessFrostFSException;
|
||||
import info.frostfs.sdk.exceptions.ValidationFrostFSException;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static info.frostfs.sdk.ArrayHelper.concat;
|
||||
import static info.frostfs.sdk.Helper.getSha256;
|
||||
import static info.frostfs.sdk.constants.ErrorConst.*;
|
||||
import static java.util.Objects.isNull;
|
||||
|
||||
public class Base58 {
|
||||
|
@ -16,8 +19,6 @@ public class Base58 {
|
|||
private static final char ENCODED_ZERO = ALPHABET[0];
|
||||
private static final char BASE58_ASCII_MAX_VALUE = 128;
|
||||
private static final int[] INDEXES = new int[BASE58_ASCII_MAX_VALUE];
|
||||
private static final String ERROR_VALUE_MISSING_MESSAGE = "Input value is missing";
|
||||
private static final String ERROR_INVALID_CHARACTER_TEMPLATE = "Invalid character in Base58: 0x%04x";
|
||||
|
||||
static {
|
||||
Arrays.fill(INDEXES, -1);
|
||||
|
@ -31,12 +32,12 @@ public class Base58 {
|
|||
|
||||
public static byte[] base58CheckDecode(String input) {
|
||||
if (StringUtils.isEmpty(input)) {
|
||||
throw new IllegalArgumentException(ERROR_VALUE_MISSING_MESSAGE);
|
||||
throw new ValidationFrostFSException(INPUT_PARAM_IS_MISSING);
|
||||
}
|
||||
|
||||
byte[] buffer = decode(input);
|
||||
if (buffer.length < 4) {
|
||||
throw new IllegalArgumentException();
|
||||
throw new ProcessFrostFSException(String.format(DECODE_LENGTH_VALUE_TEMPLATE, buffer.length));
|
||||
}
|
||||
|
||||
byte[] decode = Arrays.copyOfRange(buffer, 0, buffer.length - 4);
|
||||
|
@ -44,7 +45,7 @@ public class Base58 {
|
|||
var bufferEnd = Arrays.copyOfRange(buffer, buffer.length - 4, buffer.length);
|
||||
var checksumStart = Arrays.copyOfRange(checksum, 0, 4);
|
||||
if (!Arrays.equals(bufferEnd, checksumStart)) {
|
||||
throw new IllegalArgumentException();
|
||||
throw new ProcessFrostFSException(INVALID_CHECKSUM);
|
||||
}
|
||||
|
||||
return decode;
|
||||
|
@ -52,7 +53,7 @@ public class Base58 {
|
|||
|
||||
public static String base58CheckEncode(byte[] data) {
|
||||
if (isNull(data)) {
|
||||
throw new IllegalArgumentException(ERROR_VALUE_MISSING_MESSAGE);
|
||||
throw new ValidationFrostFSException(INPUT_PARAM_IS_MISSING);
|
||||
}
|
||||
|
||||
byte[] checksum = getSha256(getSha256(data));
|
||||
|
@ -101,7 +102,7 @@ public class Base58 {
|
|||
char c = input.charAt(i);
|
||||
int digit = c < BASE58_ASCII_MAX_VALUE ? INDEXES[c] : -1;
|
||||
if (digit < 0) {
|
||||
throw new IllegalArgumentException(String.format(ERROR_INVALID_CHARACTER_TEMPLATE, (int) c));
|
||||
throw new ValidationFrostFSException(String.format(INVALID_BASE58_CHARACTER_TEMPLATE, (int) c));
|
||||
}
|
||||
input58[i] = (byte) digit;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue