下面列出了android.os.Parcel#dataSize ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
boolean writeToParcelInner(AssistStructure as, Parcel out) {
if (mNumWindows == 0) {
return false;
}
if (DEBUG_PARCEL) Log.d(TAG, "Creating PooledStringWriter @ " + out.dataPosition());
PooledStringWriter pwriter = new PooledStringWriter(out);
while (writeNextEntryToParcel(as, out, pwriter)) {
// If the parcel is above the IPC limit, then we are getting too
// large for a single IPC so stop here and let the caller come back when it
// is ready for more.
if (out.dataSize() > IBinder.MAX_IPC_SIZE) {
if (DEBUG_PARCEL) Log.d(TAG, "Assist data size is " + out.dataSize()
+ " @ pos " + out.dataPosition() + "; returning partial result");
out.writeInt(0);
out.writeStrongBinder(this);
if (DEBUG_PARCEL) Log.d(TAG, "Finishing PooledStringWriter @ "
+ out.dataPosition() + ", size " + pwriter.getStringCount());
pwriter.finish();
return true;
}
}
if (DEBUG_PARCEL) Log.d(TAG, "Finishing PooledStringWriter @ "
+ out.dataPosition() + ", size " + pwriter.getStringCount());
pwriter.finish();
mViewStack.clear();
return false;
}
@NonNull public Bundle end() {
Parcel parcel = Parcel.obtain();
bundle.writeToParcel(parcel, 0);
int size = parcel.dataSize();
Logger.e(size);
if (size > 500000) {
bundle.clear();
}
return get();
}
public Bundle build(){
Parcel parcel = Parcel.obtain();
bundle.writeToParcel(parcel, 0);
if (parcel.dataSize() > 512 * 1024) {
bundle.clear();
throw new IllegalArgumentException("bundle data is too large, please reduce date size to avoid Exception");
}
return bundle;
}
private static int getBundleSizeInBytes(Bundle bundle) {
final Parcel parcel = Parcel.obtain();
parcel.writeBundle(bundle);
final int size = parcel.dataSize();
parcel.recycle();
return size;
}
private static int getBundleSizeInBytes(Bundle bundle) {
final Parcel parcel = Parcel.obtain();
parcel.writeBundle(bundle);
final int size = parcel.dataSize();
parcel.recycle();
return size;
}
/** @see #MAX_EXTRAS_SIZE_BYTES */
private static int measureBundleSize(Bundle extras) {
Parcel p = Parcel.obtain();
extras.writeToParcel(p, 0);
int sizeInBytes = p.dataSize();
p.recycle();
return sizeInBytes;
}
static int getSize(Bundle bundle) {
Parcel p = Parcel.obtain();
bundle.writeToParcel(p, 0);
return p.dataSize();
}
public static boolean isValidBundleSize(@NonNull Bundle bundle) {
Parcel parcel = Parcel.obtain();
bundle.writeToParcel(parcel, 0);
return parcel.dataSize() < 500000;
}
public void corruptParcel(int testWith){
Parcel p = Parcel.obtain();
p.writeInt(4);
p.writeLong(System.currentTimeMillis());
p.writeParcelable(new ComponentName(mContext, "test"), 0);
p.writeParcelable(new Intent(), 0);
p.setDataPosition(0);
SdlRouterService.LocalRouterService local = getLocalRouterService(testWith, p);
assertNotNull(local);
assertNull(local.launchIntent);
assertNull(local.name);
p.recycle();
//---------------------------------------------------------------------------
p = Parcel.obtain();
p.writeInt(4);
p.writeLong(System.currentTimeMillis());
p.writeParcelable(null,0);
p.writeParcelable(null,0);
p.setDataPosition(0);
local = getLocalRouterService(testWith, p);
assertNotNull(local);
assertNull(local.launchIntent);
assertNull(local.name);
p.recycle();
//---------------------------------------------------------------------------
p = Parcel.obtain();
p.writeInt(4);
p.writeLong(System.currentTimeMillis());
p.setDataPosition(0);
local = getLocalRouterService(testWith, p);
assertNotNull(local);
assertNull(local.launchIntent);
assertNull(local.name);
p.recycle();
//---------------------------------------------------------------------------
local = null;
p = null;
p = Parcel.obtain();
p.writeInt(4);
p.writeLong(System.currentTimeMillis());
int space = p.dataSize();
p.writeParcelable(new Intent(), 0);
p.writeParcelable(new ComponentName(mContext, "test"), 0);
p.setDataPosition(0);
byte[] raw = p.marshall();
for(;space<raw.length;space++){
raw[space] = 0x00;
}
p.recycle();
p = Parcel.obtain();
p.unmarshall(raw, 0, raw.length);
p.setDataPosition(0);
local = getLocalRouterService(testWith, p);
assertNotNull(local);
assertNull(local.launchIntent);
assertNull(local.name);
p.recycle();
}
/**
* Returns how large the Intent will be in Parcel form, which is helpful for gauging whether
* Android will deliver the Intent instead of throwing a TransactionTooLargeException.
*
* @param intent Intent to get the size of.
* @return Number of bytes required to parcel the Intent.
*/
public static int getParceledIntentSize(Intent intent) {
Parcel parcel = Parcel.obtain();
intent.writeToParcel(parcel, 0);
return parcel.dataSize();
}
/**
* Returns how large the Intent will be in Parcel form, which is helpful for gauging whether
* Android will deliver the Intent instead of throwing a TransactionTooLargeException.
*
* @param intent Intent to get the size of.
* @return Number of bytes required to parcel the Intent.
*/
public static int getParceledIntentSize(Intent intent) {
Parcel parcel = Parcel.obtain();
intent.writeToParcel(parcel, 0);
return parcel.dataSize();
}
/**
* Returns how large the Intent will be in Parcel form, which is helpful for gauging whether
* Android will deliver the Intent instead of throwing a TransactionTooLargeException.
*
* @param intent Intent to get the size of.
* @return Number of bytes required to parcel the Intent.
*/
public static int getParceledIntentSize(Intent intent) {
Parcel parcel = Parcel.obtain();
intent.writeToParcel(parcel, 0);
return parcel.dataSize();
}