Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit af8a16ad authored by Zhi Dou's avatar Zhi Dou
Browse files

Implement storage files in Java

This change implements storage files in java to remove the dependencies
on JNI.

Bug: 352078117
Test: atest aconfig_storage_file.test.java

Change-Id: I34438576d09d0aa31eabc60f472e71cebf1552a0
parent 000be965
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -98,6 +98,10 @@
    {
      // aconfig_storage file cpp integration tests
      "name": "aconfig_storage_file.test.cpp"
    },
    {
      // aconfig_storage file java integration tests
      "name": "aconfig_storage_file.test.java"
    }
  ],
  "postsubmit": [
+9 −0
Original line number Diff line number Diff line
@@ -137,3 +137,12 @@ cc_library {
    min_sdk_version: "29",
    double_loadable: true,
}

// storage file parse api java cc_library
java_library {
    name: "aconfig_storage_file_java",
    srcs: [
        "srcs/**/*.java",
    ],
    sdk_version: "core_current",
}
 No newline at end of file
+27 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.aconfig.storage;

public class AconfigStorageException extends RuntimeException {
    public AconfigStorageException(String msg) {
        super(msg);
    }

    public AconfigStorageException(String msg, Throwable cause) {
        super(msg, cause);
    }
}
+54 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.aconfig.storage;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;

public class ByteBufferReader {

    private ByteBuffer mByteBuffer;

    public ByteBufferReader(ByteBuffer byteBuffer) {
        this.mByteBuffer = byteBuffer;
        this.mByteBuffer.order(ByteOrder.LITTLE_ENDIAN);
    }

    public int readByte() {
        return Byte.toUnsignedInt(mByteBuffer.get());
    }

    public int readShort() {
        return Short.toUnsignedInt(mByteBuffer.getShort());
    }

    public int readInt() {
        return this.mByteBuffer.getInt();
    }

    public String readString() {
        int length = readInt();
        byte[] bytes = new byte[length];
        mByteBuffer.get(bytes, 0, length);
        return new String(bytes, StandardCharsets.UTF_8);
    }

    public void position(int newPosition) {
        mByteBuffer.position(newPosition);
    }
}
+45 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.aconfig.storage;

public enum FileType {
    PACKAGE_MAP(0),
    FLAG_MAP(1),
    FLAG_VAL(2),
    FLAG_INFO(3);

    public final int type;

    FileType(int type) {
        this.type = type;
    }

    public static FileType fromInt(int index) {
        switch (index) {
            case 0:
                return PACKAGE_MAP;
            case 1:
                return FLAG_MAP;
            case 2:
                return FLAG_VAL;
            case 3:
                return FLAG_INFO;
            default:
                return null;
        }
    }
}
Loading