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