1/*
2 * Copyright (C) 2017 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
18package com.android.settings.intelligence.search;
19
20import android.os.Parcel;
21import android.os.Parcelable;
22
23/**
24 * Utility class to Marshall and Unmarshall the payloads stored in the SQLite Database
25 */
26public class ResultPayloadUtils {
27
28    private static final String TAG = "PayloadUtil";
29
30    public static byte[] marshall(ResultPayload payload) {
31        Parcel parcel = Parcel.obtain();
32        payload.writeToParcel(parcel, 0);
33        byte[] bytes = parcel.marshall();
34        parcel.recycle();
35        return bytes;
36    }
37
38    public static <T> T unmarshall(byte[] bytes, Parcelable.Creator<T> creator) {
39        T result;
40        Parcel parcel = unmarshall(bytes);
41        result = creator.createFromParcel(parcel);
42        parcel.recycle();
43        return result;
44    }
45
46    private static Parcel unmarshall(byte[] bytes) {
47        Parcel parcel = Parcel.obtain();
48        parcel.unmarshall(bytes, 0, bytes.length);
49        parcel.setDataPosition(0);
50        return parcel;
51    }
52}
53