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.Arrays;
20 import java.util.Objects;
21 import javax.annotation.Nonnull;
22 import javax.annotation.Nullable;
25 * Trait providing logic for working with binary/hex-based data.
27 public interface ByteDataTranslator {
29 ByteDataTranslator INSTANCE = new ByteDataTranslator() {
36 * Returns 0 if argument is null or false, 1 otherwise.
38 * @param value Boolean value to be converted
39 * @return byte value equal to 0 or 1
41 default byte booleanToByte(@Nullable final Boolean value) {
42 return value != null && value
48 * Converts int to byte.
50 default byte toByte(final int value) {
51 return Integer.valueOf(value).byteValue();
55 * Converts short to byte.
57 default byte toByte(final short value) {
58 return Short.valueOf(value).byteValue();
62 * Checks if provided array contains only zeros
64 default boolean isArrayZeroed(final byte[] arr) {
65 return Arrays.equals(arr, new byte[arr.length]);
69 * Returns Boolean.TRUE if argument is 0, Boolean.FALSE otherwise.
71 * @param value byte value to be converted
72 * @return Boolean value
73 * @throws IllegalArgumentException if argument is neither 0 nor 1
76 default Boolean byteToBoolean(final byte value) {
77 if (value == BYTE_FALSE) {
79 } else if (value == BYTE_TRUE) {
82 throw new IllegalArgumentException(String.format("0 or 1 was expected but was %d", value));
86 * Return (interned) string from byte array while removing \u0000. Strings represented as fixed length byte[] from
89 default String toString(final byte[] vppString) {
90 return new String(vppString).replaceAll("\\u0000", "").intern();
94 * Converts signed byte(filled with unsigned value from vpp) to java integer.
95 * For example unsigned C byte 128 is converted by jvpp to -128, this will return 128.
97 default int toJavaByte(final byte vppByte) {
98 return Byte.toUnsignedInt(vppByte);
101 default String printHexBinary(@Nonnull final byte[] bytes) {
102 Objects.requireNonNull(bytes, "bytes array should not be null");
103 StringBuilder str = new StringBuilder();
105 Impl.appendHexByte(str, bytes[0]);
106 for (int i = 1; i < bytes.length; i++) {
108 Impl.appendHexByte(str, bytes[i]);
111 return str.toString();
116 private static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();
118 private static void appendHexByte(final StringBuilder sb, final byte b) {
119 final int v = b & 0xFF;
120 sb.append(HEX_CHARS[v >>> 4]);
121 sb.append(HEX_CHARS[v & 15]);