Update license and poms
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / util / RawByteHelper.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.sparky.util;
22
23 /**
24  * The Class RawByteHelper.
25  */
26 public class RawByteHelper {
27   private static final byte[] HEX_CHAR =
28       new byte[] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
29
30   /**
31    * Dump bytes.
32    *
33    * @param buffer the buffer
34    * @return the string
35    */
36   /*
37    * TODO -> DOCUMENT ME!
38    * 
39    * @param buffer DOCUMENT ME!
40    *
41    * @return DOCUMENT ME!
42    */
43   public static String dumpBytes(byte[] buffer) {
44     if (buffer == null) {
45       return "";
46     }
47     String newLine = System.getProperty("line.separator");
48     StringBuffer sb = new StringBuffer();
49
50     for (int i = 0; i < buffer.length; i++) {
51       if (i != 0 && i % 16 == 0) {
52         sb.append(newLine);
53       }
54       // sb.append("0x").append((char) (HEX_CHAR[(buffer[i] & 0x00F0) >> 4])).append((char)
55       // (HEX_CHAR[buffer[i] & 0x000F])).append(" ");
56       sb.append((char) (HEX_CHAR[(buffer[i] & 0x00F0) >> 4]))
57           .append((char) (HEX_CHAR[buffer[i] & 0x000F])).append(" ");
58     }
59
60     return sb.toString();
61   }
62
63   // if you're trying to figure out why or's w/ FF's see:
64   /**
65    * Bytes to int.
66    *
67    * @param one the one
68    * @param two the two
69    * @param three the three
70    * @param four the four
71    * @return the int
72    */
73   // http://www.darksleep.com/player/JavaAndUnsignedTypes.html
74   public static int bytesToInt(byte one, byte two, byte three, byte four) {
75     return (((0xFF & one) << 24) | ((0xFF & two) << 16) | ((0xFF & three) << 8) | ((0xFF & four)));
76   }
77
78   /**
79    * Bytes to short.
80    *
81    * @param one the one
82    * @param two the two
83    * @return the short
84    */
85   public static short bytesToShort(byte one, byte two) {
86     return (short) (((0xFF & one) << 8) | (0xFF & two));
87   }
88
89   /**
90    * First byte.
91    *
92    * @param num the num
93    * @return the byte
94    */
95   // short helper functions
96   static byte firstByte(short num) {
97     return (byte) ((num >> 8) & 0xFF);
98   }
99
100   /**
101    * First byte.
102    *
103    * @param num the num
104    * @return the byte
105    */
106   // Int helper functions
107   static byte firstByte(int num) {
108     return (byte) ((num >> 24) & 0xFF);
109   }
110
111   /**
112    * Second byte.
113    *
114    * @param num the num
115    * @return the byte
116    */
117   static byte secondByte(short num) {
118     return (byte) (num & 0xFF);
119   }
120
121   /**
122    * Second byte.
123    *
124    * @param num the num
125    * @return the byte
126    */
127   static byte secondByte(int num) {
128     return (byte) ((num >> 16) & 0xFF);
129   }
130
131   /**
132    * Third byte.
133    *
134    * @param num the num
135    * @return the byte
136    */
137   static byte thirdByte(int num) {
138     return (byte) ((num >> 8) & 0xFF);
139   }
140
141   /**
142    * Fourth byte.
143    *
144    * @param num the num
145    * @return the byte
146    */
147   static byte fourthByte(int num) {
148     return (byte) (num & 0xFF);
149   }
150
151   /**
152    * Int to byte.
153    *
154    * @param value the value
155    * @return the byte
156    */
157   public static byte intToByte(int value) {
158     return fourthByte(value);
159   }
160
161   /**
162    * Int to short.
163    *
164    * @param value the value
165    * @return the short
166    */
167   public static short intToShort(int value) {
168     return (short) ((value & 0xFF00) | (value & 0xFF));
169   }
170
171 }
172