Example usage for android.os Parcel setDataPosition

List of usage examples for android.os Parcel setDataPosition

Introduction

In this page you can find the example usage for android.os Parcel setDataPosition.

Prototype

public final void setDataPosition(int pos) 

Source Link

Document

Move the current read/write position in the parcel.

Usage

From source file:com.bmd.android.collection.SimpleArrayMapTest.java

public void testParcelable() {

    final ParcelableObjectSparseObjectEntry[] parcelableArray = AndroidCollections.iterate(mArray).only()
            .values("4", "1").toParcelableArray(ParcelableObjectSparseObjectEntry.class);

    assertThat(parcelableArray).hasSize(2);
    assertThat(parcelableArray[0].getKey()).isEqualTo(1);
    assertThat(parcelableArray[0].getValue()).isEqualTo("1");
    assertThat(parcelableArray[1].getKey()).isEqualTo(4);
    assertThat(parcelableArray[1].getValue()).isEqualTo("4");

    final ArrayList<ParcelableObjectSparseObjectEntry<Integer, String>> parcelableList = AndroidCollections
            .iterate(mArray).but().keys(Arrays.asList(1, 2, 3)).reverse().toParcelableList();

    assertThat(parcelableList).hasSize(2);
    assertThat(parcelableList.get(0).getKey()).isEqualTo(4);
    assertThat(parcelableList.get(0).getValue()).isEqualTo("4");
    assertThat(parcelableList.get(1).getKey()).isEqualTo(0);
    assertThat(parcelableList.get(1).getValue()).isEqualTo("0");
    assertThat(parcelableList).containsExactly(SparseEntries.parcelableEntry(Integer.valueOf(4), "4"),
            SparseEntries.parcelableEntry(Integer.valueOf(0), "0"));

    final Bundle bundle = new Bundle();
    bundle.putParcelableArray("array", parcelableArray);
    bundle.putParcelableArrayList("list", parcelableList);

    final Parcel parcel = Parcel.obtain();
    bundle.writeToParcel(parcel, 0);//from w  ww.  j  a va 2s.  c o  m

    parcel.setDataPosition(0);

    final Bundle out = parcel.readBundle();
    out.setClassLoader(AndroidCollections.class.getClassLoader());

    assertThat(out.getParcelableArray("array")).isEqualTo(parcelableArray);
    assertThat(out.getParcelableArrayList("list")).isEqualTo(new ArrayList<Parcelable>(parcelableList));

    final ArrayList<ParcelableObjectSparseObjectEntry<Integer, String>> filledList = new ArrayList<ParcelableObjectSparseObjectEntry<Integer, String>>(
            2);

    AndroidCollections.iterate(mArray).but().keys(1, 2, 3).reverse().fillParcelable(filledList);

    assertThat(filledList).isEqualTo(parcelableList);

    final ParcelableObjectSparseObjectEntry[] filledArray = new ParcelableObjectSparseObjectEntry[2];

    AndroidCollections.iterate(mArray).only().value("2").fillParcelable(filledArray);

    assertThat(filledArray[0]).isEqualTo(SparseEntries.parcelableEntry(Integer.valueOf(2), "2"));

    AndroidCollections.iterate(mArray).only().value("2").fillParcelable(filledArray, 1);

    assertThat(filledArray[0]).isEqualTo(SparseEntries.parcelableEntry(Integer.valueOf(2), "2"));
    assertThat(filledArray[0]).isEqualTo(filledArray[1]);

    try {

        AndroidCollections.iterate(mArray).fillParcelable(filledArray);

        fail();

    } catch (final IndexOutOfBoundsException e) {

    }

    final Parcelable[] parcelables = new Parcelable[2];

    AndroidCollections.iterate(mArray).only().to(1).fillParcelable(parcelables);

    assertThat(parcelables).containsExactly(filledArray);
}

From source file:com.bmd.android.collection.SparseArrayCompatTest.java

