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 ByteDataTranslator INSTANCE = new ByteDataTranslator() {
35 * Returns 0 if argument is null or false, 1 otherwise.
37 * @param value Boolean value to be converted
38 * @return byte value equal to 0 or 1
40 default byte booleanToByte(@Nullable final Boolean value) {
41 return value != null && value
47 * Converts int to byte.
49 default byte toByte(final int value) {
50 return Integer.valueOf(value).byteValue();
54 * Converts short to byte.
56 default byte toByte(final short value) {
57 return Short.valueOf(value).byteValue();
61 * Returns Boolean.TRUE if argument is 0, Boolean.FALSE otherwise.
63 * @param value byte value to be converted
64 * @return Boolean value
65 * @throws IllegalArgumentException if argument is neither 0 nor 1
68 default Boolean byteToBoolean(final byte value) {
69 if (value == BYTE_FALSE) {
71 } else if (value == BYTE_TRUE) {
74 throw new IllegalArgumentException(String.format("0 or 1 was expected but was %d", value));
78 * Return (interned) string from byte array while removing \u0000. Strings represented as fixed length byte[] from
81 default String toString(final byte[] vppString) {
82 return new String(vppString).replaceAll("\\u0000", "").intern();
86 * Converts signed byte(filled with unsigned value from vpp) to java integer.
87 * For example unsigned C byte 128 is converted by jvpp to -128, this will return 128.
89 default int toJavaByte(final byte vppByte) {
90 return Byte.toUnsignedInt(vppByte);
93 default String printHexBinary(@Nonnull final byte[] bytes) {
94 Objects.requireNonNull(bytes, "bytes array should not be null");
95 return printHexBinary(bytes, 0, bytes.length);
98 default String printHexBinary(@Nonnull final byte[] bytes, final int startIndex, final int endIndex) {
99 StringBuilder str = new StringBuilder();
101 Impl.appendHexByte(str, bytes[startIndex]);
102 for (int i = startIndex + 1; i < endIndex; i++) {
104 Impl.appendHexByte(str, bytes[i]);
107 return str.toString();
112 private static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();
114 private static void appendHexByte(final StringBuilder sb, final byte b) {
115 final int v = b & 0xFF;
116 sb.append(HEX_CHARS[v >>> 4]);
117 sb.append(HEX_CHARS[v & 15]);