pull/585/head
FongMi 1 year ago
parent 8e2ad4036e
commit e06641ad81
  1. 6
      quickjs/src/main/java/com/fongmi/quickjs/method/Global.java
  2. 9
      quickjs/src/main/java/com/fongmi/quickjs/utils/JSUtil.java

@ -21,7 +21,9 @@ import com.whl.quickjs.wrapper.QuickJSContext;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URLEncoder;
import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
@ -151,8 +153,8 @@ public class Global {
@Keep
@JSMethod
public String gbkDecode(JSArray buffer) throws CharacterCodingException {
String result = JSUtil.decodeTo("GB2312", buffer);
public String gbkDecode(byte[] buffer) throws CharacterCodingException {
String result = Charset.forName("GB2312").newDecoder().decode(ByteBuffer.wrap(buffer)).toString();
Logger.t("gbkDecode").d("text:%s\nresult:\n%s", buffer, result);
return result;
}

@ -4,9 +4,6 @@ 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;
@ -25,10 +22,4 @@ 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