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