public int luaCB_read(Lua l, LuaPlugin plugin) { l.pushNil(); l.pushNil(); try { long offset = (long) l.checkNumber(2); int length = (int) l.checkNumber(3); if (length <= 0) { l.pushNil(); l.pushString("Length is not positive"); return 2; } byte[] tmp = new byte[length]; object.seek(offset); length = object.read(tmp); if (length < 0) { l.pushNil(); l.pushNil(); return 2; } StringBuffer buf = new StringBuffer(length); for (byte x : tmp) buf.appendCodePoint((int) x & 0xFF); l.push(buf.toString()); } catch (IOException e) { l.pushNil(); l.pushString("IOException: " + e.getMessage()); return 2; } return 1; }
public int luaCB_set_length(Lua l, LuaPlugin plugin) { l.pushNil(); try { object.setLength((long) l.checkNumber(2)); } catch (IOException e) { l.pushNil(); l.pushString("IOException: " + e.getMessage()); return 2; } return 1; }
public int luaCB_write(Lua l, LuaPlugin plugin) { l.pushNil(); l.pushNil(); try { long offset = (long) l.checkNumber(2); String towrite = l.checkString(3); if (towrite == "") { l.pushBoolean(true); return 1; } byte[] tmp = new byte[towrite.length()]; object.seek(offset); for (int i = 0; i < towrite.length(); i++) tmp[i] = (byte) towrite.charAt(i); object.write(tmp); l.pushBoolean(true); } catch (IOException e) { l.pushBoolean(false); l.pushString("IOException: " + e.getMessage()); return 2; } return 1; }