BackupDataInput.java revision 5f15d151b5101fadfe6cba1e8f4aa6367e8c603e
11cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato/*
21cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato * Copyright (C) 2009 The Android Open Source Project
31cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato *
41cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato * Licensed under the Apache License, Version 2.0 (the "License");
51cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato * you may not use this file except in compliance with the License.
61cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato * You may obtain a copy of the License at
71cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato *
81cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato *      http://www.apache.org/licenses/LICENSE-2.0
91cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato *
101cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato * Unless required by applicable law or agreed to in writing, software
111cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato * distributed under the License is distributed on an "AS IS" BASIS,
121cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato * See the License for the specific language governing permissions and
141cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato * limitations under the License.
151cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato */
161cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
171cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onoratopackage android.backup;
181cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
191cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onoratoimport android.content.Context;
201cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
211cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onoratoimport java.io.FileDescriptor;
221cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onoratoimport java.io.IOException;
231cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
241cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato/** @hide */
251cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onoratopublic class BackupDataInput {
261cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    int mBackupReader;
271cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
281cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    private EntityHeader mHeader = new EntityHeader();
291cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    private boolean mHeaderReady;
301cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
311cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    private static class EntityHeader {
321cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato        String key;
331cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato        int dataSize;
341cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    }
351cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
361cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    public BackupDataInput(FileDescriptor fd) {
371cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato        if (fd == null) throw new NullPointerException();
381cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato        mBackupReader = ctor(fd);
391cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato        if (mBackupReader == 0) {
401cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato            throw new RuntimeException("Native initialization failed with fd=" + fd);
411cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato        }
421cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    }
431cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
441cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    protected void finalize() throws Throwable {
451cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato        try {
461cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato            dtor(mBackupReader);
471cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato        } finally {
481cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato            super.finalize();
491cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato        }
501cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    }
511cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
521cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    public boolean readNextHeader() throws IOException {
531cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato        int result = readNextHeader_native(mBackupReader, mHeader);
541cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato        if (result == 0) {
551cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato            // read successfully
561cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato            mHeaderReady = true;
571cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato            return true;
581cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato        } else if (result > 0) {
591cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato            // done
601cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato            mHeaderReady = false;
611cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato            return false;
621cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato        } else {
631cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato            // error
641cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato            mHeaderReady = false;
651cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato            throw new IOException("result=0x" + Integer.toHexString(result));
661cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato        }
671cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    }
681cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
691cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    public String getKey() {
701cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato        if (mHeaderReady) {
711cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato            return mHeader.key;
721cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato        } else {
731cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato            throw new IllegalStateException("mHeaderReady=false");
741cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato        }
751cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    }
761cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
771cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    public int getDataSize() {
781cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato        if (mHeaderReady) {
791cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato            return mHeader.dataSize;
801cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato        } else {
811cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato            throw new IllegalStateException("mHeaderReady=false");
821cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato        }
831cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    }
841cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
855f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato    public int readEntityData(byte[] data, int offset, int size) throws IOException {
861cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato        if (mHeaderReady) {
875f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato            int result = readEntityData_native(mBackupReader, data, offset, size);
881cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato            if (result >= 0) {
891cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato                return result;
901cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato            } else {
911cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato                throw new IOException("result=0x" + Integer.toHexString(result));
921cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato            }
931cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato        } else {
941cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato            throw new IllegalStateException("mHeaderReady=false");
951cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato        }
961cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    }
971cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
985f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato    public void skipEntityData() throws IOException {
995f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato        if (mHeaderReady) {
1005f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato            int result = skipEntityData_native(mBackupReader);
1015f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato            if (result >= 0) {
1025f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato                return;
1035f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato            } else {
1045f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato                throw new IOException("result=0x" + Integer.toHexString(result));
1055f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato            }
1065f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato        } else {
1075f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato            throw new IllegalStateException("mHeaderReady=false");
1085f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato        }
1095f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato    }
1105f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato
1111cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    private native static int ctor(FileDescriptor fd);
1121cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    private native static void dtor(int mBackupReader);
1131cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato
1141cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato    private native int readNextHeader_native(int mBackupReader, EntityHeader entity);
1155f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato    private native int readEntityData_native(int mBackupReader, byte[] data, int offset, int size);
1165f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato    private native int skipEntityData_native(int mBackupReader);
1171cf587496fcb1d652bab9fc6792fb106b6fefaa4Joe Onorato}
118