From 28265a8f44731be67adb29ae2382de5deeaacb9b Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Tue, 26 Dec 2023 11:26:08 +0200 Subject: [PATCH] fix: some bug fixes and improvements with File interface --- src/me/topchetoeu/jscript/filesystem/File.java | 10 +++------- .../jscript/filesystem/MemoryFile.java | 17 ++++++----------- .../jscript/filesystem/PhysicalFile.java | 11 ++--------- 3 files changed, 11 insertions(+), 27 deletions(-) diff --git a/src/me/topchetoeu/jscript/filesystem/File.java b/src/me/topchetoeu/jscript/filesystem/File.java index f6e625a..d17fc23 100644 --- a/src/me/topchetoeu/jscript/filesystem/File.java +++ b/src/me/topchetoeu/jscript/filesystem/File.java @@ -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); diff --git a/src/me/topchetoeu/jscript/filesystem/MemoryFile.java b/src/me/topchetoeu/jscript/filesystem/MemoryFile.java index 7fd4afc..613177d 100644 --- a/src/me/topchetoeu/jscript/filesystem/MemoryFile.java +++ b/src/me/topchetoeu/jscript/filesystem/MemoryFile.java @@ -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; diff --git a/src/me/topchetoeu/jscript/filesystem/PhysicalFile.java b/src/me/topchetoeu/jscript/filesystem/PhysicalFile.java index 0514e0e..cc3b78c 100644 --- a/src/me/topchetoeu/jscript/filesystem/PhysicalFile.java +++ b/src/me/topchetoeu/jscript/filesystem/PhysicalFile.java @@ -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;