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