First cut at read(). Will need to refactor after write() is done.

This commit is contained in:
Alan Kligman 2012-10-12 00:24:35 -04:00
parent cc82c68b4b
commit 4b4f3a76be
2 changed files with 49 additions and 10 deletions

View File

@ -22,6 +22,10 @@ require(["src/filesystem"], function(IDBFS) {
fs.open("/tmp/0", "CREATE", "RW", function(error, fd) { fs.open("/tmp/0", "CREATE", "RW", function(error, fd) {
fs.close(fd, function(error) { fs.close(fd, function(error) {
fs.dump(document.getElementById("stdout")); fs.dump(document.getElementById("stdout"));
var buffer = new Uint8Array(16);
fd.read(buffer, 0, 16, function(error, bytes_read, buffer) {
console.log(error, bytes_read);
});
}); });
}); });
}); });

View File

@ -454,11 +454,48 @@ define(function(require) {
} }
} }
function read(buffer, bytes, callback) { function read(buffer, offset, bytes, callback) {
debug.info("read -->");
start();
transaction = db.transaction([FILE_STORE_NAME], IDB_RO);
var store = transaction.objectStore(FILE_STORE_NAME);
var onerror = genericIDBErrorHandler("read", callback);
if(MIME_FILE === ofd.entry["content-type"]) {
var oid = ofd.entry["object-id"];
var getRequest = store.get(oid);
getRequest.onsuccess = function(e) {
var file = e.target.result;
if(!file) {
// There's no file data, so return zero bytes read
if(callback && "function" === typeof callback) {
callback.call(undefined, undefined, 0, buffer);
}
} else {
// Make sure we're not going to read past the end of the file
bytes = (ofd.pointer + bytes > file.size) ? file.size - ofd.pointer : bytes;
var reader = new FileReader();
reader.readAsArrayBuffer(file);
reader.onload = function(e) {
// Copy the desired region from the file into the buffer supplied
var source = e.target.result;
var sourceView = new Uint8Array(data).subarray(ofd.pointer, ofd.pointer + bytes);
var target = buffer;
target.set(sourceView, offset);
if(callback && "function" === typeof callback) {
callback.call(undefined, undefined, bytes, buffer);
}
}
reader.onerror = onerror;
}
};
getRequest.onerror = onerror;
} else if(MIME_DIRECTORY === ofd.entry["content-type"]) {
}
} }
function write(buffer, bytes, callback) { function write(buffer, offset, bytes, callback) {
} }
@ -469,16 +506,14 @@ define(function(require) {
} }
function isValid() {
return ofd.valid;
}
this.read = read; this.read = read;
this.seek = seek; this.seek = seek;
if(ofd.mode === OM_RW) { this.write = write;
this.write = write; Object.defineProperty(this, "valid", {
} get: function() {
this.isValid = isValid; return ofd.valid;
}
});
fds[descriptor] = ofd; fds[descriptor] = ofd;
} }