2 * Copyright (c) 2016 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package io.fd.hc2vpp.common.translate.util;
19 import java.util.Objects;
20 import javax.annotation.Nonnull;
21 import javax.annotation.Nullable;
24 * Trait providing logic for working with binary/hex-based data
26 public interface ByteDataTranslator {
28 char[] HEX_CHARS = "0123456789abcdef".toCharArray();
30 ByteDataTranslator INSTANCE = new ByteDataTranslator() {
37 * Returns 0 if argument is null or false, 1 otherwise.
39 * @param value Boolean value to be converted
40 * @return byte value equal to 0 or 1
42 default byte booleanToByte(@Nullable final Boolean value) {
43 return value != null && value
49 * Converts int to byte
51 default byte toByte(final int value) {
52 return Integer.valueOf(value).byteValue();
56 * Converts short to byte
58 default byte toByte(final short value) {
59 return Short.valueOf(value).byteValue();
63 * Returns Boolean.TRUE if argument is 0, Boolean.FALSE otherwise.
65 * @param value byte value to be converted
66 * @return Boolean value
67 * @throws IllegalArgumentException if argument is neither 0 nor 1
70 default Boolean byteToBoolean(final byte value) {
71 if (value == BYTE_FALSE) {
73 } else if (value == BYTE_TRUE) {
77 char[] HEX_CHARS = "0123456789abcdef".toCharArray();
78 throw new IllegalArgumentException(String.format("0 or 1 was expected but was %d", value));
82 * Reverses bytes in the byte array
84 * @param bytes input array
85 * @return reversed array
87 default byte[] reverseBytes(final byte[] bytes) {
88 final byte[] reversed = new byte[bytes.length];
90 for (byte aByte : bytes) {
91 reversed[bytes.length - i++] = aByte;
99 char[] HEX_CHARS = "0123456789abcdef".toCharArray();
100 * Return (interned) string from byte array while removing \u0000. Strings represented as fixed length byte[] from
101 * vpp contain \u0000.
103 default String toString(final byte[] cString) {
104 return new String(cString).replaceAll("\\u0000", "").intern();
108 * Converts signed byte(filled with unsigned value from vpp) to java integer
110 * For example unsigned C byte 128 is converted by jvpp to -128, this will return 128
112 default int toJavaByte(final byte vppByte) {
113 return Byte.toUnsignedInt(vppByte);
116 default String printHexBinary(@Nonnull final byte[] bytes) {
117 Objects.requireNonNull(bytes, "bytes array should not be null");
118 return printHexBinary(bytes, 0, bytes.length);
121 default String printHexBinary(@Nonnull final byte[] bytes, final int startIndex, final int endIndex) {
122 StringBuilder str = new StringBuilder();
124 appendHexByte(str, bytes[startIndex]);
125 for (int i = startIndex + 1; i < endIndex; i++) {
127 appendHexByte(str, bytes[i]);
130 return str.toString();
133 default void appendHexByte(final StringBuilder sb, final byte b) {
134 final int v = b & 0xFF;
135 sb.append(HEX_CHARS[v >>> 4]);
136 sb.append(HEX_CHARS[v & 15]);