Support gbkDecode for js

pull/445/head
FongMi 2 years ago
parent fde6c59919
commit 43f9c2c5f2
  1. 9
      quickjs/src/main/java/com/fongmi/quickjs/method/Global.java
  2. 9
      quickjs/src/main/java/com/fongmi/quickjs/utils/JSUtil.java

@ -20,6 +20,7 @@ import com.whl.quickjs.wrapper.QuickJSContext;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URLEncoder;
import java.nio.charset.CharacterCodingException;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
@ -147,6 +148,14 @@ public class Global {
return parser.joinUrl(parent, child);
}
@Keep
@JSMethod
public String gbkDecode(JSArray buffer) throws CharacterCodingException {
String result = JSUtil.decodeTo("GB2312", buffer);
Logger.t("gbkDecode").d("text:%s\nresult:\n%s", buffer, result);
return result;
}
@Keep
@JSMethod
public String md5X(String text) {

@ -4,6 +4,9 @@ import com.whl.quickjs.wrapper.JSArray;
import com.whl.quickjs.wrapper.JSObject;
import com.whl.quickjs.wrapper.QuickJSContext;
import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
@ -29,4 +32,10 @@ public class JSUtil {
for (String s : map.keySet()) obj.setProperty(s, map.get(s));
return obj;
}
public static String decodeTo(String charset, JSArray buffer) throws CharacterCodingException {
byte[] bytes = new byte[buffer.length()];
for (int i = 0; i < buffer.length(); i++) bytes[i] = (byte) (int) buffer.get(i);
return Charset.forName(charset).newDecoder().decode(ByteBuffer.wrap(bytes)).toString();
}
}

Loading…
Cancel
Save