Java Code Examples for android.os.Parcel.dataAvail()

The following are Jave code examples for showing how to use dataAvail() of the android.os.Parcel class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: aos-MediaLib   File: MediaMetadata.java   View Source Code Vote up 5 votes
/**
 * Check a parcel containing metadata is well formed. The header
 * is checked as well as the individual records format. However, the
 * data inside the record is not checked because we do lazy access
 * (we check/unmarshall only data the user asks for.)
 *
 * Format of a metadata parcel:
 <pre>
                     1                   2                   3
  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  |                     metadata total size                       |
  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  |     'M'       |     'E'       |     'T'       |     'A'       |
  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  |                                                               |
  |                .... metadata records ....                     |
  |                                                               |
  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 </pre>
 *
 * @param parcel With the serialized data. Metadata keeps a
 *               reference on it to access it later on. The caller
 *               should not modify the parcel after this call (and
 *               not call recycle on it.)
 * @return false if an error occurred.
 * {@hide}
 */
public boolean parse(Parcel parcel) {
    if (parcel.dataAvail() < kMetaHeaderSize) {
        Log.e(TAG, "Not enough data " + parcel.dataAvail());
        return false;
    }

    final int pin = parcel.dataPosition();  // to roll back in case of errors.
    final int size = parcel.readInt();

    // The extra kInt32Size below is to account for the int32 'size' just read.
    if (parcel.dataAvail() + kInt32Size < size || size < kMetaHeaderSize) {
        Log.e(TAG, "Bad size " + size + " avail " + parcel.dataAvail() + " position " + pin);
        parcel.setDataPosition(pin);
        return false;
    }

    // Checks if the 'M' 'E' 'T' 'A' marker is present.
    final int kShouldBeMetaMarker = parcel.readInt();
    if (kShouldBeMetaMarker != kMetaMarker ) {
        Log.e(TAG, "Marker missing " + Integer.toHexString(kShouldBeMetaMarker));
        parcel.setDataPosition(pin);
        return false;
    }

    // Scan the records to collect metadata ids and offsets.
    if (!scanAllRecords(parcel, size - kMetaHeaderSize)) {
        parcel.setDataPosition(pin);
        return false;
    }
    mBegin = pin;
    mParcel = parcel;
    return true;
}