Search in sources :

Example 1 with NativeUint8Array

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;
}
Also used : NativeArrayBuffer(net.sourceforge.htmlunit.corejs.javascript.typedarrays.NativeArrayBuffer) NativeUint8Array(net.sourceforge.htmlunit.corejs.javascript.typedarrays.NativeUint8Array) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Aggregations

JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)1 NativeArrayBuffer (net.sourceforge.htmlunit.corejs.javascript.typedarrays.NativeArrayBuffer)1 NativeUint8Array (net.sourceforge.htmlunit.corejs.javascript.typedarrays.NativeUint8Array)1