Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 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 | |
| 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 | |
| 44 | protected void finalize() throws Throwable { |
| 45 | try { |
| 46 | dtor(mBackupReader); |
| 47 | } finally { |
| 48 | super.finalize(); |
| 49 | } |
| 50 | } |
| 51 | |
| 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 | |
| 69 | public String getKey() { |
| 70 | if (mHeaderReady) { |
| 71 | return mHeader.key; |
| 72 | } else { |
| 73 | throw new IllegalStateException("mHeaderReady=false"); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | public int getDataSize() { |
| 78 | if (mHeaderReady) { |
| 79 | return mHeader.dataSize; |
| 80 | } else { |
| 81 | throw new IllegalStateException("mHeaderReady=false"); |
| 82 | } |
| 83 | } |
| 84 | |
Joe Onorato | 5f15d15 | 2009-06-16 16:31:35 -0400 | [diff] [blame] | 85 | public int readEntityData(byte[] data, int offset, int size) throws IOException { |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 86 | if (mHeaderReady) { |
Joe Onorato | 5f15d15 | 2009-06-16 16:31:35 -0400 | [diff] [blame] | 87 | int result = readEntityData_native(mBackupReader, data, offset, size); |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 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 | |
Joe Onorato | 5f15d15 | 2009-06-16 16:31:35 -0400 | [diff] [blame] | 98 | public void skipEntityData() throws IOException { |
| 99 | if (mHeaderReady) { |
| 100 | int result = skipEntityData_native(mBackupReader); |
| 101 | if (result >= 0) { |
| 102 | return; |
| 103 | } else { |
| 104 | throw new IOException("result=0x" + Integer.toHexString(result)); |
| 105 | } |
| 106 | } else { |
| 107 | throw new IllegalStateException("mHeaderReady=false"); |
| 108 | } |
| 109 | } |
| 110 | |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 111 | private native static int ctor(FileDescriptor fd); |
| 112 | private native static void dtor(int mBackupReader); |
| 113 | |
| 114 | private native int readNextHeader_native(int mBackupReader, EntityHeader entity); |
Joe Onorato | 5f15d15 | 2009-06-16 16:31:35 -0400 | [diff] [blame] | 115 | private native int readEntityData_native(int mBackupReader, byte[] data, int offset, int size); |
| 116 | private native int skipEntityData_native(int mBackupReader); |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 117 | } |