Skip to content

Commit

Permalink
Base64 functions
Browse files Browse the repository at this point in the history
  • Loading branch information
PastorGL committed Jan 22, 2025
1 parent 3e930f2 commit 70612bf
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.github.pastorgl.datacooker.scripting.Function.Ternary;
import io.github.pastorgl.datacooker.scripting.Function.Unary;

import java.util.Base64;
import java.util.Deque;
import java.util.Locale;

Expand Down Expand Up @@ -202,4 +203,38 @@ public String descr() {
" See Java Formatter class for complete reference";
}
}

public static class DECODE64 extends Unary<String, String> {
@Override
public String call(Deque<Object> args) {
return new String(Base64.getDecoder().decode(Evaluator.popString(args)));
}

@Override
public String name() {
return "STR_DECODE64";
}

@Override
public String descr() {
return "Decode a BASE64 encoded string";
}
}

public static class ENCODE64 extends Unary<String, Object> {
@Override
public String call(Deque<Object> args) {
return Base64.getEncoder().encodeToString(Evaluator.popBytes(args));
}

@Override
public String name() {
return "STR_BASE64";
}

@Override
public String descr() {
return "Encode stringified Object or byte array to a BASE64 encoded string";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ static String popString(Deque<Object> args) {
return String.valueOf(a);
}

static byte[] popBytes(Deque<Object> args) {
Object a = args.pop();
if (!(a instanceof byte[])) {
return String.valueOf(a).getBytes();
}

return (byte[]) a;
}

static Number popNumeric(Deque<Object> args) {
Object a = args.pop();
if (a instanceof Number) {
Expand Down

0 comments on commit 70612bf

Please sign in to comment.