File tostring respect closed.

This commit is contained in:
Enyby
2019-10-06 16:54:44 +03:00
parent 868928779f
commit c62ba1f22e

View File

@@ -36,20 +36,20 @@ import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.IoLib; import org.luaj.vm2.lib.IoLib;
import org.luaj.vm2.lib.LibFunction; import org.luaj.vm2.lib.LibFunction;
/** /**
* Subclass of {@link IoLib} and therefore {@link LibFunction} which implements the lua standard {@code io} * Subclass of {@link IoLib} and therefore {@link LibFunction} which implements the lua standard {@code io}
* library for the JSE platform. * library for the JSE platform.
* <p>
* It uses RandomAccessFile to implement seek on files.
* <p> * <p>
* Typically, this library is included as part of a call to * It uses RandomAccessFile to implement seek on files.
* <p>
* Typically, this library is included as part of a call to
* {@link org.luaj.vm2.lib.jse.JsePlatform#standardGlobals()} * {@link org.luaj.vm2.lib.jse.JsePlatform#standardGlobals()}
* <pre> {@code * <pre> {@code
* Globals globals = JsePlatform.standardGlobals(); * Globals globals = JsePlatform.standardGlobals();
* globals.get("io").get("write").call(LuaValue.valueOf("hello, world\n")); * globals.get("io").get("write").call(LuaValue.valueOf("hello, world\n"));
* } </pre> * } </pre>
* <p> * <p>
* For special cases where the smallest possible footprint is desired, * For special cases where the smallest possible footprint is desired,
* a minimal set of libraries could be loaded * a minimal set of libraries could be loaded
* directly via {@link Globals#load(LuaValue)} using code such as: * directly via {@link Globals#load(LuaValue)} using code such as:
* <pre> {@code * <pre> {@code
@@ -96,9 +96,9 @@ public class JseIoLib extends IoLib {
protected File openProgram(String prog, String mode) throws IOException { protected File openProgram(String prog, String mode) throws IOException {
final Process p = Runtime.getRuntime().exec(prog); final Process p = Runtime.getRuntime().exec(prog);
return "w".equals(mode)? return "w".equals(mode)?
new FileImpl( p.getOutputStream() ): new FileImpl( p.getOutputStream() ):
new FileImpl( p.getInputStream() ); new FileImpl( p.getInputStream() );
} }
protected File tmpFile() throws IOException { protected File tmpFile() throws IOException {
@@ -133,7 +133,7 @@ public class JseIoLib extends IoLib {
this( null, null, o ); this( null, null, o );
} }
public String tojstring() { public String tojstring() {
return "file ("+this.hashCode()+")"; return "file (" + (this.closed ? "closed" : this.hashCode()) + ")";
} }
public boolean isstdfile() { public boolean isstdfile() {
return file == null; return file == null;
@@ -199,11 +199,11 @@ public class JseIoLib extends IoLib {
} }
notimplemented(); notimplemented();
return 0; return 0;
} }
// return char if read, -1 if eof, throw IOException on other exception // return char if read, -1 if eof, throw IOException on other exception
public int read() throws IOException { public int read() throws IOException {
if ( is != null ) if ( is != null )
return is.read(); return is.read();
else if ( file != null ) { else if ( file != null ) {
return file.read(); return file.read();