blob: d8351f57cbbf5919666868c9d47ef1661d55e6f7 [file] [log] [blame]
The Android Open Source Projectd245d1d2008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2006 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// Class providing access to a read-only asset. Asset objects are NOT
19// thread-safe, and should not be shared across threads.
20//
21#ifndef __LIBS_ASSET_H
22#define __LIBS_ASSET_H
23
24#include <stdio.h>
25#include <sys/types.h>
26#include "FileMap.h"
27#include "String8.h"
28#include "Errors.h"
29
30namespace android {
31
32/*
33 * Instances of this class provide read-only operations on a byte stream.
34 *
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??]
38 *
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.
42 */
43class Asset {
44public:
45 virtual ~Asset(void);
46
47 static int32_t getGlobalCount();
48
49 /* used when opening an asset */
50 typedef enum AccessMode {
51 ACCESS_UNKNOWN = 0,
52
53 /* read chunks, and seek forward and backward */
54 ACCESS_RANDOM,
55
56 /* read sequentially, with an occasional forward seek */
57 ACCESS_STREAMING,
58
59 /* caller plans to ask for a read-only buffer with all data */
60 ACCESS_BUFFER,
61 } AccessMode;
62
63 enum {
64 /* data larger than this does not get uncompressed into a buffer */
65 UNCOMPRESS_DATA_MAX = 1 * 1024 * 1024
66 };
67
68 /*
69 * Read data from the current offset. Returns the actual number of
70 * bytes read, 0 on EOF, or -1 on error.
71 */
72 virtual ssize_t read(void* buf, size_t count) = 0;
73
74 /*
75 * Seek to the specified offset. "whence" uses the same values as
76 * lseek/fseek. Returns the new position on success, or (off_t) -1
77 * on failure.
78 */
79 virtual off_t seek(off_t offset, int whence) = 0;
80
81 /*
82 * Close the asset, freeing all associated resources.
83 */
84 virtual void close(void) = 0;
85
86 /*
87 * Get a pointer to a buffer with the entire contents of the file.
88 */
89 virtual const void* getBuffer(bool wordAligned) = 0;
90
91 /*
92 * Get the total amount of data that can be read.
93 */
94 virtual off_t getLength(void) const = 0;
95
96 /*
97 * Get the total amount of data that can be read from the current position.
98 */
99 virtual off_t getRemainingLength(void) const = 0;
100
101 /*
102 * Open a new file descriptor that can be used to read this asset.
103 * Returns -1 if you can not use the file descriptor (for example if the
104 * asset is compressed).
105 */
106 virtual int openFileDescriptor(off_t* outStart, off_t* outLength) const = 0;
107
108 /*
109 * Get a string identifying the asset's source. This might be a full
110 * path, it might be a colon-separated list of identifiers.
111 *
112 * This is NOT intended to be used for anything except debug output.
113 * DO NOT try to parse this or use it to open a file.
114 */
115 const char* getAssetSource(void) const { return mAssetSource.string(); }
116
117protected:
118 Asset(void); // constructor; only invoked indirectly
119
120 /* handle common seek() housekeeping */
121 off_t handleSeek(off_t offset, int whence, off_t curPosn, off_t maxPosn);
122
123 /* set the asset source string */
124 void setAssetSource(const String8& path) { mAssetSource = path; }
125
126 AccessMode getAccessMode(void) const { return mAccessMode; }
127
128private:
129 /* these operations are not implemented */
130 Asset(const Asset& src);
131 Asset& operator=(const Asset& src);
132
133 /* AssetManager needs access to our "create" functions */
134 friend class AssetManager;
135
136 /*
137 * Create the asset from a named file on disk.
138 */
139 static Asset* createFromFile(const char* fileName, AccessMode mode);
140
141 /*
142 * Create the asset from a named, compressed file on disk (e.g. ".gz").
143 */
144 static Asset* createFromCompressedFile(const char* fileName,
145 AccessMode mode);
146
147#if 0
148 /*
149 * Create the asset from a segment of an open file. This will fail
150 * if "offset" and "length" don't fit within the bounds of the file.
151 *
152 * The asset takes ownership of the file descriptor.
153 */
154 static Asset* createFromFileSegment(int fd, off_t offset, size_t length,
155 AccessMode mode);
156
157 /*
158 * Create from compressed data. "fd" should be seeked to the start of
159 * the compressed data. This could be inside a gzip file or part of a
160 * Zip archive.
161 *
162 * The asset takes ownership of the file descriptor.
163 *
164 * This may not verify the validity of the compressed data until first
165 * use.
166 */
167 static Asset* createFromCompressedData(int fd, off_t offset,
168 int compressionMethod, size_t compressedLength,
169 size_t uncompressedLength, AccessMode mode);
170#endif
171
172 /*
173 * Create the asset from a memory-mapped file segment.
174 *
175 * The asset takes ownership of the FileMap.
176 */
177 static Asset* createFromUncompressedMap(FileMap* dataMap, AccessMode mode);
178
179 /*
180 * Create the asset from a memory-mapped file segment with compressed
181 * data. "method" is a Zip archive compression method constant.
182 *
183 * The asset takes ownership of the FileMap.
184 */
185 static Asset* createFromCompressedMap(FileMap* dataMap, int method,
186 size_t uncompressedLen, AccessMode mode);
187
188
189 /*
190 * Create from a reference-counted chunk of shared memory.
191 */
192 // TODO
193
194 AccessMode mAccessMode; // how the asset was opened
195 String8 mAssetSource; // debug string
196};
197
198
199/*
200 * ===========================================================================
201 *
202 * Innards follow. Do not use these classes directly.
203 */
204
205/*
206 * An asset based on an uncompressed file on disk. It may encompass the
207 * entire file or just a piece of it. Access is through fread/fseek.
208 */
209class _FileAsset : public Asset {
210public:
211 _FileAsset(void);
212 virtual ~_FileAsset(void);
213
214 /*
215 * Use a piece of an already-open file.
216 *
217 * On success, the object takes ownership of "fd".
218 */
219 status_t openChunk(const char* fileName, int fd, off_t offset, size_t length);
220
221 /*
222 * Use a memory-mapped region.
223 *
224 * On success, the object takes ownership of "dataMap".
225 */
226 status_t openChunk(FileMap* dataMap);
227
228 /*
229 * Standard Asset interfaces.
230 */
231 virtual ssize_t read(void* buf, size_t count);
232 virtual off_t seek(off_t offset, int whence);
233 virtual void close(void);
234 virtual const void* getBuffer(bool wordAligned);
235 virtual off_t getLength(void) const { return mLength; }
236 virtual off_t getRemainingLength(void) const { return mLength-mOffset; }
237 virtual int openFileDescriptor(off_t* outStart, off_t* outLength) const;
238
239private:
240 off_t mStart; // absolute file offset of start of chunk
241 off_t mLength; // length of the chunk
242 off_t mOffset; // current local offset, 0 == mStart
243 FILE* mFp; // for read/seek
244 char* mFileName; // for opening
245
246 /*
247 * To support getBuffer() we either need to read the entire thing into
248 * a buffer or memory-map it. For small files it's probably best to
249 * just read them in.
250 */
251 enum { kReadVsMapThreshold = 4096 };
252
253 FileMap* mMap; // for memory map
254 unsigned char* mBuf; // for read
255
256 const void* ensureAlignment(FileMap* map);
257};
258
259
260/*
261 * An asset based on compressed data in a file.
262 */
263class _CompressedAsset : public Asset {
264public:
265 _CompressedAsset(void);
266 virtual ~_CompressedAsset(void);
267
268 /*
269 * Use a piece of an already-open file.
270 *
271 * On success, the object takes ownership of "fd".
272 */
273 status_t openChunk(int fd, off_t offset, int compressionMethod,
274 size_t uncompressedLen, size_t compressedLen);
275
276 /*
277 * Use a memory-mapped region.
278 *
279 * On success, the object takes ownership of "fd".
280 */
281 status_t openChunk(FileMap* dataMap, int compressionMethod,
282 size_t uncompressedLen);
283
284 /*
285 * Standard Asset interfaces.
286 */
287 virtual ssize_t read(void* buf, size_t count);
288 virtual off_t seek(off_t offset, int whence);
289 virtual void close(void);
290 virtual const void* getBuffer(bool wordAligned);
291 virtual off_t getLength(void) const { return mUncompressedLen; }
292 virtual off_t getRemainingLength(void) const { return mUncompressedLen-mOffset; }
293 virtual int openFileDescriptor(off_t* outStart, off_t* outLength) const { return -1; }
294
295private:
296 off_t mStart; // offset to start of compressed data
297 off_t mCompressedLen; // length of the compressed data
298 off_t mUncompressedLen; // length of the uncompressed data
299 off_t mOffset; // current offset, 0 == start of uncomp data
300
301 FileMap* mMap; // for memory-mapped input
302 int mFd; // for file input
303
304 unsigned char* mBuf; // for getBuffer()
305};
306
307// need: shared mmap version?
308
309}; // namespace android
310
311#endif // __LIBS_ASSET_H