• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 com.android.ondevicepersonalization.services.federatedcompute;
18 
19 import android.annotation.NonNull;
20 
21 import java.io.ByteArrayInputStream;
22 import java.io.ByteArrayOutputStream;
23 import java.io.IOException;
24 import java.io.ObjectInputStream;
25 import java.io.ObjectOutputStream;
26 import java.io.Serializable;
27 
28 /** ContextData object to pass to federatedcompute. */
29 class ContextData implements Serializable {
30     @NonNull private final String mPackageName;
31 
32     @NonNull private final String mClassName;
33 
ContextData(@onNull String packageName, @NonNull String className)34     ContextData(@NonNull String packageName, @NonNull String className) {
35         this.mPackageName = packageName;
36         this.mClassName = className;
37     }
38 
39     /** Converts the given ContextData into a serialized byte[] */
toByteArray(ContextData contextData)40     static byte[] toByteArray(ContextData contextData) throws IOException {
41         try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
42              ObjectOutputStream objectOutputStream = new ObjectOutputStream(
43                      byteArrayOutputStream)) {
44             objectOutputStream.writeObject(contextData);
45             return byteArrayOutputStream.toByteArray();
46         }
47     }
48 
49     /** Converts the given serialized byte[] into a ContextData object */
fromByteArray(byte[] arr)50     static ContextData fromByteArray(byte[] arr) throws IOException, ClassNotFoundException {
51         try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(arr);
52              ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream)) {
53             return (ContextData) objectInputStream.readObject();
54         }
55     }
56 
57     @NonNull
getPackageName()58     String getPackageName() {
59         return mPackageName;
60     }
61 
62     @NonNull
getClassName()63     String getClassName() {
64         return mClassName;
65     }
66 }
67