Skip to content

Commit b0396b6

Browse files
committed
Добавлены функции http::download, files::writeBytes, files::rename
1 parent e4ec1fb commit b0396b6

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

src/com/annimon/ownlang/lib/modules/files.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public void init() {
3131
Functions.set("fopen", new fopen());
3232
Functions.set("listFiles", new listFiles());
3333
Functions.set("delete", new delete());
34+
Functions.set("rename", new rename());
3435
Functions.set("exists", new exists());
3536
Functions.set("isDirectory", new isDirectory());
3637
Functions.set("isFile", new isFile());
@@ -51,6 +52,7 @@ public void init() {
5152
Functions.set("readText", new readText());
5253
Functions.set("writeBoolean", new writeBoolean());
5354
Functions.set("writeByte", new writeByte());
55+
Functions.set("writeBytes", new writeBytes());
5456
Functions.set("writeChar", new writeChar());
5557
Functions.set("writeShort", new writeShort());
5658
Functions.set("writeInt", new writeInt());
@@ -161,6 +163,16 @@ protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
161163
return NumberValue.fromBoolean(fileInfo.file.mkdir());
162164
}
163165
}
166+
167+
private static class rename extends FileFunction {
168+
@Override
169+
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
170+
final File dest = files.get(args[1].asInt()).file;
171+
System.out.println(fileInfo.file);
172+
System.out.println(dest);
173+
return NumberValue.fromBoolean(fileInfo.file.renameTo(dest));
174+
}
175+
}
164176

165177
private static class fileSize extends FileFunction {
166178
@Override
@@ -306,6 +318,24 @@ protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
306318
return NumberValue.ONE;
307319
}
308320
}
321+
322+
private static class writeBytes extends FileFunction {
323+
@Override
324+
protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
325+
final ArrayValue array = (ArrayValue) args[1];
326+
int offset = 0, length = array.size();
327+
final byte[] bytes = new byte[length];
328+
for (int i = 0; i < length; i++) {
329+
bytes[i] = (byte) (array.get(i).asInt() & 0xFF);
330+
}
331+
if (args.length > 3) {
332+
offset = args[2].asInt();
333+
length = args[3].asInt();
334+
}
335+
fileInfo.dos.write(bytes, offset, length);
336+
return NumberValue.ONE;
337+
}
338+
}
309339

310340
private static class writeChar extends FileFunction {
311341
@Override
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.annimon.ownlang.lib.modules.functions;
2+
3+
import com.annimon.ownlang.lib.*;
4+
import java.io.IOException;
5+
import okhttp3.*;
6+
7+
public final class http_download implements Function {
8+
9+
private final OkHttpClient client = new OkHttpClient();
10+
11+
@Override
12+
public Value execute(Value... args) {
13+
Arguments.check(1, args.length);
14+
try {
15+
final Response response = client.newCall(
16+
new Request.Builder().url(args[0].asString()).build())
17+
.execute();
18+
return ArrayValue.of(response.body().bytes());
19+
} catch (IOException ex) {
20+
return new ArrayValue(0);
21+
}
22+
}
23+
}

src/com/annimon/ownlang/lib/modules/http.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ public final class http implements Module {
1313
public void init() {
1414
Functions.set("urlencode", new http_urlencode());
1515
Functions.set("http", new http_http());
16+
Functions.set("download", new http_download());
1617
}
1718
}

0 commit comments

Comments
 (0)