2 * Copyright (C) 2006 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 // Class providing access to a read-only asset. Asset objects are NOT
19 // thread-safe, and should not be shared across threads.
21 #ifndef __LIBS_ASSET_H
22 #define __LIBS_ASSET_H
25 #include <sys/types.h>
33 * Instances of this class provide read-only operations on a byte stream.
35 * Access may be optimized for streaming, random, or whole buffer modes. All
36 * operations are supported regardless of how the file was opened, but some
37 * things will be less efficient. [pass that in??]
39 * "Asset" is the base class for all types of assets. The classes below
40 * provide most of the implementation. The AssetManager uses one of the
41 * static "create" functions defined here to create a new instance.
47 static int32_t getGlobalCount();
48 static String8 getAssetAllocations();
50 /* used when opening an asset */
51 typedef enum AccessMode {
54 /* read chunks, and seek forward and backward */
57 /* read sequentially, with an occasional forward seek */
60 /* caller plans to ask for a read-only buffer with all data */
65 * Read data from the current offset. Returns the actual number of
66 * bytes read, 0 on EOF, or -1 on error.
68 virtual ssize_t read(void* buf, size_t count) = 0;
71 * Seek to the specified offset. "whence" uses the same values as
72 * lseek/fseek. Returns the new position on success, or (off_t) -1
75 virtual off_t seek(off_t offset, int whence) = 0;
78 * Close the asset, freeing all associated resources.
80 virtual void close(void) = 0;
83 * Get a pointer to a buffer with the entire contents of the file.
85 virtual const void* getBuffer(bool wordAligned) = 0;
88 * Get the total amount of data that can be read.
90 virtual off_t getLength(void) const = 0;
93 * Get the total amount of data that can be read from the current position.
95 virtual off_t getRemainingLength(void) const = 0;
98 * Open a new file descriptor that can be used to read this asset.
99 * Returns -1 if you can not use the file descriptor (for example if the
100 * asset is compressed).
102 virtual int openFileDescriptor(off_t* outStart, off_t* outLength) const = 0;
105 * Return whether this asset's buffer is allocated in RAM (not mmapped).
106 * Note: not virtual so it is safe to call even when being destroyed.
108 virtual bool isAllocated(void) const { return false; }
111 * Get a string identifying the asset's source. This might be a full
112 * path, it might be a colon-separated list of identifiers.
114 * This is NOT intended to be used for anything except debug output.
115 * DO NOT try to parse this or use it to open a file.
117 const char* getAssetSource(void) const { return mAssetSource.string(); }
120 Asset(void); // constructor; only invoked indirectly
122 /* handle common seek() housekeeping */
123 off_t handleSeek(off_t offset, int whence, off_t curPosn, off_t maxPosn);
125 /* set the asset source string */
126 void setAssetSource(const String8& path) { mAssetSource = path; }
128 AccessMode getAccessMode(void) const { return mAccessMode; }
131 /* these operations are not implemented */
132 Asset(const Asset& src);
133 Asset& operator=(const Asset& src);
135 /* AssetManager needs access to our "create" functions */
136 friend class AssetManager;
139 * Create the asset from a named file on disk.
141 static Asset* createFromFile(const char* fileName, AccessMode mode);
144 * Create the asset from a named, compressed file on disk (e.g. ".gz").
146 static Asset* createFromCompressedFile(const char* fileName,
151 * Create the asset from a segment of an open file. This will fail
152 * if "offset" and "length" don't fit within the bounds of the file.
154 * The asset takes ownership of the file descriptor.
156 static Asset* createFromFileSegment(int fd, off_t offset, size_t length,
160 * Create from compressed data. "fd" should be seeked to the start of
161 * the compressed data. This could be inside a gzip file or part of a
164 * The asset takes ownership of the file descriptor.
166 * This may not verify the validity of the compressed data until first
169 static Asset* createFromCompressedData(int fd, off_t offset,
170 int compressionMethod, size_t compressedLength,
171 size_t uncompressedLength, AccessMode mode);
175 * Create the asset from a memory-mapped file segment.
177 * The asset takes ownership of the FileMap.
179 static Asset* createFromUncompressedMap(FileMap* dataMap, AccessMode mode);
182 * Create the asset from a memory-mapped file segment with compressed
183 * data. "method" is a Zip archive compression method constant.
185 * The asset takes ownership of the FileMap.
187 static Asset* createFromCompressedMap(FileMap* dataMap, int method,
188 size_t uncompressedLen, AccessMode mode);
192 * Create from a reference-counted chunk of shared memory.
196 AccessMode mAccessMode; // how the asset was opened
197 String8 mAssetSource; // debug string
199 Asset* mNext; // linked list.
205 * ===========================================================================
207 * Innards follow. Do not use these classes directly.
211 * An asset based on an uncompressed file on disk. It may encompass the
212 * entire file or just a piece of it. Access is through fread/fseek.
214 class _FileAsset : public Asset {
217 virtual ~_FileAsset(void);
220 * Use a piece of an already-open file.
222 * On success, the object takes ownership of "fd".
224 status_t openChunk(const char* fileName, int fd, off_t offset, size_t length);
227 * Use a memory-mapped region.
229 * On success, the object takes ownership of "dataMap".
231 status_t openChunk(FileMap* dataMap);
234 * Standard Asset interfaces.
236 virtual ssize_t read(void* buf, size_t count);
237 virtual off_t seek(off_t offset, int whence);
238 virtual void close(void);
239 virtual const void* getBuffer(bool wordAligned);
240 virtual off_t getLength(void) const { return mLength; }
241 virtual off_t getRemainingLength(void) const { return mLength-mOffset; }
242 virtual int openFileDescriptor(off_t* outStart, off_t* outLength) const;
243 virtual bool isAllocated(void) const { return mBuf != NULL; }
246 off_t mStart; // absolute file offset of start of chunk
247 off_t mLength; // length of the chunk
248 off_t mOffset; // current local offset, 0 == mStart
249 FILE* mFp; // for read/seek
250 char* mFileName; // for opening
253 * To support getBuffer() we either need to read the entire thing into
254 * a buffer or memory-map it. For small files it's probably best to
257 enum { kReadVsMapThreshold = 4096 };
259 FileMap* mMap; // for memory map
260 unsigned char* mBuf; // for read
262 const void* ensureAlignment(FileMap* map);
267 * An asset based on compressed data in a file.
269 class _CompressedAsset : public Asset {
271 _CompressedAsset(void);
272 virtual ~_CompressedAsset(void);
275 * Use a piece of an already-open file.
277 * On success, the object takes ownership of "fd".
279 status_t openChunk(int fd, off_t offset, int compressionMethod,
280 size_t uncompressedLen, size_t compressedLen);
283 * Use a memory-mapped region.
285 * On success, the object takes ownership of "fd".
287 status_t openChunk(FileMap* dataMap, int compressionMethod,
288 size_t uncompressedLen);
291 * Standard Asset interfaces.
293 virtual ssize_t read(void* buf, size_t count);
294 virtual off_t seek(off_t offset, int whence);
295 virtual void close(void);
296 virtual const void* getBuffer(bool wordAligned);
297 virtual off_t getLength(void) const { return mUncompressedLen; }
298 virtual off_t getRemainingLength(void) const { return mUncompressedLen-mOffset; }
299 virtual int openFileDescriptor(off_t* outStart, off_t* outLength) const { return -1; }
300 virtual bool isAllocated(void) const { return mBuf != NULL; }
303 off_t mStart; // offset to start of compressed data
304 off_t mCompressedLen; // length of the compressed data
305 off_t mUncompressedLen; // length of the uncompressed data
306 off_t mOffset; // current offset, 0 == start of uncomp data
308 FileMap* mMap; // for memory-mapped input
309 int mFd; // for file input
311 class StreamingZipInflater* mZipInflater; // for streaming large compressed assets
313 unsigned char* mBuf; // for getBuffer()
316 // need: shared mmap version?
318 }; // namespace android
320 #endif // __LIBS_ASSET_H