25b8d50f0a3b55a3c11b124a0c12f10fd0cdb969
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package io.fd.hc2vpp.common.translate.util;
18
19 import java.util.Arrays;
20 import java.util.Objects;
21 import javax.annotation.Nonnull;
22 import javax.annotation.Nullable;
23
24 /**
25  * Trait providing logic for working with binary/hex-based data.
26  */
27 public interface ByteDataTranslator {
28
29     ByteDataTranslator INSTANCE = new ByteDataTranslator() {
30     };
31
32     byte BYTE_FALSE = 0;
33     byte BYTE_TRUE = 1;
34
35     /**
36      * Returns 0 if argument is null or false, 1 otherwise.
37      *
38      * @param value Boolean value to be converted
39      * @return byte value equal to 0 or 1
40      */
41     default byte booleanToByte(@Nullable final Boolean value) {
42         return value != null && value
43                 ? BYTE_TRUE
44                 : BYTE_FALSE;
45     }
46
47     /**
48      * Converts int to byte.
49      */
50     default byte toByte(final int value) {
51         return Integer.valueOf(value).byteValue();
52     }
53
54     /**
55      * Converts short to byte.
56      */
57     default byte toByte(final short value) {
58         return Short.valueOf(value).byteValue();
59     }
60
61     /**
62      * Checks if provided array contains only zeros
63      */
64     default boolean isArrayZeroed(final byte[] arr) {
65         return Arrays.equals(arr, new byte[arr.length]);
66     }
67
68     /**
69      * Returns Boolean.TRUE if argument is 0, Boolean.FALSE otherwise.
70      *
71      * @param value byte value to be converted
72      * @return Boolean value
73      * @throws IllegalArgumentException if argument is neither 0 nor 1
74      */
75     @Nonnull
76     default Boolean byteToBoolean(final byte value) {
77         if (value == BYTE_FALSE) {
78             return Boolean.FALSE;
79         } else if (value == BYTE_TRUE) {
80             return Boolean.TRUE;
81         }
82         throw new IllegalArgumentException(String.format("0 or 1 was expected but was %d", value));
83     }
84
85     /**
86      * Return (interned) string from byte array while removing \u0000. Strings represented as fixed length byte[] from
87      * vpp contain \u0000.
88      */
89     default String toString(final byte[] vppString) {
90         return new String(vppString).replaceAll("\\u0000", "").intern();
91     }
92
93     /**
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.
96      */
97     default int toJavaByte(final byte vppByte) {
98         return Byte.toUnsignedInt(vppByte);
99     }
100
101     default String printHexBinary(@Nonnull final byte[] bytes) {
102         Objects.requireNonNull(bytes, "bytes array should not be null");
103         StringBuilder str = new StringBuilder();
104
105         Impl.appendHexByte(str, bytes[0]);
106         for (int i = 1; i < bytes.length; i++) {
107             str.append(":");
108             Impl.appendHexByte(str, bytes[i]);
109         }
110
111         return str.toString();
112     }
113
114     final class Impl {
115
116         private static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();
117
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]);
122         }
123     }
124 }