Skip to content

Commit 4ef2256

Browse files
committed
add file operate in lua
1 parent eddef68 commit 4ef2256

File tree

4 files changed

+272
-0
lines changed

4 files changed

+272
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Created by LuaView.
3+
* Copyright (c) 2017, Alibaba Group. All rights reserved.
4+
*
5+
* This source code is licensed under the MIT.
6+
* For the full copyright and license information,please view the LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
package com.taobao.luaview.fun.binder.kit;
10+
11+
import com.taobao.luaview.fun.base.BaseFunctionBinder;
12+
import com.taobao.luaview.userdata.kit.UDFile;
13+
14+
import org.luaj.vm2.LuaValue;
15+
import org.luaj.vm2.lib.LibFunction;
16+
17+
/**
18+
* File operation
19+
*
20+
* @author song
21+
* @date 16/12/5
22+
* 主要功能描述
23+
* 修改描述
24+
* 下午2:41 song XXX
25+
*/
26+
public class FileBinder extends BaseFunctionBinder {
27+
28+
public FileBinder() {
29+
super("File");
30+
}
31+
32+
@Override
33+
public Class<? extends LibFunction> getMapperClass() {
34+
return null;
35+
}
36+
37+
@Override
38+
public LuaValue createCreator(LuaValue env, final LuaValue metaTable) {
39+
return new UDFile(env.checkglobals(), metaTable);
40+
}
41+
}