public void testParcelable() {

    final ParcelableIntSparseObjectEntry[] parcelableArray = AndroidCollections.iterate(mArray).only()
            .values("4", "1").toParcelableArray(ParcelableIntSparseObjectEntry.class);

    assertThat(parcelableArray).hasSize(2);
    assertThat(parcelableArray[0].getKey()).isEqualTo(1);
    assertThat(parcelableArray[0].getValue()).isEqualTo("1");
    assertThat(parcelableArray[1].getKey()).isEqualTo(4);
    assertThat(parcelableArray[1].getValue()).isEqualTo("4");

    final ArrayList<ParcelableIntSparseObjectEntry<String>> parcelableList = AndroidCollections.iterate(mArray)
            .but().keys(Arrays.asList(1, 2, 3)).reverse().toParcelableList();

    assertThat(parcelableList).hasSize(2);
    assertThat(parcelableList.get(0).getKey()).isEqualTo(4);
    assertThat(parcelableList.get(0).getValue()).isEqualTo("4");
    assertThat(parcelableList.get(1).getKey()).isEqualTo(0);
    assertThat(parcelableList.get(1).getValue()).isEqualTo("0");
    assertThat(parcelableList).containsExactly(SparseEntries.parcelableEntry(4, "4"),
            SparseEntries.parcelableEntry(0, "0"));

    final Bundle bundle = new Bundle();
    bundle.putParcelableArray("array", parcelableArray);
    bundle.putParcelableArrayList("list", parcelableList);

    final Parcel parcel = Parcel.obtain();
    bundle.writeToParcel(parcel, 0);/* ww  w  .j a va 2 s .  c om*/

    parcel.setDataPosition(0);

    final Bundle out = parcel.readBundle();
    out.setClassLoader(AndroidCollections.class.getClassLoader());

    assertThat(out.getParcelableArray("array")).isEqualTo(parcelableArray);
    assertThat(out.getParcelableArrayList("list")).isEqualTo(new ArrayList<Parcelable>(parcelableList));

    final ArrayList<ParcelableIntSparseObjectEntry<String>> filledList = new ArrayList<ParcelableIntSparseObjectEntry<String>>(
            2);

    AndroidCollections.iterate(mArray).but().keys(1, 2, 3).reverse().fillParcelable(filledList);

    assertThat(filledList).isEqualTo(parcelableList);

    final ParcelableIntSparseObjectEntry[] filledArray = new ParcelableIntSparseObjectEntry[2];

    AndroidCollections.iterate(mArray).only().value("2").fillParcelable(filledArray);

    assertThat(filledArray[0]).isEqualTo(SparseEntries.parcelableEntry(2, "2"));

    AndroidCollections.iterate(mArray).only().value("2").fillParcelable(filledArray, 1);

    assertThat(filledArray[0]).isEqualTo(SparseEntries.parcelableEntry(2, "2"));
    assertThat(filledArray[0]).isEqualTo(filledArray[1]);

    try {

        AndroidCollections.iterate(mArray).fillParcelable(filledArray);

        fail();

    } catch (final IndexOutOfBoundsException e) {

    }

    final Parcelable[] parcelables = new Parcelable[2];

    AndroidCollections.iterate(mArray).only().to(1).fillParcelable(parcelables);

    assertThat(parcelables).containsExactly(filledArray);
}

From source file:com.bmd.android.collection.SupportLongSparseArrayTest.java

