feat: create memory-light directory list file
This commit is contained in:
parent
28265a8f44
commit
1c64912786
@ -12,7 +12,8 @@ public class FilesystemException extends RuntimeException {
|
|||||||
NO_PERMISSIONS_RW(0x5),
|
NO_PERMISSIONS_RW(0x5),
|
||||||
FOLDER_NOT_EMPTY(0x6),
|
FOLDER_NOT_EMPTY(0x6),
|
||||||
ALREADY_EXISTS(0x7),
|
ALREADY_EXISTS(0x7),
|
||||||
FOLDER_EXISTS(0x8);
|
FOLDER_EXISTS(0x8),
|
||||||
|
UNSUPPORTED_OPERATION(0x9);
|
||||||
|
|
||||||
public final int code;
|
public final int code;
|
||||||
|
|
||||||
@ -27,7 +28,8 @@ public class FilesystemException extends RuntimeException {
|
|||||||
"No permissions to read '%s'",
|
"No permissions to read '%s'",
|
||||||
"No permissions to write '%s'",
|
"No permissions to write '%s'",
|
||||||
"Can't delete '%s', since it is a full folder.",
|
"Can't delete '%s', since it is a full folder.",
|
||||||
"'%s' already exists."
|
"'%s' already exists.",
|
||||||
|
"An unsupported operation was performed on the file '%s'."
|
||||||
};
|
};
|
||||||
|
|
||||||
public final String message, filename;
|
public final String message, filename;
|
||||||
|
73
src/me/topchetoeu/jscript/filesystem/ListFile.java
Normal file
73
src/me/topchetoeu/jscript/filesystem/ListFile.java
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
package me.topchetoeu.jscript.filesystem;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import me.topchetoeu.jscript.filesystem.FilesystemException.FSCode;
|
||||||
|
|
||||||
|
public class ListFile implements File {
|
||||||
|
private Stream<Path> stream;
|
||||||
|
private String filename;
|
||||||
|
private byte[] currFile;
|
||||||
|
private long ptr = 0, start = 0, end = 0;
|
||||||
|
|
||||||
|
private void next() {
|
||||||
|
while (stream != null) {
|
||||||
|
var opt = stream.findFirst();
|
||||||
|
|
||||||
|
if (opt.isPresent()) {
|
||||||
|
start = end;
|
||||||
|
currFile = (opt.get().toString() + "\n").getBytes();
|
||||||
|
end = start + currFile.length;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
stream = null;
|
||||||
|
currFile = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() {
|
||||||
|
stream = null;
|
||||||
|
currFile = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int read(byte[] buff) {
|
||||||
|
if (stream == null) throw new FilesystemException(filename, FSCode.NO_PERMISSIONS_R);
|
||||||
|
if (ptr < start) return 0;
|
||||||
|
|
||||||
|
var i = 0;
|
||||||
|
|
||||||
|
while (i < buff.length) {
|
||||||
|
while (i > end) next();
|
||||||
|
|
||||||
|
int cpyN = Math.min(currFile.length, buff.length - i);
|
||||||
|
System.arraycopy(buff, i, currFile, 0, cpyN);
|
||||||
|
|
||||||
|
i += cpyN;
|
||||||
|
}
|
||||||
|
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long seek(long offset, int pos) {
|
||||||
|
if (pos == 2) throw new FilesystemException(filename, FSCode.UNSUPPORTED_OPERATION);
|
||||||
|
if (pos == 1) offset += ptr;
|
||||||
|
return ptr = offset;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(byte[] buff) {
|
||||||
|
throw new FilesystemException(filename, FSCode.NO_PERMISSIONS_RW);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ListFile(Path path) throws IOException {
|
||||||
|
stream = Files.list(path);
|
||||||
|
filename = path.toString();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user