Merge "Fix Blocker/Critical sonar issues"
[aai/sparky-be.git] / src / main / java / org / openecomp / sparky / util / RawByteHelper.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.openecomp.sparky.util;
24
25 /**
26  * The Class RawByteHelper.
27  */
28 public class RawByteHelper {
29   private static final byte[] HEX_CHAR =
30       new byte[] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
31
32   /**
33    * Dump bytes.
34    *
35    * @param buffer the buffer
36    * @return the string
37    */
38   /*
39    * TODO -> DOCUMENT ME!
40    * 
41    * @param buffer DOCUMENT ME!
42    *
43    * @return DOCUMENT ME!
44    */
45   public static String dumpBytes(byte[] buffer) {
46     if (buffer == null) {
47       return "";
48     }
49     String newLine = System.getProperty("line.separator");
50     StringBuffer sb = new StringBuffer();
51
52     for (int i = 0; i < buffer.length; i++) {
53       if (i != 0 && i % 16 == 0) {
54         sb.append(newLine);
55       }
56       // sb.append("0x").append((char) (HEX_CHAR[(buffer[i] & 0x00F0) >> 4])).append((char)
57       // (HEX_CHAR[buffer[i] & 0x000F])).append(" ");
58       sb.append((char) (HEX_CHAR[(buffer[i] & 0x00F0) >> 4]))
59           .append((char) (HEX_CHAR[buffer[i] & 0x000F])).append(" ");
60     }
61
62     return sb.toString();
63   }
64
65   // if you're trying to figure out why or's w/ FF's see:
66   /**
67    * Bytes to int.
68    *
69    * @param one the one
70    * @param two the two
71    * @param three the three
72    * @param four the four
73    * @return the int
74    */
75   // http://www.darksleep.com/player/JavaAndUnsignedTypes.html
76   public static int bytesToInt(byte one, byte two, byte three, byte four) {
77     return (((0xFF & one) << 24) | ((0xFF & two) << 16) | ((0xFF & three) << 8) | ((0xFF & four)));
78   }
79
80   /**
81    * Bytes to short.
82    *
83    * @param one the one
84    * @param two the two
85    * @return the short
86    */
87   public static short bytesToShort(byte one, byte two) {
88     return (short) (((0xFF & one) << 8) | (0xFF & two));
89   }
90
91   /**
92    * First byte.
93    *
94    * @param num the num
95    * @return the byte
96    */
97   // short helper functions
98   static byte firstByte(short num) {
99     return (byte) ((num >> 8) & 0xFF);
100   }
101
102   /**
103    * First byte.
104    *
105    * @param num the num
106    * @return the byte
107    */
108   // Int helper functions
109   static byte firstByte(int num) {
110     return (byte) ((num >> 24) & 0xFF);
111   }
112
113   /**
114    * Second byte.
115    *
116    * @param num the num
117    * @return the byte
118    */
119   static byte secondByte(short num) {
120     return (byte) (num & 0xFF);
121   }
122
123   /**
124    * Second byte.
125    *
126    * @param num the num
127    * @return the byte
128    */
129   static byte secondByte(int num) {
130     return (byte) ((num >> 16) & 0xFF);
131   }
132
133   /**
134    * Third byte.
135    *
136    * @param num the num
137    * @return the byte
138    */
139   static byte thirdByte(int num) {
140     return (byte) ((num >> 8) & 0xFF);
141   }
142
143   /**
144    * Fourth byte.
145    *
146    * @param num the num
147    * @return the byte
148    */
149   static byte fourthByte(int num) {
150     return (byte) (num & 0xFF);
151   }
152
153   /**
154    * Int to byte.
155    *
156    * @param value the value
157    * @return the byte
158    */
159   public static byte intToByte(int value) {
160     return fourthByte(value);
161   }
162
163   /**
164    * Int to short.
165    *
166    * @param value the value
167    * @return the short
168    */
169   public static short intToShort(int value) {
170     return (short) ((value & 0xFF00) | (value & 0xFF));
171   }
172
173 }
174