Skip to content

Commit cd90045

Browse files
committed
Добавлен модуль base64
1 parent 879ffb7 commit cd90045

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.annimon.ownlang.modules.base64;
2+
3+
import com.annimon.ownlang.lib.*;
4+
import com.annimon.ownlang.modules.Module;
5+
import java.io.UnsupportedEncodingException;
6+
import java.util.Base64;
7+
8+
public class base64 implements Module {
9+
10+
private static final int TYPE_URL_SAFE = 8;
11+
12+
public static void initConstants() {
13+
Variables.define("BASE64_URL_SAFE", NumberValue.of(TYPE_URL_SAFE));
14+
}
15+
16+
@Override
17+
public void init() {
18+
initConstants();
19+
Functions.set("base64encode", this::base64encode);
20+
Functions.set("base64decode", this::base64decode);
21+
Functions.set("base64encodeToString", this::base64encodeToString);
22+
}
23+
24+
private Value base64encode(Value... args) {
25+
Arguments.checkOrOr(1, 2, args.length);
26+
return ArrayValue.of(getEncoder(args).encode(getInputToEncode(args)));
27+
}
28+
29+
private Value base64encodeToString(Value... args) {
30+
Arguments.checkOrOr(1, 2, args.length);
31+
return new StringValue(getEncoder(args).encodeToString(getInputToEncode(args)));
32+
}
33+
34+
private Value base64decode(Value... args) {
35+
Arguments.checkOrOr(1, 2, args.length);
36+
final Base64.Decoder decoder = getDecoder(args);
37+
final byte[] result;
38+
if (args[0].type() == Types.ARRAY) {
39+
result = decoder.decode(ValueUtils.toByteArray((ArrayValue) args[0]));
40+
} else {
41+
result = decoder.decode(args[0].asString());
42+
}
43+
return ArrayValue.of(result);
44+
}
45+
46+
47+
private byte[] getInputToEncode(Value[] args) {
48+
byte[] input;
49+
if (args[0].type() == Types.ARRAY) {
50+
input = ValueUtils.toByteArray((ArrayValue) args[0]);
51+
} else {
52+
try {
53+
input = args[0].asString().getBytes("UTF-8");
54+
} catch (UnsupportedEncodingException ex) {
55+
input = args[0].asString().getBytes();
56+
}
57+
}
58+
return input;
59+
}
60+
61+
private Base64.Encoder getEncoder(Value[] args) {
62+
if (args.length == 2 && args[1].asInt() == TYPE_URL_SAFE) {
63+
return Base64.getUrlEncoder();
64+
}
65+
return Base64.getEncoder();
66+
}
67+
68+
private Base64.Decoder getDecoder(Value[] args) {
69+
if (args.length == 2 && args[1].asInt() == TYPE_URL_SAFE) {
70+
return Base64.getUrlDecoder();
71+
}
72+
return Base64.getDecoder();
73+
}
74+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use "base64"
2+
use "functional"
3+
use "types"
4+
5+
base64Example = [0x42, 0x61, 0x73, 0x65, 0x36, 0x34, 0x20, 0x45, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65]
6+
base64Example_enc = [0x51, 0x6D, 0x46, 0x7A, 0x5A, 0x54, 0x59, 0x30,
7+
0x49, 0x45, 0x56, 0x34, 0x59, 0x57, 0x31, 0x77,
8+
0x62, 0x47, 0x55, 0x3D]
9+
unicode = map([0xD0, 0xAE, 0xD0, 0xBD, 0xD0, 0xB8, 0xD0, 0xBA, 0xD0, 0xBE, 0xD0, 0xB4], ::byte)
10+
unicode_enc = [0x30, 0x4B, 0x37, 0x51, 0x76, 0x64, 0x43, 0x34, 0x30, 0x4C, 0x72, 0x51, 0x76, 0x74, 0x43, 0x30]
11+
12+
def testBase64Encode() {
13+
assertEquals(base64Example_enc, base64encode(base64Example))
14+
assertEquals(base64Example_enc, base64encode("Base64 Example"))
15+
assertEquals(unicode_enc, base64encode(unicode))
16+
assertEquals(unicode_enc, base64encode("Юникод"))
17+
}
18+
19+
def testBase64EncodeToString() {
20+
assertEquals("QmFzZTY0IEV4YW1wbGU=", base64encodeToString(base64Example))
21+
assertEquals("QmFzZTY0IEV4YW1wbGU=", base64encodeToString("Base64 Example"))
22+
assertEquals("0K7QvdC40LrQvtC0", base64encodeToString(unicode))
23+
assertEquals("0K7QvdC40LrQvtC0", base64encodeToString("Юникод"))
24+
}
25+
26+
def testBase64Decode() {
27+
assertEquals(base64Example, base64decode("QmFzZTY0IEV4YW1wbGU="))
28+
assertEquals(base64Example, base64decode(base64Example_enc))
29+
assertEquals(unicode, base64decode("0K7QvdC40LrQvtC0"))
30+
assertEquals(unicode, base64decode(unicode_enc))
31+
}

0 commit comments

Comments
 (0)