org.luaj.vm2.lib
Class IoLib

java.lang.Object
  extended by org.luaj.vm2.Varargs
      extended by org.luaj.vm2.LuaValue
          extended by org.luaj.vm2.LuaFunction
              extended by org.luaj.vm2.lib.LibFunction
                  extended by org.luaj.vm2.lib.TwoArgFunction
                      extended by org.luaj.vm2.lib.IoLib
Direct Known Subclasses:
JmeIoLib, JseIoLib

public abstract class IoLib
extends TwoArgFunction

Abstract base class extending LibFunction which implements the core of the lua standard io library.

It contains the implementation of the io library support that is common to the JSE and JME platforms. In practice on of the concrete IOLib subclasses is chosen: JseIoLib for the JSE platform, and JmeIoLib for the JME platform.

The JSE implementation conforms almost completely to the C-based lua library, while the JME implementation follows closely except in the area of random-access files, which are difficult to support properly on JME.

Typically, this library is included as part of a call to either JsePlatform.standardGlobals() or JmePlatform.standardGlobals()

 Globals globals = JsePlatform.standardGlobals();
 globals.get("io").get("write").call(LuaValue.valueOf("hello, world\n"));
  
In this example the platform-specific JseIoLib library will be loaded, which will include the base functionality provided by this class, whereas the JsePlatform would load the JseIoLib.

To instantiate and use it directly, link it into your globals table via LuaValue.load(LuaValue) using code such as:

 Globals globals = new Globals();
 globals.load(new JseBaseLib());
 globals.load(new PackageLib());
 globals.load(new OsLib());
 globals.get("io").get("write").call(LuaValue.valueOf("hello, world\n"));
  

This has been implemented to match as closely as possible the behavior in the corresponding library in C.

See Also:
LibFunction, JsePlatform, JmePlatform, JseIoLib, JmeIoLib, http://www.lua.org/manual/5.1/manual.html#5.7

Nested Class Summary
protected  class IoLib.File
           
 
Field Summary
static java.lang.String[] FILE_NAMES
           
protected static int FTYPE_NAMED
          Enumerated value representing a file type for a named file
protected static int FTYPE_STDERR
          Enumerated value representing stderr
protected static int FTYPE_STDIN
          Enumerated value representing stdin
protected static int FTYPE_STDOUT
          Enumerated value representing stdout
protected  Globals globals
           
static java.lang.String[] IO_NAMES
           
 
Fields inherited from class org.luaj.vm2.lib.LibFunction
name, opcode
 
Fields inherited from class org.luaj.vm2.LuaFunction
s_metatable
 
Fields inherited from class org.luaj.vm2.LuaValue
ADD, CALL, CONCAT, DIV, EMPTYSTRING, ENV, EQ, FALSE, INDEX, LE, LEN, LT, METATABLE, MINUSONE, MOD, MODE, MUL, NEWINDEX, NIL, NILS, NONE, NOVALS, ONE, POW, SUB, TBOOLEAN, TFUNCTION, TINT, TLIGHTUSERDATA, TNIL, TNONE, TNUMBER, TOSTRING, TRUE, TSTRING, TTABLE, TTHREAD, TUSERDATA, TVALUE, TYPE_NAMES, UNM, ZERO
 
