fix: some bug fixes and improvements with File interface
This commit is contained in:
parent
e9e020512e
commit
28265a8f44
@ -5,17 +5,13 @@ import me.topchetoeu.jscript.Buffer;
|
|||||||
public interface File {
|
public interface File {
|
||||||
int read(byte[] buff);
|
int read(byte[] buff);
|
||||||
void write(byte[] buff);
|
void write(byte[] buff);
|
||||||
long getPtr();
|
long seek(long offset, int pos);
|
||||||
void setPtr(long offset, int pos);
|
|
||||||
void close();
|
void close();
|
||||||
Mode mode();
|
|
||||||
|
|
||||||
default String readToString() {
|
default String readToString() {
|
||||||
setPtr(0, 2);
|
long len = seek(0, 2);
|
||||||
long len = getPtr();
|
|
||||||
if (len < 0) return null;
|
if (len < 0) return null;
|
||||||
|
seek(0, 0);
|
||||||
setPtr(0, 0);
|
|
||||||
|
|
||||||
byte[] res = new byte[(int)len];
|
byte[] res = new byte[(int)len];
|
||||||
len = read(res);
|
len = read(res);
|
||||||
|
@ -27,17 +27,17 @@ public class MemoryFile implements File {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long getPtr() {
|
public long seek(long offset, int pos) {
|
||||||
if (data == null || !mode.readable) throw new FilesystemException(filename, FSCode.NO_PERMISSIONS_R);
|
|
||||||
return ptr;
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public void setPtr(long offset, int pos) {
|
|
||||||
if (data == null || !mode.readable) throw new FilesystemException(filename, FSCode.NO_PERMISSIONS_R);
|
if (data == null || !mode.readable) throw new FilesystemException(filename, FSCode.NO_PERMISSIONS_R);
|
||||||
|
|
||||||
if (pos == 0) ptr = (int)offset;
|
if (pos == 0) ptr = (int)offset;
|
||||||
else if (pos == 1) ptr += (int)offset;
|
else if (pos == 1) ptr += (int)offset;
|
||||||
else if (pos == 2) ptr = data.length() - (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
|
@Override
|
||||||
@ -45,11 +45,6 @@ public class MemoryFile implements File {
|
|||||||
mode = Mode.NONE;
|
mode = Mode.NONE;
|
||||||
ptr = 0;
|
ptr = 0;
|
||||||
}
|
}
|
||||||
@Override
|
|
||||||
public Mode mode() {
|
|
||||||
if (data == null) return Mode.NONE;
|
|
||||||
return mode;
|
|
||||||
}
|
|
||||||
|
|
||||||
public MemoryFile(String filename, Buffer buff, Mode mode) {
|
public MemoryFile(String filename, Buffer buff, Mode mode) {
|
||||||
this.filename = filename;
|
this.filename = filename;
|
||||||
|
@ -25,19 +25,14 @@ public class PhysicalFile implements File {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long getPtr() {
|
public long seek(long offset, int pos) {
|
||||||
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) {
|
|
||||||
if (file == null || !perms.readable) throw new FilesystemException(filename, FSCode.NO_PERMISSIONS_R);
|
if (file == null || !perms.readable) throw new FilesystemException(filename, FSCode.NO_PERMISSIONS_R);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (pos == 1) offset += file.getFilePointer();
|
if (pos == 1) offset += file.getFilePointer();
|
||||||
else if (pos == 2) offset += file.length();
|
else if (pos == 2) offset += file.length();
|
||||||
file.seek(offset);
|
file.seek(offset);
|
||||||
|
return offset;
|
||||||
}
|
}
|
||||||
catch (IOException e) { throw new FilesystemException(filename, FSCode.NO_PERMISSIONS_R); }
|
catch (IOException e) { throw new FilesystemException(filename, FSCode.NO_PERMISSIONS_R); }
|
||||||
}
|
}
|
||||||
@ -50,8 +45,6 @@ public class PhysicalFile implements File {
|
|||||||
file = null;
|
file = null;
|
||||||
perms = Mode.NONE;
|
perms = Mode.NONE;
|
||||||
}
|
}
|
||||||
@Override
|
|
||||||
public Mode mode() { return perms; }
|
|
||||||
|
|
||||||
public PhysicalFile(String path, Mode mode) throws FileNotFoundException {
|
public PhysicalFile(String path, Mode mode) throws FileNotFoundException {
|
||||||
if (mode == Mode.NONE) file = null;
|
if (mode == Mode.NONE) file = null;
|
||||||
|
Loading…
Reference in New Issue
Block a user