Path: blob/master/src/java.base/share/classes/java/nio/charset/spi/CharsetProvider.java
41161 views
/*1* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.nio.charset.spi;2627import java.nio.charset.Charset;28import java.util.Iterator;293031/**32* Charset service-provider class.33*34* <p> A charset provider is a concrete subclass of this class that has a35* zero-argument constructor and some number of associated charset36* implementation classes. Charset providers may be installed in an instance37* of the Java platform as extensions. Providers may also be made available by38* adding them to the applet or application class path or by some other39* platform-specific means. Charset providers are looked up via the current40* thread's {@link java.lang.Thread#getContextClassLoader() context class41* loader}.42*43* <p> A charset provider identifies itself with a provider-configuration file44* named {@code java.nio.charset.spi.CharsetProvider} in the resource45* directory {@code META-INF/services}. The file should contain a list of46* fully-qualified concrete charset-provider class names, one per line. A line47* is terminated by any one of a line feed ({@code '\n'}), a carriage return48* ({@code '\r'}), or a carriage return followed immediately by a line feed.49* Space and tab characters surrounding each name, as well as blank lines, are50* ignored. The comment character is {@code '#'} (<code>'\u0023'</code>); on51* each line all characters following the first comment character are ignored.52* The file must be encoded in UTF-8.53*54* <p> If a particular concrete charset provider class is named in more than55* one configuration file, or is named in the same configuration file more than56* once, then the duplicates will be ignored. The configuration file naming a57* particular provider need not be in the same jar file or other distribution58* unit as the provider itself. The provider must be accessible from the same59* class loader that was initially queried to locate the configuration file;60* this is not necessarily the class loader that loaded the file. </p>61*62*63* @author Mark Reinhold64* @author JSR-51 Expert Group65* @since 1.466*67* @see java.nio.charset.Charset68*/6970public abstract class CharsetProvider {7172private static Void checkPermission() {73@SuppressWarnings("removal")74SecurityManager sm = System.getSecurityManager();75if (sm != null)76sm.checkPermission(new RuntimePermission("charsetProvider"));77return null;78}79private CharsetProvider(Void ignore) { }8081/**82* Initializes a new charset provider.83*84* @throws SecurityException85* If a security manager has been installed and it denies86* {@link RuntimePermission}{@code ("charsetProvider")}87*/88protected CharsetProvider() {89this(checkPermission());90}9192/**93* Creates an iterator that iterates over the charsets supported by this94* provider. This method is used in the implementation of the {@link95* java.nio.charset.Charset#availableCharsets Charset.availableCharsets}96* method.97*98* @return The new iterator99*/100public abstract Iterator<Charset> charsets();101102/**103* Retrieves a charset for the given charset name.104*105* @param charsetName106* The name of the requested charset; may be either107* a canonical name or an alias108*109* @return A charset object for the named charset,110* or {@code null} if the named charset111* is not supported by this provider112*/113public abstract Charset charsetForName(String charsetName);114115}116117118