• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 android.aconfig.storage;
18 
19 import java.nio.ByteBuffer;
20 import java.nio.ByteOrder;
21 import java.nio.charset.StandardCharsets;
22 import java.util.Objects;
23 
24 public class ByteBufferReader {
25 
26     private ByteBuffer mByteBuffer;
27     private int mPosition;
28 
ByteBufferReader(ByteBuffer byteBuffer)29     public ByteBufferReader(ByteBuffer byteBuffer) {
30         this.mByteBuffer = byteBuffer;
31         this.mByteBuffer.order(ByteOrder.LITTLE_ENDIAN);
32     }
33 
readByte()34     public int readByte() {
35         return Byte.toUnsignedInt(mByteBuffer.get(nextGetIndex(1)));
36     }
37 
readShort()38     public int readShort() {
39         return Short.toUnsignedInt(mByteBuffer.getShort(nextGetIndex(2)));
40     }
41 
readInt()42     public int readInt() {
43         return this.mByteBuffer.getInt(nextGetIndex(4));
44     }
45 
readLong()46     public long readLong() {
47         return this.mByteBuffer.getLong(nextGetIndex(8));
48     }
49 
readString()50     public String readString() {
51         int length = readInt();
52         if (length > 1024) {
53             throw new AconfigStorageException(
54                     "String length exceeds maximum allowed size (1024 bytes): " + length);
55         }
56         byte[] bytes = new byte[length];
57         getArray(nextGetIndex(length), bytes, 0, length);
58         return new String(bytes, StandardCharsets.UTF_8);
59     }
60 
readByte(int i)61     public int readByte(int i) {
62         return Byte.toUnsignedInt(mByteBuffer.get(i));
63     }
64 
position(int newPosition)65     public void position(int newPosition) {
66         mPosition = newPosition;
67     }
68 
position()69     public int position() {
70         return mPosition;
71     }
72 
nextGetIndex(int nb)73     private int nextGetIndex(int nb) {
74         int p = mPosition;
75         mPosition += nb;
76         return p;
77     }
78 
getArray(int index, byte[] dst, int offset, int length)79     private void getArray(int index, byte[] dst, int offset, int length) {
80         Objects.checkFromIndexSize(index, length, mByteBuffer.limit());
81         Objects.checkFromIndexSize(offset, length, dst.length);
82 
83         int end = offset + length;
84         for (int i = offset, j = index; i < end; i++, j++) {
85             dst[i] = mByteBuffer.get(j);
86         }
87     }
88 }
89