1 /* 2 * Copyright (C) 2019 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.providers.media.util; 18 19 import java.nio.ByteOrder; 20 21 public class Memory { 22 Memory()23 private Memory() { 24 // Utility class, cannot be instantiated 25 } 26 peekInt(byte[] src, int offset, ByteOrder order)27 public static int peekInt(byte[] src, int offset, ByteOrder order) { 28 if (order == ByteOrder.BIG_ENDIAN) { 29 return (((src[offset++] & 0xff) << 24) | 30 ((src[offset++] & 0xff) << 16) | 31 ((src[offset++] & 0xff) << 8) | 32 ((src[offset ] & 0xff) << 0)); 33 } else { 34 return (((src[offset++] & 0xff) << 0) | 35 ((src[offset++] & 0xff) << 8) | 36 ((src[offset++] & 0xff) << 16) | 37 ((src[offset ] & 0xff) << 24)); 38 } 39 } 40 pokeInt(byte[] dst, int offset, int value, ByteOrder order)41 public static void pokeInt(byte[] dst, int offset, int value, ByteOrder order) { 42 if (order == ByteOrder.BIG_ENDIAN) { 43 dst[offset++] = (byte) ((value >> 24) & 0xff); 44 dst[offset++] = (byte) ((value >> 16) & 0xff); 45 dst[offset++] = (byte) ((value >> 8) & 0xff); 46 dst[offset ] = (byte) ((value >> 0) & 0xff); 47 } else { 48 dst[offset++] = (byte) ((value >> 0) & 0xff); 49 dst[offset++] = (byte) ((value >> 8) & 0xff); 50 dst[offset++] = (byte) ((value >> 16) & 0xff); 51 dst[offset ] = (byte) ((value >> 24) & 0xff); 52 } 53 } 54 peekLong(byte[] src, int offset, ByteOrder order)55 public static long peekLong(byte[] src, int offset, ByteOrder order) { 56 if (order == ByteOrder.BIG_ENDIAN) { 57 int h = ((src[offset++] & 0xff) << 24) | 58 ((src[offset++] & 0xff) << 16) | 59 ((src[offset++] & 0xff) << 8) | 60 ((src[offset++] & 0xff) << 0); 61 int l = ((src[offset++] & 0xff) << 24) | 62 ((src[offset++] & 0xff) << 16) | 63 ((src[offset++] & 0xff) << 8) | 64 ((src[offset ] & 0xff) << 0); 65 return (((long) h) << 32L) | ((long) l) & 0xffffffffL; 66 } else { 67 int l = ((src[offset++] & 0xff) << 0) | 68 ((src[offset++] & 0xff) << 8) | 69 ((src[offset++] & 0xff) << 16) | 70 ((src[offset++] & 0xff) << 24); 71 int h = ((src[offset++] & 0xff) << 0) | 72 ((src[offset++] & 0xff) << 8) | 73 ((src[offset++] & 0xff) << 16) | 74 ((src[offset ] & 0xff) << 24); 75 return (((long) h) << 32L) | ((long) l) & 0xffffffffL; 76 } 77 } 78 pokeLong(byte[] dst, int offset, long value, ByteOrder order)79 public static void pokeLong(byte[] dst, int offset, long value, ByteOrder order) { 80 if (order == ByteOrder.BIG_ENDIAN) { 81 int i = (int) (value >> 32); 82 dst[offset++] = (byte) ((i >> 24) & 0xff); 83 dst[offset++] = (byte) ((i >> 16) & 0xff); 84 dst[offset++] = (byte) ((i >> 8) & 0xff); 85 dst[offset++] = (byte) ((i >> 0) & 0xff); 86 i = (int) value; 87 dst[offset++] = (byte) ((i >> 24) & 0xff); 88 dst[offset++] = (byte) ((i >> 16) & 0xff); 89 dst[offset++] = (byte) ((i >> 8) & 0xff); 90 dst[offset ] = (byte) ((i >> 0) & 0xff); 91 } else { 92 int i = (int) value; 93 dst[offset++] = (byte) ((i >> 0) & 0xff); 94 dst[offset++] = (byte) ((i >> 8) & 0xff); 95 dst[offset++] = (byte) ((i >> 16) & 0xff); 96 dst[offset++] = (byte) ((i >> 24) & 0xff); 97 i = (int) (value >> 32); 98 dst[offset++] = (byte) ((i >> 0) & 0xff); 99 dst[offset++] = (byte) ((i >> 8) & 0xff); 100 dst[offset++] = (byte) ((i >> 16) & 0xff); 101 dst[offset ] = (byte) ((i >> 24) & 0xff); 102 } 103 } 104 } 105