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);
}
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 "";
}
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;
}
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_;
}
Aggregations