2014-08-29 01:04:17 +04:00
|
|
|
/* -*- 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-08-29 01:04:17 +04:00
|
|
|
|
2014-09-30 20:17:16 +04:00
|
|
|
var RECORD_STORE_BASE = "/RecordStore";
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/sun/midp/io/j2me/storage/File.initConfigRoot.(I)Ljava/lang/String;", function(storageId) {
|
2014-10-15 01:51:40 +04:00
|
|
|
return "assets/" + storageId + "/";
|
2014-10-15 03:38:16 +04:00
|
|
|
});
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/sun/midp/midletsuite/MIDletSuiteStorage.getSecureFilenameBase.(I)Ljava/lang/String;", function(id) {
|
2014-10-15 01:51:40 +04:00
|
|
|
return "";
|
|
|
|
});
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-17 02:04:31 +04:00
|
|
|
Native.create("com/sun/midp/rms/RecordStoreUtil.exists.(Ljava/lang/String;Ljava/lang/String;I)Z",
|
2014-10-27 14:37:53 +03:00
|
|
|
function(filenameBase, name, ext) {
|
2014-10-17 02:04:31 +04:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
var path = RECORD_STORE_BASE + "/" + util.fromJavaString(filenameBase) + "/" + util.fromJavaString(name) + "." + ext;
|
|
|
|
fs.exists(path, resolve);
|
2014-08-29 01:04:17 +04:00
|
|
|
});
|
2014-11-04 04:47:51 +03:00
|
|
|
}, true);
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-16 06:48:17 +04:00
|
|
|
Native.create("com/sun/midp/rms/RecordStoreUtil.deleteFile.(Ljava/lang/String;Ljava/lang/String;I)V",
|
2014-10-27 14:37:53 +03:00
|
|
|
function(filenameBase, name, ext) {
|
2014-10-17 02:04:31 +04:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
var path = RECORD_STORE_BASE + "/" + util.fromJavaString(filenameBase) + "/" + util.fromJavaString(name) + "." + ext;
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-17 02:04:31 +04:00
|
|
|
fs.remove(path, resolve);
|
2014-08-29 01:04:17 +04:00
|
|
|
});
|
2014-11-04 04:47:51 +03:00
|
|
|
}, true);
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/sun/midp/rms/RecordStoreFile.spaceAvailableNewRecordStore0.(Ljava/lang/String;I)I", function(filenameBase, storageId) {
|
2014-08-29 01:04:17 +04:00
|
|
|
// 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.
|
2014-10-16 06:48:17 +04:00
|
|
|
return 50 * 1024 * 1024;
|
|
|
|
});
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/sun/midp/rms/RecordStoreFile.spaceAvailableRecordStore.(ILjava/lang/String;I)I", function(handle, filenameBase, storageId) {
|
2014-08-29 01:04:17 +04:00
|
|
|
// 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.
|
2014-10-16 06:48:17 +04:00
|
|
|
return 50 * 1024 * 1024;
|
|
|
|
});
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-17 02:04:31 +04:00
|
|
|
Native.create("com/sun/midp/rms/RecordStoreFile.openRecordStoreFile.(Ljava/lang/String;Ljava/lang/String;I)I",
|
2014-10-27 14:37:53 +03:00
|
|
|
function(filenameBase, name, ext) {
|
2014-10-17 02:04:31 +04:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
var path = RECORD_STORE_BASE + "/" + util.fromJavaString(filenameBase) + "/" + util.fromJavaString(name) + "." + ext;
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-17 02:04:31 +04:00
|
|
|
function openCallback(fd) {
|
|
|
|
if (fd == -1) {
|
|
|
|
reject(new JavaException("java/io/IOException", "openRecordStoreFile: open failed"));
|
|
|
|
} else {
|
|
|
|
resolve(fd); // handle
|
|
|
|
}
|
2014-08-29 01:04:17 +04:00
|
|
|
}
|
|
|
|
|
2014-10-17 02:04:31 +04:00
|
|
|
fs.exists(path, function(exists) {
|
|
|
|
if (exists) {
|
|
|
|
fs.open(path, openCallback);
|
|
|
|
} else {
|
|
|
|
// Per the reference impl, create the file if it doesn't exist.
|
|
|
|
var dirname = fs.dirname(path);
|
|
|
|
fs.mkdirp(dirname, function(created) {
|
|
|
|
if (created) {
|
|
|
|
fs.create(path, new Blob(), function(created) {
|
|
|
|
if (created) {
|
|
|
|
fs.open(path, openCallback);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
reject(new JavaException("java/io/IOException", "openRecordStoreFile: create failed"));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
reject(new JavaException("java/io/IOException", "openRecordStoreFile: mkdirp failed"));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2014-08-29 01:04:17 +04:00
|
|
|
});
|
2014-11-04 04:47:51 +03:00
|
|
|
}, true);
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/sun/midp/rms/RecordStoreFile.setPosition.(II)V", function(handle, pos) {
|
2014-08-29 01:04:17 +04:00
|
|
|
fs.setpos(handle, pos);
|
2014-10-16 06:48:17 +04:00
|
|
|
});
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/sun/midp/rms/RecordStoreFile.readBytes.(I[BII)I", function(handle, buf, offset, numBytes) {
|
2014-08-29 01:04:17 +04:00
|
|
|
var from = fs.getpos(handle);
|
|
|
|
var to = from + numBytes;
|
|
|
|
var readBytes = fs.read(handle, from, to);
|
|
|
|
|
|
|
|
if (readBytes.byteLength <= 0) {
|
2014-10-16 06:48:17 +04:00
|
|
|
throw new JavaException("java/io/IOException", "handle invalid or segment indices out of bounds");
|
2014-08-29 01:04:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
var subBuffer = buf.subarray(offset, offset + readBytes.byteLength);
|
|
|
|
for (var i = 0; i < readBytes.byteLength; i++) {
|
|
|
|
subBuffer[i] = readBytes[i];
|
|
|
|
}
|
2014-10-16 06:48:17 +04:00
|
|
|
return readBytes.byteLength;
|
|
|
|
});
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/sun/midp/rms/RecordStoreFile.writeBytes.(I[BII)V", function(handle, buf, offset, numBytes) {
|
2014-08-29 01:04:17 +04:00
|
|
|
fs.write(handle, buf.subarray(offset, offset + numBytes));
|
2014-10-16 06:48:17 +04:00
|
|
|
});
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/sun/midp/rms/RecordStoreFile.commitWrite.(I)V", function(handle) {
|
2014-11-24 12:49:56 +03:00
|
|
|
fs.flush(handle);
|
|
|
|
});
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/sun/midp/rms/RecordStoreFile.closeFile.(I)V", function(handle) {
|
2014-11-24 12:49:56 +03:00
|
|
|
fs.close(handle);
|
|
|
|
});
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("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-08-29 01:04:17 +04:00
|
|
|
|
|
|
|
MIDP.RecordStoreCache = [];
|
|
|
|
|
2014-10-16 06:48:17 +04:00
|
|
|
Native.create("com/sun/midp/rms/RecordStoreSharedDBHeader.getLookupId0.(ILjava/lang/String;I)I",
|
2014-10-27 14:37:53 +03:00
|
|
|
function(suiteId, jStoreName, headerDataSize) {
|
2014-10-16 06:48:17 +04:00
|
|
|
var storeName = util.fromJavaString(jStoreName);
|
2014-08-29 01:04:17 +04:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2014-10-16 06:48:17 +04:00
|
|
|
return sharedHeader.lookupId;
|
|
|
|
});
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/sun/midp/rms/RecordStoreSharedDBHeader.shareCachedData0.(I[BI)I", function(lookupId, headerData, headerDataSize) {
|
2014-08-29 01:04:17 +04:00
|
|
|
var sharedHeader = MIDP.RecordStoreCache[lookupId];
|
|
|
|
if (!sharedHeader) {
|
2014-10-16 06:48:17 +04:00
|
|
|
throw new JavaException("java/lang/IllegalStateException", "invalid header lookup ID");
|
2014-08-29 01:04:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!headerData) {
|
2014-10-16 06:48:17 +04:00
|
|
|
throw new JavaException("java/lang/IllegalArgumentException", "header data is null");
|
2014-08-29 01:04:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
var size = headerDataSize;
|
|
|
|
if (size > sharedHeader.headerDataSize) {
|
|
|
|
size = sharedHeader.headerDataSize;
|
|
|
|
}
|
|
|
|
sharedHeader.headerData = headerData.buffer.slice(0, size);
|
|
|
|
++sharedHeader.headerVersion;
|
|
|
|
|
2014-10-16 06:48:17 +04:00
|
|
|
return sharedHeader.headerVersion;
|
|
|
|
});
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-16 06:48:17 +04:00
|
|
|
Native.create("com/sun/midp/rms/RecordStoreSharedDBHeader.updateCachedData0.(I[BII)I",
|
2014-10-27 14:37:53 +03:00
|
|
|
function(lookupId, headerData, headerDataSize, headerVersion) {
|
2014-08-29 01:04:17 +04:00
|
|
|
var sharedHeader = MIDP.RecordStoreCache[lookupId];
|
|
|
|
if (!sharedHeader) {
|
2014-10-16 06:48:17 +04:00
|
|
|
throw new JavaException("java/lang/IllegalStateException", "invalid header lookup ID");
|
2014-08-29 01:04:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!headerData) {
|
2014-10-16 06:48:17 +04:00
|
|
|
throw new JavaException("java/lang/IllegalArgumentException", "header data is null");
|
2014-08-29 01:04:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sharedHeader.headerVersion > headerVersion && sharedHeader.headerData) {
|
|
|
|
var size = sharedHeader.headerDataSize;
|
|
|
|
if (size > headerDataSize) {
|
|
|
|
size = headerDataSize;
|
|
|
|
}
|
2014-12-12 01:36:47 +03:00
|
|
|
var sharedHeaderData = new Int8Array(sharedHeader.headerData);
|
2014-08-29 01:04:17 +04:00
|
|
|
for (var i = 0; i < size; i++) {
|
2014-12-12 01:36:47 +03:00
|
|
|
headerData[i] = sharedHeaderData[i];
|
2014-08-29 01:04:17 +04:00
|
|
|
}
|
2014-10-16 06:48:17 +04:00
|
|
|
return sharedHeader.headerVersion;
|
2014-08-29 01:04:17 +04:00
|
|
|
}
|
|
|
|
|
2014-10-16 06:48:17 +04:00
|
|
|
return headerVersion;
|
|
|
|
});
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/sun/midp/rms/RecordStoreSharedDBHeader.getHeaderRefCount0.(I)I", function(lookupId) {
|
2014-08-29 01:04:17 +04:00
|
|
|
var sharedHeader = MIDP.RecordStoreCache[lookupId];
|
|
|
|
if (!sharedHeader) {
|
2014-10-16 06:48:17 +04:00
|
|
|
throw new JavaException("java/lang/IllegalStateException", "invalid header lookup ID");
|
2014-08-29 01:04:17 +04:00
|
|
|
}
|
|
|
|
|
2014-10-16 06:48:17 +04:00
|
|
|
return sharedHeader.refCount;
|
|
|
|
});
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/sun/midp/rms/RecordStoreSharedDBHeader.cleanup0.()V", function() {
|
2014-10-21 11:21:51 +04:00
|
|
|
var lookupId = this.class.getField("I.lookupId.I").get(this);
|
|
|
|
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-08-29 01:04:17 +04:00
|
|
|
}
|
2014-10-16 06:48:17 +04:00
|
|
|
});
|
2014-08-29 01:04:17 +04: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-10-16 06:48:17 +04:00
|
|
|
Native.create("com/sun/midp/rms/RecordStoreRegistry.getRecordStoreListeners.(ILjava/lang/String;)[I",
|
2014-10-27 14:37:53 +03:00
|
|
|
function(suiteId, storeName) {
|
2014-08-29 01:04:17 +04:00
|
|
|
console.warn("RecordStoreRegistry.getRecordStoreListeners.(IL...String;)[I not implemented (" +
|
2014-10-16 06:48:17 +04:00
|
|
|
suiteId + ", " + util.fromJavaString(storeName) + ")");
|
|
|
|
return null;
|
|
|
|
});
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-16 06:48:17 +04:00
|
|
|
Native.create("com/sun/midp/rms/RecordStoreRegistry.sendRecordStoreChangeEvent.(ILjava/lang/String;II)V",
|
2014-10-27 14:37:53 +03:00
|
|
|
function(suiteId, storeName, changeType, recordId) {
|
2014-08-29 01:04:17 +04:00
|
|
|
console.warn("RecordStoreRegistry.sendRecordStoreChangeEvent.(IL...String;II)V not implemented (" +
|
2014-10-16 06:48:17 +04:00
|
|
|
suiteId + ", " + util.fromJavaString(storeName) + ", " + changeType + ", " + recordId + ")");
|
|
|
|
});
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-16 06:48:17 +04:00
|
|
|
Native.create("com/sun/midp/rms/RecordStoreRegistry.startRecordStoreListening.(ILjava/lang/String;)V",
|
2014-10-27 14:37:53 +03:00
|
|
|
function(suiteId, storeName) {
|
2014-09-04 20:54:20 +04:00
|
|
|
console.warn("RecordStoreRegistry.startRecordStoreListening.(IL...String;)V not implemented (" +
|
2014-10-16 06:48:17 +04:00
|
|
|
suiteId + ", " + util.fromJavaString(storeName) + ")");
|
|
|
|
});
|
2014-09-04 20:54:20 +04:00
|
|
|
|
2014-10-16 06:48:17 +04:00
|
|
|
Native.create("com/sun/midp/rms/RecordStoreRegistry.stopRecordStoreListening.(ILjava/lang/String;)V",
|
2014-10-27 14:37:53 +03:00
|
|
|
function(suiteId, storeName) {
|
2014-08-29 01:04:17 +04:00
|
|
|
console.warn("RecordStoreRegistry.stopRecordStoreListening.(IL...String;)V not implemented (" +
|
2014-10-16 06:48:17 +04:00
|
|
|
suiteId + ", " + util.fromJavaString(storeName) + ")");
|
|
|
|
});
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/sun/midp/rms/RecordStoreRegistry.stopAllRecordStoreListeners.(I)V", function(taskId) {
|
2014-08-29 01:04:17 +04:00
|
|
|
console.warn("RecordStoreRegistry.stopAllRecordStoreListeners.(I)V not implemented (" + taskId + ")");
|
2014-10-16 06:48:17 +04:00
|
|
|
});
|
2014-09-09 03:36:43 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/ibm/oti/connection/file/Connection.isValidFilenameImpl.([B)Z", function(path) {
|
2014-09-09 03:36:43 +04:00
|
|
|
var invalid = ['<', '>', ':', '"', '/', '\\', '|', '*', '?'].map(function(char) {
|
|
|
|
return char.charCodeAt(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
for (var i = 0; i < path.length; i++) {
|
|
|
|
if (path[i] <= 31 || invalid.indexOf(path[i]) != -1) {
|
2014-10-16 06:48:17 +04:00
|
|
|
return false;
|
2014-09-09 03:36:43 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-16 06:48:17 +04:00
|
|
|
return true;
|
|
|
|
});
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/ibm/oti/connection/file/Connection.availableSizeImpl.([B)J", function(path) {
|
2014-09-04 03:31:27 +04:00
|
|
|
// Pretend there is 1 GB available
|
2014-10-16 06:48:17 +04:00
|
|
|
return Long.fromNumber(1024 * 1024 * 1024);
|
|
|
|
});
|
2014-09-04 03:31:27 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/ibm/oti/connection/file/Connection.existsImpl.([B)Z", function(path) {
|
2014-10-17 02:04:31 +04:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
fs.exists(util.decodeUtf8(path), resolve);
|
2014-08-29 01:04:17 +04:00
|
|
|
});
|
2014-11-04 04:47:51 +03:00
|
|
|
}, true);
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/ibm/oti/connection/file/Connection.fileSizeImpl.([B)J", function(path) {
|
2014-10-17 02:04:31 +04:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
fs.size(util.decodeUtf8(path), function(size) {
|
|
|
|
resolve(Long.fromNumber(size));
|
|
|
|
});
|
2014-08-29 01:04:17 +04:00
|
|
|
});
|
2014-11-04 04:47:51 +03:00
|
|
|
}, true);
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/ibm/oti/connection/file/Connection.isDirectoryImpl.([B)Z", function(path) {
|
2014-10-17 02:04:31 +04:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
fs.stat(util.decodeUtf8(path), function(stat) {
|
|
|
|
resolve(!!stat && stat.isDir);
|
|
|
|
});
|
2014-08-29 01:04:17 +04:00
|
|
|
});
|
2014-11-04 04:47:51 +03:00
|
|
|
}, true);
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-17 02:04:31 +04:00
|
|
|
Native.create("com/ibm/oti/connection/file/Connection.listImpl.([B[BZ)[[B",
|
2014-10-27 15:13:54 +03:00
|
|
|
function(jPath, filterArray, includeHidden) {
|
2014-10-17 02:04:31 +04:00
|
|
|
var path = util.decodeUtf8(jPath);
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
var filter = "";
|
|
|
|
if (filterArray) {
|
|
|
|
filter = util.decodeUtf8(filterArray);
|
|
|
|
if (filter.contains("?")) {
|
|
|
|
console.warn("Our implementation of Connection::listImpl assumes the filter doesn't contain the ? wildcard character");
|
|
|
|
}
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-17 02:04:31 +04:00
|
|
|
// Translate the filter to a regular expression
|
2014-09-12 02:40:31 +04:00
|
|
|
|
2014-10-17 02:04:31 +04:00
|
|
|
// Escape regular expression (everything but * and ?)
|
|
|
|
// Source of the regexp: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
|
|
|
|
filter = filter.replace(/([.+^${}()|\[\]\/\\])/g, "\\$1");
|
2014-09-12 21:37:33 +04:00
|
|
|
|
2014-10-17 02:04:31 +04:00
|
|
|
// Transform * to .+
|
|
|
|
filter = filter.replace(/\*/g, ".+");
|
2014-09-12 21:37:33 +04:00
|
|
|
|
2014-10-17 02:04:31 +04:00
|
|
|
filter += "$";
|
|
|
|
}
|
2014-09-12 21:37:33 +04:00
|
|
|
|
2014-12-22 12:04:18 +03:00
|
|
|
fs.list(path, function(error, files) {
|
|
|
|
// For these exceptions, we append a URL representation of the path
|
|
|
|
// in Connection.listInternal, so we don't have to implement getURL
|
|
|
|
// in native code.
|
|
|
|
if (error && error.message == "Path does not exist") {
|
|
|
|
return reject(new JavaException("java/io/IOException", "Directory does not exist: "));
|
|
|
|
}
|
|
|
|
if (error && error.message == "Path is not a directory") {
|
|
|
|
return reject(new JavaException("java/io/IOException", "Connection is open on a file: "));
|
|
|
|
}
|
|
|
|
|
2014-10-17 02:04:31 +04:00
|
|
|
var regexp = new RegExp(filter);
|
|
|
|
files = files.filter(regexp.test.bind(regexp));
|
2014-10-27 15:13:54 +03:00
|
|
|
var filesArray = util.newArray("[[B", files.length);
|
2014-12-22 13:27:19 +03:00
|
|
|
var encoder = new TextEncoder("utf-8");
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-12-22 13:27:19 +03:00
|
|
|
files.forEach(function(file, i) {
|
|
|
|
var bytesFile = encoder.encode(file);
|
|
|
|
var fileArray = util.newPrimitiveArray("B", bytesFile.byteLength);
|
|
|
|
fileArray.set(bytesFile);
|
|
|
|
filesArray[i] = fileArray;
|
2014-10-17 02:04:31 +04:00
|
|
|
});
|
2014-12-22 13:27:19 +03:00
|
|
|
|
|
|
|
resolve(filesArray);
|
2014-09-23 03:16:44 +04:00
|
|
|
});
|
2014-08-29 01:04:17 +04:00
|
|
|
});
|
2014-11-04 04:47:51 +03:00
|
|
|
}, true);
|
2014-08-29 01:04:17 +04:00
|
|
|
|
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/ibm/oti/connection/file/Connection.mkdirImpl.([B)I", function(path) {
|
2014-10-17 02:04:31 +04:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
fs.mkdir(util.decodeUtf8(path), function(created) {
|
|
|
|
// IBM's implementation returns different error numbers, we don't care
|
|
|
|
resolve(created ? 0 : 42);
|
|
|
|
});
|
2014-08-29 01:04:17 +04:00
|
|
|
});
|
2014-11-04 04:47:51 +03:00
|
|
|
}, true);
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/ibm/oti/connection/file/Connection.newFileImpl.([B)I", function(jPath) {
|
2014-10-17 02:04:31 +04:00
|
|
|
var path = util.decodeUtf8(jPath);
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-17 02:04:31 +04:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
// IBM's implementation returns different error numbers, we don't care
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-17 02:04:31 +04:00
|
|
|
fs.exists(path, function(exists) {
|
|
|
|
if (exists) {
|
|
|
|
fs.truncate(path, function(truncated) {
|
|
|
|
resolve(truncated ? 0 : 42);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
fs.create(path, new Blob(), function(created) {
|
|
|
|
resolve(created ? 0 : 42);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2014-08-29 01:04:17 +04:00
|
|
|
});
|
2014-11-04 04:47:51 +03:00
|
|
|
}, true);
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/ibm/oti/connection/file/Connection.deleteFileImpl.([B)Z", function(path) {
|
2014-10-17 02:04:31 +04:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
fs.remove(util.decodeUtf8(path), resolve);
|
2014-08-29 01:04:17 +04:00
|
|
|
});
|
2014-11-04 04:47:51 +03:00
|
|
|
}, true);
|
2014-10-17 02:04:31 +04:00
|
|
|
|
|
|
|
Native["com/ibm/oti/connection/file/Connection.deleteDirImpl.([B)Z"] =
|
|
|
|
Native["com/ibm/oti/connection/file/Connection.deleteFileImpl.([B)Z"]
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/ibm/oti/connection/file/Connection.isReadOnlyImpl.([B)Z", function(path) {
|
2014-10-16 06:48:17 +04:00
|
|
|
console.warn("Connection.isReadOnlyImpl.([B)Z not implemented (" + util.decodeUtf8(path) + ")");
|
|
|
|
return false;
|
|
|
|
});
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/ibm/oti/connection/file/Connection.isWriteOnlyImpl.([B)Z", function(path) {
|
2014-10-16 06:48:17 +04:00
|
|
|
console.warn("Connection.isWriteOnlyImpl.([B)Z not implemented (" + util.decodeUtf8(path) + ")");
|
|
|
|
return false;
|
|
|
|
});
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/ibm/oti/connection/file/Connection.lastModifiedImpl.([B)J", function(path) {
|
2014-10-17 02:04:31 +04:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
fs.stat(util.decodeUtf8(path), function(stat) {
|
|
|
|
resolve(Long.fromNumber(stat != null ? stat.mtime : 0));
|
|
|
|
});
|
2014-09-17 03:40:34 +04:00
|
|
|
});
|
2014-11-04 04:47:51 +03:00
|
|
|
}, true);
|
2014-09-11 03:42:44 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/ibm/oti/connection/file/Connection.renameImpl.([B[B)V", function(oldPath, newPath) {
|
2014-10-17 02:04:31 +04:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
fs.rename(util.decodeUtf8(oldPath), util.decodeUtf8(newPath), function(renamed) {
|
|
|
|
if (!renamed) {
|
|
|
|
reject(new JavaException("java/io/IOException", "Rename failed"));
|
|
|
|
return;
|
|
|
|
}
|
2014-10-02 04:19:33 +04:00
|
|
|
|
2014-10-17 02:04:31 +04:00
|
|
|
resolve();
|
|
|
|
});
|
2014-08-29 01:04:17 +04:00
|
|
|
});
|
2014-11-04 04:47:51 +03:00
|
|
|
}, true);
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/ibm/oti/connection/file/Connection.truncateImpl.([BJ)V", function(path, newLength, _) {
|
2014-10-17 02:04:31 +04:00
|
|
|
return new Promise(function(resolve, reject) {
|
2014-11-07 20:34:18 +03:00
|
|
|
fs.open(util.decodeUtf8(path), function(fd) {
|
|
|
|
if (fd == -1) {
|
2014-10-17 02:04:31 +04:00
|
|
|
reject(new JavaException("java/io/IOException", "truncate failed"));
|
2014-11-07 20:34:18 +03:00
|
|
|
return;
|
2014-10-17 02:04:31 +04:00
|
|
|
}
|
2014-11-07 20:34:18 +03:00
|
|
|
|
|
|
|
fs.ftruncate(fd, newLength.toNumber());
|
2014-11-24 12:49:56 +03:00
|
|
|
fs.close(fd);
|
|
|
|
resolve();
|
2014-08-29 01:04:17 +04:00
|
|
|
});
|
|
|
|
});
|
2014-11-04 04:47:51 +03:00
|
|
|
}, true);
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/ibm/oti/connection/file/FCInputStream.openImpl.([B)I", function(path) {
|
2014-10-17 02:04:31 +04:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
fs.open(util.decodeUtf8(path), resolve);
|
2014-08-29 01:04:17 +04:00
|
|
|
});
|
2014-11-04 04:47:51 +03:00
|
|
|
}, true);
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/ibm/oti/connection/file/FCInputStream.availableImpl.(I)I", function(fd) {
|
2014-10-16 06:48:17 +04:00
|
|
|
return fs.getsize(fd) - fs.getpos(fd);
|
|
|
|
});
|
2014-09-11 03:59:10 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/ibm/oti/connection/file/FCInputStream.skipImpl.(JI)J", function(count, _, fd) {
|
2014-09-11 03:59:10 +04:00
|
|
|
var curpos = fs.getpos(fd);
|
|
|
|
var size = fs.getsize(fd);
|
2014-09-11 04:51:08 +04:00
|
|
|
if (curpos + count.toNumber() > size) {
|
2014-09-11 03:59:10 +04:00
|
|
|
fs.setpos(fd, size);
|
2014-10-16 06:48:17 +04:00
|
|
|
return Long.fromNumber(size - curpos);
|
2014-09-11 03:59:10 +04:00
|
|
|
}
|
|
|
|
|
2014-10-16 06:48:17 +04:00
|
|
|
fs.setpos(fd, curpos + count.toNumber());
|
|
|
|
return count;
|
|
|
|
});
|
2014-09-11 21:10:49 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/ibm/oti/connection/file/FCInputStream.readImpl.([BIII)I", function(buffer, offset, count, fd) {
|
2014-09-11 21:10:49 +04:00
|
|
|
if (offset < 0 || count < 0 || offset > buffer.byteLength || (buffer.byteLength - offset) < count) {
|
2014-10-16 06:48:17 +04:00
|
|
|
throw new JavaException("java/lang/IndexOutOfBoundsException");
|
2014-09-11 21:12:29 +04:00
|
|
|
}
|
2014-09-11 21:10:49 +04:00
|
|
|
|
|
|
|
if (buffer.byteLength == 0 || count == 0) {
|
2014-10-16 06:48:17 +04:00
|
|
|
return 0;
|
2014-09-11 21:10:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
var curpos = fs.getpos(fd);
|
|
|
|
var data = fs.read(fd, curpos, curpos + count);
|
2014-09-11 21:45:27 +04:00
|
|
|
buffer.set(data, offset);
|
2014-09-11 21:10:49 +04:00
|
|
|
|
2014-10-16 06:48:17 +04:00
|
|
|
return (data.byteLength > 0) ? data.byteLength : -1;
|
|
|
|
});
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/ibm/oti/connection/file/FCInputStream.readByteImpl.(I)I", function(fd) {
|
2014-08-29 01:04:17 +04:00
|
|
|
var curpos = fs.getpos(fd);
|
|
|
|
|
|
|
|
var data = fs.read(fd, curpos, curpos+1);
|
|
|
|
|
2014-10-16 06:48:17 +04:00
|
|
|
return (data.byteLength > 0) ? data[0] : -1;
|
|
|
|
});
|
2014-09-12 04:58:29 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/ibm/oti/connection/file/FCInputStream.closeImpl.(I)V", function(fd) {
|
2014-09-12 04:58:29 +04:00
|
|
|
if (fd >= 0) {
|
|
|
|
fs.close(fd);
|
|
|
|
}
|
2014-10-16 06:48:17 +04:00
|
|
|
});
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/ibm/oti/connection/file/FCOutputStream.closeImpl.(I)V", function(fd) {
|
2014-11-24 03:59:08 +03:00
|
|
|
fs.close(fd);
|
|
|
|
});
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/ibm/oti/connection/file/FCOutputStream.openImpl.([B)I", function(jPath) {
|
2014-10-17 02:04:31 +04:00
|
|
|
var path = util.decodeUtf8(jPath);
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-17 02:04:31 +04:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
fs.exists(path, function(exists) {
|
|
|
|
if (exists) {
|
2014-12-23 03:03:43 +03:00
|
|
|
fs.open(path, function(fd) {
|
|
|
|
if (fd != -1) {
|
|
|
|
fs.ftruncate(fd, 0);
|
2014-10-17 02:04:31 +04:00
|
|
|
}
|
2014-12-23 03:03:43 +03:00
|
|
|
resolve(fd);
|
2014-10-17 02:04:31 +04:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
fs.create(path, new Blob(), function(created) {
|
|
|
|
if (created) {
|
|
|
|
fs.open(path, resolve);
|
|
|
|
} else {
|
|
|
|
resolve(-1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2014-08-29 01:04:17 +04:00
|
|
|
});
|
|
|
|
});
|
2014-11-04 04:47:51 +03:00
|
|
|
}, true);
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/ibm/oti/connection/file/FCOutputStream.openOffsetImpl.([BJ)I", function(jPath, offset, _) {
|
2014-10-17 02:04:31 +04:00
|
|
|
var path = util.decodeUtf8(jPath);
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-17 02:04:31 +04:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
function open() {
|
|
|
|
fs.open(path, function(fd) {
|
|
|
|
fs.setpos(fd, offset.toNumber());
|
|
|
|
resolve(fd);
|
2014-08-29 01:04:17 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-10-17 02:04:31 +04:00
|
|
|
fs.exists(path, function(exists) {
|
|
|
|
if (exists) {
|
|
|
|
open();
|
|
|
|
} else {
|
|
|
|
fs.create(path, new Blob(), function(created) {
|
|
|
|
if (created) {
|
|
|
|
open();
|
|
|
|
} else {
|
|
|
|
resolve(-1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2014-11-04 04:47:51 +03:00
|
|
|
}, true);
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/ibm/oti/connection/file/FCOutputStream.syncImpl.(I)V", function(fd) {
|
2014-11-24 12:49:56 +03:00
|
|
|
fs.flush(fd);
|
2014-11-24 03:59:08 +03:00
|
|
|
});
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/ibm/oti/connection/file/FCOutputStream.writeByteImpl.(II)V", function(val, fd) {
|
2014-09-11 23:35:07 +04:00
|
|
|
var buf = new Uint8Array(1);
|
2014-09-12 21:29:41 +04:00
|
|
|
buf[0] = val;
|
2014-09-11 23:35:07 +04:00
|
|
|
fs.write(fd, buf);
|
2014-10-16 06:48:17 +04:00
|
|
|
});
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-16 06:48:17 +04:00
|
|
|
Native.create("com/ibm/oti/connection/file/FCOutputStream.writeImpl.([BIII)V",
|
2014-10-27 14:37:53 +03:00
|
|
|
function(byteArray, offset, count, fd) {
|
2014-08-29 01:04:17 +04:00
|
|
|
fs.write(fd, byteArray.subarray(offset, offset+count));
|
2014-10-16 06:48:17 +04:00
|
|
|
});
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/sun/midp/io/j2me/storage/RandomAccessStream.open.(Ljava/lang/String;I)I", function(fileName, mode) {
|
2014-10-17 02:04:31 +04:00
|
|
|
var path = "/" + util.fromJavaString(fileName);
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-17 02:04:31 +04:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
function open() {
|
|
|
|
fs.open(path, function(fd) {
|
|
|
|
if (fd == -1) {
|
|
|
|
reject(new JavaException("java/io/IOException",
|
|
|
|
"RandomAccessStream::open(" + path + ") failed opening the file"));
|
2014-08-29 01:04:17 +04:00
|
|
|
} else {
|
2014-10-17 02:04:31 +04:00
|
|
|
resolve(fd);
|
2014-08-29 01:04:17 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-10-17 02:04:31 +04:00
|
|
|
fs.exists(path, function(exists) {
|
|
|
|
if (exists) {
|
|
|
|
open();
|
|
|
|
} else {
|
|
|
|
fs.create(path, new Blob(), function(created) {
|
|
|
|
if (created) {
|
|
|
|
open();
|
|
|
|
} else {
|
|
|
|
reject(new JavaException("java/io/IOException",
|
|
|
|
"RandomAccessStream::open(" + path + ") failed creating the file"));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2014-11-04 04:47:51 +03:00
|
|
|
}, true);
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-16 06:48:17 +04:00
|
|
|
Native.create("com/sun/midp/io/j2me/storage/RandomAccessStream.read.(I[BII)I",
|
2014-10-27 14:37:53 +03:00
|
|
|
function(handle, buffer, offset, length) {
|
2014-08-29 01:04:17 +04:00
|
|
|
var from = fs.getpos(handle);
|
|
|
|
var to = from + length;
|
|
|
|
var readBytes = fs.read(handle, from, to);
|
|
|
|
|
|
|
|
if (readBytes.byteLength <= 0) {
|
2014-10-16 06:48:17 +04:00
|
|
|
return -1;
|
2014-08-29 01:04:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
var subBuffer = buffer.subarray(offset, offset + readBytes.byteLength);
|
|
|
|
for (var i = 0; i < readBytes.byteLength; i++) {
|
|
|
|
subBuffer[i] = readBytes[i];
|
|
|
|
}
|
2014-10-16 06:48:17 +04:00
|
|
|
return readBytes.byteLength;
|
|
|
|
});
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-16 06:48:17 +04:00
|
|
|
Native.create("com/sun/midp/io/j2me/storage/RandomAccessStream.write.(I[BII)V",
|
2014-10-27 14:37:53 +03:00
|
|
|
function(handle, buffer, offset, length) {
|
2014-08-29 01:04:17 +04:00
|
|
|
fs.write(handle, buffer.subarray(offset, offset + length));
|
2014-10-16 06:48:17 +04:00
|
|
|
});
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/sun/midp/io/j2me/storage/RandomAccessStream.commitWrite.(I)V", function(handle) {
|
2014-11-24 12:49:56 +03:00
|
|
|
fs.flush(handle);
|
|
|
|
});
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/sun/midp/io/j2me/storage/RandomAccessStream.position.(II)V", function(handle, position) {
|
2014-08-29 01:04:17 +04:00
|
|
|
fs.setpos(handle, position);
|
2014-10-16 06:48:17 +04:00
|
|
|
});
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/sun/midp/io/j2me/storage/RandomAccessStream.sizeOf.(I)I", function(handle) {
|
2014-08-29 01:04:17 +04:00
|
|
|
var size = fs.getsize(handle);
|
|
|
|
|
|
|
|
if (size == -1) {
|
2014-10-16 06:48:17 +04:00
|
|
|
throw new JavaException("java/io/IOException", "RandomAccessStream::sizeOf(" + handle + ") failed");
|
2014-08-29 01:04:17 +04:00
|
|
|
}
|
|
|
|
|
2014-10-16 06:48:17 +04:00
|
|
|
return size;
|
|
|
|
});
|
2014-08-29 01:04:17 +04:00
|
|
|
|
2014-10-27 14:37:53 +03:00
|
|
|
Native.create("com/sun/midp/io/j2me/storage/RandomAccessStream.close.(I)V", function(handle) {
|
2014-11-24 12:49:56 +03:00
|
|
|
fs.close(handle);
|
|
|
|
});
|
2014-11-19 17:45:21 +03:00
|
|
|
|
|
|
|
Native.create("javax/microedition/io/file/FileSystemRegistry.getRootsImpl.()[Ljava/lang/String;", function() {
|
|
|
|
var array = util.newArray("[Ljava/lang/String;", 1);
|
|
|
|
array[0] = util.newString("");
|
|
|
|
return array;
|
|
|
|
});
|