From 823599b4a86ba20638a41bac66cadefeeae05108 Mon Sep 17 00:00:00 2001 From: Benedict Wong Date: Tue, 3 May 2022 04:29:38 +0000 Subject: [PATCH] Add to/from disk stable format This change adds a utility to convert persistable bundles to and from disk-stable byte arrays. Test: atest FrameworksVcnTests:PersistableBundleUtilsTest Change-Id: Ifafb7081ec6a34ce3ab8e2aa502d46411405cb6d --- .../vcn/util/PersistableBundleUtils.java | 26 +++++++++++++++++++ .../vcn/util/PersistableBundleUtilsTest.java | 9 +++++++ 2 files changed, 35 insertions(+) diff --git a/services/core/java/com/android/server/vcn/util/PersistableBundleUtils.java b/services/core/java/com/android/server/vcn/util/PersistableBundleUtils.java index 08e8eebb87406..999d4064c9516 100644 --- a/services/core/java/com/android/server/vcn/util/PersistableBundleUtils.java +++ b/services/core/java/com/android/server/vcn/util/PersistableBundleUtils.java @@ -23,6 +23,8 @@ import android.os.PersistableBundle; import com.android.internal.util.HexDump; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -295,6 +297,30 @@ public class PersistableBundleUtils { return result; } + /** + * Converts a PersistableBundle into a disk-stable byte array format + * + * @param bundle the PersistableBundle to be converted to a disk-stable format + * @return the byte array representation of the PersistableBundle + */ + @Nullable + public static byte[] toDiskStableBytes(@NonNull PersistableBundle bundle) throws IOException { + final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + bundle.writeToStream(outputStream); + return outputStream.toByteArray(); + } + + /** + * Converts from a disk-stable byte array format to a PersistableBundle + * + * @param bytes the disk-stable byte array + * @return the PersistableBundle parsed from this byte array. + */ + public static PersistableBundle fromDiskStableBytes(@NonNull byte[] bytes) throws IOException { + final ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes); + return PersistableBundle.readFromStream(inputStream); + } + /** * Ensures safe reading and writing of {@link PersistableBundle}s to and from disk. * diff --git a/tests/vcn/java/com/android/server/vcn/util/PersistableBundleUtilsTest.java b/tests/vcn/java/com/android/server/vcn/util/PersistableBundleUtilsTest.java index 294f5c1f4842a..9c6d85238b774 100644 --- a/tests/vcn/java/com/android/server/vcn/util/PersistableBundleUtilsTest.java +++ b/tests/vcn/java/com/android/server/vcn/util/PersistableBundleUtilsTest.java @@ -267,6 +267,15 @@ public class PersistableBundleUtilsTest { assertTrue(PersistableBundleUtils.isEqual(testBundle, minimized)); } + @Test + public void testToFromDiskStableBytes() throws Exception { + final PersistableBundle testBundle = getTestBundle(); + final PersistableBundle result = + PersistableBundleUtils.fromDiskStableBytes( + PersistableBundleUtils.toDiskStableBytes(testBundle)); + assertTrue(PersistableBundleUtils.isEqual(testBundle, result)); + } + @Test public void testEquality_identical() throws Exception { final PersistableBundle left = getTestBundle();