public void testParcelable() {

    final ParcelableLongSparseObjectEntry[] parcelableArray = AndroidCollections.iterate(mArray).only()
            .values("4", "1").toParcelableArray(ParcelableLongSparseObjectEntry.class);

    assertThat(parcelableArray).hasSize(2);
    assertThat(parcelableArray[0].getKey()).isEqualTo(1);
    assertThat(parcelableArray[0].getValue()).isEqualTo("1");
    assertThat(parcelableArray[1].getKey()).isEqualTo(4);
    assertThat(parcelableArray[1].getValue()).isEqualTo("4");

    final ArrayList<ParcelableLongSparseObjectEntry<String>> parcelableList = AndroidCollections.iterate(mArray)
            .but().keys(Arrays.asList(1L, 2L, 3L)).reverse().toParcelableList();

    assertThat(parcelableList).hasSize(2);
    assertThat(parcelableList.get(0).getKey()).isEqualTo(4);
    assertThat(parcelableList.get(0).getValue()).isEqualTo("4");
    assertThat(parcelableList.get(1).getKey()).isEqualTo(0);
    assertThat(parcelableList.get(1).getValue()).isEqualTo("0");
    assertThat(parcelableList).containsExactly(SparseEntries.parcelableEntry(4L, "4"),
            SparseEntries.parcelableEntry(0L, "0"));

    final Bundle bundle = new Bundle();
    bundle.putParcelableArray("array", parcelableArray);
    bundle.putParcelableArrayList("list", parcelableList);

    final Parcel parcel = Parcel.obtain();
    bundle.writeToParcel(parcel, 0);/* w  w  w .  j a  v a 2 s  .c  om*/

    parcel.setDataPosition(0);

    final Bundle out = parcel.readBundle();
    out.setClassLoader(AndroidCollections.class.getClassLoader());

    assertThat(out.getParcelableArray("array")).isEqualTo(parcelableArray);
    assertThat(out.getParcelableArrayList("list")).isEqualTo(new ArrayList<Parcelable>(parcelableList));

    final ArrayList<ParcelableLongSparseObjectEntry<String>> filledList = new ArrayList<ParcelableLongSparseObjectEntry<String>>(
            2);

    AndroidCollections.iterate(mArray).but().keys(1, 2, 3).reverse().fillParcelable(filledList);

    assertThat(filledList).isEqualTo(parcelableList);

    final ParcelableLongSparseObjectEntry[] filledArray = new ParcelableLongSparseObjectEntry[2];

    AndroidCollections.iterate(mArray).only().value("2").fillParcelable(filledArray);

    assertThat(filledArray[0]).isEqualTo(SparseEntries.parcelableEntry(2L, "2"));

    AndroidCollections.iterate(mArray).only().value("2").fillParcelable(filledArray, 1);

    assertThat(filledArray[0]).isEqualTo(SparseEntries.parcelableEntry(2L, "2"));
    assertThat(filledArray[0]).isEqualTo(filledArray[1]);

    try {

        AndroidCollections.iterate(mArray).fillParcelable(filledArray);

        fail();

    } catch (final IndexOutOfBoundsException e) {

    }

    final Parcelable[] parcelables = new Parcelable[2];

    AndroidCollections.iterate(mArray).only().to(1).fillParcelable(parcelables);

    assertThat(parcelables).containsExactly(filledArray);
}

From source file:com.bmd.android.collection.LongSparseArrayTest.java

public void testParcelable() {

    if (VERSION.SDK_INT < VERSION_CODES.JELLY_BEAN) {

        return;/*w w w. java 2  s  .c o m*/
    }

    final ParcelableLongSparseObjectEntry[] parcelableArray = AndroidCollections.iterate(mArray).only()
            .values("4", "1").toParcelableArray(ParcelableLongSparseObjectEntry.class);

    assertThat(parcelableArray).hasSize(2);
    assertThat(parcelableArray[0].getKey()).isEqualTo(1);
    assertThat(parcelableArray[0].getValue()).isEqualTo("1");
    assertThat(parcelableArray[1].getKey()).isEqualTo(4);
    assertThat(parcelableArray[1].getValue()).isEqualTo("4");

    final ArrayList<ParcelableLongSparseObjectEntry<String>> parcelableList = AndroidCollections.iterate(mArray)
            .but().keys(Arrays.asList(1L, 2L, 3L)).reverse().toParcelableList();

    assertThat(parcelableList).hasSize(2);
    assertThat(parcelableList.get(0).getKey()).isEqualTo(4);
    assertThat(parcelableList.get(0).getValue()).isEqualTo("4");
    assertThat(parcelableList.get(1).getKey()).isEqualTo(0);
    assertThat(parcelableList.get(1).getValue()).isEqualTo("0");
    assertThat(parcelableList).containsExactly(SparseEntries.parcelableEntry(4L, "4"),
            SparseEntries.parcelableEntry(0L, "0"));

    final Bundle bundle = new Bundle();
    bundle.putParcelableArray("array", parcelableArray);
    bundle.putParcelableArrayList("list", parcelableList);

    final Parcel parcel = Parcel.obtain();
    bundle.writeToParcel(parcel, 0);

    parcel.setDataPosition(0);

    final Bundle out = parcel.readBundle();
    out.setClassLoader(AndroidCollections.class.getClassLoader());

    assertThat(out.getParcelableArray("array")).isEqualTo(parcelableArray);
    assertThat(out.getParcelableArrayList("list")).isEqualTo(new ArrayList<Parcelable>(parcelableList));

    final ArrayList<ParcelableLongSparseObjectEntry<String>> filledList = new ArrayList<ParcelableLongSparseObjectEntry<String>>(
            2);

    AndroidCollections.iterate(mArray).but().keys(1, 2, 3).reverse().fillParcelable(filledList);

    assertThat(filledList).isEqualTo(parcelableList);

    final ParcelableLongSparseObjectEntry[] filledArray = new ParcelableLongSparseObjectEntry[2];

    AndroidCollections.iterate(mArray).only().value("2").fillParcelable(filledArray);

    assertThat(filledArray[0]).isEqualTo(SparseEntries.parcelableEntry(2L, "2"));

    AndroidCollections.iterate(mArray).only().value("2").fillParcelable(filledArray, 1);

    assertThat(filledArray[0]).isEqualTo(SparseEntries.parcelableEntry(2L, "2"));
    assertThat(filledArray[0]).isEqualTo(filledArray[1]);

    try {

        AndroidCollections.iterate(mArray).fillParcelable(filledArray);

        fail();

    } catch (final IndexOutOfBoundsException e) {

    }

    final Parcelable[] parcelables = new Parcelable[2];

    AndroidCollections.iterate(mArray).only().to(1).fillParcelable(parcelables);

    assertThat(parcelables).containsExactly(filledArray);
}

