Search in sources :

Example 1 with NativeArrayBuffer

use of net.sourceforge.htmlunit.corejs.javascript.typedarrays.NativeArrayBuffer in project htmlunit by HtmlUnit.

the class FileReader method readAsArrayBuffer.

/**
 * Reads the contents of the specified {@link Blob} or {@link File}.
 * @param object the {@link Blob} or {@link File} from which to read
 * @throws IOException if an error occurs
 */
@JsxFunction
public void readAsArrayBuffer(final Object object) throws IOException {
    readyState_ = LOADING;
    if (object instanceof Blob) {
        final byte[] bytes = ((Blob) object).getBytes();
        final NativeArrayBuffer buffer = new NativeArrayBuffer(bytes.length);
        System.arraycopy(bytes, 0, buffer.getBuffer(), 0, bytes.length);
        buffer.setParentScope(getParentScope());
        buffer.setPrototype(ScriptableObject.getClassPrototype(getWindow(), buffer.getClassName()));
        result_ = buffer;
    }
    readyState_ = DONE;
    final Event event = new Event(this, Event.TYPE_LOAD);
    fireEvent(event);
}
Also used : Event(com.gargoylesoftware.htmlunit.javascript.host.event.Event) NativeArrayBuffer(net.sourceforge.htmlunit.corejs.javascript.typedarrays.NativeArrayBuffer) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 2 with NativeArrayBuffer

use of net.sourceforge.htmlunit.corejs.javascript.typedarrays.NativeArrayBuffer in project htmlunit by HtmlUnit.

the class XMLHttpRequest method getResponse.

/**
 * @return returns the response's body content as an ArrayBuffer, Blob, Document, JavaScript Object,
 * or DOMString, depending on the value of the request's responseType property.
 */
@JsxGetter
public Object getResponse() {
    if (RESPONSE_TYPE_DEFAULT.equals(responseType_) || RESPONSE_TYPE_TEXT.equals(responseType_)) {
        return getResponseText();
    }
    if (state_ != DONE) {
        return null;
    }
    if (webResponse_ instanceof NetworkErrorWebResponse) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("XMLHttpRequest.responseXML returns of a network error (" + ((NetworkErrorWebResponse) webResponse_).getError() + ")");
        }
        return null;
    }
    if (RESPONSE_TYPE_ARRAYBUFFER.equals(responseType_)) {
        final NativeArrayBuffer nativeArrayBuffer = new NativeArrayBuffer(webResponse_.getContentLength());
        try {
            final int bufferLength = 1;
            final byte[] buffer = new byte[bufferLength];
            try (InputStream inputStream = webResponse_.getContentAsStream()) {
                int offset = 0;
                int readLen;
                while ((readLen = inputStream.read(buffer, 0, bufferLength)) != -1) {
                    System.arraycopy(buffer, 0, nativeArrayBuffer.getBuffer(), offset, readLen);
                    offset += readLen;
                }
            }
            nativeArrayBuffer.setParentScope(getParentScope());
            nativeArrayBuffer.setPrototype(ScriptableObject.getClassPrototype(getWindow(), nativeArrayBuffer.getClassName()));
            return nativeArrayBuffer;
        } catch (final IOException e) {
            webResponse_ = new NetworkErrorWebResponse(webRequest_, e);
            return null;
        }
    } else if (RESPONSE_TYPE_BLOB.equals(responseType_)) {
        try {
            if (webResponse_ != null) {
                try (InputStream inputStream = webResponse_.getContentAsStream()) {
                    final Blob blob = new Blob(IOUtils.toByteArray(inputStream), webResponse_.getContentType());
                    blob.setParentScope(getParentScope());
                    blob.setPrototype(ScriptableObject.getClassPrototype(getWindow(), blob.getClassName()));
                    return blob;
                }
            }
        } catch (final IOException e) {
            webResponse_ = new NetworkErrorWebResponse(webRequest_, e);
            return null;
        }
    } else if (RESPONSE_TYPE_DOCUMENT.equals(responseType_)) {
        if (webResponse_ != null) {
            try {
                final Charset encoding = webResponse_.getContentCharset();
                if (encoding == null) {
                    return "";
                }
                final String content = webResponse_.getContentAsString(encoding);
                if (content == null) {
                    return "";
                }
                return DOMParser.parseFromString(this, content, webResponse_.getContentType());
            } catch (final IOException e) {
                webResponse_ = new NetworkErrorWebResponse(webRequest_, e);
                return null;
            }
        }
    } else if (RESPONSE_TYPE_JSON.equals(responseType_)) {
        if (webResponse_ != null) {
            final Charset encoding = webResponse_.getContentCharset();
            if (encoding == null) {
                return null;
            }
            final String content = webResponse_.getContentAsString(encoding);
            if (content == null) {
                return null;
            }
            try {
                return new JsonParser(Context.getCurrentContext(), this).parseValue(content);
            } catch (final ParseException e) {
                webResponse_ = new NetworkErrorWebResponse(webRequest_, new IOException(e));
                return null;
            }
        }
    }
    return "";
}
Also used : Blob(com.gargoylesoftware.htmlunit.javascript.host.file.Blob) InputStream(java.io.InputStream) Charset(java.nio.charset.Charset) NativeArrayBuffer(net.sourceforge.htmlunit.corejs.javascript.typedarrays.NativeArrayBuffer) IOException(java.io.IOException) ParseException(net.sourceforge.htmlunit.corejs.javascript.json.JsonParser.ParseException) HttpHint(com.gargoylesoftware.htmlunit.WebRequest.HttpHint) JsonParser(net.sourceforge.htmlunit.corejs.javascript.json.JsonParser) JsxGetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)

