1 /* 2 * Copyright (C) 2009 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.backup; 18 19 import android.content.Context; 20 21 import java.io.FileDescriptor; 22 import java.io.IOException; 23 24 /** @hide */ 25 public class BackupDataInput { 26 int mBackupReader; 27 28 private EntityHeader mHeader = new EntityHeader(); 29 private boolean mHeaderReady; 30 31 private static class EntityHeader { 32 String key; 33 int dataSize; 34 } 35 BackupDataInput(FileDescriptor fd)36 public BackupDataInput(FileDescriptor fd) { 37 if (fd == null) throw new NullPointerException(); 38 mBackupReader = ctor(fd); 39 if (mBackupReader == 0) { 40 throw new RuntimeException("Native initialization failed with fd=" + fd); 41 } 42 } 43 finalize()44 protected void finalize() throws Throwable { 45 try { 46 dtor(mBackupReader); 47 } finally { 48 super.finalize(); 49 } 50 } 51 readNextHeader()52 public boolean readNextHeader() throws IOException { 53 int result = readNextHeader_native(mBackupReader, mHeader); 54 if (result == 0) { 55 // read successfully 56 mHeaderReady = true; 57 return true; 58 } else if (result > 0) { 59 // done 60 mHeaderReady = false; 61 return false; 62 } else { 63 // error 64 mHeaderReady = false; 65 throw new IOException("result=0x" + Integer.toHexString(result)); 66 } 67 } 68 getKey()69 public String getKey() { 70 if (mHeaderReady) { 71 return mHeader.key; 72 } else { 73 throw new IllegalStateException("mHeaderReady=false"); 74 } 75 } 76 getDataSize()77 public int getDataSize() { 78 if (mHeaderReady) { 79 return mHeader.dataSize; 80 } else { 81 throw new IllegalStateException("mHeaderReady=false"); 82 } 83 } 84 readEntityData(byte[] data, int offset, int size)85 public int readEntityData(byte[] data, int offset, int size) throws IOException { 86 if (mHeaderReady) { 87 int result = readEntityData_native(mBackupReader, data, offset, size); 88 if (result >= 0) { 89 return result; 90 } else { 91 throw new IOException("result=0x" + Integer.toHexString(result)); 92 } 93 } else { 94 throw new IllegalStateException("mHeaderReady=false"); 95 } 96 } 97 skipEntityData()98 public void skipEntityData() throws IOException { 99 if (mHeaderReady) { 100 skipEntityData_native(mBackupReader); 101 } else { 102 throw new IllegalStateException("mHeaderReady=false"); 103 } 104 } 105 ctor(FileDescriptor fd)106 private native static int ctor(FileDescriptor fd); dtor(int mBackupReader)107 private native static void dtor(int mBackupReader); 108 readNextHeader_native(int mBackupReader, EntityHeader entity)109 private native int readNextHeader_native(int mBackupReader, EntityHeader entity); readEntityData_native(int mBackupReader, byte[] data, int offset, int size)110 private native int readEntityData_native(int mBackupReader, byte[] data, int offset, int size); skipEntityData_native(int mBackupReader)111 private native int skipEntityData_native(int mBackupReader); 112 } 113