From source file:edu.umich.flowfence.common.QMDetails.java

@Override
public void writeToParcel(Parcel dest, int flags) {
    // This space will be filled in with the final size of the object, minus this header.
    final int headerPos = dest.dataPosition();
    dest.writeInt(0);//from w  ww . j a  v a  2  s  . co  m
    final int startPos = dest.dataPosition();
    // Special handling for completely null objects - leave length at zero.
    if (ObjectUtils.firstNonNull(descriptor, resultType, paramInfo, requiredTaints, optionalTaints) == null) {
        if (localLOGV) {
            Log.v(TAG, "Null case, writing size 0");
        }
        return;
    }
    QMDescriptor.writeToParcel(descriptor, dest, flags);
    dest.writeString(resultType);
    dest.writeTypedList(paramInfo);
    TaintSet.writeToParcel(requiredTaints, dest, flags);
    TaintSet.writeToParcel(optionalTaints, dest, flags);

    final int endPos = dest.dataPosition();
    dest.setDataPosition(headerPos);
    dest.writeInt(endPos - startPos);
    dest.setDataPosition(endPos);
    if (localLOGV) {
        Log.v(TAG, "Total size: " + (endPos - startPos));
    }
}

From source file:edu.umich.oasis.common.SodaDetails.java

@Override
public void writeToParcel(Parcel dest, int flags) {
    // This space will be filled in with the final size of the object, minus this header.
    final int headerPos = dest.dataPosition();
    dest.writeInt(0);/* w w w . j  av  a2s . co m*/
    final int startPos = dest.dataPosition();
    // Special handling for completely null objects - leave length at zero.
    if (ObjectUtils.firstNonNull(descriptor, resultType, paramInfo, requiredTaints, optionalTaints) == null) {
        if (localLOGV) {
            Log.v(TAG, "Null case, writing size 0");
        }
        return;
    }
    SodaDescriptor.writeToParcel(descriptor, dest, flags);
    dest.writeString(resultType);
    dest.writeTypedList(paramInfo);
    TaintSet.writeToParcel(requiredTaints, dest, flags);
    TaintSet.writeToParcel(optionalTaints, dest, flags);

    final int endPos = dest.dataPosition();
    dest.setDataPosition(headerPos);
    dest.writeInt(endPos - startPos);
    dest.setDataPosition(endPos);
    if (localLOGV) {
        Log.v(TAG, "Total size: " + (endPos - startPos));
    }
}

From source file:com.google.android.gms.internal.fv.java

public static fv m1980e(byte[] bArr) {
    Parcel obtain = Parcel.obtain();
    obtain.unmarshall(bArr, 0, bArr.length);
    obtain.setDataPosition(0);
    fv D = CREATOR.m622D(obtain);/*  ww w.j av  a  2s  .  c o m*/
    obtain.recycle();
    return D;
}

From source file:com.google.android.apps.dashclock.api.ExtensionData.java

