2 * ============LICENSE_START===================================================
3 * SPARKY (AAI UI service)
4 * ============================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=====================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
26 package org.openecomp.sparky.util;
29 * The Class RawByteHelper.
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'};
38 * @param buffer the buffer
42 * TODO -> DOCUMENT ME!
44 * @param buffer DOCUMENT ME!
46 * @return DOCUMENT ME!
48 public static String dumpBytes(byte[] buffer) {
52 String newLine = System.getProperty("line.separator");
53 StringBuffer sb = new StringBuffer();
55 for (int i = 0; i < buffer.length; i++) {
56 if (i != 0 && i % 16 == 0) {
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(" ");
68 // if you're trying to figure out why or's w/ FF's see:
74 * @param three the three
75 * @param four the four
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)));
90 public static short bytesToShort(byte one, byte two) {
91 return (short) (((0xFF & one) << 8) | (0xFF & two));
100 // short helper functions
101 static byte firstByte(short num) {
102 return (byte) ((num >> 8) & 0xFF);
111 // Int helper functions
112 static byte firstByte(int num) {
113 return (byte) ((num >> 24) & 0xFF);
122 static byte secondByte(short num) {
123 return (byte) (num & 0xFF);
132 static byte secondByte(int num) {
133 return (byte) ((num >> 16) & 0xFF);
142 static byte thirdByte(int num) {
143 return (byte) ((num >> 8) & 0xFF);
152 static byte fourthByte(int num) {
153 return (byte) (num & 0xFF);
159 * @param value the value
162 public static byte intToByte(int value) {
163 return fourthByte(value);
169 * @param value the value
172 public static short intToShort(int value) {
173 return (short) ((value & 0xFF00) | (value & 0xFF));