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