Example 3 with NativeArrayBuffer

use of net.sourceforge.htmlunit.corejs.javascript.typedarrays.NativeArrayBuffer 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)

Example 4 with NativeArrayBuffer

use of net.sourceforge.htmlunit.corejs.javascript.typedarrays.NativeArrayBuffer in project htmlunit by HtmlUnit.

the class ImageData method getData.

/**
 * Returns a {@link NativeUint8ClampedArray} representing a one-dimensional array containing
 * the data in the RGBA order, with integer values between 0 and 255 (included).
 * @return the {@code data} property
 */
@JsxGetter
public NativeUint8ClampedArray getData() {
    if (data_ == null) {
        final NativeArrayBuffer arrayBuffer = new NativeArrayBuffer(bytes_.length);
        System.arraycopy(bytes_, 0, arrayBuffer.getBuffer(), 0, bytes_.length);
        data_ = new NativeUint8ClampedArray(arrayBuffer, 0, bytes_.length);
        data_.setParentScope(getParentScope());
        data_.setPrototype(ScriptableObject.getClassPrototype(getWindow(this), data_.getClassName()));
    }
    return data_;
}
Also used : NativeUint8ClampedArray(net.sourceforge.htmlunit.corejs.javascript.typedarrays.NativeUint8ClampedArray) NativeArrayBuffer(net.sourceforge.htmlunit.corejs.javascript.typedarrays.NativeArrayBuffer) JsxGetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)

Aggregations

NativeArrayBuffer (net.sourceforge.htmlunit.corejs.javascript.typedarrays.NativeArrayBuffer)4 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)2 JsxGetter (com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)2 HttpHint (com.gargoylesoftware.htmlunit.WebRequest.HttpHint)1 Event (com.gargoylesoftware.htmlunit.javascript.host.event.Event)1 Blob (com.gargoylesoftware.htmlunit.javascript.host.file.Blob)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Charset (java.nio.charset.Charset)1 JsonParser (net.sourceforge.htmlunit.corejs.javascript.json.JsonParser)1 ParseException (net.sourceforge.htmlunit.corejs.javascript.json.JsonParser.ParseException)1 NativeUint8Array (net.sourceforge.htmlunit.corejs.javascript.typedarrays.NativeUint8Array)1 NativeUint8ClampedArray (net.sourceforge.htmlunit.corejs.javascript.typedarrays.NativeUint8ClampedArray)1