2 * //******************************************************************
4 * // Copyright 2016 Samsung Electronics All Rights Reserved.
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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.
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 package org.iotivity.cloud.util;
24 import java.nio.ByteBuffer;
27 public static byte[] longTo8Bytes(long l) {
28 ByteBuffer buffer = ByteBuffer.allocate(8);
30 return buffer.array();
33 public static long bytesToLong(byte[] bytes) {
34 ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
35 if (bytes.length < Long.BYTES) {
36 buffer.put(new byte[Long.BYTES - bytes.length]);
40 return buffer.getLong();
43 public static int bytesToInt(byte[] bytes) {
44 ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES);
45 if (bytes.length < Integer.BYTES) {
46 buffer.put(new byte[Integer.BYTES - bytes.length]);
50 return buffer.getInt();
53 public static byte[] intToMax4Bytes(int i) {
54 ByteBuffer buffer = ByteBuffer.allocate(4);
57 if (buffer.get(0) != 0) {
58 return buffer.array();
61 if (buffer.get(1) != 0) {
62 byte b[] = new byte[3];
68 if (buffer.get(2) != 0) {
69 byte b[] = new byte[2];
76 return new byte[] { buffer.get(3) };