Module support #11

Merged
TopchetoEU merged 20 commits from TopchetoEU/modules into master 2023-12-26 12:20:55 +00:00
3 changed files with 11 additions and 27 deletions
Showing only changes of commit 28265a8f44 - Show all commits

View File

@ -5,17 +5,13 @@ import me.topchetoeu.jscript.Buffer;
public interface File {
int read(byte[] buff);
void write(byte[] buff);
long getPtr();
void setPtr(long offset, int pos);
long seek(long offset, int pos);
void close();
Mode mode();
default String readToString() {
setPtr(0, 2);
long len = getPtr();
long len = seek(0, 2);
if (len < 0) return null;
setPtr(0, 0);
seek(0, 0);
byte[] res = new byte[(int)len];
len = read(res);

View File

@ -27,17 +27,17 @@ public class MemoryFile implements File {
}
@Override
public long getPtr() {
if (data == null || !mode.readable) throw new FilesystemException(filename, FSCode.NO_PERMISSIONS_R);
return ptr;
}
@Override
public void setPtr(long offset, int pos) {
public long seek(long offset, int pos) {
if (data == null || !mode.readable) throw new FilesystemException(filename, FSCode.NO_PERMISSIONS_R);
if (pos == 0) ptr = (int)offset;
else if (pos == 1) ptr += (int)offset;
else if (pos == 2) ptr = data.length() - (int)offset;
if (ptr < 0) ptr = 0;
if (ptr > data.length()) ptr = data.length();
return pos;
}
@Override
@ -45,11 +45,6 @@ public class MemoryFile implements File {
mode = Mode.NONE;
ptr = 0;
}
@Override
public Mode mode() {
if (data == null) return Mode.NONE;
return mode;
}
public MemoryFile(String filename, Buffer buff, Mode mode) {
this.filename = filename;

View File

@ -25,19 +25,14 @@ public class PhysicalFile implements File {
}
@Override
public long getPtr() {
if (file == null || !perms.readable) throw new FilesystemException(filename, FSCode.NO_PERMISSIONS_R);
else try { return file.getFilePointer(); }
catch (IOException e) { throw new FilesystemException(filename, FSCode.NO_PERMISSIONS_R); }
}
@Override
public void setPtr(long offset, int pos) {
public long seek(long offset, int pos) {
if (file == null || !perms.readable) throw new FilesystemException(filename, FSCode.NO_PERMISSIONS_R);
try {
if (pos == 1) offset += file.getFilePointer();
else if (pos == 2) offset += file.length();
file.seek(offset);
return offset;
}
catch (IOException e) { throw new FilesystemException(filename, FSCode.NO_PERMISSIONS_R); }
}
@ -50,8 +45,6 @@ public class PhysicalFile implements File {
file = null;
perms = Mode.NONE;
}
@Override
public Mode mode() { return perms; }
public PhysicalFile(String path, Mode mode) throws FileNotFoundException {
if (mode == Mode.NONE) file = null;