2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.sparky.util;
24 * The Class RawByteHelper.
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'};
33 * @param buffer the buffer
37 * TODO -> DOCUMENT ME!
39 * @param buffer DOCUMENT ME!
41 * @return DOCUMENT ME!
43 public static String dumpBytes(byte[] buffer) {
47 String newLine = System.getProperty("line.separator");
48 StringBuffer sb = new StringBuffer();
50 for (int i = 0; i < buffer.length; i++) {
51 if (i != 0 && i % 16 == 0) {
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(" ");
63 // if you're trying to figure out why or's w/ FF's see:
69 * @param three the three
70 * @param four the four
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)));
85 public static short bytesToShort(byte one, byte two) {
86 return (short) (((0xFF & one) << 8) | (0xFF & two));
95 // short helper functions
96 static byte firstByte(short num) {
97 return (byte) ((num >> 8) & 0xFF);
106 // Int helper functions
107 static byte firstByte(int num) {
108 return (byte) ((num >> 24) & 0xFF);
117 static byte secondByte(short num) {
118 return (byte) (num & 0xFF);
127 static byte secondByte(int num) {
128 return (byte) ((num >> 16) & 0xFF);
137 static byte thirdByte(int num) {
138 return (byte) ((num >> 8) & 0xFF);
147 static byte fourthByte(int num) {
148 return (byte) (num & 0xFF);
154 * @param value the value
157 public static byte intToByte(int value) {
158 return fourthByte(value);
164 * @param value the value
167 public static short intToShort(int value) {
168 return (short) ((value & 0xFF00) | (value & 0xFF));