Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ public void testBasic() {
assertEquals(0x80042001, view.getInt32(0, true));
}

public void testDecoder() {
Int8Array array = TypedArrays.createInt8Array(9);
array.set(new int[] {'A', 'L', 'E', 'X', 'A', 'N', 'D', 'E', 'R'});

TextDecoder textDecoder = TypedArrays.createTextDecoder("utf-8");
assertEquals("ALEXANDER", textDecoder.decode(array));

DataView view = TypedArrays.createDataView(array.buffer(), 1, 3);
assertEquals("LEX", textDecoder.decode(view));
}

// public void testFoo() {
// System.out.println((short)0x80);
// System.out.println((byte)((short)0x80));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.gwtproject.typedarrays.shared.Int16Array;
import org.gwtproject.typedarrays.shared.Int32Array;
import org.gwtproject.typedarrays.shared.Int8Array;
import org.gwtproject.typedarrays.shared.TextDecoder;
import org.gwtproject.typedarrays.shared.TypedArrays;
import org.gwtproject.typedarrays.shared.Uint16Array;
import org.gwtproject.typedarrays.shared.Uint32Array;
Expand Down Expand Up @@ -345,4 +346,9 @@ protected boolean checkUint8ClampedArraySupport() {
protected boolean runtimeSupportCheck() {
return Js.<JsPropertyMap<Object>>uncheckedCast(DomGlobal.window).has("ArrayBuffer");
}

@Override
protected TextDecoder createDecoder(String label) {
return new TextDecoderNative(label);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2012 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package org.gwtproject.typedarrays.client;

import jsinterop.annotations.JsPackage;
import jsinterop.annotations.JsType;
import org.gwtproject.typedarrays.shared.ArrayBuffer;
import org.gwtproject.typedarrays.shared.ArrayBufferView;
import org.gwtproject.typedarrays.shared.TextDecoder;

@JsType(isNative = true, name = "TextDecoder", namespace = JsPackage.GLOBAL)
public class TextDecoderNative implements TextDecoder {

public TextDecoderNative(String label) {}

@Override
public native String decode(ArrayBuffer buffer);

@Override
public native String decode(ArrayBufferView bufferView);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class ArrayBufferImpl implements ArrayBuffer {

private final Object lock = new Object();

private final ByteBuffer buf;
protected final ByteBuffer buf;

/** @param length the size in bytes of the new array buffer instance */
public ArrayBufferImpl(int length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.gwtproject.typedarrays.shared.Int16Array;
import org.gwtproject.typedarrays.shared.Int32Array;
import org.gwtproject.typedarrays.shared.Int8Array;
import org.gwtproject.typedarrays.shared.TextDecoder;
import org.gwtproject.typedarrays.shared.TypedArrays;
import org.gwtproject.typedarrays.shared.Uint16Array;
import org.gwtproject.typedarrays.shared.Uint32Array;
Expand Down Expand Up @@ -365,6 +366,12 @@ public Uint8ClampedArray createUint8ClampedArray(short[] array) {
return result;
}

@GwtIncompatible
@Override
protected TextDecoder createDecoder(String label) {
return new TextDecoderImpl(label);
}

@GwtIncompatible
@Override
protected boolean runtimeSupportCheck() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2012 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package org.gwtproject.typedarrays.server;

import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.StandardCharsets;
import org.gwtproject.core.shared.GwtIncompatible;
import org.gwtproject.typedarrays.shared.ArrayBuffer;
import org.gwtproject.typedarrays.shared.ArrayBufferView;
import org.gwtproject.typedarrays.shared.TextDecoder;

@GwtIncompatible
public class TextDecoderImpl implements TextDecoder {

private final CharsetDecoder decoder;

public TextDecoderImpl(CharsetDecoder decoder) {
this.decoder = decoder;
}

public TextDecoderImpl(String label) {
if (label.equals("utf-8")) {
this.decoder = StandardCharsets.UTF_8.newDecoder();
} else {
throw new RuntimeException("Label: " + label);
}
}

@Override
public String decode(ArrayBuffer buffer) {
ArrayBufferImpl impl = (ArrayBufferImpl) buffer;
try {
return decoder.decode(impl.buf).toString();
} catch (CharacterCodingException e) {
throw new RuntimeException(e);
}
}

@Override
public String decode(ArrayBufferView bufferView) {
ArrayBufferViewImpl viewImpl = (ArrayBufferViewImpl) bufferView;
ByteBuffer buf = viewImpl.arrayBuf.buf.slice();
buf.position(viewImpl.byteOffset);
buf.limit(viewImpl.byteOffset + viewImpl.byteLength);
try {
return decoder.decode(buf).toString();
} catch (CharacterCodingException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2012 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package org.gwtproject.typedarrays.shared;

import jsinterop.annotations.JsPackage;
import jsinterop.annotations.JsType;

/**
* The TextDecoder interface represents a decoder for a specific text encoding, such as UTF-8,
* ISO-8859-2, KOI8-R, GBK, etc. A decoder takes a stream of bytes as input and emits a stream of
* code points.
*/
@JsType(isNative = true, name = "TextDecoder", namespace = JsPackage.GLOBAL)
public interface TextDecoder {

/**
* The TextDecoder.prototype.decode() method returns a String containing the text in {@code
* buffer}, decoded with the specific method for this TextDecoder object.
*/
String decode(ArrayBuffer buffer);

/**
* The TextDecoder.prototype.decode() method returns a String containing the text in {@code
* buffer}, decoded with the specific method for this TextDecoder object.
*/
String decode(ArrayBufferView bufferView);
}
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ protected boolean mightBeSupported() {
* @return true if the current environment actually does support typed arrays
*/
protected abstract boolean runtimeSupportCheck();

protected abstract TextDecoder createDecoder(String label);
}
// CHECKSTYLE_ON

Expand Down Expand Up @@ -664,6 +666,18 @@ public static Uint8ClampedArray createUint8ClampedArray(int length) {
return Instance.impl.createUint8ClampedArray(length);
}

/**
* Creates a new instance of {@link TextDecoder}.
*
* @param label the "label" of the encoder, for example "utf-8". See <a
* href="https://developer.mozilla.org/en-US/docs/Web/API/Encoding_API/Encodings">all
* encodings</a>.
* @return a {@link TextDecoder} instance.
*/
public static TextDecoder createTextDecoder(String label) {
return Instance.impl.createDecoder(label);
}

/**
* Check if the current environment supports typed arrays. Behavior of the various {@code
* createXXX} methods is undefined if this method returns {@code false}, but will typically throw
Expand Down