1
0
mirror of https://github.com/quinton-ashley/java2js synced 2024-12-29 10:11:54 +01:00
java2js/jdk/java/util/Scanner.js

74 lines
1.9 KiB
JavaScript
Raw Permalink Normal View History

2021-09-24 00:36:29 -05:00
jdk.imports['java.util.Scanner'].load = async () => {
2022-04-12 11:32:32 -05:00
const InputStream = await jdk.import('java.io.InputStream');
2022-04-22 14:00:18 -05:00
const FileNotFoundException = await jdk.import('java.io.FileNotFoundException');
2021-09-23 01:41:44 -05:00
class Scanner {
constructor(input) {
2022-04-12 11:32:32 -05:00
if (input.getAbsolutePath) {
this._loading = true;
this._filePath = input.getAbsolutePath();
return;
2021-09-23 01:41:44 -05:00
}
this.in = input;
}
2022-04-12 11:32:32 -05:00
async _loadFile(filePath) {
this.in = new InputStream();
2022-04-22 14:00:18 -05:00
let data = await fetch(filePath);
if (data.status == 404 || data.status == 403) {
throw new FileNotFoundException(filePath);
}
this.in.stream = await data.text();
2022-04-12 11:32:32 -05:00
this._loading = false;
}
async hasNext(pattern) {
if (this._loading) {
await this._loadFile(this._filePath);
}
2022-04-30 10:07:11 -05:00
let s = this.in.stream.slice(this.in.mark);
if (!s) return;
2021-09-23 01:41:44 -05:00
if (pattern instanceof RegExp) {
2022-04-30 10:07:11 -05:00
return pattern.test(s);
2021-09-23 01:41:44 -05:00
}
// if pattern is string
2022-04-30 10:07:11 -05:00
return s.includes(pattern);
2021-09-23 01:41:44 -05:00
}
2022-04-20 22:06:16 -05:00
hasNextLine() {
2021-09-23 01:41:44 -05:00
return this.hasNext('\n');
}
2022-04-20 22:06:16 -05:00
nextLine() {
2022-04-30 10:07:11 -05:00
return this.next(/(.*)\r*\n/);
2021-09-23 01:41:44 -05:00
}
async next(pattern) {
2022-04-12 12:36:32 -05:00
while (this._loading || !(await this.hasNext(pattern))) {
2022-04-20 22:06:16 -05:00
await new Promise((resolve) => setTimeout(resolve, 100));
2021-09-23 01:41:44 -05:00
}
let buf = this.in.stream.slice(this.in.mark);
let end = buf.indexOf('\n');
2022-04-30 10:07:11 -05:00
let substr = buf.match(pattern)[1];
if (!substr || end == -1) {
2022-04-20 22:06:16 -05:00
throw 'NoSuchElementException: No ' + pattern.toString() + ' found in buffer ' + buf;
}
2022-04-30 10:07:11 -05:00
let start = buf.indexOf(substr);
2021-09-23 01:41:44 -05:00
this.in.read(end - start + 1);
2022-04-20 22:06:16 -05:00
return buf.slice(start, substr.length);
2021-09-23 01:41:44 -05:00
}
2022-04-20 22:06:16 -05:00
nextShort() {
return this.nextInt();
2021-09-23 01:41:44 -05:00
}
async nextInt() {
2022-04-30 10:07:11 -05:00
return Number(await this.next(/(\d+)\r*\n/));
2021-09-23 01:41:44 -05:00
}
2022-04-20 22:06:16 -05:00
nextLong() {
return this.nextInt();
2021-09-23 01:41:44 -05:00
}
async nextFloat() {
2022-04-30 10:07:11 -05:00
return Number(await this.next(/([0-9\.]+)\r*\n/));
2021-09-23 01:41:44 -05:00
}
2022-04-20 22:06:16 -05:00
nextDouble() {
return this.nextFloat();
2021-09-23 01:41:44 -05:00
}
close() {}
}
2021-09-23 23:26:42 -05:00
jdk.java.util.Scanner = Scanner;
2021-09-23 01:41:44 -05:00
};