pluotsorbet/midp/fs.js

715 строки
26 KiB
JavaScript
Исходник Обычный вид История

/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* vim: set shiftwidth=4 tabstop=4 autoindent cindent expandtab: */
2014-08-29 01:13:45 +04:00
'use strict';
2014-09-30 20:17:16 +04:00
var RECORD_STORE_BASE = "/RecordStore";
// The filesystem roots, which are used by both FileSystemRegistry.getRootsImpl
// and System.getProperty to provide inquiring midlets with the list. Each root
// must have a trailing slash. See FileSystemRegistry.listRoots for more info.
MIDP.fsRoots = [
"MemoryCard/",
"Phone/",
"Private/",
];
// The names here should be localized.
MIDP.fsRootNames = [
"Memory card",
"Phone memory",
"Private",
];
2014-12-19 00:03:59 +03:00
Native["com/sun/midp/io/j2me/storage/File.initConfigRoot.(I)Ljava/lang/String;"] = function(storageId) {
2014-12-23 23:17:16 +03:00
return J2ME.newString("assets/" + storageId + "/");
2014-12-19 00:03:59 +03:00
};
Native["com/sun/midp/io/j2me/storage/File.initStorageRoot.(I)Ljava/lang/String;"] = function(storageId) {
return J2ME.newString("assets/" + storageId + "/");
};
2014-12-19 00:03:59 +03:00
Native["com/sun/midp/midletsuite/MIDletSuiteStorage.getSecureFilenameBase.(I)Ljava/lang/String;"] = function(id) {
2014-12-23 23:17:16 +03:00
return J2ME.newString("");
2014-12-19 00:03:59 +03:00
};
2014-12-19 00:03:59 +03:00
Native["com/sun/midp/rms/RecordStoreUtil.exists.(Ljava/lang/String;Ljava/lang/String;I)Z"] =
function(filenameBase, name, ext) {
2015-02-02 14:10:57 +03:00
var path = RECORD_STORE_BASE + "/" + util.fromJavaString(filenameBase) + "/" + util.fromJavaString(name) + "." + ext;
return fs.exists(path) ? 1 : 0;
2014-12-19 00:03:59 +03:00
};
2014-12-19 00:03:59 +03:00
Native["com/sun/midp/rms/RecordStoreUtil.deleteFile.(Ljava/lang/String;Ljava/lang/String;I)V"] =
function(filenameBase, name, ext) {
2014-12-23 14:30:18 +03:00
var path = RECORD_STORE_BASE + "/" + util.fromJavaString(filenameBase) + "/" + util.fromJavaString(name) + "." + ext;
fs.remove(path);
2014-12-19 00:03:59 +03:00
};
2014-12-19 00:03:59 +03:00
Native["com/sun/midp/rms/RecordStoreFile.spaceAvailableNewRecordStore0.(Ljava/lang/String;I)I"] = function(filenameBase, storageId) {
// Pretend there is 50MiB available. Our implementation is backed
// by IndexedDB, which has no actual limit beyond space available on device,
// which I don't think we can determine. But this should be sufficient
// to convince the MIDlet to use the API as needed.
return 50 * 1024 * 1024;
2014-12-19 00:03:59 +03:00
};
2014-12-19 00:03:59 +03:00
Native["com/sun/midp/rms/RecordStoreFile.spaceAvailableRecordStore.(ILjava/lang/String;I)I"] = function(handle, filenameBase, storageId) {
// Pretend there is 50MiB available. Our implementation is backed
// by IndexedDB, which has no actual limit beyond space available on device,
// which I don't think we can determine. But this should be sufficient
// to convince the MIDlet to use the API as needed.
return 50 * 1024 * 1024;
2014-12-19 00:03:59 +03:00
};
2014-12-19 00:03:59 +03:00
Native["com/sun/midp/rms/RecordStoreFile.openRecordStoreFile.(Ljava/lang/String;Ljava/lang/String;I)I"] =
function(filenameBase, name, ext) {
var ctx = $.ctx;
2014-12-19 00:03:59 +03:00
asyncImpl("I", new Promise(function(resolve, reject) {
2014-10-17 02:04:31 +04:00
var path = RECORD_STORE_BASE + "/" + util.fromJavaString(filenameBase) + "/" + util.fromJavaString(name) + "." + ext;
2014-10-17 02:04:31 +04:00
function openCallback(fd) {
ctx.setAsCurrentContext();
2014-10-17 02:04:31 +04:00
if (fd == -1) {
reject($.newIOException("openRecordStoreFile: open failed"));
2014-10-17 02:04:31 +04:00
} else {
resolve(fd); // handle
}
}
2014-12-23 14:30:18 +03:00
if (fs.exists(path)) {
fs.open(path, openCallback);
} else {
// Per the reference impl, create the file if it doesn't exist.
var dirname = fs.dirname(path);
if (fs.mkdirp(dirname)) {
if (fs.create(path, new Blob())) {
fs.open(path, openCallback);
} else {
// TODO: determine if this is actually necessary, as I think
// we're still in synchronous execution of the native here.
ctx.setAsCurrentContext();
reject($.newIOException("openRecordStoreFile: create failed"));
2014-12-23 14:30:18 +03:00
}
2014-10-17 02:04:31 +04:00
} else {
// TODO: determine if this is actually necessary, as I think
// we're still in synchronous execution of the native here.
ctx.setAsCurrentContext();
reject($.newIOException("openRecordStoreFile: mkdirp failed"));
2014-10-17 02:04:31 +04:00
}
2014-12-23 14:30:18 +03:00
}
2014-12-19 00:03:59 +03:00
}));
};
2014-12-19 00:03:59 +03:00
Native["com/sun/midp/rms/RecordStoreFile.setPosition.(II)V"] = function(handle, pos) {
fs.setpos(handle, pos);
2014-12-19 00:03:59 +03:00
};
2014-12-19 00:03:59 +03:00
Native["com/sun/midp/rms/RecordStoreFile.readBytes.(I[BII)I"] = function(handle, buf, offset, numBytes) {
var from = fs.getpos(handle);
var to = from + numBytes;
var readBytes = fs.read(handle, from, to);
if (readBytes.byteLength <= 0) {
throw $.newIOException("handle invalid or segment indices out of bounds");
}
var subBuffer = buf.subarray(offset, offset + readBytes.byteLength);
for (var i = 0; i < readBytes.byteLength; i++) {
subBuffer[i] = readBytes[i];
}
return readBytes.byteLength;
2014-12-19 00:03:59 +03:00
};
2014-12-19 00:03:59 +03:00
Native["com/sun/midp/rms/RecordStoreFile.writeBytes.(I[BII)V"] = function(handle, buf, offset, numBytes) {
fs.write(handle, buf.subarray(offset, offset + numBytes));
2014-12-19 00:03:59 +03:00
};
2014-12-19 00:03:59 +03:00
Native["com/sun/midp/rms/RecordStoreFile.commitWrite.(I)V"] = function(handle) {
2015-02-02 14:10:57 +03:00
fs.flush(handle);
2014-12-19 00:03:59 +03:00
};
2014-12-19 00:03:59 +03:00
Native["com/sun/midp/rms/RecordStoreFile.closeFile.(I)V"] = function(handle) {
2015-02-02 14:10:57 +03:00
fs.close(handle);
2014-12-19 00:03:59 +03:00
};
2014-12-19 00:03:59 +03:00
Native["com/sun/midp/rms/RecordStoreFile.truncateFile.(II)V"] = function(handle, size) {
2014-11-24 12:49:56 +03:00
fs.flush(handle);
fs.ftruncate(handle, size);
2014-12-19 00:03:59 +03:00
};
MIDP.RecordStoreCache = [];
2014-12-19 00:03:59 +03:00
Native["com/sun/midp/rms/RecordStoreSharedDBHeader.getLookupId0.(ILjava/lang/String;I)I"] =
function(suiteId, jStoreName, headerDataSize) {
var storeName = util.fromJavaString(jStoreName);
var sharedHeader =
MIDP.RecordStoreCache.filter(function(v) { return (v && v.suiteId == suiteId && v.storeName == storeName); })[0];
if (!sharedHeader) {
sharedHeader = {
suiteId: suiteId,
storeName: storeName,
headerVersion: 0,
headerData: null,
headerDataSize: headerDataSize,
refCount: 0,
// Use cache indices as IDs, so we can look up objects by index.
lookupId: MIDP.RecordStoreCache.length,
};
MIDP.RecordStoreCache.push(sharedHeader);
}
++sharedHeader.refCount;
return sharedHeader.lookupId;
2014-12-19 00:03:59 +03:00
};
2014-12-19 00:03:59 +03:00
Native["com/sun/midp/rms/RecordStoreSharedDBHeader.shareCachedData0.(I[BI)I"] = function(lookupId, headerData, headerDataSize) {
var sharedHeader = MIDP.RecordStoreCache[lookupId];
if (!sharedHeader) {
throw $.newIllegalStateException("invalid header lookup ID");
}
if (!headerData) {
throw $.newIllegalArgumentException("header data is null");
}
var size = headerDataSize;
if (size > sharedHeader.headerDataSize) {
size = sharedHeader.headerDataSize;
}
sharedHeader.headerData = headerData.buffer.slice(0, size);
++sharedHeader.headerVersion;
return sharedHeader.headerVersion;
2014-12-19 00:03:59 +03:00
};
2014-12-19 00:03:59 +03:00
Native["com/sun/midp/rms/RecordStoreSharedDBHeader.updateCachedData0.(I[BII)I"] =
function(lookupId, headerData, headerDataSize, headerVersion) {
var sharedHeader = MIDP.RecordStoreCache[lookupId];
if (!sharedHeader) {
throw $.newIllegalStateException("invalid header lookup ID");
}
if (!headerData) {
throw $.newIllegalArgumentException("header data is null");
}
if (sharedHeader.headerVersion > headerVersion && sharedHeader.headerData) {
var size = sharedHeader.headerDataSize;
if (size > headerDataSize) {
size = headerDataSize;
}
var sharedHeaderData = new Int8Array(sharedHeader.headerData);
for (var i = 0; i < size; i++) {
headerData[i] = sharedHeaderData[i];
}
return sharedHeader.headerVersion;
}
return headerVersion;
2014-12-19 00:03:59 +03:00
};
2014-12-19 00:03:59 +03:00
Native["com/sun/midp/rms/RecordStoreSharedDBHeader.getHeaderRefCount0.(I)I"] = function(lookupId) {
var sharedHeader = MIDP.RecordStoreCache[lookupId];
if (!sharedHeader) {
throw $.newIllegalStateException("invalid header lookup ID");
}
return sharedHeader.refCount;
2014-12-19 00:03:59 +03:00
};
2014-12-19 00:03:59 +03:00
Native["com/sun/midp/rms/RecordStoreSharedDBHeader.cleanup0.()V"] = function() {
2014-11-27 05:40:02 +03:00
var lookupId = this.klass.classInfo.getField("I.lookupId.I").get(this);
2014-10-21 11:21:51 +04:00
if (MIDP.RecordStoreCache[lookupId] &&
--MIDP.RecordStoreCache[lookupId].refCount <= 0) {
// Set to null instead of removing from array to maintain
// correspondence between lookup IDs and array indices.
MIDP.RecordStoreCache[lookupId] = null;
}
2014-12-19 00:03:59 +03:00
};
// In the reference implementation, finalize is identical to cleanup0.
Native["com/sun/midp/rms/RecordStoreSharedDBHeader.finalize.()V"] =
Native["com/sun/midp/rms/RecordStoreSharedDBHeader.cleanup0.()V"];
2014-12-19 00:03:59 +03:00
Native["com/sun/midp/rms/RecordStoreRegistry.getRecordStoreListeners.(ILjava/lang/String;)[I"] =
function(suiteId, storeName) {
console.warn("RecordStoreRegistry.getRecordStoreListeners.(IL...String;)[I not implemented (" +
suiteId + ", " + util.fromJavaString(storeName) + ")");
return null;
2014-12-19 00:03:59 +03:00
};
2014-12-19 00:03:59 +03:00
Native["com/sun/midp/rms/RecordStoreRegistry.sendRecordStoreChangeEvent.(ILjava/lang/String;II)V"] =
function(suiteId, storeName, changeType, recordId) {
console.warn("RecordStoreRegistry.sendRecordStoreChangeEvent.(IL...String;II)V not implemented (" +
suiteId + ", " + util.fromJavaString(storeName) + ", " + changeType + ", " + recordId + ")");
2014-12-19 00:03:59 +03:00
};
2014-12-19 00:03:59 +03:00
Native["com/sun/midp/rms/RecordStoreRegistry.startRecordStoreListening.(ILjava/lang/String;)V"] =
function(suiteId, storeName) {
2014-09-04 20:54:20 +04:00
console.warn("RecordStoreRegistry.startRecordStoreListening.(IL...String;)V not implemented (" +
suiteId + ", " + util.fromJavaString(storeName) + ")");
2014-12-19 00:03:59 +03:00
};
2014-09-04 20:54:20 +04:00
2014-12-19 00:03:59 +03:00
Native["com/sun/midp/rms/RecordStoreRegistry.stopRecordStoreListening.(ILjava/lang/String;)V"] =
function(suiteId, storeName) {
console.warn("RecordStoreRegistry.stopRecordStoreListening.(IL...String;)V not implemented (" +
suiteId + ", " + util.fromJavaString(storeName) + ")");
2014-12-19 00:03:59 +03:00
};
2014-12-19 00:03:59 +03:00
Native["com/sun/midp/rms/RecordStoreRegistry.stopAllRecordStoreListeners.(I)V"] = function(taskId) {
console.warn("RecordStoreRegistry.stopAllRecordStoreListeners.(I)V not implemented (" + taskId + ")");
2014-12-19 00:03:59 +03:00
};
2014-09-09 03:36:43 +04:00
2015-02-07 00:07:38 +03:00
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.create.()V"] = function() {
var pathname = util.fromJavaString(this.$nativePath);
DEBUG_FS && console.log("DefaultFileHandler.create: " + pathname);
2015-02-05 23:34:32 +03:00
2015-02-07 00:07:38 +03:00
var stat = fs.stat(pathname);
2015-02-05 23:34:32 +03:00
2015-02-07 00:07:38 +03:00
if (stat !== null || !fs.create(pathname, new Blob())) {
throw $.newIOException("error creating " + pathname);
2015-02-05 23:34:32 +03:00
}
};
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.exists.()Z"] = function() {
2015-02-06 22:48:20 +03:00
var pathname = util.fromJavaString(this.$nativePath);
2015-02-05 23:34:32 +03:00
var exists = fs.exists(pathname);
DEBUG_FS && console.log("DefaultFileHandler.exists: " + pathname + " " + exists);
2015-02-05 23:34:32 +03:00
return exists ? 1 : 0;
};
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.isDirectory.()Z"] = function() {
2015-02-06 22:48:20 +03:00
var pathname = util.fromJavaString(this.$nativePath);
2015-02-05 23:34:32 +03:00
var stat = fs.stat(pathname);
var isDirectory = !!stat && stat.isDir;
DEBUG_FS && console.log("DefaultFileHandler.isDirectory: " + pathname + " " + isDirectory);
2015-02-05 23:34:32 +03:00
return isDirectory ? 1 : 0;
}
2015-02-07 00:07:38 +03:00
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.delete.()V"] = function() {
2015-02-06 22:48:20 +03:00
var pathname = util.fromJavaString(this.$nativePath);
DEBUG_FS && console.log("DefaultFileHandler.delete: " + pathname);
2015-02-07 00:07:38 +03:00
if (!fs.remove(pathname)) {
throw $.newIOException();
2015-02-05 23:34:32 +03:00
}
};
2015-02-07 00:07:38 +03:00
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.rename0.(Ljava/lang/String;)V"] = function(newName) {
var pathname = util.fromJavaString(this.$nativePath);
var newPathname = util.fromJavaString(newName);
DEBUG_FS && console.log("DefaultFileHandler.rename0: " + pathname + " to " + newPathname);
if (fs.exists(newPathname)) {
throw $.newIOException("file with new name exists");
}
if (!fs.rename(pathname, newPathname)) {
throw $.newIOException("error renaming file");
2015-02-07 00:07:38 +03:00
}
2015-02-05 23:34:32 +03:00
};
2015-02-07 00:07:38 +03:00
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.truncate.(J)V"] = function(byteOffset) {
2015-02-06 22:48:20 +03:00
var pathname = util.fromJavaString(this.$nativePath);
DEBUG_FS && console.log("DefaultFileHandler.lastModified: " + pathname);
2015-02-05 23:34:32 +03:00
var stat = fs.stat(pathname);
2015-02-07 00:07:38 +03:00
if (!stat) {
throw $.newIOException("file does not exist");
}
if (stat.isDir) {
throw $.newIOException("file is directory");
2015-02-05 23:34:32 +03:00
}
2015-02-07 00:07:38 +03:00
// TODO: If the file is open, flush it first.
2015-02-07 00:07:38 +03:00
fs.truncate(pathname, byteOffset.toNumber());
2015-02-05 23:34:32 +03:00
};
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.fileSize.()J"] = function() {
2015-02-06 22:48:20 +03:00
var pathname = util.fromJavaString(this.$nativePath);
DEBUG_FS && console.log("DefaultFileHandler.fileSize: " + pathname);
2015-02-05 23:34:32 +03:00
return Long.fromNumber(fs.size(pathname));
};
2015-02-07 00:07:38 +03:00
addUnimplementedNative("com/sun/cdc/io/j2me/file/DefaultFileHandler.directorySize.(Z)J", Long.fromNumber(0));
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.canRead.()Z"] = function() {
var pathname = util.fromJavaString(this.$nativePath);
DEBUG_FS && console.log("DefaultFileHandler.canRead: " + pathname);
2015-02-07 00:07:38 +03:00
return fs.exists(pathname) ? 1 : 0;
};
2015-02-06 22:48:20 +03:00
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.canWrite.()Z"] = function() {
var pathname = util.fromJavaString(this.$nativePath);
DEBUG_FS && console.log("DefaultFileHandler.canWrite: " + pathname);
2015-02-06 22:48:20 +03:00
return fs.exists(pathname) ? 1 : 0;
};
2015-02-05 23:34:32 +03:00
2015-02-07 00:07:38 +03:00
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.isHidden0.()Z"] = function() {
// Per the comment in DefaultFileHandler.isHidden, we pretend we're Unix
// and always return false.
return 0;
};
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.setReadable.(Z)V"] = function() {
var pathname = util.fromJavaString(this.$nativePath);
if (!fs.exists(pathname)) {
throw $.newIOException("file does not exist");
}
// Otherwise this is a noop, as files are always readable in our filesystem.
2015-02-07 00:07:38 +03:00
};
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.setWritable.(Z)V"] = function() {
var pathname = util.fromJavaString(this.$nativePath);
if (!fs.exists(pathname)) {
throw $.newIOException("file does not exist");
}
// Otherwise this is a noop, as files are always writable in our filesystem.
2015-02-07 00:07:38 +03:00
};
addUnimplementedNative("com/sun/cdc/io/j2me/file/DefaultFileHandler.setHidden0.(Z)V");
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.mkdir.()V"] = function() {
var pathname = util.fromJavaString(this.$nativePath);
DEBUG_FS && console.log("DefaultFileHandler.mkdir: " + pathname);
2015-02-07 00:07:38 +03:00
if (!fs.mkdir(pathname)) {
throw $.newIOException("error creating " + pathname);
};
};
// Pretend there is 1GiB in total and available.
addUnimplementedNative("com/sun/cdc/io/j2me/file/DefaultFileHandler.availableSize.()J",
Long.fromNumber(1024 * 1024 * 1024));
addUnimplementedNative("com/sun/cdc/io/j2me/file/DefaultFileHandler.totalSize.()J",
Long.fromNumber(1024 * 1024 * 1024));
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.lastModified.()J"] = function() {
var pathname = util.fromJavaString(this.$nativePath);
DEBUG_FS && console.log("DefaultFileHandler.lastModified: " + pathname);
2015-02-07 00:07:38 +03:00
var stat = fs.stat(pathname);
return Long.fromNumber(stat != null ? stat.mtime : 0);
};
MIDP.markFileHandler = function(fileHandler, mode, state) {
switch(mode) {
case "read":
fileHandler.$isOpenForRead = state ? 1 : 0;
break;
case "write":
fileHandler.$isOpenForWrite = state ? 1 : 0;
break;
}
};
MIDP.openFileHandler = function(fileHandler, mode) {
var pathname = util.fromJavaString(fileHandler.$nativePath);
DEBUG_FS && console.log("MIDP.openFileHandler: " + pathname + " for " + mode);
2015-02-06 22:48:20 +03:00
if (fileHandler.$nativeDescriptor !== -1) {
// The file is already open, so we only have to reset its position
// and mark it as open.
var fd = fileHandler.$nativeDescriptor;
2015-02-07 00:07:38 +03:00
fs.setpos(fd, 0);
MIDP.markFileHandler(fileHandler, mode, true);
2015-02-06 22:48:20 +03:00
return;
}
var stat = fs.stat(pathname);
2015-02-06 22:48:20 +03:00
if (!stat) {
throw $.newIOException("file does not exist");
2015-02-06 22:48:20 +03:00
}
2015-02-06 22:48:20 +03:00
if (stat.isDir) {
throw $.newIOException("file is a directory");
}
2015-02-05 23:34:32 +03:00
asyncImpl("I", new Promise(function(resolve, reject) {
fs.open(pathname, function(fd) {
fileHandler.$nativeDescriptor = fd;
MIDP.markFileHandler(fileHandler, mode, true);
2015-02-05 23:34:32 +03:00
resolve();
});
}));
};
MIDP.closeFileHandler = function(fileHandler, mode) {
var pathname = util.fromJavaString(fileHandler.$nativePath);
DEBUG_FS && console.log("MIDP.closeFileHandler: " + pathname + " for " + mode);
2015-02-05 23:34:32 +03:00
MIDP.markFileHandler(fileHandler, mode, false);
2015-02-06 22:48:20 +03:00
var isOpenForOtherMode;
switch(mode) {
case "read":
isOpenForOtherMode = fileHandler.$isOpenForWrite;
break;
case "write":
isOpenForOtherMode = fileHandler.$isOpenForRead;
break;
2015-02-06 22:48:20 +03:00
}
// If the file isn't open for the other mode, but it still has a native
// descriptor, then it's time to close the native file. Otherwise, we leave
// it open until it gets closed for the other mode.
if (isOpenForOtherMode === 0 && fileHandler.$nativeDescriptor !== -1) {
fs.close(fileHandler.$nativeDescriptor);
fileHandler.$nativeDescriptor = -1;
2015-02-06 22:48:20 +03:00
}
};
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.openForRead.()V"] = function() {
MIDP.openFileHandler(this, "read");
};
2015-02-06 22:48:20 +03:00
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.closeForRead.()V"] = function() {
MIDP.closeFileHandler(this, "read");
};
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.openForWrite.()V"] = function() {
MIDP.openFileHandler(this, "write");
2015-02-05 23:34:32 +03:00
};
2015-02-07 00:07:38 +03:00
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.closeForWrite.()V"] = function() {
MIDP.closeFileHandler(this, "write");
2015-02-07 00:07:38 +03:00
};
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.closeForReadWrite.()V"] = function() {
MIDP.closeFileHandler(this, "read");
MIDP.closeFileHandler(this, "write");
2015-02-07 00:07:38 +03:00
};
2015-02-05 23:34:32 +03:00
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.read.([BII)I"] = function(b, off, len) {
2015-02-06 22:48:20 +03:00
var pathname = util.fromJavaString(this.$nativePath);
DEBUG_FS && console.log("DefaultFileHandler.read: " + pathname);
var fd = this.$nativeDescriptor;
2015-02-05 23:34:32 +03:00
if (off < 0 || len < 0 || off > b.byteLength || (b.byteLength - off) < len) {
throw $.newIOException();
}
if (b.byteLength == 0 || len == 0) {
return 0;
}
var curpos = fs.getpos(fd);
var data = fs.read(fd, curpos, curpos + len);
b.set(data, off);
return (data.byteLength > 0) ? data.byteLength : -1;
};
2015-02-07 00:07:38 +03:00
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.write.([BII)I"] = function(b, off, len) {
2015-02-06 22:48:20 +03:00
var pathname = util.fromJavaString(this.$nativePath);
DEBUG_FS && console.log("DefaultFileHandler.write: " + pathname);
var fd = this.$nativeDescriptor;
2015-02-07 00:07:38 +03:00
fs.write(fd, b.subarray(off, off + len));
// The "length of data really written," which is always the length requested
// in our implementation.
return len;
2015-02-05 23:34:32 +03:00
};
2015-02-07 00:07:38 +03:00
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.positionForWrite.(J)V"] = function(offset) {
2015-02-06 22:48:20 +03:00
var pathname = util.fromJavaString(this.$nativePath);
DEBUG_FS && console.log("DefaultFileHandler.positionForWrite: " + pathname);
var fd = this.$nativeDescriptor;
2015-02-07 00:07:38 +03:00
fs.setpos(fd, offset.toNumber());
2015-02-05 23:34:32 +03:00
};
2015-02-07 00:07:38 +03:00
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.flush.()V"] = function() {
2015-02-06 22:48:20 +03:00
var pathname = util.fromJavaString(this.$nativePath);
DEBUG_FS && console.log("DefaultFileHandler.flush: " + pathname);
var fd = this.$nativeDescriptor;
2015-02-10 02:47:19 +03:00
fs.flush(fd);
2015-02-05 23:34:32 +03:00
};
2015-02-07 00:07:38 +03:00
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.close.()V"] = function() {
2015-02-06 22:48:20 +03:00
var pathname = util.fromJavaString(this.$nativePath);
DEBUG_FS && console.log("DefaultFileHandler.close: " + pathname);
MIDP.closeFileHandler(this, "read");
MIDP.closeFileHandler(this, "write");
2015-02-05 23:34:32 +03:00
};
2015-02-07 00:29:05 +03:00
// Not implemented because we don't use native pointers, so we've commented out
// calls to this private method in DefaultFileHandler.
addUnimplementedNative("com/sun/cdc/io/j2me/file/DefaultFileHandler.getNativeName.(Ljava/lang/String;J)J",
Long.fromNumber(0));
2015-02-07 00:07:38 +03:00
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.getFileSeparator.()C"] = function() {
return "/".charCodeAt(0);
}
2015-02-06 22:48:20 +03:00
2015-02-07 00:07:38 +03:00
MIDP.openDirs = new Map();
MIDP.openDirHandle = 0;
2015-02-06 22:48:20 +03:00
2015-02-07 00:07:38 +03:00
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.openDir.()J"] = function() {
var pathname = util.fromJavaString(this.$nativePath);
DEBUG_FS && console.log("DefaultFileHandler.openDir: " + pathname);
2015-02-07 00:07:38 +03:00
try {
var files = fs.list(pathname);
} catch(ex) {
if (ex.message == "Path does not exist") {
throw $.newIOException("Directory does not exist: file://" + pathname);
}
if (ex.message == "Path is not a directory") {
throw $.newIOException("Connection is open on a file: file://" + pathname);
}
2015-02-06 22:48:20 +03:00
}
2015-02-07 00:07:38 +03:00
var openDirHandle = ++MIDP.openDirHandle;
2015-02-07 00:07:38 +03:00
MIDP.openDirs.set(openDirHandle, {
files: files,
index: -1,
});
return Long.fromNumber(openDirHandle);
2015-02-05 23:34:32 +03:00
};
2015-02-07 00:07:38 +03:00
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.closeDir.(J)V"] = function(dirHandle) {
MIDP.openDirs.delete(dirHandle.toNumber());
};
2015-02-07 00:07:38 +03:00
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.dirGetNextFile.(JZ)Ljava/lang/String;"] =
function(dirHandle, includeHidden) {
var iterator = MIDP.openDirs.get(dirHandle.toNumber());
var nextFile = iterator.files[++iterator.index];
DEBUG_FS && console.log(iterator.index + " " + nextFile);
2015-02-07 00:07:38 +03:00
return nextFile ? J2ME.newString(nextFile) : null;
};
2015-02-06 19:30:41 +03:00
2015-02-07 00:07:38 +03:00
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.getMountedRoots.()Ljava/lang/String;"] = function() {
return J2ME.newString(MIDP.fsRoots.join("\n"));
};
2015-02-06 19:30:41 +03:00
2015-02-07 00:07:38 +03:00
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.getNativePathForRoot.(Ljava/lang/String;)Ljava/lang/String;"] =
function(root) {
// XXX Ensure root is in MIDP.fsRoots?
DEBUG_FS && console.log("getNativePathForRoot: " + util.fromJavaString(root));
2015-02-07 00:07:38 +03:00
var nativePath = J2ME.newString("/" + util.fromJavaString(root));
return nativePath;
2015-02-06 22:48:20 +03:00
};
2015-02-07 00:07:38 +03:00
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.illegalFileNameChars0.()Ljava/lang/String;"] = function() {
return J2ME.newString('<>:"\\|?');
2015-02-06 22:48:20 +03:00
};
2015-02-07 00:07:38 +03:00
addUnimplementedNative("com/sun/cdc/io/j2me/file/DefaultFileHandler.initialize.()V");
2015-02-06 19:30:41 +03:00
2015-02-07 00:07:38 +03:00
Native["com/sun/cdc/io/j2me/file/DefaultFileHandler.getSuiteIdString.(I)Ljava/lang/String;"] = function(id) {
DEBUG_FS && console.log("getSuiteIdString: " + id);
2015-02-07 00:07:38 +03:00
// return J2ME.newString(id.toString());
// The implementation adds this to the path of the file, presumably
// to segregate files by midlet, but we only run a single midlet
// per installation, so presumably we don't have to do that.
return J2ME.newString("");
};
Native["com/sun/cdc/io/j2me/file/Protocol.available.()I"] = function() {
var pathname = util.fromJavaString(this.$fileHandler.$nativePath);
var fd = this.$fileHandler.$nativeDescriptor;
2015-02-07 00:07:38 +03:00
var available = fs.getsize(fd) - fs.getpos(fd);
DEBUG_FS && console.log("Protocol.available: " + pathname + ": " + available);
2015-02-07 00:07:38 +03:00
return available;
2015-02-06 19:30:41 +03:00
};
Native["com/sun/midp/io/j2me/storage/RandomAccessStream.open.(Ljava/lang/String;I)I"] = function(fileName, mode) {
var path = "/" + util.fromJavaString(fileName);
var ctx = $.ctx;
asyncImpl("I", new Promise(function(resolve, reject) {
function open() {
fs.open(path, function(fd) {
ctx.setAsCurrentContext();
if (fd == -1) {
reject($.newIOException("RandomAccessStream::open(" + path + ") failed opening the file"));
} else {
resolve(fd);
}
});
}
if (fs.exists(path)) {
open();
} else if (mode == 1) {
ctx.setAsCurrentContext();
reject($.newIOException("RandomAccessStream::open(" + path + ") file doesn't exist"));
} else if (fs.create(path, new Blob())) {
open();
} else {
ctx.setAsCurrentContext();
reject($.newIOException("RandomAccessStream::open(" + path + ") failed creating the file"));
}
}));
};
Native["com/sun/midp/io/j2me/storage/RandomAccessStream.read.(I[BII)I"] =
function(handle, buffer, offset, length) {
var from = fs.getpos(handle);
var to = from + length;
var readBytes = fs.read(handle, from, to);
if (readBytes.byteLength <= 0) {
return -1;
}
var subBuffer = buffer.subarray(offset, offset + readBytes.byteLength);
for (var i = 0; i < readBytes.byteLength; i++) {
subBuffer[i] = readBytes[i];
}
return readBytes.byteLength;
};
Native["com/sun/midp/io/j2me/storage/RandomAccessStream.write.(I[BII)V"] =
function(handle, buffer, offset, length) {
fs.write(handle, buffer.subarray(offset, offset + length));
};
Native["com/sun/midp/io/j2me/storage/RandomAccessStream.commitWrite.(I)V"] = function(handle) {
fs.flush(handle);
};
Native["com/sun/midp/io/j2me/storage/RandomAccessStream.position.(II)V"] = function(handle, position) {
fs.setpos(handle, position);
};
Native["com/sun/midp/io/j2me/storage/RandomAccessStream.sizeOf.(I)I"] = function(handle) {
var size = fs.getsize(handle);
if (size == -1) {
throw $.newIOException("RandomAccessStream::sizeOf(" + handle + ") failed");
}
return size;
};
Native["com/sun/midp/io/j2me/storage/RandomAccessStream.close.(I)V"] = function(handle) {
fs.close(handle);
};
Native["javax/microedition/io/file/FileSystemRegistry.initImpl.()V"] = function() {
console.warn("javax/microedition/io/file/FileSystemRegistry.initImpl.()V not implemented");
};
Native["javax/microedition/io/file/FileSystemRegistry.getRootsImpl.()[Ljava/lang/String;"] = function() {
var array = J2ME.newStringArray(MIDP.fsRoots.length);
for (var i = 0; i < MIDP.fsRoots.length; i++) {
array[i] = J2ME.newString(MIDP.fsRoots[i]);
}
return array;
};