Добавлены функции http::download, files::writeBytes, files::rename

This commit is contained in:
Victor 2016-05-19 15:21:48 +03:00
parent e4ec1fba18
commit b0396b6105
3 changed files with 54 additions and 0 deletions

View File

@ -31,6 +31,7 @@ public final class files implements Module {
Functions.set("fopen", new fopen()); Functions.set("fopen", new fopen());
Functions.set("listFiles", new listFiles()); Functions.set("listFiles", new listFiles());
Functions.set("delete", new delete()); Functions.set("delete", new delete());
Functions.set("rename", new rename());
Functions.set("exists", new exists()); Functions.set("exists", new exists());
Functions.set("isDirectory", new isDirectory()); Functions.set("isDirectory", new isDirectory());
Functions.set("isFile", new isFile()); Functions.set("isFile", new isFile());
@ -51,6 +52,7 @@ public final class files implements Module {
Functions.set("readText", new readText()); Functions.set("readText", new readText());
Functions.set("writeBoolean", new writeBoolean()); Functions.set("writeBoolean", new writeBoolean());
Functions.set("writeByte", new writeByte()); Functions.set("writeByte", new writeByte());
Functions.set("writeBytes", new writeBytes());
Functions.set("writeChar", new writeChar()); Functions.set("writeChar", new writeChar());
Functions.set("writeShort", new writeShort()); Functions.set("writeShort", new writeShort());
Functions.set("writeInt", new writeInt()); Functions.set("writeInt", new writeInt());
@ -162,6 +164,16 @@ public final class files implements Module {
} }
} }
private static class rename extends FileFunction {
@Override
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
final File dest = files.get(args[1].asInt()).file;
System.out.println(fileInfo.file);
System.out.println(dest);
return NumberValue.fromBoolean(fileInfo.file.renameTo(dest));
}
}
private static class fileSize extends FileFunction { private static class fileSize extends FileFunction {
@Override @Override
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException { protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
@ -307,6 +319,24 @@ public final class files implements Module {
} }
} }
private static class writeBytes extends FileFunction {
@Override
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
final ArrayValue array = (ArrayValue) args[1];
int offset = 0, length = array.size();
final byte[] bytes = new byte[length];
for (int i = 0; i < length; i++) {
bytes[i] = (byte) (array.get(i).asInt() & 0xFF);
}
if (args.length > 3) {
offset = args[2].asInt();
length = args[3].asInt();
}
fileInfo.dos.write(bytes, offset, length);
return NumberValue.ONE;
}
}
private static class writeChar extends FileFunction { private static class writeChar extends FileFunction {
@Override @Override
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException { protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {

View File

@ -0,0 +1,23 @@
package com.annimon.ownlang.lib.modules.functions;
import com.annimon.ownlang.lib.*;
import java.io.IOException;
import okhttp3.*;
public final class http_download implements Function {
private final OkHttpClient client = new OkHttpClient();
@Override
public Value execute(Value... args) {
Arguments.check(1, args.length);
try {
final Response response = client.newCall(
new Request.Builder().url(args[0].asString()).build())
.execute();
return ArrayValue.of(response.body().bytes());
} catch (IOException ex) {
return new ArrayValue(0);
}
}
}

View File

@ -13,5 +13,6 @@ public final class http implements Module {
public void init() { public void init() {
Functions.set("urlencode", new http_urlencode()); Functions.set("urlencode", new http_urlencode());
Functions.set("http", new http_http()); Functions.set("http", new http_http());
Functions.set("download", new http_download());
} }
} }