Constructor Summary
IoLib()
           
 
Method Summary
 Varargs _file_close(LuaValue file)
           
 Varargs _file_flush(LuaValue file)
           
 Varargs _file_lines(LuaValue file)
           
 Varargs _file_read(LuaValue file, Varargs subargs)
           
 Varargs _file_seek(LuaValue file, java.lang.String whence, int offset)
           
 Varargs _file_setvbuf(LuaValue file, java.lang.String mode, int size)
           
 Varargs _file_write(LuaValue file, Varargs subargs)
           
 Varargs _io_close(LuaValue file)
           
 Varargs _io_flush()
           
 Varargs _io_index(LuaValue v)
           
 Varargs _io_input(LuaValue file)
           
 Varargs _io_lines(java.lang.String filename)
           
 Varargs _io_open(java.lang.String filename, java.lang.String mode)
           
 Varargs _io_output(LuaValue filename)
           
 Varargs _io_popen(java.lang.String prog, java.lang.String mode)
           
 Varargs _io_read(Varargs args)
           
 Varargs _io_tmpfile()
           
 Varargs _io_type(LuaValue obj)
           
 Varargs _io_write(Varargs args)
           
 Varargs _lines_iter(LuaValue file)
           
 LuaValue call(LuaValue modname, LuaValue env)
          Call this with 2 arguments, including metatag processing, and return only the first return value.
static LuaValue freadall(IoLib.File f)
           
static LuaValue freadbytes(IoLib.File f, int count)
           
static LuaValue freadline(IoLib.File f)
           
static LuaValue freadnumber(IoLib.File f)
           
static LuaValue freaduntil(IoLib.File f, boolean lineonly)
           
protected abstract  IoLib.File openFile(java.lang.String filename, boolean readMode, boolean appendMode, boolean updateMode, boolean binaryMode)
          Open a file in a particular mode.
protected abstract  IoLib.File openProgram(java.lang.String prog, java.lang.String mode)
          Start a new process and return a file for input or output
protected abstract  IoLib.File tmpFile()
          Open a temporary file.
protected abstract  IoLib.File wrapStderr()
          Wrap the standard error output.
protected abstract  IoLib.File wrapStdin()
          Wrap the standard input.
protected abstract  IoLib.File wrapStdout()
          Wrap the standard output.
 
Methods inherited from class org.luaj.vm2.lib.TwoArgFunction
call, call, call, invoke
 
Methods inherited from class org.luaj.vm2.lib.LibFunction
bind, bind, call, newupe, newupl, newupn, tojstring
 
Methods inherited from class org.luaj.vm2.LuaFunction
checkfunction, classnamestub, getmetatable, isfunction, name, optfunction, strvalue, type, typename
 
Methods inherited from class org.luaj.vm2.LuaValue
add, add, add, and, arg, arg1, argerror, argerror, aritherror, aritherror, arithmt, arithmtwith, assert_, buffer, call, callmt, checkboolean, checkclosure, checkdouble, checkglobals, checkint, checkinteger, checkjstring, checklong, checkmetatag, checknotnil, checknumber, checknumber, checkstring, checktable, checkthread, checkuserdata, checkuserdata, compareerror, compareerror, comparemt, concat, concat, concatmt, concatTo, concatTo, concatTo, div, div, div, divInto, eq_b, eq, eqmtcall, equals, error, get, get, get, gettable, gt_b, gt_b, gt_b, gt, gt, gt, gteq_b, gteq_b, gteq_b, gteq, gteq, gteq, illegal, inext, initupvalue1, invoke, invoke, invoke, invoke, invoke, invokemethod, invokemethod, invokemethod, invokemethod, invokemethod, invokemethod, isboolean, isclosure, isint, isinttype, islong, isnil, isnumber, isstring, istable, isthread, isuserdata, isuserdata, isvalidkey, len, lenerror, length, listOf, listOf, load, lt_b, lt_b, lt_b, lt, lt, lt, lteq_b, lteq_b, lteq_b, lteq, lteq, lteq, metatableOf, metatag, method, method, method, method, method, method, mod, mod, mod, modFrom, mul, mul, mul, narg, neg, neq_b, neq, next, not, onInvoke, optboolean, optclosure, optdouble, optint, optinteger, optjstring, optlong, optnumber, optstring, opttable, optthread, optuserdata, optuserdata, optvalue, or, pow, pow, pow, powWith, powWith, presize, raweq, raweq, raweq, raweq, raweq, rawget, rawget, rawget, rawlen, rawset, rawset, rawset, rawset, rawset, rawset, rawset, rawsetlist, set, set, set, set, set, set, set, setmetatable, settable, strcmp, strcmp, strongvalue, sub, sub, sub, subargs, subFrom, subFrom, tableOf, tableOf, tableOf, tableOf, tableOf, tableOf, tailcallOf, testfor_b, toboolean, tobyte, tochar, todouble, tofloat, toint, tolong, tonumber, toshort, tostring, toString, touserdata, touserdata, typerror, unimplemented, userdataOf, userdataOf, valueOf, valueOf, valueOf, valueOf, valueOf, valueOf, varargsOf, varargsOf, varargsOf, varargsOf, varargsOf, varargsOf
 
