1 /* 2 * Copyright (C) 2010 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 /** 18 * @addtogroup Asset 19 * @{ 20 */ 21 22 /** 23 * @file asset_manager.h 24 */ 25 26 #ifndef ANDROID_ASSET_MANAGER_H 27 #define ANDROID_ASSET_MANAGER_H 28 29 #include <sys/cdefs.h> 30 #include <sys/types.h> 31 32 #ifdef __cplusplus 33 extern "C" { 34 #endif 35 36 struct AAssetManager; 37 /** 38 * {@link AAssetManager} provides access to an application's raw assets by 39 * creating {@link AAsset} objects. 40 * 41 * AAssetManager is a wrapper to the low-level native implementation 42 * of the java {@link AAssetManager}, a pointer can be obtained using 43 * AAssetManager_fromJava(). 44 * 45 * The asset hierarchy may be examined like a filesystem, using 46 * {@link AAssetDir} objects to peruse a single directory. 47 * 48 * A native {@link AAssetManager} pointer may be shared across multiple threads. 49 */ 50 typedef struct AAssetManager AAssetManager; 51 52 struct AAssetDir; 53 /** 54 * {@link AAssetDir} provides access to a chunk of the asset hierarchy as if 55 * it were a single directory. The contents are populated by the 56 * {@link AAssetManager}. 57 * 58 * The list of files will be sorted in ascending order by ASCII value. 59 */ 60 typedef struct AAssetDir AAssetDir; 61 62 struct AAsset; 63 /** 64 * {@link AAsset} provides access to a read-only asset. 65 * 66 * {@link AAsset} objects are NOT thread-safe, and should not be shared across 67 * threads. 68 */ 69 typedef struct AAsset AAsset; 70 71 /** Available access modes for opening assets with {@link AAssetManager_open} */ 72 enum { 73 /** No specific information about how data will be accessed. **/ 74 AASSET_MODE_UNKNOWN = 0, 75 /** Read chunks, and seek forward and backward. */ 76 AASSET_MODE_RANDOM = 1, 77 /** Read sequentially, with an occasional forward seek. */ 78 AASSET_MODE_STREAMING = 2, 79 /** Caller plans to ask for a read-only buffer with all data. */ 80 AASSET_MODE_BUFFER = 3 81 }; 82 83 84 /** 85 * Open the named directory within the asset hierarchy. The directory can then 86 * be inspected with the AAssetDir functions. To open the top-level directory, 87 * pass in "" as the dirName. 88 * 89 * The object returned here should be freed by calling AAssetDir_close(). 90 */ 91 AAssetDir* AAssetManager_openDir(AAssetManager* mgr, const char* dirName); 92 93 /** 94 * Open an asset. 95 * 96 * The object returned here should be freed by calling AAsset_close(). 97 */ 98 AAsset* AAssetManager_open(AAssetManager* mgr, const char* filename, int mode); 99 100 /** 101 * Iterate over the files in an asset directory. A NULL string is returned 102 * when all the file names have been returned. 103 * 104 * The returned file name is suitable for passing to AAssetManager_open(). 105 * 106 * The string returned here is owned by the AssetDir implementation and is not 107 * guaranteed to remain valid if any other calls are made on this AAssetDir 108 * instance. 109 */ 110 const char* AAssetDir_getNextFileName(AAssetDir* assetDir); 111 112 /** 113 * Reset the iteration state of AAssetDir_getNextFileName() to the beginning. 114 */ 115 void AAssetDir_rewind(AAssetDir* assetDir); 116 117 /** 118 * Close an opened AAssetDir, freeing any related resources. 119 */ 120 void AAssetDir_close(AAssetDir* assetDir); 121 122 /** 123 * Attempt to read 'count' bytes of data from the current offset. 124 * 125 * Returns the number of bytes read, zero on EOF, or < 0 on error. 126 */ 127 int AAsset_read(AAsset* asset, void* buf, size_t count); 128 129 /** 130 * Seek to the specified offset within the asset data. 'whence' uses the 131 * same constants as lseek()/fseek(). 132 * 133 * Returns the new position on success, or (off_t) -1 on error. 134 */ 135 off_t AAsset_seek(AAsset* asset, off_t offset, int whence); 136 137 #if __ANDROID_API__ >= 13 138 /** 139 * Seek to the specified offset within the asset data. 'whence' uses the 140 * same constants as lseek()/fseek(). 141 * 142 * Uses 64-bit data type for large files as opposed to the 32-bit type used 143 * by AAsset_seek. 144 * 145 * Returns the new position on success, or (off64_t) -1 on error. 146 */ 147 off64_t AAsset_seek64(AAsset* asset, off64_t offset, int whence); 148 #endif 149 150 /** 151 * Close the asset, freeing all associated resources. 152 */ 153 void AAsset_close(AAsset* asset); 154 155 /** 156 * Get a pointer to a buffer holding the entire contents of the assset. 157 * 158 * Returns NULL on failure. 159 */ 160 const void* AAsset_getBuffer(AAsset* asset); 161 162 /** 163 * Report the total size of the asset data. 164 */ 165 off_t AAsset_getLength(AAsset* asset); 166 167 #if __ANDROID_API__ >= 13 168 /** 169 * Report the total size of the asset data. Reports the size using a 64-bit 170 * number insted of 32-bit as AAsset_getLength. 171 */ 172 off64_t AAsset_getLength64(AAsset* asset); 173 #endif 174 175 /** 176 * Report the total amount of asset data that can be read from the current position. 177 */ 178 off_t AAsset_getRemainingLength(AAsset* asset); 179 180 #if __ANDROID_API__ >= 13 181 /** 182 * Report the total amount of asset data that can be read from the current position. 183 * 184 * Uses a 64-bit number instead of a 32-bit number as AAsset_getRemainingLength does. 185 */ 186 off64_t AAsset_getRemainingLength64(AAsset* asset); 187 #endif 188 189 /** 190 * Open a new file descriptor that can be used to read the asset data. If the 191 * start or length cannot be represented by a 32-bit number, it will be 192 * truncated. If the file is large, use AAsset_openFileDescriptor64 instead. 193 * 194 * Returns < 0 if direct fd access is not possible (for example, if the asset is 195 * compressed). 196 */ 197 int AAsset_openFileDescriptor(AAsset* asset, off_t* outStart, off_t* outLength); 198 199 #if __ANDROID_API__ >= 13 200 /** 201 * Open a new file descriptor that can be used to read the asset data. 202 * 203 * Uses a 64-bit number for the offset and length instead of 32-bit instead of 204 * as AAsset_openFileDescriptor does. 205 * 206 * Returns < 0 if direct fd access is not possible (for example, if the asset is 207 * compressed). 208 */ 209 int AAsset_openFileDescriptor64(AAsset* asset, off64_t* outStart, off64_t* outLength); 210 #endif 211 212 /** 213 * Returns whether this asset's internal buffer is allocated in ordinary RAM (i.e. not 214 * mmapped). 215 */ 216 int AAsset_isAllocated(AAsset* asset); 217 218 219 220 #ifdef __cplusplus 221 }; 222 #endif 223 224 #endif // ANDROID_ASSET_MANAGER_H 225 226 /** @} */ 227