VideoOS/LuaViewSDK/src/com/taobao/luaview/global/LuaViewManager.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.taobao.luaview.fun.binder.indicator.UICircleViewPagerIndicatorBinder;
2626
import com.taobao.luaview.fun.binder.kit.ActionBarBinder;
2727
import com.taobao.luaview.fun.binder.kit.DataBinder;
28+
import com.taobao.luaview.fun.binder.kit.FileBinder;
2829
import com.taobao.luaview.fun.binder.kit.JsonBinder;
2930
import com.taobao.luaview.fun.binder.kit.SystemBinder;
3031
import com.taobao.luaview.fun.binder.kit.TimerBinder;
@@ -192,6 +193,7 @@ static void loadLuaViewLibs(final Globals globals) {
192193
globals.tryLazyLoad(new ActionBarBinder());
193194
globals.tryLazyLoad(new DataBinder());
194195
globals.tryLazyLoad(new JsonBinder());
196+
globals.tryLazyLoad(new FileBinder());
195197

196198
//常量
197199
globals.tryLazyLoad(new AlignBinder());
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
/*
2+
* Created by LuaView.
3+
* Copyright (c) 2017, Alibaba Group. All rights reserved.
4+
*
5+
* This source code is licensed under the MIT.
6+
* For the full copyright and license information,please view the LICENSE file in the root
7+
* directory of this source tree.
8+
*/
9+
10+
package com.taobao.luaview.userdata.kit;
11+
12+
import android.text.TextUtils;
13+
14+
import com.taobao.luaview.fun.mapper.LuaViewApi;
15+
import com.taobao.luaview.fun.mapper.LuaViewLib;
16+
import com.taobao.luaview.global.LuaResourceFinder;
17+
import com.taobao.luaview.global.VmVersion;
18+
import com.taobao.luaview.scriptbundle.asynctask.SimpleTask1;
19+
import com.taobao.luaview.userdata.base.BaseLuaTable;
20+
import com.taobao.luaview.util.FileUtil;
21+
import com.taobao.luaview.util.LuaUtil;
22+
23+
import org.luaj.vm2.Globals;
24+
import org.luaj.vm2.LuaString;
25+
import org.luaj.vm2.LuaValue;
26+
import org.luaj.vm2.Varargs;
27+
import org.luaj.vm2.lib.VarArgFunction;
28+
29+
import java.io.File;
30+
31+
import cn.com.venvy.App;
32+
import cn.com.venvy.common.utils.VenvyFileUtil;
33+
import cn.com.venvy.common.utils.VenvyIOUtils;
34+
35+
/**
36+
* File 用户数据封装
37+
*
38+
* @author song
39+
* @date 15/9/6
40+
*/
41+
@LuaViewLib(revisions = {"20170306已对标"})
42+
@LuaViewApi(since = VmVersion.V_550)
43+
public class UDFile extends BaseLuaTable {
44+
45+
public UDFile(Globals globals, LuaValue metatable) {
46+
super(globals, metatable);
47+
init();
48+
}
49+
50+
private void init() {
51+
set("save", new save());
52+
set("read", new read());
53+
set("exists", new exists());
54+
set("path", new path());
55+
}
56+
57+
private int fixIndex(Varargs args) {
58+
return args != null && args.arg1() instanceof UDFile ? 1 : 0;
59+
}
60+
61+
/**
62+
* 保存文件
63+
*/
64+
class save extends VarArgFunction {
65+
@Override
66+
public Varargs invoke(Varargs args) {
67+
final int fixIndex = fixIndex(args);
68+
if (args.narg() > fixIndex) {
69+
final LuaResourceFinder finder = getLuaResourceFinder();
70+
if (finder != null) {
71+
final LuaValue param1 = args.arg(fixIndex + 1);
72+
final LuaValue param2 = args.arg(fixIndex + 2);
73+
74+
String name = null;
75+
byte[] data = null;
76+
if (LuaUtil.isString(param1)) {
77+
name = param1.optjstring(null);
78+
79+
if (param2 instanceof UDData) {
80+
data = ((UDData) param2).bytes();
81+
} else if (param2 instanceof LuaString) {
82+
data = ((LuaString) param2).m_bytes;
83+
} else if (param2 instanceof CharSequence) {
84+
data = param2.toString().getBytes();
85+
}
86+
87+
} else if (param1 instanceof UDData) {
88+
data = ((UDData) param1).bytes();
89+
name = param2.optjstring(null);
90+
}
91+
92+
if (data != null && data.length > 0 && !TextUtils.isEmpty(name)) {
93+
if (args.isfunction(fixIndex + 3)) {
94+
final LuaValue callback = LuaUtil.getFunction(args, fixIndex + 3);
95+
new SimpleTask1<Boolean>() {
96+
@Override
97+
protected Boolean doInBackground(Object... params) {
98+
final String path =
99+
finder.buildSecurePathInSdcard((String) params[0]);
100+
return FileUtil.save(path, (byte[]) params[1]);
101+
}
102+
103+
@Override
104+
protected void onPostExecute(Boolean o) {
105+
LuaUtil.callFunction(callback, o);
106+
}
107+
}.executeInPool(name, data);
108+
} else {
109+
final String path = finder.buildSecurePathInSdcard(name);
110+
return valueOf(FileUtil.save(path, data));
111+
}
112+
}
113+
}
114+
}
115+
return FALSE;
116+
}
117+
}
118+
119+
120+
/**
121+
* 读取文件
122+
*/
123+
class read extends VarArgFunction {
124+
@Override
125+
public Varargs invoke(Varargs args) {
126+
final int fixIndex = fixIndex(args);
127+
if (args.narg() > fixIndex) {
128+
final LuaResourceFinder finder = getLuaResourceFinder();
129+
if (finder != null) {
130+
final String name = LuaUtil.getString(args, fixIndex + 1);
131+
final String path = finder.buildSecurePathInSdcard(name);
132+
133+
if (args.isfunction(fixIndex + 2)) {
134+
final LuaValue callback = LuaUtil.getFunction(args, fixIndex + 2);
135+
if (path != null) {
136+
new SimpleTask1<UDData>() {
137+
@Override
138+
protected UDData doInBackground(Object... params) {
139+
byte[] data = VenvyFileUtil.readBytes(new File(path));
140+
if (data != null) {
141+
return new UDData(getGlobals(), getmetatable(), null).append(data);
142+
} else { // 外存储卡读取不到的情况下,尝试在assets资源包下读取
143+
data = VenvyIOUtils.toBytes(VenvyIOUtils.open(App.getContext(),
144+
name));
145+
return new UDData(getGlobals(), getmetatable(), null).append(data);
146+
}
147+
}
148+
149+
@Override
150+
protected void onPostExecute(UDData udData) {
151+
LuaUtil.callFunction(callback, udData);
152+
}
153+
}.executeInPool();
154+
} else {
155+
LuaUtil.callFunction(callback, NIL);
156+
}
157+
} else {
158+
if (path != null) {
159+
byte[] data = VenvyFileUtil.readBytes(new File(path));
160+
if (data != null) {
161+
return new UDData(getGlobals(), getmetatable(), null).append(data);
162+
} else { // 外存储卡读取不到的情况下,尝试在assets资源包下读取
163+
data = VenvyIOUtils.toBytes(VenvyIOUtils.open(App.getContext(),
164+
name));
165+
if (data != null) {
166+
return new UDData(getGlobals(), getmetatable(), null).append(data);
167+
} else {
168+
return NIL;
169+
}
170+
}
171+
} else {
172+
return NIL;
173+
}
174+
}
175+
}
176+
}
177+
return NIL;
178+
}
179+
}
180+
181+
/**
182+
* 文件是否存在
183+
*/
184+
class exists extends VarArgFunction {
185+
@Override
186+
public Varargs invoke(Varargs args) {
187+
final int fixIndex = fixIndex(args);
188+
if (args.narg() > fixIndex) {
189+
final String fileName = LuaUtil.getString(args, fixIndex + 1);
190+
return valueOf(VenvyFileUtil.isExistFile(fileName));
191+
}
192+
return valueOf(false);
193+
}
194+
}
195+
196+
/**
197+
* 获取文件绝对路径
198+
*/
199+
class path extends VarArgFunction {
200+
@Override
201+
public Varargs invoke(Varargs args) {
202+
final int fixIndex = fixIndex(args);
203+
if (args.narg() > fixIndex) {
204+
final LuaResourceFinder finder = getLuaResourceFinder();
205+
if (finder != null) {
206+
final String fileName = LuaUtil.getString(args, fixIndex + 1);
207+
final String path = finder.buildSecurePathInSdcard(fileName);
208+
return path != null ? valueOf(path) : NIL;
209+
}
210+
}
211+
return NIL;
212+
}
213+
}
214+
}

VideoOS/VenvyLibrary/src/main/java/cn/com/venvy/common/utils/VenvyFileUtil.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,4 +444,19 @@ public static File createFile(final String fullpath) {
444444
return new File(parent, fileName);
445445
}
446446
}
447+
448+
public static byte[] readBytes(File file) {
449+
if (file != null && file.exists() && file.isFile()) {
450+
InputStream inputStream = null;
451+
try {
452+
inputStream = new FileInputStream(file);
453+
return VenvyIOUtils.toBytes(inputStream);
454+
} catch (FileNotFoundException e) {
455+
e.printStackTrace();
456+
} finally {
457+
VenvyIOUtils.close(inputStream);
458+
}
459+
}
460+
return null;
461+
}
447462
}

0 commit comments

Comments
 (0)