Browse Source

stringToBytes: refactoring, Input parameter Object as HexString, DigString, Integer, Byte

pull/9696/head
nick 3 years ago
parent
commit
248bcefa80
  1. 18
      common/script/script-api/src/main/java/org/thingsboard/script/api/tbel/TbUtils.java
  2. 57
      common/script/script-api/src/test/java/org/thingsboard/script/api/tbel/TbUtilsTest.java

18
common/script/script-api/src/main/java/org/thingsboard/script/api/tbel/TbUtils.java

@ -182,7 +182,7 @@ public class TbUtils {
return TbJson.parse(ctx, jsonStr);
}
public static String bytesToString(List<Byte> bytesList) {
public static String bytesToString(List<?> bytesList) {
byte[] bytes = bytesFromList(bytesList);
return new String(bytes);
}
@ -210,10 +210,22 @@ public class TbUtils {
}
}
private static byte[] bytesFromList(List<Byte> bytesList) {
private static byte[] bytesFromList(List<?> bytesList) {
byte[] bytes = new byte[bytesList.size()];
for (int i = 0; i < bytesList.size(); i++) {
bytes[i] = bytesList.get(i);
if (bytesList.get(i) instanceof Integer) {
byte val = ((Integer) bytesList.get(i)).byteValue();
if (((Integer) bytesList.get(i)).intValue() > 255 || ((Integer) bytesList.get(i)).intValue() < -128){
throw new NumberFormatException("The value " + bytesList.get(i) + " could not be converted to a byte. Integer to byte needs only 8-bits (min/max = -128/127 or 0/255) !");
} else {
bytes[i] = val; }
} else if (bytesList.get(i) instanceof String) {
bytes[i] = parseInt((String) bytesList.get(i)).byteValue();
} else if (bytesList.get(i) instanceof Byte) {
bytes[i] = (byte) bytesList.get(i);
} else {
throw new NumberFormatException("Value \"" + bytesList.get(i) + " could not be converted to a byte. Must be format HexDecimal/String/Integer !");
}
}
return bytes;
}

57
common/script/script-api/src/test/java/org/thingsboard/script/api/tbel/TbUtilsTest.java

@ -31,6 +31,7 @@ import java.io.IOException;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.Random;
@ -385,6 +386,62 @@ public class TbUtilsTest {
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, 252, 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\"!"));
}
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 converted to a byte. Integer to byte needs only 8-bits (min/max = -128/127 or 0/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 converted to a byte. Integer to byte needs only 8-bits (min/max = -128/127 or 0/255"));
}
}
private static List<Byte> toList(byte[] data) {
List<Byte> result = new ArrayList<>(data.length);
for (Byte b : data) {

Loading…
Cancel
Save