use of net.sourceforge.htmlunit.corejs.javascript.typedarrays.NativeUint8Array in project htmlunit by HtmlUnit.
the class TextEncoder method encode.
/**
* @param toEncode the string to encode
* @return returns a Uint8Array containing the text given encoded .
*/
@JsxFunction
public NativeUint8Array encode(final Object toEncode) {
if (Undefined.isUndefined(toEncode)) {
final NativeUint8Array result = new NativeUint8Array(0);
result.setParentScope(getParentScope());
result.setPrototype(ScriptableObject.getClassPrototype(getWindow(this), result.getClassName()));
return result;
}
final String txt;
if (toEncode == null) {
txt = "null";
} else {
txt = Context.toString(toEncode);
}
final byte[] bytes = txt.getBytes(StandardCharsets.UTF_8);
final NativeArrayBuffer arrayBuffer = new NativeArrayBuffer(bytes.length);
System.arraycopy(bytes, 0, arrayBuffer.getBuffer(), 0, bytes.length);
final NativeUint8Array result = new NativeUint8Array(arrayBuffer, 0, bytes.length);
result.setParentScope(getParentScope());
result.setPrototype(ScriptableObject.getClassPrototype(getWindow(this), result.getClassName()));
return result;
}
Aggregations