d24157bf6ffe6c3433d838429eddb69db8894124
[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     char[] HEX_CHARS = "0123456789abcdef".toCharArray();
29
30     ByteDataTranslator INSTANCE = new ByteDataTranslator() {
31     };
32
33     byte BYTE_FALSE = 0;
34     byte BYTE_TRUE = 1;
35
36     /**
37      * Returns 0 if argument is null or false, 1 otherwise.
38      *
39      * @param value Boolean value to be converted
40      * @return byte value equal to 0 or 1
41      */
42     default byte booleanToByte(@Nullable final Boolean value) {
43         return value != null && value
44                 ? BYTE_TRUE
45                 : BYTE_FALSE;
46     }
47
48     /**
49      * Converts int to byte
50      */
51     default byte toByte(final int value) {
52         return Integer.valueOf(value).byteValue();
53     }
54
55     /**
56      * Converts short to byte
57      */
58     default byte toByte(final short value) {
59         return Short.valueOf(value).byteValue();
60     }
61
62     /**
63      * Returns Boolean.TRUE if argument is 0, Boolean.FALSE otherwise.
64      *
65      * @param value byte value to be converted
66      * @return Boolean value
67      * @throws IllegalArgumentException if argument is neither 0 nor 1
68      */
69     @Nonnull
70     default Boolean byteToBoolean(final byte value) {
71         if (value == BYTE_FALSE) {
72             return Boolean.FALSE;
73         } else if (value == BYTE_TRUE) {
74             return Boolean.TRUE;
75         }
76
77     char[] HEX_CHARS = "0123456789abcdef".toCharArray();
78         throw new IllegalArgumentException(String.format("0 or 1 was expected but was %d", value));
79     }
80
81     /**
82      * Reverses bytes in the byte array
83      *
84      * @param bytes input array
85      * @return reversed array
86      */
87     default byte[] reverseBytes(final byte[] bytes) {
88         final byte[] reversed = new byte[bytes.length];
89         int i = 1;
90         for (byte aByte : bytes) {
91             reversed[bytes.length - i++] = aByte;
92         }
93
94         return reversed;
95     }
96
97     /**
98
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.
102      */
103     default String toString(final byte[] cString) {
104         return new String(cString).replaceAll("\\u0000", "").intern();
105     }
106
107     /**
108      * Converts signed byte(filled with unsigned value from vpp) to java integer
109      *
110      * For example unsigned C byte 128 is converted by jvpp to -128, this will return 128
111      */
112     default int toJavaByte(final byte vppByte) {
113         return Byte.toUnsignedInt(vppByte);
114     }
115
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);
119     }
120
121     default String printHexBinary(@Nonnull final byte[] bytes, final int startIndex, final int endIndex) {
122         StringBuilder str = new StringBuilder();
123
124         appendHexByte(str, bytes[startIndex]);
125         for (int i = startIndex + 1; i < endIndex; i++) {
126             str.append(":");
127             appendHexByte(str, bytes[i]);
128         }
129
130         return str.toString();
131     }
132
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]);
137     }
138 }