All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.commons.jre.java.nio.charset.CharsetEncoder Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 3.17.0-0
Show newest version
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))); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy