1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.ddmlib.utils; 18 19 /** 20 * Utility class providing array to int/long conversion for data received from devices through adb. 21 */ 22 public final class ArrayHelper { 23 24 /** 25 * Swaps an unsigned value around, and puts the result in an array that can be sent to a device. 26 * @param value The value to swap. 27 * @param dest the destination array 28 * @param offset the offset in the array where to put the swapped value. 29 * Array length must be at least offset + 4 30 */ swap32bitsToArray(int value, byte[] dest, int offset)31 public static void swap32bitsToArray(int value, byte[] dest, int offset) { 32 dest[offset] = (byte)(value & 0x000000FF); 33 dest[offset + 1] = (byte)((value & 0x0000FF00) >> 8); 34 dest[offset + 2] = (byte)((value & 0x00FF0000) >> 16); 35 dest[offset + 3] = (byte)((value & 0xFF000000) >> 24); 36 } 37 38 /** 39 * Reads a signed 32 bit integer from an array coming from a device. 40 * @param value the array containing the int 41 * @param offset the offset in the array at which the int starts 42 * @return the integer read from the array 43 */ swap32bitFromArray(byte[] value, int offset)44 public static int swap32bitFromArray(byte[] value, int offset) { 45 int v = 0; 46 v |= ((int)value[offset]) & 0x000000FF; 47 v |= (((int)value[offset + 1]) & 0x000000FF) << 8; 48 v |= (((int)value[offset + 2]) & 0x000000FF) << 16; 49 v |= (((int)value[offset + 3]) & 0x000000FF) << 24; 50 51 return v; 52 } 53 54 /** 55 * Reads an unsigned 16 bit integer from an array coming from a device, 56 * and returns it as an 'int' 57 * @param value the array containing the 16 bit int (2 byte). 58 * @param offset the offset in the array at which the int starts 59 * Array length must be at least offset + 2 60 * @return the integer read from the array. 61 */ swapU16bitFromArray(byte[] value, int offset)62 public static int swapU16bitFromArray(byte[] value, int offset) { 63 int v = 0; 64 v |= ((int)value[offset]) & 0x000000FF; 65 v |= (((int)value[offset + 1]) & 0x000000FF) << 8; 66 67 return v; 68 } 69 70 /** 71 * Reads a signed 64 bit integer from an array coming from a device. 72 * @param value the array containing the int 73 * @param offset the offset in the array at which the int starts 74 * Array length must be at least offset + 8 75 * @return the integer read from the array 76 */ swap64bitFromArray(byte[] value, int offset)77 public static long swap64bitFromArray(byte[] value, int offset) { 78 long v = 0; 79 v |= ((long)value[offset]) & 0x00000000000000FFL; 80 v |= (((long)value[offset + 1]) & 0x00000000000000FFL) << 8; 81 v |= (((long)value[offset + 2]) & 0x00000000000000FFL) << 16; 82 v |= (((long)value[offset + 3]) & 0x00000000000000FFL) << 24; 83 v |= (((long)value[offset + 4]) & 0x00000000000000FFL) << 32; 84 v |= (((long)value[offset + 5]) & 0x00000000000000FFL) << 40; 85 v |= (((long)value[offset + 6]) & 0x00000000000000FFL) << 48; 86 v |= (((long)value[offset + 7]) & 0x00000000000000FFL) << 56; 87 88 return v; 89 } 90 } 91