2 * ============LICENSE_START=======================================================
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
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.sparky.util;
26 * The Class RawByteHelper.
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'};
35 * @param buffer the buffer
39 * TODO -> DOCUMENT ME!
41 * @param buffer DOCUMENT ME!
43 * @return DOCUMENT ME!
45 public static String dumpBytes(byte[] buffer) {
49 String newLine = System.getProperty("line.separator");
50 StringBuffer sb = new StringBuffer();
52 for (int i = 0; i < buffer.length; i++) {
53 if (i != 0 && i % 16 == 0) {
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(" ");
65 // if you're trying to figure out why or's w/ FF's see:
71 * @param three the three
72 * @param four the four
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)));
87 public static short bytesToShort(byte one, byte two) {
88 return (short) (((0xFF & one) << 8) | (0xFF & two));
97 // short helper functions
98 static byte firstByte(short num) {
99 return (byte) ((num >> 8) & 0xFF);
108 // Int helper functions
109 static byte firstByte(int num) {
110 return (byte) ((num >> 24) & 0xFF);
119 static byte secondByte(short num) {
120 return (byte) (num & 0xFF);
129 static byte secondByte(int num) {
130 return (byte) ((num >> 16) & 0xFF);
139 static byte thirdByte(int num) {
140 return (byte) ((num >> 8) & 0xFF);
149 static byte fourthByte(int num) {
150 return (byte) (num & 0xFF);
156 * @param value the value
159 public static byte intToByte(int value) {
160 return fourthByte(value);
166 * @param value the value
169 public static short intToShort(int value) {
170 return (short) ((value & 0xFF00) | (value & 0xFF));