use field to store native file descriptor

This commit is contained in:
Myk Melez 2015-02-09 14:38:54 -08:00
Родитель 2a131627b3
Коммит 469ed9edd2
2 изменённых файлов: 53 добавлений и 34 удалений

Просмотреть файл

@ -53,6 +53,17 @@ class DefaultFileHandler implements BaseFileHandler {
*/ */
private String nativePath; private String nativePath;
/**
* Native descriptor of the file while it is open for reading or writing.
*/
private int nativeDescriptor = -1;
/**
* Whether or not the file is currently open for reading or writing.
*/
private boolean isOpenForRead = false;
private boolean isOpenForWrite = false;
/** File name. */ /** File name. */
private long fileName = 0; private long fileName = 0;
// The dir name is required when checking the fileSystem sizes // The dir name is required when checking the fileSystem sizes

Просмотреть файл

@ -488,21 +488,16 @@ Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.lastModified.()J"] = functio
return Long.fromNumber(stat != null ? stat.mtime : 0); return Long.fromNumber(stat != null ? stat.mtime : 0);
}; };
// A map from DefaultFileHandler instances with open input/output streams
// to the file descriptors used by the fs API to identify open files.
MIDP.openFileHandlers = new Map();
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.openForRead.()V"] = function() { Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.openForRead.()V"] = function() {
var pathname = util.fromJavaString(this.$nativePath); var pathname = util.fromJavaString(this.$nativePath);
DEBUG_FS && console.log("DefaultFileHandler.openForRead: " + pathname); DEBUG_FS && console.log("DefaultFileHandler.openForRead: " + pathname);
if (MIDP.openFileHandlers.has(this)) { if (this.$nativeDescriptor !== -1) {
// The file is already open, so we reset its position so the caller // The file is already open, so we only have to reset its position
// will start reading from the beginning of the file. // so the caller will start reading from the beginning of the file.
// XXX Should we track the position separately for the input and output var fd = this.$nativeDescriptor;
// streams?
var fd = MIDP.openFileHandlers.get(this);
fs.setpos(fd, 0); fs.setpos(fd, 0);
this.$isOpenForRead = 1;
return; return;
} }
@ -520,7 +515,8 @@ Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.openForRead.()V"] = function
asyncImpl("I", new Promise(function(resolve, reject) { asyncImpl("I", new Promise(function(resolve, reject) {
fs.open(pathname, function(fd) { fs.open(pathname, function(fd) {
MIDP.openFileHandlers.set(fileHandler, fd); fileHandler.$isOpenForRead = 1;
fileHandler.$nativeDescriptor = fd;
resolve(); resolve();
}); });
})); }));
@ -529,22 +525,24 @@ Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.openForRead.()V"] = function
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.closeForRead.()V"] = function() { Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.closeForRead.()V"] = function() {
var pathname = util.fromJavaString(this.$nativePath); var pathname = util.fromJavaString(this.$nativePath);
DEBUG_FS && console.log("DefaultFileHandler.closeForRead: " + pathname); DEBUG_FS && console.log("DefaultFileHandler.closeForRead: " + pathname);
var fd = MIDP.openFileHandlers.get(this); this.$isOpenForRead = 0;
if (this.$isOpenForWrite === 0) {
var fd = this.$nativeDescriptor;
this.$nativeDescriptor = -1;
fs.close(fd); fs.close(fd);
MIDP.openFileHandlers.delete(this); }
}; };
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.openForWrite.()V"] = function() { Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.openForWrite.()V"] = function() {
var pathname = util.fromJavaString(this.$nativePath); var pathname = util.fromJavaString(this.$nativePath);
DEBUG_FS && console.log("DefaultFileHandler.openForWrite: " + pathname); DEBUG_FS && console.log("DefaultFileHandler.openForWrite: " + pathname);
if (MIDP.openFileHandlers.has(this)) { if (this.$nativeDescriptor !== -1) {
// The file is already open, so we reset its position so the caller // The file is already open, so we only have to reset its position
// will start writing from the beginning of the file. // so the caller will start writing from the beginning of the file.
// XXX Should we track the position separately for the input and output var fd = this.$nativeDescriptor;
// streams?
var fd = MIDP.openFileHandlers.get(this);
fs.setpos(fd, 0); fs.setpos(fd, 0);
this.$isOpenForWrite = 1;
return; return;
} }
@ -562,7 +560,8 @@ Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.openForWrite.()V"] = functio
asyncImpl("I", new Promise(function(resolve, reject) { asyncImpl("I", new Promise(function(resolve, reject) {
fs.open(pathname, function(fd) { fs.open(pathname, function(fd) {
MIDP.openFileHandlers.set(fileHandler, fd); fileHandler.$isOpenForWrite = 1;
fileHandler.$nativeDescriptor = fd;
resolve(); resolve();
}); });
})); }));
@ -571,23 +570,28 @@ Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.openForWrite.()V"] = functio
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.closeForWrite.()V"] = function() { Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.closeForWrite.()V"] = function() {
var pathname = util.fromJavaString(this.$nativePath); var pathname = util.fromJavaString(this.$nativePath);
DEBUG_FS && console.log("DefaultFileHandler.closeForWrite: " + pathname); DEBUG_FS && console.log("DefaultFileHandler.closeForWrite: " + pathname);
var fd = MIDP.openFileHandlers.get(this); this.$isOpenForWrite = 0;
if (this.$isOpenForRead === 0) {
var fd = this.$nativeDescriptor;
this.$nativeDescriptor = -1;
fs.close(fd); fs.close(fd);
MIDP.openFileHandlers.delete(this); }
}; };
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.closeForReadWrite.()V"] = function() { Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.closeForReadWrite.()V"] = function() {
var pathname = util.fromJavaString(this.$nativePath); var pathname = util.fromJavaString(this.$nativePath);
DEBUG_FS && console.log("DefaultFileHandler.closeForReadWrite: " + pathname); DEBUG_FS && console.log("DefaultFileHandler.closeForReadWrite: " + pathname);
var fd = MIDP.openFileHandlers.get(this); this.$isOpenForRead = 0;
this.$isOpenForWrite = 0;
var fd = this.$nativeDescriptor;
this.$nativeDescriptor = -1;
fs.close(fd); fs.close(fd);
MIDP.openFileHandlers.delete(this);
}; };
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.read.([BII)I"] = function(b, off, len) { Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.read.([BII)I"] = function(b, off, len) {
var pathname = util.fromJavaString(this.$nativePath); var pathname = util.fromJavaString(this.$nativePath);
DEBUG_FS && console.log("DefaultFileHandler.read: " + pathname); DEBUG_FS && console.log("DefaultFileHandler.read: " + pathname);
var fd = MIDP.openFileHandlers.get(this); var fd = this.$nativeDescriptor;
if (off < 0 || len < 0 || off > b.byteLength || (b.byteLength - off) < len) { if (off < 0 || len < 0 || off > b.byteLength || (b.byteLength - off) < len) {
throw $.newIOException(); throw $.newIOException();
@ -607,7 +611,7 @@ Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.read.([BII)I"] = function(b,
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.write.([BII)I"] = function(b, off, len) { Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.write.([BII)I"] = function(b, off, len) {
var pathname = util.fromJavaString(this.$nativePath); var pathname = util.fromJavaString(this.$nativePath);
DEBUG_FS && console.log("DefaultFileHandler.write: " + pathname); DEBUG_FS && console.log("DefaultFileHandler.write: " + pathname);
var fd = MIDP.openFileHandlers.get(this); var fd = this.$nativeDescriptor;
fs.write(fd, b.subarray(off, off + len)); fs.write(fd, b.subarray(off, off + len));
// The "length of data really written," which is always the length requested // The "length of data really written," which is always the length requested
// in our implementation. // in our implementation.
@ -617,14 +621,14 @@ Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.write.([BII)I"] = function(b
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.positionForWrite.(J)V"] = function(offset) { Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.positionForWrite.(J)V"] = function(offset) {
var pathname = util.fromJavaString(this.$nativePath); var pathname = util.fromJavaString(this.$nativePath);
DEBUG_FS && console.log("DefaultFileHandler.positionForWrite: " + pathname); DEBUG_FS && console.log("DefaultFileHandler.positionForWrite: " + pathname);
var fd = MIDP.openFileHandlers.get(this); var fd = this.$nativeDescriptor;
fs.setpos(fd, offset.toNumber()); fs.setpos(fd, offset.toNumber());
}; };
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.flush.()V"] = function() { Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.flush.()V"] = function() {
var pathname = util.fromJavaString(this.$nativePath); var pathname = util.fromJavaString(this.$nativePath);
DEBUG_FS && console.log("DefaultFileHandler.flush: " + pathname); DEBUG_FS && console.log("DefaultFileHandler.flush: " + pathname);
var fd = MIDP.openFileHandlers.get(this); var fd = this.$nativeDescriptor;
if (fd) { if (fd) {
fs.flush(fd); fs.flush(fd);
} }
@ -633,9 +637,13 @@ Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.flush.()V"] = function() {
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.close.()V"] = function() { Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.close.()V"] = function() {
var pathname = util.fromJavaString(this.$nativePath); var pathname = util.fromJavaString(this.$nativePath);
DEBUG_FS && console.log("DefaultFileHandler.close: " + pathname); DEBUG_FS && console.log("DefaultFileHandler.close: " + pathname);
var fd = MIDP.openFileHandlers.get(this); this.$isOpenForRead = 0;
this.$isOpenForWrite = 0;
var fd = this.$nativeDescriptor;
this.$nativeDescriptor = -1;
if (fd !== -1) {
fs.close(fd); fs.close(fd);
MIDP.openFileHandlers.delete(this); }
}; };
// Not implemented because we don't use native pointers, so we've commented out // Not implemented because we don't use native pointers, so we've commented out
@ -716,7 +724,7 @@ DEBUG_FS && console.log("getSuiteIdString: " + id);
Native["com/sun/cdc/io/j2me/file/Protocol.available.()I"] = function() { Native["com/sun/cdc/io/j2me/file/Protocol.available.()I"] = function() {
var pathname = util.fromJavaString(this.$fileHandler.$nativePath); var pathname = util.fromJavaString(this.$fileHandler.$nativePath);
var fd = MIDP.openFileHandlers.get(this.$fileHandler); var fd = this.$fileHandler.$nativeDescriptor;
var available = fs.getsize(fd) - fs.getpos(fd); var available = fs.getsize(fd) - fs.getpos(fd);
DEBUG_FS && console.log("Protocol.available: " + pathname + ": " + available); DEBUG_FS && console.log("Protocol.available: " + pathname + ": " + available);
return available; return available;