15f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato/* 25f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato * Copyright (C) 2009 The Android Open Source Project 35f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato * 45f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato * Licensed under the Apache License, Version 2.0 (the "License"); 55f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato * you may not use this file except in compliance with the License. 65f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato * You may obtain a copy of the License at 75f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato * 85f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato * http://www.apache.org/licenses/LICENSE-2.0 95f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato * 105f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato * Unless required by applicable law or agreed to in writing, software 115f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato * distributed under the License is distributed on an "AS IS" BASIS, 125f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 135f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato * See the License for the specific language governing permissions and 145f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato * limitations under the License. 155f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato */ 165f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato 174528186e0d65fc68ef0dd1941aa2ac8aefcd55a3Christopher Tatepackage android.app.backup; 185f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato 195f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onoratoimport java.io.InputStream; 205f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onoratoimport java.io.IOException; 215f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato 22e28290e21f908b4e917099ff2aa41e3aab9310c2Christopher Tate/** 23d17da43c82c4edb97514d6138bc208eeba321636Scott Main * Provides an {@link java.io.InputStream}-like interface for accessing an 24d17da43c82c4edb97514d6138bc208eeba321636Scott Main * entity's data during a restore operation. Used by {@link BackupHelper} classes within the {@link 25d17da43c82c4edb97514d6138bc208eeba321636Scott Main * BackupAgentHelper} mechanism. 267cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate * <p> 27d17da43c82c4edb97514d6138bc208eeba321636Scott Main * When {@link BackupHelper#restoreEntity(BackupDataInputStream) BackupHelper.restoreEntity()} 287cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate * is called, the current entity's header has already been read from the underlying 297cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate * {@link BackupDataInput}. The entity's key string and total data size are available 307cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate * through this class's {@link #getKey()} and {@link #size()} methods, respectively. 317cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate * <p class="note"> 32d17da43c82c4edb97514d6138bc208eeba321636Scott Main * <strong>Note:</strong> The caller should take care not to seek or close the underlying data 33d17da43c82c4edb97514d6138bc208eeba321636Scott Main * source, nor read more than {@link #size()} bytes from the stream.</p> 347cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate * 357cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate * @see BackupAgentHelper 367cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate * @see BackupHelper 377cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate */ 385f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onoratopublic class BackupDataInputStream extends InputStream { 395f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato 405f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato String key; 415f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato int dataSize; 425f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato 435f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato BackupDataInput mData; 445f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato byte[] mOneByte; 455f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato 46e28290e21f908b4e917099ff2aa41e3aab9310c2Christopher Tate /** @hide */ 475f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato BackupDataInputStream(BackupDataInput data) { 485f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato mData = data; 495f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato } 505f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato 517cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate /** 527cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate * Read one byte of entity data from the stream, returning it as 537cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate * an integer value. If more than {@link #size()} bytes of data 547cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate * are read from the stream, the output of this method is undefined. 557cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate * 567cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate * @return The byte read, or undefined if the end of the stream has been reached. 577cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate */ 585f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato public int read() throws IOException { 595f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato byte[] one = mOneByte; 605f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato if (mOneByte == null) { 615f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato one = mOneByte = new byte[1]; 625f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato } 635f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato mData.readEntityData(one, 0, 1); 645f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato return one[0]; 655f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato } 665f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato 677cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate /** 687cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate * Read up to {@code size} bytes of data into a byte array, beginning at position 697cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate * {@code offset} within the array. 707cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate * 717cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate * @param b Byte array into which the data will be read 727cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate * @param offset The data will be stored in {@code b} beginning at this index 737cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate * within the array. 747cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate * @param size The number of bytes to read in this operation. If insufficient 757cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate * data exists within the entity to fulfill this request, only as much data 767cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate * will be read as is available. 777cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate * @return The number of bytes of data read, or zero if all of the entity's 787cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate * data has already been read. 797cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate */ 805f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato public int read(byte[] b, int offset, int size) throws IOException { 815f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato return mData.readEntityData(b, offset, size); 825f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato } 835f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato 847cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate /** 857cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate * Read enough entity data into a byte array to fill the array. 867cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate * 877cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate * @param b Byte array to fill with data from the stream. If the stream does not 887cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate * have sufficient data to fill the array, then the contents of the remainder of 897cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate * the array will be undefined. 907cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate * @return The number of bytes of data read, or zero if all of the entity's 917cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate * data has already been read. 927cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate */ 935f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato public int read(byte[] b) throws IOException { 945f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato return mData.readEntityData(b, 0, b.length); 955f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato } 965f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato 977cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate /** 987cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate * Report the key string associated with this entity within the backup data set. 997cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate * 1007cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate * @return The key string for this entity, equivalent to calling 1017cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate * {@link BackupDataInput#getKey()} on the underlying {@link BackupDataInput}. 1027cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate */ 1035f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato public String getKey() { 1045f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato return this.key; 1055f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato } 1067cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate 1077cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate /** 1087cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate * Report the total number of bytes of data available for the current entity. 1097cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate * 1107cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate * @return The number of data bytes available, equivalent to calling 1117cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate * {@link BackupDataInput#getDataSize()} on the underlying {@link BackupDataInput}. 1127cc088232d20a63d24dd1f79f529ddad1b792cdeChristopher Tate */ 1135f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato public int size() { 1145f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato return this.dataSize; 1155f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato } 1165f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato} 1175f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato 1185f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato 119