Methods inherited from class org.luaj.vm2.Varargs
argcheck, checkboolean, checkclosure, checkdouble, checkfunction, checkint, checkinteger, checkjstring, checklong, checknotnil, checknumber, checkstring, checktable, checkthread, checkuserdata, checkuserdata, checkvalue, eval, isfunction, isnil, isnoneornil, isnumber, isstring, istable, isTailcall, isthread, isuserdata, isvalue, optboolean, optclosure, optdouble, optfunction, optint, optinteger, optjstring, optlong, optnumber, optstring, opttable, optthread, optuserdata, optuserdata, optvalue, toboolean, tobyte, tochar, todouble, tofloat, toint, tojstring, tolong, toshort, touserdata, touserdata, type
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

FTYPE_STDIN

protected static final int FTYPE_STDIN
Enumerated value representing stdin

See Also:
Constant Field Values

FTYPE_STDOUT

protected static final int FTYPE_STDOUT
Enumerated value representing stdout

See Also:
Constant Field Values

FTYPE_STDERR

protected static final int FTYPE_STDERR
Enumerated value representing stderr

See Also:
Constant Field Values

FTYPE_NAMED

protected static final int FTYPE_NAMED
Enumerated value representing a file type for a named file

See Also:
Constant Field Values

IO_NAMES

public static final java.lang.String[] IO_NAMES

FILE_NAMES

public static final java.lang.String[] FILE_NAMES

globals

protected Globals globals
Constructor Detail

IoLib

public IoLib()
Method Detail

wrapStdin

protected abstract IoLib.File wrapStdin()
                                 throws java.io.IOException
Wrap the standard input.

Returns:
File
Throws:
java.io.IOException

wrapStdout

protected abstract IoLib.File wrapStdout()
                                  throws java.io.IOException
Wrap the standard output.

Returns:
File
Throws:
java.io.IOException

wrapStderr

protected abstract IoLib.File wrapStderr()
                                  throws java.io.IOException
Wrap the standard error output.

Returns:
File
Throws:
java.io.IOException

openFile

protected abstract IoLib.File openFile(java.lang.String filename,
                                       boolean readMode,
                                       boolean appendMode,
                                       boolean updateMode,
                                       boolean binaryMode)
                                throws java.io.IOException
Open a file in a particular mode.

Parameters:
filename -
readMode - true if opening in read mode
appendMode - true if opening in append mode
updateMode - true if opening in update mode
binaryMode - true if opening in binary mode
Returns:
File object if successful
Throws:
java.io.IOException - if could not be opened

tmpFile

protected abstract IoLib.File tmpFile()
                               throws java.io.IOException
Open a temporary file.

Returns:
File object if successful
Throws:
java.io.IOException - if could not be opened

openProgram

protected abstract IoLib.File openProgram(java.lang.String prog,
                                          java.lang.String mode)
                                   throws java.io.IOException
Start a new process and return a file for input or output

Parameters:
prog - the program to execute
mode - "r" to read, "w" to write
Returns:
File to read to or write from
Throws:
java.io.IOException - if an i/o exception occurs

call

public LuaValue call(LuaValue modname,
                     LuaValue env)
Description copied from class: LuaValue
Call this with 2 arguments, including metatag processing, and return only the first return value.

