1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.os;
18 
19 import android.util.MathUtils;
20 
21 /**
22  * Parcelable containing a raw Parcel of data.
23  * @hide
24  */
25 public class ParcelableParcel implements Parcelable {
26     final Parcel mParcel;
27     final ClassLoader mClassLoader;
28 
ParcelableParcel(ClassLoader loader)29     public ParcelableParcel(ClassLoader loader) {
30         mParcel = Parcel.obtain();
31         mClassLoader = loader;
32     }
33 
ParcelableParcel(Parcel src, ClassLoader loader)34     public ParcelableParcel(Parcel src, ClassLoader loader) {
35         mParcel = Parcel.obtain();
36         mClassLoader = loader;
37         int size = src.readInt();
38         if (size < 0) {
39             throw new IllegalArgumentException("Negative size read from parcel");
40         }
41 
42         int pos = src.dataPosition();
43         src.setDataPosition(MathUtils.addOrThrow(pos, size));
44         mParcel.appendFrom(src, pos, size);
45     }
46 
getParcel()47     public Parcel getParcel() {
48         mParcel.setDataPosition(0);
49         return mParcel;
50     }
51 
getClassLoader()52     public ClassLoader getClassLoader() {
53         return mClassLoader;
54     }
55 
56     @Override
describeContents()57     public int describeContents() {
58         return 0;
59     }
60 
61     @Override
writeToParcel(Parcel dest, int flags)62     public void writeToParcel(Parcel dest, int flags) {
63         dest.writeInt(mParcel.dataSize());
64         dest.appendFrom(mParcel, 0, mParcel.dataSize());
65     }
66 
67     public static final Parcelable.ClassLoaderCreator<ParcelableParcel> CREATOR
68             = new Parcelable.ClassLoaderCreator<ParcelableParcel>() {
69         public ParcelableParcel createFromParcel(Parcel in) {
70             return new ParcelableParcel(in, null);
71         }
72 
73         public ParcelableParcel createFromParcel(Parcel in, ClassLoader loader) {
74             return new ParcelableParcel(in, loader);
75         }
76 
77         public ParcelableParcel[] newArray(int size) {
78             return new ParcelableParcel[size];
79         }
80     };
81 }
82