org.apache.commons.jre.java.nio.charset.CharsetEncoder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gwt-commons-codec Show documentation
Show all versions of gwt-commons-codec Show documentation
The Apache Commons Codec component contains encoders and decoders for
various formats such as Base16, Base32, Base64, digest, and Hexadecimal. In addition to these
widely used encoders and decoders, the codec package also maintains a
collection of phonetic encoding utilities.
This is a port for GWT, which enables program, to use Apache Commons Codec
also in the frontend compiled by the gwt compiler to java-script.
package java.nio.charset;
import javaemul.internal.EmulatedCharset;
import java.util.Objects;
public class CharsetEncoder {
private final EmulatedCharset charset;
public CharsetEncoder(final Charset pcharset) {
this.charset = (EmulatedCharset) pcharset;
}
/**
* Tells whether or not this encoder can encode the given character sequence.
*
*
* If this method returns false for a particular character sequence then more information
* about why the sequence cannot be encoded may be obtained by performing a full
* encoding operation.
*
*
*
* This method may modify this encoder's state; it should therefore not be invoked if an encoding
* operation is already in progress.
*
*
*
* The default implementation of this method is not very efficient; it should generally be
* overridden to improve performance.
*
*
* @param cs The given character sequence
*
* @return true if, and only if, this encoder can encode the given character without
* throwing any exceptions and without performing any replacements
*
*/
public boolean canEncode(final CharSequence cs) {
if (cs == null) {
return true;
}
final String cstring = Objects.toString(cs);
final byte[] stringAsByte = this.charset.getBytes(cstring);
return Objects.equals(cstring, String.valueOf(
this.charset.decodeString(this.charset.getBytes(cstring), 0, stringAsByte.length)));
}
}