CharsetProvider (Java SE 26 & JDK 26)
Description: declaration: module: java.base, package: java.nio.charset.spi, class: CharsetProvider
Keywords: java.nio.charset.spi.CharsetProvider class

# Class CharsetProvider

java.lang.Object
java.nio.charset.spi.CharsetProvider

public abstract class CharsetProvider extends Object
Charset service-provider class.

A charset provider is a concrete subclass of this class that has a zero-argument constructor and some number of associated `Charset` implementation classes. Charset providers are deployed on the application module path or the application class path. In order to be looked up, charset providers must be visible to the `system class loader`. See `Deploying Service Providers` for further detail on deploying a charset provider as a module or on the class path.

For a charset provider deployed in a module, the provides directive must be specified in the module declaration. The provides directive specifies both the service and the service provider. In this case, the service is `java.nio.charset.spi.CharsetProvider`.

As an example, a charset provider deployed as a module might specify the following directive:

        provides java.nio.charset.spi.CharsetProvider with com.example.ExternalCharsetProvider;

For a charset provider deployed on the class path, it identifies itself with a provider-configuration file named `java.nio.charset.spi.CharsetProvider` in the resource directory `META-INF/services`. The file should contain a list of fully-qualified concrete charset-provider class names, one per line. A line is terminated by any one of a line feed (`'\n'`), a carriage return (`'\r'`), or a carriage return followed immediately by a line feed. Space and tab characters surrounding each name, as well as blank lines, are ignored. The comment character is `'#'` (`'\u0023'`); on each line all characters following the first comment character are ignored. The file must be encoded in UTF-8.

If a particular concrete charset provider class is named in more than one configuration file, or is named in the same configuration file more than once, then the duplicates will be ignored. The configuration file naming a particular provider need not be in the same jar file or other distribution unit as the provider itself. The provider must be accessible from the same class loader that was initially queried to locate the configuration file; this is not necessarily the class loader that loaded the file.

Since:
  1.4
See Also:
  - `Charset`

- ## Constructor Summary

  Constructors
  Modifier
  Constructor
  Description
  `protected`
  `CharsetProvider()`
  Initializes a new charset provider.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `abstract Charset`
  `charsetForName(String charsetName)`
  Retrieves a charset for the given charset name.
  `abstract Iterator<Charset>`
  `charsets()`
  Creates an iterator that iterates over the charsets supported by this provider.

  ### Methods declared in class Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`
  Modifier and Type
  Method
  Description
  `protected Object`
  `clone()`
  Creates and returns a copy of this object.
  `boolean`
  `equals(Object obj)`
  Indicates whether some other object is "equal to" this one.
  `protected void`
  `finalize()`
  Deprecated, for removal: This API element is subject to removal in a future version.
  Finalization is deprecated and subject to removal in a future release.
  `final Class<?>`
  `getClass()`
  Returns the runtime class of this `Object`.
  `int`
  `hashCode()`
  Returns a hash code value for this object.
  `final void`
  `notify()`
  Wakes up a single thread that is waiting on this object's monitor.
  `final void`
  `notifyAll()`
  Wakes up all threads that are waiting on this object's monitor.
  `String`
  `toString()`
  Returns a string representation of the object.
  `final void`
  `wait()`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted.
  `final void`
  `wait(long timeoutMillis)`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.
  `final void`
  `wait(long timeoutMillis, int nanos)`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

- ## Constructor Details

  - ### CharsetProvider

    protected CharsetProvider()
    Initializes a new charset provider.

- ## Method Details

  - ### charsets

    public abstract Iterator<Charset> charsets()
    Creates an iterator that iterates over the charsets supported by this provider. This method is used in the implementation of the `Charset.availableCharsets` method.
    Returns:
      The new iterator

  - ### charsetForName

    public abstract Charset charsetForName(String charsetName)
    Retrieves a charset for the given charset name.
    Parameters:
      `charsetName` - The name of the requested charset; may be either a canonical name or an alias
    Returns:
      A charset object for the named charset, or `null` if the named charset is not supported by this provider