下面列出了android.os.Parcel#marshall ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected String encodeOpenWithIntent(Intent owpIntent) {
/*
* Encode OpenWith intent
* Real 3rd party apps should never run this function as DropboxApp does this entirely
*/
Bundle extras = owpIntent.getExtras();
extras.putString("_action", owpIntent.getAction());
extras.putParcelable("_uri", owpIntent.getData());
extras.putString("_type", owpIntent.getType());
// marshall it!
final Parcel parcel = Parcel.obtain();
parcel.writeBundle(extras);
byte[] b = parcel.marshall();
parcel.recycle();
return new String(Base64.encode(b, 0));
}
@NonNull
/* default */ Bundle encryptBundle(@NonNull Bundle bundle) throws GeneralSecurityException {
Preconditions.checkNotNull(bundle, "Cannot encrypt null bundle.");
Parcel parcel = Parcel.obtain();
bundle.writeToParcel(parcel, 0);
byte[] clearBytes = parcel.marshall();
parcel.recycle();
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, mEncryptionKey);
byte[] encryptedBytes = cipher.doFinal(clearBytes);
byte[] iv = cipher.getIV();
byte[] mac = createMac(encryptedBytes, iv);
Bundle encryptedBundle = new Bundle();
encryptedBundle.putByteArray(KEY_CIPHER, encryptedBytes);
encryptedBundle.putByteArray(KEY_MAC, mac);
encryptedBundle.putByteArray(KEY_IV, iv);
return encryptedBundle;
}
@Override
public int calculateMarshalSize(T value) {
Parcel parcel = Parcel.obtain();
try {
value.writeToParcel(parcel, /*flags*/0);
int length = parcel.marshall().length;
if (DEBUG) {
Log.v(TAG, "calculateMarshalSize, length when parceling "
+ value + " is " + length);
}
return length;
} finally {
parcel.recycle();
}
}
public MasterSecret parcelClone() {
Parcel thisParcel = Parcel.obtain();
Parcel thatParcel = Parcel.obtain();
byte[] bytes = null;
thisParcel.writeValue(this);
bytes = thisParcel.marshall();
thatParcel.unmarshall(bytes, 0, bytes.length);
thatParcel.setDataPosition(0);
MasterSecret that = (MasterSecret)thatParcel.readValue(MasterSecret.class.getClassLoader());
thisParcel.recycle();
thatParcel.recycle();
return that;
}
public MockConnection(final CharSequence text, final int cursorPosition) {
super(null, false);
// Interaction of spans with Parcels is completely non-trivial, but in the actual case
// the CharSequences do go through Parcels because they go through IPC. There
// are some significant differences between the behavior of Spanned objects that
// have and that have not gone through parceling, so it's much easier to simulate
// the environment with Parcels than try to emulate things by hand.
final Parcel p = Parcel.obtain();
TextUtils.writeToParcel(text.subSequence(0, cursorPosition), p, 0 /* flags */);
TextUtils.writeToParcel(text.subSequence(cursorPosition, text.length()), p,
0 /* flags */);
final byte[] marshalled = p.marshall();
p.unmarshall(marshalled, 0, marshalled.length);
p.setDataPosition(0);
mTextBefore = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(p);
mTextAfter = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(p);
mExtractedText = null;
p.recycle();
}
@Test
public void testParcelling() {
PersistableBundle bundle = new PersistableBundle();
bundle.putString("string", "string");
bundle.putInt("int", 0);
bundle.putLong("long", 0);
bundle.putBoolean("boolean", true);
// Can't use double or any array, as the instances would be different and equals() would fail.
Parcel parcel = Parcel.obtain();
parcel.writeValue(bundle);
byte[] bytes = parcel.marshall();
parcel.recycle();
parcel = Parcel.obtain();
parcel.unmarshall(bytes, 0, bytes.length);
parcel.setDataPosition(0);
PersistableBundle parcelledBundle =
(PersistableBundle) parcel.readValue(PersistableBundle.class.getClassLoader());
assertEquals(bundle.toMap(1), parcelledBundle.toMap(1));
}
private static byte[] parcelable2Bytes(Parcelable parcelable) {
if (parcelable == null) return null;
Parcel parcel = Parcel.obtain();
parcelable.writeToParcel(parcel, 0);
byte[] bytes = parcel.marshall();
parcel.recycle();
return bytes;
}
public static byte[] marshall(Parcelable parceable) {
Parcel parcel = Parcel.obtain();
parceable.writeToParcel(parcel, 0);
byte[] bytes = parcel.marshall();
parcel.recycle();
return bytes;
}
public void onSaveInstanceState(Bundle outState) {
Parcel outParcel = Parcel.obtain();
outParcel.writeSparseBooleanArray(mCheckStates);
final int numStates = mCheckedIdStates.size();
outParcel.writeInt(numStates);
for (int i=0; i<numStates; i++) {
outParcel.writeLong(mCheckedIdStates.keyAt(i));
outParcel.writeInt(mCheckedIdStates.valueAt(i));
}
byte[] states = outParcel.marshall();
outState.putByteArray(SELECTED_ITEMS_KEY, states);
outParcel.recycle();
}
/**
* Converts the Parcelable object to a byte[]
*
* @param parcelable
* @return
*/
public static byte[] parcelToByte(@NonNull Parcelable parcelable) {
Parcel parcel = Parcel.obtain();
parcelable.writeToParcel(parcel, 0);
byte[] bytes = parcel.marshall();
parcel.recycle();
return bytes;
}
public static byte[] serialize(Parcelable parceable) {
Parcel parcel = Parcel.obtain();
parceable.writeToParcel(parcel, 0);
byte[] bytes = parcel.marshall();
parcel.recycle();
return bytes;
}
public byte[] getCurrentStats(List<ParcelFileDescriptor> historic) {
mAm.mContext.enforceCallingOrSelfPermission(
android.Manifest.permission.PACKAGE_USAGE_STATS, null);
Parcel current = Parcel.obtain();
synchronized (mAm) {
long now = SystemClock.uptimeMillis();
mProcessStats.mTimePeriodEndRealtime = SystemClock.elapsedRealtime();
mProcessStats.mTimePeriodEndUptime = now;
mProcessStats.writeToParcel(current, now, 0);
}
mWriteLock.lock();
try {
if (historic != null) {
ArrayList<String> files = getCommittedFiles(0, false, true);
if (files != null) {
for (int i=files.size()-1; i>=0; i--) {
try {
ParcelFileDescriptor pfd = ParcelFileDescriptor.open(
new File(files.get(i)), ParcelFileDescriptor.MODE_READ_ONLY);
historic.add(pfd);
} catch (IOException e) {
Slog.w(TAG, "Failure opening procstat file " + files.get(i), e);
}
}
}
}
} finally {
mWriteLock.unlock();
}
return current.marshall();
}
private static byte[] marshall(final InputMethodSubtype[] array) {
Parcel parcel = null;
try {
parcel = Parcel.obtain();
parcel.writeTypedArray(array, 0);
return parcel.marshall();
} finally {
if (parcel != null) {
parcel.recycle();
parcel = null;
}
}
}
public ServiceTransport clone() {
Parcel p = Parcel.obtain();
Parcel p2 = Parcel.obtain();
getMap().writeToParcel(p, 0);
byte[] bytes = p.marshall();
p2.unmarshall(bytes, 0, bytes.length);
p2.setDataPosition(0);
Bundle b = p2.readBundle();
ServiceTransport rval = new ServiceTransport();
rval.setMap(b);
return rval;
}
/**
* Serializes the provided intent to a byte array.
*/
@NonNull
public static byte[] toBytes(@NonNull Intent intent) {
require(intent, notNullValue());
Parcel parcel = Parcel.obtain();
parcel.writeParcelable(intent, 0);
byte[] intentBytes = parcel.marshall();
parcel.recycle();
return intentBytes;
}
public static byte[] getAddressAsBytes(Address address) {
if (address == null) {
return null;
}
final Parcel parcel = Parcel.obtain();
address.writeToParcel(parcel, 0);
byte[] addressBytes = parcel.marshall();
parcel.recycle();
return addressBytes;
}
/**
* Marshalls a list of {@link Parcelable} into parcel bytes for adding into an {@link Intent}
* extras. Workaround for bug: https://code.google.com/p/android/issues/detail?id=6822
*/
public static byte[] marshallParcelableList(ArrayList<? extends Parcelable> parcelables) {
Parcel parcel = Parcel.obtain();
parcel.writeList(parcelables);
parcel.setDataPosition(0);
return parcel.marshall();
}
private static byte[] marshall(final CharSequence cahrSequence) {
Parcel parcel = null;
try {
parcel = Parcel.obtain();
TextUtils.writeToParcel(cahrSequence, parcel, 0);
return parcel.marshall();
} finally {
if (parcel != null) {
parcel.recycle();
parcel = null;
}
}
}
private <T extends Parcelable> byte[] parcelableToByteArray(T parcelable) {
Parcel parcel = Parcel.obtain();
parcel.writeParcelable(parcelable, 0);
byte[] data = parcel.marshall();
parcel.recycle();
return data;
}
private byte[] marshallRequest(int token, byte data[]) {
Parcel p = Parcel.obtain();
p.writeInt(RIL_REQUEST_OEM_RAW);
p.writeInt(token);
p.writeByteArray(data);
byte[] res = p.marshall();
p.recycle();
return res;
}