diff --git a/src/main/java/com/beanit/openiec61850/BdaBitString.java b/src/main/java/com/beanit/openiec61850/BdaBitString.java index 81c07a2..c6b7a0e 100644 --- a/src/main/java/com/beanit/openiec61850/BdaBitString.java +++ b/src/main/java/com/beanit/openiec61850/BdaBitString.java @@ -14,6 +14,7 @@ package com.beanit.openiec61850; import com.beanit.jasn1.ber.types.BerBitString; +import com.beanit.openiec61850.internal.HexString; import com.beanit.openiec61850.internal.mms.asn1.Data; import com.beanit.openiec61850.internal.mms.asn1.Integer32; import com.beanit.openiec61850.internal.mms.asn1.TypeDescription; @@ -97,11 +98,11 @@ public abstract class BdaBitString extends BasicDataAttribute { @Override public String toString() { - return getReference().toString() + ": " + HexConverter.toHexString(value); + return getReference().toString() + ": " + HexString.fromBytes(value); } @Override public String getValueString() { - return HexConverter.toHexString(value); + return HexString.fromBytes(value); } } diff --git a/src/main/java/com/beanit/openiec61850/HexConverter.java b/src/main/java/com/beanit/openiec61850/HexConverter.java deleted file mode 100644 index de8d1a2..0000000 --- a/src/main/java/com/beanit/openiec61850/HexConverter.java +++ /dev/null @@ -1,174 +0,0 @@ -/* - * Copyright 2018 beanit - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License - * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing permissions and limitations under - * the License. - */ -package com.beanit.openiec61850; - -public class HexConverter { - - private static final char[] hexArray = "0123456789ABCDEF".toCharArray(); - - /** Don't let anyone instantiate this class. */ - private HexConverter() {} - - public static String toHexString(byte b) { - StringBuilder builder = new StringBuilder(); - appendHexString(b, builder); - return builder.toString(); - } - - public static String toHexString(byte[] bytes) { - return toHexString(bytes, 0, bytes.length); - } - - public static String toHexString(byte[] bytes, int offset, int length) { - StringBuilder builder = new StringBuilder(); - - int l = 1; - for (int i = offset; i < (offset + length); i++) { - if ((l != 1) && ((l - 1) % 8 == 0)) { - builder.append(' '); - } - if ((l != 1) && ((l - 1) % 16 == 0)) { - builder.append('\n'); - } - l++; - appendHexString(bytes[i], builder); - if (i != offset + length - 1) { - builder.append(' '); - } - } - - return builder.toString(); - } - - /** - * Returns the integer value as hex string filled with leading zeros. If you do not want leading - * zeros use Integer.toHexString(int i) instead. - * - * @param i the integer value to be converted - * @return the hex string - */ - public static String toShortHexString(int i) { - byte[] bytes = new byte[] {(byte) (i >> 24), (byte) (i >> 16), (byte) (i >> 8), (byte) (i)}; - return toShortHexString(bytes); - } - - /** - * Returns the long value as hex string filled with leading zeros. If you do not want leading - * zeros use Long.toHexString(long i) instead. - * - * @param l the long value to be converted - * @return the hex string - */ - public static String toShortHexString(long l) { - byte[] bytes = - new byte[] { - (byte) (l >> 56), - (byte) (l >> 48), - (byte) (l >> 40), - (byte) (l >> 32), - (byte) (l >> 24), - (byte) (l >> 16), - (byte) (l >> 8), - (byte) (l) - }; - return toShortHexString(bytes); - } - - /** - * Returns the byte as a hex string. If b is less than 16 the hex string returned contains a - * leading zero. - * - * @param b the byte to be converted - * @return the byte as a hex string. - */ - public static String toShortHexString(byte b) { - return toShortHexString(new byte[] {b}); - } - - public static String toShortHexString(byte[] bytes) { - return toShortHexString(bytes, 0, bytes.length); - } - - public static String toShortHexString(byte[] bytes, int offset, int length) { - char[] hexChars = new char[length * 2]; - for (int j = 0; j < length; j++) { - int v = bytes[j + offset] & 0xff; - hexChars[j * 2] = hexArray[v >>> 4]; - hexChars[j * 2 + 1] = hexArray[v & 0x0f]; - } - return new String(hexChars); - } - - public static byte[] fromShortHexString(String shortHexString) throws NumberFormatException { - - validate(shortHexString); - - int length = shortHexString.length(); - - byte[] data = new byte[length / 2]; - for (int i = 0; i < length; i += 2) { - int firstCharacter = Character.digit(shortHexString.charAt(i), 16); - int secondCharacter = Character.digit(shortHexString.charAt(i + 1), 16); - - if (firstCharacter == -1 || secondCharacter == -1) { - throw new NumberFormatException("string is not a legal hex string."); - } - - data[i / 2] = (byte) ((firstCharacter << 4) + secondCharacter); - } - return data; - } - - public static void appendShortHexString(byte b, StringBuilder builder) { - builder.append(toShortHexString(b)); - } - - public static void appendShortHexString( - StringBuilder builder, byte[] bytes, int offset, int length) { - builder.append(toShortHexString(bytes, offset, length)); - } - - public static void appendHexString(byte b, StringBuilder builder) { - builder.append("0x"); - appendShortHexString(b, builder); - } - - public static void appendHexString( - StringBuilder builder, byte[] byteArray, int offset, int length) { - int l = 1; - for (int i = offset; i < (offset + length); i++) { - if ((l != 1) && ((l - 1) % 8 == 0)) { - builder.append(' '); - } - if ((l != 1) && ((l - 1) % 16 == 0)) { - builder.append('\n'); - } - l++; - appendHexString(byteArray[i], builder); - if (i != offset + length - 1) { - builder.append(' '); - } - } - } - - private static void validate(String s) { - if (s == null) { - throw new IllegalArgumentException("string s may not be null"); - } - - if ((s.length() == 0) || ((s.length() % 2) != 0)) { - throw new NumberFormatException("string is not a legal hex string."); - } - } -} diff --git a/src/main/java/com/beanit/openiec61850/Report.java b/src/main/java/com/beanit/openiec61850/Report.java index fb6f1cc..aaa33e3 100644 --- a/src/main/java/com/beanit/openiec61850/Report.java +++ b/src/main/java/com/beanit/openiec61850/Report.java @@ -13,6 +13,7 @@ */ package com.beanit.openiec61850; +import com.beanit.openiec61850.internal.HexString; import java.util.List; public class Report { @@ -168,7 +169,7 @@ public class Report { sb.append("\nBuffer overflow: ").append(bufOvfl); } if (entryId != null) { - sb.append("\nEntry ID: ").append(HexConverter.toHexString(entryId.getValue())); + sb.append("\nEntry ID: ").append(HexString.fromBytes(entryId.getValue())); } if (confRev != null) { sb.append("\nConfiguration revision: ").append(confRev.toString()); diff --git a/src/main/java/com/beanit/openiec61850/internal/HexString.java b/src/main/java/com/beanit/openiec61850/internal/HexString.java new file mode 100644 index 0000000..cb2362d --- /dev/null +++ b/src/main/java/com/beanit/openiec61850/internal/HexString.java @@ -0,0 +1,152 @@ +/* + * Copyright 2019 beanit + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package com.beanit.openiec61850.internal; + +import java.nio.ByteBuffer; +import java.util.Objects; + +public class HexString { + + private static final char[] hexArray = "0123456789ABCDEF".toCharArray(); + + /** Don't let anyone instantiate this class. */ + private HexString() {} + + /** + * Returns the byte as a hex string. If b is less than 16 the hex string returned contains a + * leading zero. + * + * @param b the byte to be converted + * @return the hex string. + */ + public static String fromByte(byte b) { + return fromBytes(new byte[] {b}); + } + + public static String fromByte(int b) { + return fromBytes(new byte[] {(byte) b}); + } + + /** + * Returns the integer value as hex string filled with leading zeros. + * + * @param i the integer value to be converted + * @return the hex string + */ + public static String fromInt(int i) { + byte[] bytes = new byte[] {(byte) (i >> 24), (byte) (i >> 16), (byte) (i >> 8), (byte) (i)}; + return fromBytes(bytes); + } + + /** + * Returns the long value as hex string filled with leading zeros. + * + * @param l the long value to be converted + * @return the hex string + */ + public static String fromLong(long l) { + byte[] bytes = + new byte[] { + (byte) (l >> 56), + (byte) (l >> 48), + (byte) (l >> 40), + (byte) (l >> 32), + (byte) (l >> 24), + (byte) (l >> 16), + (byte) (l >> 8), + (byte) (l) + }; + return fromBytes(bytes); + } + + public static String fromBytes(byte[] bytes) { + return fromBytes(bytes, 0, bytes.length); + } + + public static String fromBytesFormatted(byte[] bytes) { + return fromBytesFormatted(bytes, 0, bytes.length); + } + + public static String fromBytes(byte[] bytes, int offset, int length) { + char[] hexChars = new char[length * 2]; + for (int j = 0; j < length; j++) { + int v = bytes[j + offset] & 0xff; + hexChars[j * 2] = hexArray[v >>> 4]; + hexChars[j * 2 + 1] = hexArray[v & 0x0f]; + } + return new String(hexChars); + } + + public static String fromBytes(ByteBuffer buffer) { + return fromBytes(buffer.array(), buffer.arrayOffset(), buffer.arrayOffset() + buffer.limit()); + } + + public static String fromBytesFormatted(byte[] bytes, int offset, int length) { + StringBuilder builder = new StringBuilder(); + + int l = 1; + for (int i = offset; i < (offset + length); i++) { + if ((l != 1) && ((l - 1) % 8 == 0)) { + builder.append(' '); + } + if ((l != 1) && ((l - 1) % 16 == 0)) { + builder.append('\n'); + } + l++; + appendFromByte(bytes[i], builder); + if (i != offset + length - 1) { + builder.append(' '); + } + } + return builder.toString(); + } + + /** + * Converts the given hex string to a byte array. + * + * @param hexString the hex string + * @return the bytes + * @throws NumberFormatException if the string is not a valid hex string + */ + public static byte[] toBytes(String hexString) { + + Objects.requireNonNull(hexString); + if ((hexString.length() == 0) || ((hexString.length() % 2) != 0)) { + throw new NumberFormatException("argument is not a valid hex string"); + } + + int length = hexString.length(); + + byte[] data = new byte[length / 2]; + for (int i = 0; i < length; i += 2) { + int firstCharacter = Character.digit(hexString.charAt(i), 16); + int secondCharacter = Character.digit(hexString.charAt(i + 1), 16); + + if (firstCharacter == -1 || secondCharacter == -1) { + throw new NumberFormatException("argument is not a valid hex string"); + } + + data[i / 2] = (byte) ((firstCharacter << 4) + secondCharacter); + } + return data; + } + + public static void appendFromByte(byte b, StringBuilder builder) { + builder.append(fromByte(b)); + } + + public static void appendFromBytes(StringBuilder builder, byte[] bytes, int offset, int length) { + builder.append(fromBytes(bytes, offset, length)); + } +}