If this is a LuaFunction, call it, and return only its first return value, dropping any others. Otherwise, look for the LuaValue.CALL metatag and call that.

If the return value is a Varargs, only the 1st value will be returned. To get multiple values, use LuaValue.invoke() instead.

To call this as a method call, use LuaValue.method(LuaValue) instead.

Specified by:
call in class TwoArgFunction
Parameters:
modname - First argument to supply to the called function
env - Second argument to supply to the called function
Returns:
First return value (this(arg1,arg2)), or LuaValue.NIL if there were none.
See Also:
LuaValue.call(), LuaValue.call(LuaValue), LuaValue.call(LuaValue, LuaValue, LuaValue), LuaValue.invoke(LuaValue, Varargs), LuaValue.method(String,LuaValue,LuaValue), LuaValue.method(LuaValue,LuaValue,LuaValue)

_io_flush

public Varargs _io_flush()
                  throws java.io.IOException
Throws:
java.io.IOException

_io_tmpfile

public Varargs _io_tmpfile()
                    throws java.io.IOException
Throws:
java.io.IOException

_io_close

public Varargs _io_close(LuaValue file)
                  throws java.io.IOException
Throws:
java.io.IOException

_io_input

public Varargs _io_input(LuaValue file)

_io_output

public Varargs _io_output(LuaValue filename)

_io_type

public Varargs _io_type(LuaValue obj)

_io_popen

public Varargs _io_popen(java.lang.String prog,
                         java.lang.String mode)
                  throws java.io.IOException
Throws:
java.io.IOException

_io_open

public Varargs _io_open(java.lang.String filename,
                        java.lang.String mode)
                 throws java.io.IOException
Throws:
java.io.IOException

_io_lines

public Varargs _io_lines(java.lang.String filename)

_io_read

public Varargs _io_read(Varargs args)
                 throws java.io.IOException
Throws:
java.io.IOException

_io_write

public Varargs _io_write(Varargs args)
                  throws java.io.IOException
Throws:
java.io.IOException

_file_close

public Varargs _file_close(LuaValue file)
                    throws java.io.IOException
Throws:
java.io.IOException

_file_flush

public Varargs _file_flush(LuaValue file)
                    throws java.io.IOException
Throws:
java.io.IOException

_file_setvbuf

public Varargs _file_setvbuf(LuaValue file,
                             java.lang.String mode,
                             int size)

_file_lines

public Varargs _file_lines(LuaValue file)

_file_read

public Varargs _file_read(LuaValue file,
                          Varargs subargs)
                   throws java.io.IOException
Throws:
java.io.IOException

_file_seek

public Varargs _file_seek(LuaValue file,
                          java.lang.String whence,
                          int offset)
                   throws java.io.IOException
Throws:
java.io.IOException

_file_write

public Varargs _file_write(LuaValue file,
                           Varargs subargs)
                    throws java.io.IOException
Throws:
java.io.IOException

_io_index

public Varargs _io_index(LuaValue v)

_lines_iter

public Varargs _lines_iter(LuaValue file)
                    throws java.io.IOException
Throws:
java.io.IOException

freadbytes

public static LuaValue freadbytes(IoLib.File f,
                                  int count)
                           throws java.io.IOException
Throws:
java.io.IOException

freaduntil

public static LuaValue freaduntil(IoLib.File f,
                                  boolean lineonly)
                           throws java.io.IOException
Throws:
java.io.IOException

freadline

public static LuaValue freadline(IoLib.File f)
                          throws java.io.IOException
Throws:
java.io.IOException

freadall

public static LuaValue freadall(IoLib.File f)
                         throws java.io.IOException
Throws:
java.io.IOException

freadnumber

public static LuaValue freadnumber(IoLib.File f)
                            throws java.io.IOException
Throws:
java.io.IOException


Copyright © 2007-2015 Luaj.org. All Rights Reserved.