am 744043fc: Update FileSource to also accept a file descriptor and a range.
Merge commit '744043fcbf48c32c2051f222eca552fa2df5dfcb' into eclair-mr2-plus-aosp * commit '744043fcbf48c32c2051f222eca552fa2df5dfcb': Update FileSource to also accept a file descriptor and a range.
This commit is contained in:
@@ -29,16 +29,21 @@ namespace android {
|
|||||||
class FileSource : public DataSource {
|
class FileSource : public DataSource {
|
||||||
public:
|
public:
|
||||||
FileSource(const char *filename);
|
FileSource(const char *filename);
|
||||||
|
FileSource(int fd, int64_t offset, int64_t length);
|
||||||
|
|
||||||
virtual status_t initCheck() const;
|
virtual status_t initCheck() const;
|
||||||
|
|
||||||
virtual ssize_t readAt(off_t offset, void *data, size_t size);
|
virtual ssize_t readAt(off_t offset, void *data, size_t size);
|
||||||
|
|
||||||
|
virtual status_t getSize(off_t *size);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual ~FileSource();
|
virtual ~FileSource();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
FILE *mFile;
|
FILE *mFile;
|
||||||
|
int64_t mOffset;
|
||||||
|
int64_t mLength;
|
||||||
Mutex mLock;
|
Mutex mLock;
|
||||||
|
|
||||||
FileSource(const FileSource &);
|
FileSource(const FileSource &);
|
||||||
|
|||||||
@@ -20,7 +20,17 @@
|
|||||||
namespace android {
|
namespace android {
|
||||||
|
|
||||||
FileSource::FileSource(const char *filename)
|
FileSource::FileSource(const char *filename)
|
||||||
: mFile(fopen(filename, "rb")) {
|
: mFile(fopen(filename, "rb")),
|
||||||
|
mOffset(0),
|
||||||
|
mLength(-1) {
|
||||||
|
}
|
||||||
|
|
||||||
|
FileSource::FileSource(int fd, int64_t offset, int64_t length)
|
||||||
|
: mFile(fdopen(fd, "rb")),
|
||||||
|
mOffset(offset),
|
||||||
|
mLength(length) {
|
||||||
|
CHECK(offset >= 0);
|
||||||
|
CHECK(length >= 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
FileSource::~FileSource() {
|
FileSource::~FileSource() {
|
||||||
@@ -37,12 +47,33 @@ status_t FileSource::initCheck() const {
|
|||||||
ssize_t FileSource::readAt(off_t offset, void *data, size_t size) {
|
ssize_t FileSource::readAt(off_t offset, void *data, size_t size) {
|
||||||
Mutex::Autolock autoLock(mLock);
|
Mutex::Autolock autoLock(mLock);
|
||||||
|
|
||||||
int err = fseeko(mFile, offset, SEEK_SET);
|
if (mLength >= 0) {
|
||||||
|
if (offset >= mLength) {
|
||||||
|
return 0; // read beyond EOF.
|
||||||
|
}
|
||||||
|
int64_t numAvailable = mLength - offset;
|
||||||
|
if ((int64_t)size > numAvailable) {
|
||||||
|
size = numAvailable;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int err = fseeko(mFile, offset + mOffset, SEEK_SET);
|
||||||
CHECK(err != -1);
|
CHECK(err != -1);
|
||||||
|
|
||||||
ssize_t result = fread(data, 1, size, mFile);
|
return fread(data, 1, size, mFile);
|
||||||
|
}
|
||||||
|
|
||||||
return result;
|
status_t FileSource::getSize(off_t *size) {
|
||||||
|
if (mLength >= 0) {
|
||||||
|
*size = mLength;
|
||||||
|
|
||||||
|
return OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
fseek(mFile, SEEK_END, 0);
|
||||||
|
*size = ftello(mFile);
|
||||||
|
|
||||||
|
return OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace android
|
} // namespace android
|
||||||
|
|||||||
Reference in New Issue
Block a user