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 com.android.documentsui.base;
18 
19 import static com.android.documentsui.base.SharedMinimal.TAG;
20 
21 import android.os.BadParcelableException;
22 import android.os.Parcel;
23 import android.util.Log;
24 
25 import java.io.ByteArrayInputStream;
26 import java.io.ByteArrayOutputStream;
27 import java.io.DataInputStream;
28 import java.io.DataOutputStream;
29 import java.io.IOException;
30 
31 public class DurableUtils {
writeToArray(D d)32     public static <D extends Durable> byte[] writeToArray(D d) throws IOException {
33         final ByteArrayOutputStream out = new ByteArrayOutputStream();
34         d.write(new DataOutputStream(out));
35         return out.toByteArray();
36     }
37 
readFromArray(byte[] data, D d)38     public static <D extends Durable> D readFromArray(byte[] data, D d) throws IOException {
39         if (data == null) throw new IOException("Missing data");
40         final ByteArrayInputStream in = new ByteArrayInputStream(data);
41         d.reset();
42         try {
43             d.read(new DataInputStream(in));
44         } catch (IOException e) {
45             d.reset();
46             throw e;
47         }
48         return d;
49     }
50 
writeToArrayOrNull(D d)51     public static <D extends Durable> byte[] writeToArrayOrNull(D d) {
52         try {
53             return writeToArray(d);
54         } catch (IOException e) {
55             Log.w(TAG, "Failed to write", e);
56             return null;
57         }
58     }
59 
readFromArrayOrNull(byte[] data, D d)60     public static <D extends Durable> D readFromArrayOrNull(byte[] data, D d) {
61         try {
62             return readFromArray(data, d);
63         } catch (IOException e) {
64             Log.w(TAG, "Failed to read", e);
65             return null;
66         }
67     }
68 
writeToParcel(Parcel parcel, D d)69     public static <D extends Durable> void writeToParcel(Parcel parcel, D d) {
70         try {
71             parcel.writeByteArray(writeToArray(d));
72         } catch (IOException e) {
73             throw new BadParcelableException(e);
74         }
75     }
76 
readFromParcel(Parcel parcel, D d)77     public static <D extends Durable> D readFromParcel(Parcel parcel, D d) {
78         try {
79             return readFromArray(parcel.createByteArray(), d);
80         } catch (IOException e) {
81             throw new BadParcelableException(e);
82         }
83     }
84 
writeNullableString(DataOutputStream out, String value)85     public static void writeNullableString(DataOutputStream out, String value) throws IOException {
86         if (value != null) {
87             out.write(1);
88             out.writeUTF(value);
89         } else {
90             out.write(0);
91         }
92     }
93 
readNullableString(DataInputStream in)94     public static String readNullableString(DataInputStream in) throws IOException {
95         if (in.read() != 0) {
96             return in.readUTF();
97         } else {
98             return null;
99         }
100     }
101 }
102