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-lang3 Show documentation
Show all versions of gwt-commons-lang3 Show documentation
Apache Commons Lang, a package of Java utility classes for the
classes that are in java.lang's hierarchy, or are considered to be so
standard as to justify existence in java.lang.
This is a port for GWT, which enables program, to use Apache Commons Lang
also in the frontend compiled by the gwt compiler to java-script.
The code is tested using the latest revision of the JDK for supported
LTS releases: 8, 11, 17 and 21 currently.
See https://github.com/apache/commons-lang/blob/master/.github/workflows/maven.yml
Please ensure your build environment is up-to-date and kindly report any build issues.
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)));
}
}