public static final Bundle EMPTY;
static final int BUNDLE_MAGIC = 0x4C444E42; // 'B' 'N' 'D' 'L'
+ static final Parcel EMPTY_PARCEL;
static {
EMPTY = new Bundle();
EMPTY.mMap = ArrayMap.EMPTY;
+ EMPTY_PARCEL = Parcel.obtain();
}
// Invariant - exactly one of mMap / mParcelledData will be null
*/
public Bundle(Bundle b) {
if (b.mParcelledData != null) {
- mParcelledData = Parcel.obtain();
- mParcelledData.appendFrom(b.mParcelledData, 0, b.mParcelledData.dataSize());
- mParcelledData.setDataPosition(0);
+ if (b.mParcelledData == EMPTY_PARCEL) {
+ mParcelledData = EMPTY_PARCEL;
+ } else {
+ mParcelledData = Parcel.obtain();
+ mParcelledData.appendFrom(b.mParcelledData, 0, b.mParcelledData.dataSize());
+ mParcelledData.setDataPosition(0);
+ }
} else {
mParcelledData = null;
}
return;
}
+ if (mParcelledData == EMPTY_PARCEL) {
+ if (DEBUG) Log.d(TAG, "unparcel " + Integer.toHexString(System.identityHashCode(this))
+ + ": empty");
+ if (mMap == null) {
+ mMap = new ArrayMap<String, Object>(1);
+ } else {
+ mMap.erase();
+ }
+ mParcelledData = null;
+ return;
+ }
+
int N = mParcelledData.readInt();
if (DEBUG) Log.d(TAG, "unparcel " + Integer.toHexString(System.identityHashCode(this))
+ ": reading " + N + " maps");
final boolean oldAllowFds = parcel.pushAllowFds(mAllowFds);
try {
if (mParcelledData != null) {
- int length = mParcelledData.dataSize();
- parcel.writeInt(length);
- parcel.writeInt(BUNDLE_MAGIC);
- parcel.appendFrom(mParcelledData, 0, length);
+ if (mParcelledData == EMPTY_PARCEL) {
+ parcel.writeInt(0);
+ } else {
+ int length = mParcelledData.dataSize();
+ parcel.writeInt(length);
+ parcel.writeInt(BUNDLE_MAGIC);
+ parcel.appendFrom(mParcelledData, 0, length);
+ }
} else {
+ // Special case for empty bundles.
+ if (mMap == null || mMap.size() <= 0) {
+ parcel.writeInt(0);
+ return;
+ }
int lengthPos = parcel.dataPosition();
parcel.writeInt(-1); // dummy, will hold length
parcel.writeInt(BUNDLE_MAGIC);
}
void readFromParcelInner(Parcel parcel, int length) {
+ if (length == 0) {
+ // Empty Bundle or end of data.
+ mParcelledData = EMPTY_PARCEL;
+ mHasFds = false;
+ mFdsKnown = true;
+ return;
+ }
int magic = parcel.readInt();
if (magic != BUNDLE_MAGIC) {
//noinspection ThrowableInstanceNeverThrown
@Override
public synchronized String toString() {
if (mParcelledData != null) {
- return "Bundle[mParcelledData.dataSize=" +
- mParcelledData.dataSize() + "]";
+ if (mParcelledData == EMPTY_PARCEL) {
+ return "Bundle[EMPTY_PARCEL]";
+ } else {
+ return "Bundle[mParcelledData.dataSize=" +
+ mParcelledData.dataSize() + "]";
+ }
}
return "Bundle[" + mMap.toString() + "]";
}
here.fillInStackTrace();
Log.d(TAG, "Writing " + N + " ArrayMap entries", here);
}
+ int startPos;
for (int i=0; i<N; i++) {
- if (DEBUG_ARRAY_MAP) Log.d(TAG, " Write #" + i + ": key=0x"
- + (val.keyAt(i) != null ? val.keyAt(i).hashCode() : 0) + " " + val.keyAt(i));
+ if (DEBUG_ARRAY_MAP) startPos = dataPosition();
writeValue(val.keyAt(i));
writeValue(val.valueAt(i));
+ if (DEBUG_ARRAY_MAP) Log.d(TAG, " Write #" + i + " "
+ + (dataPosition()-startPos) + " bytes: key=0x"
+ + Integer.toHexString(val.keyAt(i) != null ? val.keyAt(i).hashCode() : 0)
+ + " " + val.keyAt(i));
}
}
here.fillInStackTrace();
Log.d(TAG, "Reading " + N + " ArrayMap entries", here);
}
+ int startPos;
while (N > 0) {
+ if (DEBUG_ARRAY_MAP) startPos = dataPosition();
Object key = readValue(loader);
- if (DEBUG_ARRAY_MAP) Log.d(TAG, " Read #" + (N-1) + ": key=0x"
- + (key != null ? key.hashCode() : 0) + " " + key);
Object value = readValue(loader);
+ if (DEBUG_ARRAY_MAP) Log.d(TAG, " Read #" + (N-1) + " "
+ + (dataPosition()-startPos) + " bytes: key=0x"
+ + Integer.toHexString((key != null ? key.hashCode() : 0)) + " " + key);
outVal.append(key, value);
N--;
}