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 {
peekInt(byte[] src, int offset, ByteOrder order)22     public static int peekInt(byte[] src, int offset, ByteOrder order) {
23         if (order == ByteOrder.BIG_ENDIAN) {
24             return (((src[offset++] & 0xff) << 24) |
25                     ((src[offset++] & 0xff) << 16) |
26                     ((src[offset++] & 0xff) <<  8) |
27                     ((src[offset  ] & 0xff) <<  0));
28         } else {
29             return (((src[offset++] & 0xff) <<  0) |
30                     ((src[offset++] & 0xff) <<  8) |
31                     ((src[offset++] & 0xff) << 16) |
32                     ((src[offset  ] & 0xff) << 24));
33         }
34     }
35 
pokeInt(byte[] dst, int offset, int value, ByteOrder order)36     public static void pokeInt(byte[] dst, int offset, int value, ByteOrder order) {
37         if (order == ByteOrder.BIG_ENDIAN) {
38             dst[offset++] = (byte) ((value >> 24) & 0xff);
39             dst[offset++] = (byte) ((value >> 16) & 0xff);
40             dst[offset++] = (byte) ((value >>  8) & 0xff);
41             dst[offset  ] = (byte) ((value >>  0) & 0xff);
42         } else {
43             dst[offset++] = (byte) ((value >>  0) & 0xff);
44             dst[offset++] = (byte) ((value >>  8) & 0xff);
45             dst[offset++] = (byte) ((value >> 16) & 0xff);
46             dst[offset  ] = (byte) ((value >> 24) & 0xff);
47         }
48     }
49 }
50