3424af534aec02ef2f4bec8a2eb94abf54ebb815
[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.Objects;
20 import javax.annotation.Nonnull;
21 import javax.annotation.Nullable;
22
23 /**
24  * Trait providing logic for working with binary/hex-based data.
25  */
26 public interface ByteDataTranslator {
27
28     ByteDataTranslator INSTANCE = new ByteDataTranslator() {
29     };
30
31     byte BYTE_FALSE = 0;
32     byte BYTE_TRUE = 1;
33
34     /**
35      * Returns 0 if argument is null or false, 1 otherwise.
36      *
37      * @param value Boolean value to be converted
38      * @return byte value equal to 0 or 1
39      */
40     default byte booleanToByte(@Nullable final Boolean value) {
41         return value != null && value
42                 ? BYTE_TRUE
43                 : BYTE_FALSE;
44     }
45
46     /**
47      * Converts int to byte.
48      */
49     default byte toByte(final int value) {
50         return Integer.valueOf(value).byteValue();
51     }
52
53     /**
54      * Converts short to byte.
55      */
56     default byte toByte(final short value) {
57         return Short.valueOf(value).byteValue();
58     }
59
60     /**
61      * Returns Boolean.TRUE if argument is 0, Boolean.FALSE otherwise.
62      *
63      * @param value byte value to be converted
64      * @return Boolean value
65      * @throws IllegalArgumentException if argument is neither 0 nor 1
66      */
67     @Nonnull
68     default Boolean byteToBoolean(final byte value) {
69         if (value == BYTE_FALSE) {
70             return Boolean.FALSE;
71         } else if (value == BYTE_TRUE) {
72             return Boolean.TRUE;
73         }
74         throw new IllegalArgumentException(String.format("0 or 1 was expected but was %d", value));
75     }
76
77     /**
78      * Return (interned) string from byte array while removing \u0000. Strings represented as fixed length byte[] from
79      * vpp contain \u0000.
80      */
81     default String toString(final byte[] vppString) {
82         return new String(vppString).replaceAll("\\u0000", "").intern();
83     }
84
85     /**
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.
88      */
89     default int toJavaByte(final byte vppByte) {
90         return Byte.toUnsignedInt(vppByte);
91     }
92
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);
96     }
97
98     default String printHexBinary(@Nonnull final byte[] bytes, final int startIndex, final int endIndex) {
99         StringBuilder str = new StringBuilder();
100
101         Impl.appendHexByte(str, bytes[startIndex]);
102         for (int i = startIndex + 1; i < endIndex; i++) {
103             str.append(":");
104             Impl.appendHexByte(str, bytes[i]);
105         }
106
107         return str.toString();
108     }
109
110     final class Impl {
111
112         private static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();
113
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]);
118         }
119     }
120 }