Add to/from disk stable format am: 1e3c0ae64d
Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/18218766 Change-Id: Ie300931825c0ab687a25586777d291ec4f0bc1b9 Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
@@ -23,6 +23,8 @@ import android.os.PersistableBundle;
|
|||||||
|
|
||||||
import com.android.internal.util.HexDump;
|
import com.android.internal.util.HexDump;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
@@ -295,6 +297,30 @@ public class PersistableBundleUtils {
|
|||||||
return result;
|
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.
|
* Ensures safe reading and writing of {@link PersistableBundle}s to and from disk.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -267,6 +267,15 @@ public class PersistableBundleUtilsTest {
|
|||||||
assertTrue(PersistableBundleUtils.isEqual(testBundle, minimized));
|
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
|
@Test
|
||||||
public void testEquality_identical() throws Exception {
|
public void testEquality_identical() throws Exception {
|
||||||
final PersistableBundle left = getTestBundle();
|
final PersistableBundle left = getTestBundle();
|
||||||
|
|||||||
Reference in New Issue
Block a user