Merge pull request #9696 from thingsboard/fix_bug_bytesToString
[fix_bug][3.6.2]stringToBytes_Input_parameter_Objects
This commit is contained in:
commit
85f5b3e32b
@ -182,12 +182,12 @@ public class TbUtils {
|
|||||||
return TbJson.parse(ctx, jsonStr);
|
return TbJson.parse(ctx, jsonStr);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String bytesToString(List<Byte> bytesList) {
|
public static String bytesToString(List<?> bytesList) {
|
||||||
byte[] bytes = bytesFromList(bytesList);
|
byte[] bytes = bytesFromList(bytesList);
|
||||||
return new String(bytes);
|
return new String(bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String bytesToString(List<Byte> bytesList, String charsetName) throws UnsupportedEncodingException {
|
public static String bytesToString(List<?> bytesList, String charsetName) throws UnsupportedEncodingException {
|
||||||
byte[] bytes = bytesFromList(bytesList);
|
byte[] bytes = bytesFromList(bytesList);
|
||||||
return new String(bytes, charsetName);
|
return new String(bytes, charsetName);
|
||||||
}
|
}
|
||||||
@ -210,10 +210,20 @@ public class TbUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static byte[] bytesFromList(List<Byte> bytesList) {
|
private static byte[] bytesFromList(List<?> bytesList) {
|
||||||
byte[] bytes = new byte[bytesList.size()];
|
byte[] bytes = new byte[bytesList.size()];
|
||||||
for (int i = 0; i < bytesList.size(); i++) {
|
for (int i = 0; i < bytesList.size(); i++) {
|
||||||
bytes[i] = bytesList.get(i);
|
Object objectVal = bytesList.get(i);
|
||||||
|
if (objectVal instanceof Integer) {
|
||||||
|
bytes[i] = isValidIntegerToByte((Integer) objectVal);
|
||||||
|
} else if (objectVal instanceof String) {
|
||||||
|
bytes[i] = isValidIntegerToByte(parseInt((String) objectVal));
|
||||||
|
} else if (objectVal instanceof Byte) {
|
||||||
|
bytes[i] = (byte) objectVal;
|
||||||
|
} else {
|
||||||
|
throw new NumberFormatException("The value '" + objectVal + "' could not be correctly converted to a byte. " +
|
||||||
|
"Must be a HexDecimal/String/Integer/Byte format !");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return bytes;
|
return bytes;
|
||||||
}
|
}
|
||||||
@ -643,7 +653,7 @@ public class TbUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isValidRadix(String value, int radix) {
|
private static boolean isValidRadix(String value, int radix) {
|
||||||
for (int i = 0; i < value.length(); i++) {
|
for (int i = 0; i < value.length(); i++) {
|
||||||
if (i == 0 && value.charAt(i) == '-') {
|
if (i == 0 && value.charAt(i) == '-') {
|
||||||
if (value.length() == 1)
|
if (value.length() == 1)
|
||||||
@ -657,4 +667,12 @@ public class TbUtils {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static byte isValidIntegerToByte (Integer val) {
|
||||||
|
if (val > 255 || val.intValue() < -128) {
|
||||||
|
throw new NumberFormatException("The value '" + val + "' could not be correctly converted to a byte. " +
|
||||||
|
"Integer to byte conversion requires the use of only 8 bits (with a range of min/max = -128/255)!");
|
||||||
|
} else {
|
||||||
|
return val.byteValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -31,6 +31,7 @@ import java.io.IOException;
|
|||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
@ -385,6 +386,95 @@ public class TbUtilsTest {
|
|||||||
Assert.assertThrows(IllegalAccessException.class, () -> TbUtils.stringToBytes(ctx, ((ExecutionHashMap) finalInputJson).get("hello"), "UTF-8"));
|
Assert.assertThrows(IllegalAccessException.class, () -> TbUtils.stringToBytes(ctx, ((ExecutionHashMap) finalInputJson).get("hello"), "UTF-8"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void bytesFromList() {
|
||||||
|
byte[] arrayBytes = {(byte)0x00, (byte)0x08, (byte)0x10, (byte)0x1C, (byte)0xFF, (byte)0xFC, (byte)0xAD, (byte)0x88, (byte)0x75, (byte)0x74, (byte)0x8A, (byte)0x82};
|
||||||
|
Object[] arrayMix = { "0x00", 8, "16", "0x1C", 255, (byte)0xFC, 173, 136, 117, 116, -118, "-126"};
|
||||||
|
|
||||||
|
String expected = new String(arrayBytes);
|
||||||
|
ArrayList<Byte> listBytes = new ArrayList<>(arrayBytes.length);
|
||||||
|
for (Byte element : arrayBytes) {
|
||||||
|
listBytes.add(element);
|
||||||
|
}
|
||||||
|
Assert.assertEquals(expected, TbUtils.bytesToString(listBytes));
|
||||||
|
|
||||||
|
ArrayList<Object> listMix = new ArrayList<>(arrayMix.length);
|
||||||
|
for (Object element : arrayMix) {
|
||||||
|
listMix.add(element);
|
||||||
|
}
|
||||||
|
Assert.assertEquals(expected, TbUtils.bytesToString(listMix));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void bytesFromList_Error() {
|
||||||
|
List<String> listHex = new ArrayList<>();
|
||||||
|
listHex.add("0xFG");
|
||||||
|
try {
|
||||||
|
TbUtils.bytesToString(listHex);
|
||||||
|
Assert.fail("Should throw NumberFormatException");
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
Assert.assertTrue(e.getMessage().contains("Failed radix: [16] for value: \"FG\"!"));
|
||||||
|
}
|
||||||
|
listHex.add(0, "1F");
|
||||||
|
try {
|
||||||
|
TbUtils.bytesToString(listHex);
|
||||||
|
Assert.fail("Should throw NumberFormatException");
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
Assert.assertTrue(e.getMessage().contains("Failed radix: [10] for value: \"1F\"!"));
|
||||||
|
}
|
||||||
|
|
||||||
|
List<String> listIntString = new ArrayList<>();
|
||||||
|
listIntString.add("-129");
|
||||||
|
try {
|
||||||
|
TbUtils.bytesToString(listIntString);
|
||||||
|
Assert.fail("Should throw NumberFormatException");
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
Assert.assertTrue(e.getMessage().contains("The value '-129' could not be correctly converted to a byte. " +
|
||||||
|
"Integer to byte conversion requires the use of only 8 bits (with a range of min/max = -128/255)!"));
|
||||||
|
}
|
||||||
|
|
||||||
|
listIntString.add(0, "256");
|
||||||
|
try {
|
||||||
|
TbUtils.bytesToString(listIntString);
|
||||||
|
Assert.fail("Should throw NumberFormatException");
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
Assert.assertTrue(e.getMessage().contains("The value '256' could not be correctly converted to a byte. " +
|
||||||
|
"Integer to byte conversion requires the use of only 8 bits (with a range of min/max = -128/255)!"));
|
||||||
|
}
|
||||||
|
|
||||||
|
ArrayList<Integer> listIntBytes = new ArrayList<>();
|
||||||
|
listIntBytes.add(-129);
|
||||||
|
try {
|
||||||
|
TbUtils.bytesToString(listIntBytes);
|
||||||
|
Assert.fail("Should throw NumberFormatException");
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
Assert.assertTrue(e.getMessage().contains("The value '-129' could not be correctly converted to a byte. " +
|
||||||
|
"Integer to byte conversion requires the use of only 8 bits (with a range of min/max = -128/255)!"));
|
||||||
|
}
|
||||||
|
|
||||||
|
listIntBytes.add(0, 256);
|
||||||
|
try {
|
||||||
|
TbUtils.bytesToString(listIntBytes);
|
||||||
|
Assert.fail("Should throw NumberFormatException");
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
Assert.assertTrue(e.getMessage().contains("The value '256' could not be correctly converted to a byte. " +
|
||||||
|
"Integer to byte conversion requires the use of only 8 bits (with a range of min/max = -128/255)!"));
|
||||||
|
}
|
||||||
|
|
||||||
|
ArrayList<Object> listObjects = new ArrayList<>();
|
||||||
|
ArrayList<String> listStringObjects = new ArrayList<>();
|
||||||
|
listStringObjects.add("0xFD");
|
||||||
|
listObjects.add(listStringObjects);
|
||||||
|
try {
|
||||||
|
TbUtils.bytesToString(listObjects);
|
||||||
|
Assert.fail("Should throw NumberFormatException");
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
Assert.assertTrue(e.getMessage().contains("The value '[0xFD]' could not be correctly converted to a byte. " +
|
||||||
|
"Must be a HexDecimal/String/Integer/Byte format !"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private static List<Byte> toList(byte[] data) {
|
private static List<Byte> toList(byte[] data) {
|
||||||
List<Byte> result = new ArrayList<>(data.length);
|
List<Byte> result = new ArrayList<>(data.length);
|
||||||
for (Byte b : data) {
|
for (Byte b : data) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user