@Override
public void writeToParcel(Parcel parcel, int i) {
    /**/*from  w w  w. j  a  v a  2s  .c om*/
     * NOTE: When adding fields in the process of updating this API, make sure to bump
     * {@link #PARCELABLE_VERSION}.
     */
    parcel.writeInt(PARCELABLE_VERSION);
    // Inject a placeholder that will store the parcel size from this point on
    // (not including the size itself).
    int sizePosition = parcel.dataPosition();
    parcel.writeInt(0);
    int startPosition = parcel.dataPosition();
    // Version 1 below
    parcel.writeInt(mVisible ? 1 : 0);
    parcel.writeInt(mIcon);
    parcel.writeString(TextUtils.isEmpty(mStatus) ? "" : mStatus);
    parcel.writeString(TextUtils.isEmpty(mExpandedTitle) ? "" : mExpandedTitle);
    parcel.writeString(TextUtils.isEmpty(mExpandedBody) ? "" : mExpandedBody);
    parcel.writeString((mClickIntent == null) ? "" : mClickIntent.toUri(0));
    // Version 2 below
    parcel.writeString(TextUtils.isEmpty(mContentDescription) ? "" : mContentDescription);
    parcel.writeString(mIconUri == null ? "" : mIconUri.toString());
    // Go back and write the size
    int parcelableSize = parcel.dataPosition() - startPosition;
    parcel.setDataPosition(sizePosition);
    parcel.writeInt(parcelableSize);
    parcel.setDataPosition(startPosition + parcelableSize);
}

From source file:edu.umich.flowfence.common.QMDetails.java

public void readFromParcel(Parcel source) {
    final int length = source.readInt();
    final int endPos = source.dataPosition() + length;

    if (localLOGV) {
        Log.v(TAG, "Unparceling, length " + length);
    }/*from w w  w  . j a  v a 2s .c  o  m*/

    if (source.dataPosition() < endPos) {
        descriptor = QMDescriptor.readFromParcel(source);
        if (localLOGV) {
            Log.v(TAG, "Descriptor: " + descriptor);
        }
    }
    if (source.dataPosition() < endPos) {
        resultType = source.readString();
        if (localLOGV) {
            Log.v(TAG, "Result type: " + resultType);
        }
    }
    if (source.dataPosition() < endPos) {
        paramInfo = source.createTypedArrayList(ParamInfo.CREATOR);
        if (paramInfo != null) {
            if (localLOGV) {
                Log.v(TAG, "Param info (size " + paramInfo.size() + "):");
                for (ParamInfo pi : paramInfo) {
                    Log.v(TAG, "    " + pi);
                }
            }
            paramInfo = Collections.unmodifiableList(paramInfo);
        } else if (localLOGV) {
            Log.v(TAG, "Param info (null)");
        }
    }
    if (source.dataPosition() < endPos) {
        requiredTaints = TaintSet.readFromParcel(source);
        if (localLOGV) {
            Log.v(TAG, "Required taints: " + requiredTaints);
        }
    }
    if (source.dataPosition() < endPos) {
        optionalTaints = TaintSet.readFromParcel(source);
        if (localLOGV) {
            Log.v(TAG, "Optional taints: " + optionalTaints);
        }
    }
    if (source.dataPosition() < endPos) {
        if (localLOGD) {
            Log.d(TAG, "Excess data at end of parcel");
        }
        source.setDataPosition(endPos);
    }
}

From source file:edu.umich.oasis.common.SodaDetails.java

public void readFromParcel(Parcel source) {
    final int length = source.readInt();
    final int endPos = source.dataPosition() + length;

    if (localLOGV) {
        Log.v(TAG, "Unparceling, length " + length);
    }/*from w  w w  . j a v a  2 s.co  m*/

    if (source.dataPosition() < endPos) {
        descriptor = SodaDescriptor.readFromParcel(source);
        if (localLOGV) {
            Log.v(TAG, "Descriptor: " + descriptor);
        }
    }
    if (source.dataPosition() < endPos) {
        resultType = source.readString();
        if (localLOGV) {
            Log.v(TAG, "Result type: " + resultType);
        }
    }
    if (source.dataPosition() < endPos) {
        paramInfo = source.createTypedArrayList(ParamInfo.CREATOR);
        if (paramInfo != null) {
            if (localLOGV) {
                Log.v(TAG, "Param info (size " + paramInfo.size() + "):");
                for (ParamInfo pi : paramInfo) {
                    Log.v(TAG, "    " + pi);
                }
            }
            paramInfo = Collections.unmodifiableList(paramInfo);
        } else if (localLOGV) {
            Log.v(TAG, "Param info (null)");
        }
    }
    if (source.dataPosition() < endPos) {
        requiredTaints = TaintSet.readFromParcel(source);
        if (localLOGV) {
            Log.v(TAG, "Required taints: " + requiredTaints);
        }
    }
    if (source.dataPosition() < endPos) {
        optionalTaints = TaintSet.readFromParcel(source);
        if (localLOGV) {
            Log.v(TAG, "Optional taints: " + optionalTaints);
        }
    }
    if (source.dataPosition() < endPos) {
        if (localLOGD) {
            Log.d(TAG, "Excess data at end of parcel");
        }
        source.setDataPosition(endPos);
    }
}