forked from gwtproject/gwt-typedarrays
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue gwtproject#16 - TextDecoder support
- Loading branch information
Showing
8 changed files
with
182 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
gwt-typedarrays/src/main/java/org/gwtproject/typedarrays/client/TextDecoderNative.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
gwt-typedarrays/src/main/java/org/gwtproject/typedarrays/server/TextDecoderImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
gwt-typedarrays/src/main/java/org/gwtproject/typedarrays/shared/TextDecoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters