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.OneArgFunction
                      extended by org.luaj.vm2.lib.IoLib
Direct Known Subclasses:
JmeIoLib, JseIoLib

public abstract class IoLib
extends OneArgFunction

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()

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

 LuaTable _G = new LuaTable();
 _G.load(new JseIoLib());
 LuaThread.setGlobals(_G);
 _G.load(new JseBaseLib());
 _G.load(new PackageLib());
 _G.load(new JseIoLib());
 _G.get("io").get("write").call(LuaValue.valueOf("hello, world\n"));
  
Doing so will ensure the library is properly initialized and loaded into the globals table.

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
           
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
env, s_metatable
 
Fields inherited from class org.luaj.vm2.LuaValue
ADD, CALL, CONCAT, DIV, EMPTYSTRING, 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 arg)
          Call this with 1 argument, 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 wrapStdin()
          Wrap the standard input.
protected abstract  IoLib.File wrapStdout()
          Wrap the standard output.
 
Methods inherited from class org.luaj.vm2.lib.OneArgFunction
call, call, call, invoke
 
Methods inherited from class org.luaj.vm2.lib.LibFunction
bind, bind, newupe, newupl, newupn, tojstring
 
Methods inherited from class org.luaj.vm2.LuaFunction
checkfunction, getfenv, getmetatable, isfunction, optfunction, setfenv, type, typename
 
Methods inherited from class org.luaj.vm2.LuaValue
add, add, add, and, arg, arg1, argerror, argerror, aritherror, aritherror, arithmt, arithmtwith, assert_, buffer, callmt, checkboolean, checkclosure, checkdouble, checkint, checkinteger, checkjstring, checklong, checkmetatag, checknotnil, checknumber, checknumber, checkstring, checktable, checkthread, checkuserdata, checkuserdata, checkvalidkey, compareerror, compareerror, comparemt, concat, concat, concatmt, concatTo, concatTo, concatTo, div, div, div, divInto, eq_b, eq, eqmtcall, equals, error, get, get, get, getn, gettable, gt_b, gt_b, gt_b, gt, gt, gt, gteq_b, gteq_b, gteq_b, gteq, gteq, gteq, illegal, inext, invoke, invoke, invoke, invoke, invoke, invokemethod, invokemethod, invokemethod, invokemethod, invokemethod, invokemethod, isboolean, isclosure, isint, isinttype, islong, isnil, isnumber, isstring, istable, isthread, isuserdata, isuserdata, isweaknil, len, lenerror, length, listOf, listOf, load, lt_b, lt_b, lt_b, lt, lt, lt, lteq_b, lteq_b, lteq_b, lteq, lteq, lteq, 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, rawset, rawset, rawset, rawset, rawset, rawset, rawset, rawsetlist, set, set, set, set, set, set, set, setmetatable, settable, strcmp, strcmp, strongkey, strongvalue, strvalue, sub, sub, sub, 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, subargs, 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

IO_NAMES

public static final java.lang.String[] IO_NAMES

FILE_NAMES

public static final java.lang.String[] FILE_NAMES
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

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 arg)
Description copied from class: LuaValue
Call this with 1 argument, 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 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 OneArgFunction
Parameters:
arg - First argument to supply to the called function
Returns:
First return value (this(arg)), or NIL if there were none.
See Also:
LuaValue.call(), LuaValue.call(LuaValue,LuaValue), LuaValue.call(LuaValue, LuaValue, LuaValue), #invoke(LuaValue), LuaValue.method(String,LuaValue), LuaValue.method(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-2008 Luaj.org. All Rights Reserved.