1 /*
2  * Copyright (C) 2008 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.util;
18 
19 import java.io.DataInputStream;
20 import java.io.DataOutputStream;
21 import java.io.IOException;
22 
23 /**
24  * Utility methods for Backup/Restore
25  * @hide
26  */
27 // Exported to Mainline modules; cannot use annotations
28 // @android.ravenwood.annotation.RavenwoodKeepWholeClass
29 public class BackupUtils {
30 
31     public static final int NULL = 0;
32     public static final int NOT_NULL = 1;
33 
34     /**
35      * Thrown when there is a backup version mismatch
36      * between the data received and what the system can handle
37      */
38     public static class BadVersionException extends Exception {
BadVersionException(String message)39         public BadVersionException(String message) {
40             super(message);
41         }
42 
BadVersionException(String message, Throwable throwable)43         public BadVersionException(String message, Throwable throwable) {
44             super(message, throwable);
45         }
46     }
47 
readString(DataInputStream in)48     public static String readString(DataInputStream in) throws IOException {
49         return (in.readByte() == NOT_NULL) ? in.readUTF() : null;
50     }
51 
writeString(DataOutputStream out, String val)52     public static void writeString(DataOutputStream out, String val) throws IOException {
53         if (val != null) {
54             out.writeByte(NOT_NULL);
55             out.writeUTF(val);
56         } else {
57             out.writeByte(NULL);
58         }
59     }
60 }