下面列出了怎么用java.nio.charset.spi.CharsetProvider的API类实例代码及写法,或者点击链接到github查看源代码。
private static CharsetProvider[] extendedProviders() {
return AccessController.doPrivileged(new PrivilegedAction<>() {
public CharsetProvider[] run() {
CharsetProvider[] cps = new CharsetProvider[1];
int n = 0;
ServiceLoader<CharsetProvider> sl =
ServiceLoader.loadInstalled(CharsetProvider.class);
for (CharsetProvider cp : sl) {
if (n + 1 > cps.length) {
cps = Arrays.copyOf(cps, cps.length << 1);
}
cps[n++] = cp;
}
return n == cps.length ? cps : Arrays.copyOf(cps, n);
}});
}
private static CharsetProvider[] extendedProviders() {
return AccessController.doPrivileged(new PrivilegedAction<>() {
public CharsetProvider[] run() {
CharsetProvider[] cps = new CharsetProvider[1];
int n = 0;
ServiceLoader<CharsetProvider> sl =
ServiceLoader.loadInstalled(CharsetProvider.class);
for (CharsetProvider cp : sl) {
if (n + 1 > cps.length) {
cps = Arrays.copyOf(cps, cps.length << 1);
}
cps[n++] = cp;
}
return n == cps.length ? cps : Arrays.copyOf(cps, n);
}});
}
private static Charset lookupViaProviders(final String charsetName) {
// The runtime startup sequence looks up standard charsets as a
// consequence of the VM's invocation of System.initializeSystemClass
// in order to, e.g., set system properties and encode filenames. At
// that point the application class loader has not been initialized,
// however, so we can't look for providers because doing so will cause
// that loader to be prematurely initialized with incomplete
// information.
//
if (!sun.misc.VM.isBooted())
return null;
if (gate.get() != null)
// Avoid recursive provider lookups
return null;
try {
gate.set(gate);
return AccessController.doPrivileged(
new PrivilegedAction<Charset>() {
public Charset run() {
for (Iterator<CharsetProvider> i = providers();
i.hasNext();) {
CharsetProvider cp = i.next();
Charset cs = cp.charsetForName(charsetName);
if (cs != null)
return cs;
}
return null;
}
});
} finally {
gate.set(null);
}
}
private static Charset lookupViaProviders(final String charsetName) {
// The runtime startup sequence looks up standard charsets as a
// consequence of the VM's invocation of System.initializeSystemClass
// in order to, e.g., set system properties and encode filenames. At
// that point the application class loader has not been initialized,
// however, so we can't look for providers because doing so will cause
// that loader to be prematurely initialized with incomplete
// information.
//
if (!sun.misc.VM.isBooted())
return null;
if (gate.get() != null)
// Avoid recursive provider lookups
return null;
try {
gate.set(gate);
return AccessController.doPrivileged(
new PrivilegedAction<Charset>() {
public Charset run() {
for (Iterator<CharsetProvider> i = providers();
i.hasNext();) {
CharsetProvider cp = i.next();
Charset cs = cp.charsetForName(charsetName);
if (cs != null)
return cs;
}
return null;
}
});
} finally {
gate.set(null);
}
}
private static Charset lookupViaProviders(final String charsetName) {
// The runtime startup sequence looks up standard charsets as a
// consequence of the VM's invocation of System.initializeSystemClass
// in order to, e.g., set system properties and encode filenames. At
// that point the application class loader has not been initialized,
// however, so we can't look for providers because doing so will cause
// that loader to be prematurely initialized with incomplete
// information.
//
if (!sun.misc.VM.isBooted())
return null;
if (gate.get() != null)
// Avoid recursive provider lookups
return null;
try {
gate.set(gate);
return AccessController.doPrivileged(
new PrivilegedAction<Charset>() {
public Charset run() {
for (Iterator<CharsetProvider> i = providers();
i.hasNext();) {
CharsetProvider cp = i.next();
Charset cs = cp.charsetForName(charsetName);
if (cs != null)
return cs;
}
return null;
}
});
} finally {
gate.set(null);
}
}
private static Charset lookupViaProviders(final String charsetName) {
// The runtime startup sequence looks up standard charsets as a
// consequence of the VM's invocation of System.initializeSystemClass
// in order to, e.g., set system properties and encode filenames. At
// that point the application class loader has not been initialized,
// however, so we can't look for providers because doing so will cause
// that loader to be prematurely initialized with incomplete
// information.
//
if (!sun.misc.VM.isBooted())
return null;
if (gate.get() != null)
// Avoid recursive provider lookups
return null;
try {
gate.set(gate);
return AccessController.doPrivileged(
new PrivilegedAction<Charset>() {
public Charset run() {
for (Iterator<CharsetProvider> i = providers();
i.hasNext();) {
CharsetProvider cp = i.next();
Charset cs = cp.charsetForName(charsetName);
if (cs != null)
return cs;
}
return null;
}
});
} finally {
gate.set(null);
}
}
private static Charset lookupViaProviders(final String charsetName) {
// The runtime startup sequence looks up standard charsets as a
// consequence of the VM's invocation of System.initializeSystemClass
// in order to, e.g., set system properties and encode filenames. At
// that point the application class loader has not been initialized,
// however, so we can't look for providers because doing so will cause
// that loader to be prematurely initialized with incomplete
// information.
//
if (!sun.misc.VM.isBooted())
return null;
if (gate.get() != null)
// Avoid recursive provider lookups
return null;
try {
gate.set(gate);
return AccessController.doPrivileged(
new PrivilegedAction<Charset>() {
public Charset run() {
for (Iterator<CharsetProvider> i = providers();
i.hasNext();) {
CharsetProvider cp = i.next();
Charset cs = cp.charsetForName(charsetName);
if (cs != null)
return cs;
}
return null;
}
});
} finally {
gate.set(null);
}
}
private static Charset lookupViaProviders(final String charsetName) {
// The runtime startup sequence looks up standard charsets as a
// consequence of the VM's invocation of System.initializeSystemClass
// in order to, e.g., set system properties and encode filenames. At
// that point the application class loader has not been initialized,
// however, so we can't look for providers because doing so will cause
// that loader to be prematurely initialized with incomplete
// information.
//
if (!sun.misc.VM.isBooted())
return null;
if (gate.get() != null)
// Avoid recursive provider lookups
return null;
try {
gate.set(gate);
return AccessController.doPrivileged(
new PrivilegedAction<Charset>() {
public Charset run() {
for (Iterator<CharsetProvider> i = providers();
i.hasNext();) {
CharsetProvider cp = i.next();
Charset cs = cp.charsetForName(charsetName);
if (cs != null)
return cs;
}
return null;
}
});
} finally {
gate.set(null);
}
}
private static Charset lookupViaProviders(final String charsetName) {
// The runtime startup sequence looks up standard charsets as a
// consequence of the VM's invocation of System.initializeSystemClass
// in order to, e.g., set system properties and encode filenames. At
// that point the application class loader has not been initialized,
// however, so we can't look for providers because doing so will cause
// that loader to be prematurely initialized with incomplete
// information.
//
if (!sun.misc.VM.isBooted())
return null;
if (gate.get() != null)
// Avoid recursive provider lookups
return null;
try {
gate.set(gate);
return AccessController.doPrivileged(
new PrivilegedAction<Charset>() {
public Charset run() {
for (Iterator<CharsetProvider> i = providers();
i.hasNext();) {
CharsetProvider cp = i.next();
Charset cs = cp.charsetForName(charsetName);
if (cs != null)
return cs;
}
return null;
}
});
} finally {
gate.set(null);
}
}
private static Charset lookupViaProviders(final String charsetName) {
// The runtime startup sequence looks up standard charsets as a
// consequence of the VM's invocation of System.initializeSystemClass
// in order to, e.g., set system properties and encode filenames. At
// that point the application class loader has not been initialized,
// however, so we can't look for providers because doing so will cause
// that loader to be prematurely initialized with incomplete
// information.
//
if (!VM.isBooted())
return null;
if (gate.get() != null)
// Avoid recursive provider lookups
return null;
try {
gate.set(gate);
return AccessController.doPrivileged(
new PrivilegedAction<>() {
public Charset run() {
for (Iterator<CharsetProvider> i = providers();
i.hasNext();) {
CharsetProvider cp = i.next();
Charset cs = cp.charsetForName(charsetName);
if (cs != null)
return cs;
}
return null;
}
});
} finally {
gate.set(null);
}
}
private static Charset lookupExtendedCharset(String charsetName) {
if (!VM.isBooted()) // see lookupViaProviders()
return null;
CharsetProvider[] ecps = ExtendedProviderHolder.extendedProviders;
for (CharsetProvider cp : ecps) {
Charset cs = cp.charsetForName(charsetName);
if (cs != null)
return cs;
}
return null;
}
private static Charset lookupViaProviders(final String charsetName) {
// The runtime startup sequence looks up standard charsets as a
// consequence of the VM's invocation of System.initializeSystemClass
// in order to, e.g., set system properties and encode filenames. At
// that point the application class loader has not been initialized,
// however, so we can't look for providers because doing so will cause
// that loader to be prematurely initialized with incomplete
// information.
//
if (!VM.isBooted())
return null;
if (gate.get() != null)
// Avoid recursive provider lookups
return null;
try {
gate.set(gate);
return AccessController.doPrivileged(
new PrivilegedAction<>() {
public Charset run() {
for (Iterator<CharsetProvider> i = providers();
i.hasNext();) {
CharsetProvider cp = i.next();
Charset cs = cp.charsetForName(charsetName);
if (cs != null)
return cs;
}
return null;
}
});
} finally {
gate.set(null);
}
}
private static Charset lookupExtendedCharset(String charsetName) {
if (!VM.isBooted()) // see lookupViaProviders()
return null;
CharsetProvider[] ecps = ExtendedProviderHolder.extendedProviders;
for (CharsetProvider cp : ecps) {
Charset cs = cp.charsetForName(charsetName);
if (cs != null)
return cs;
}
return null;
}
private static Charset lookupViaProviders(final String charsetName) {
// The runtime startup sequence looks up standard charsets as a
// consequence of the VM's invocation of System.initializeSystemClass
// in order to, e.g., set system properties and encode filenames. At
// that point the application class loader has not been initialized,
// however, so we can't look for providers because doing so will cause
// that loader to be prematurely initialized with incomplete
// information.
//
if (!sun.misc.VM.isBooted())
return null;
if (gate.get() != null)
// Avoid recursive provider lookups
return null;
try {
gate.set(gate);
return AccessController.doPrivileged(
new PrivilegedAction<Charset>() {
public Charset run() {
for (Iterator<CharsetProvider> i = providers();
i.hasNext();) {
CharsetProvider cp = i.next();
Charset cs = cp.charsetForName(charsetName);
if (cs != null)
return cs;
}
return null;
}
});
} finally {
gate.set(null);
}
}
private static Charset lookupViaProviders(final String charsetName) {
// The runtime startup sequence looks up standard charsets as a
// consequence of the VM's invocation of System.initializeSystemClass
// in order to, e.g., set system properties and encode filenames. At
// that point the application class loader has not been initialized,
// however, so we can't look for providers because doing so will cause
// that loader to be prematurely initialized with incomplete
// information.
//
if (!sun.misc.VM.isBooted())
return null;
if (gate.get() != null)
// Avoid recursive provider lookups
return null;
try {
gate.set(gate);
return AccessController.doPrivileged(
new PrivilegedAction<Charset>() {
public Charset run() {
for (Iterator<CharsetProvider> i = providers();
i.hasNext();) {
CharsetProvider cp = i.next();
Charset cs = cp.charsetForName(charsetName);
if (cs != null)
return cs;
}
return null;
}
});
} finally {
gate.set(null);
}
}
private static Charset lookupViaProviders(final String charsetName) {
// The runtime startup sequence looks up standard charsets as a
// consequence of the VM's invocation of System.initializeSystemClass
// in order to, e.g., set system properties and encode filenames. At
// that point the application class loader has not been initialized,
// however, so we can't look for providers because doing so will cause
// that loader to be prematurely initialized with incomplete
// information.
//
if (!sun.misc.VM.isBooted())
return null;
if (gate.get() != null)
// Avoid recursive provider lookups
return null;
try {
gate.set(gate);
return AccessController.doPrivileged(
new PrivilegedAction<Charset>() {
public Charset run() {
for (Iterator<CharsetProvider> i = providers();
i.hasNext();) {
CharsetProvider cp = i.next();
Charset cs = cp.charsetForName(charsetName);
if (cs != null)
return cs;
}
return null;
}
});
} finally {
gate.set(null);
}
}
private static Charset lookupViaProviders(final String charsetName) {
// The runtime startup sequence looks up standard charsets as a
// consequence of the VM's invocation of System.initializeSystemClass
// in order to, e.g., set system properties and encode filenames. At
// that point the application class loader has not been initialized,
// however, so we can't look for providers because doing so will cause
// that loader to be prematurely initialized with incomplete
// information.
//
if (!sun.misc.VM.isBooted())
return null;
if (gate.get() != null)
// Avoid recursive provider lookups
return null;
try {
gate.set(gate);
return AccessController.doPrivileged(
new PrivilegedAction<Charset>() {
public Charset run() {
for (Iterator<CharsetProvider> i = providers();
i.hasNext();) {
CharsetProvider cp = i.next();
Charset cs = cp.charsetForName(charsetName);
if (cs != null)
return cs;
}
return null;
}
});
} finally {
gate.set(null);
}
}
private static Charset lookupViaProviders(final String charsetName) {
// The runtime startup sequence looks up standard charsets as a
// consequence of the VM's invocation of System.initializeSystemClass
// in order to, e.g., set system properties and encode filenames. At
// that point the application class loader has not been initialized,
// however, so we can't look for providers because doing so will cause
// that loader to be prematurely initialized with incomplete
// information.
//
if (!sun.misc.VM.isBooted())
return null;
if (gate.get() != null)
// Avoid recursive provider lookups
return null;
try {
gate.set(gate);
return AccessController.doPrivileged(
new PrivilegedAction<Charset>() {
public Charset run() {
for (Iterator<CharsetProvider> i = providers();
i.hasNext();) {
CharsetProvider cp = i.next();
Charset cs = cp.charsetForName(charsetName);
if (cs != null)
return cs;
}
return null;
}
});
} finally {
gate.set(null);
}
}
private static Charset lookupViaProviders(final String charsetName) {
// The runtime startup sequence looks up standard charsets as a
// consequence of the VM's invocation of System.initializeSystemClass
// in order to, e.g., set system properties and encode filenames. At
// that point the application class loader has not been initialized,
// however, so we can't look for providers because doing so will cause
// that loader to be prematurely initialized with incomplete
// information.
//
if (!sun.misc.VM.isBooted())
return null;
if (gate.get() != null)
// Avoid recursive provider lookups
return null;
try {
gate.set(gate);
return AccessController.doPrivileged(
new PrivilegedAction<Charset>() {
public Charset run() {
for (Iterator<CharsetProvider> i = providers();
i.hasNext();) {
CharsetProvider cp = i.next();
Charset cs = cp.charsetForName(charsetName);
if (cs != null)
return cs;
}
return null;
}
});
} finally {
gate.set(null);
}
}
private static Charset lookupViaProviders(final String charsetName) {
// The runtime startup sequence looks up standard charsets as a
// consequence of the VM's invocation of System.initializeSystemClass
// in order to, e.g., set system properties and encode filenames. At
// that point the application class loader has not been initialized,
// however, so we can't look for providers because doing so will cause
// that loader to be prematurely initialized with incomplete
// information.
//
if (!sun.misc.VM.isBooted())
return null;
if (gate.get() != null)
// Avoid recursive provider lookups
return null;
try {
gate.set(gate);
return AccessController.doPrivileged(
new PrivilegedAction<Charset>() {
public Charset run() {
for (Iterator<CharsetProvider> i = providers();
i.hasNext();) {
CharsetProvider cp = i.next();
Charset cs = cp.charsetForName(charsetName);
if (cs != null)
return cs;
}
return null;
}
});
} finally {
gate.set(null);
}
}
private static Charset lookupViaProviders(final String charsetName) {
// The runtime startup sequence looks up standard charsets as a
// consequence of the VM's invocation of System.initializeSystemClass
// in order to, e.g., set system properties and encode filenames. At
// that point the application class loader has not been initialized,
// however, so we can't look for providers because doing so will cause
// that loader to be prematurely initialized with incomplete
// information.
//
if (!sun.misc.VM.isBooted())
return null;
if (gate.get() != null)
// Avoid recursive provider lookups
return null;
try {
gate.set(gate);
return AccessController.doPrivileged(
new PrivilegedAction<Charset>() {
public Charset run() {
for (Iterator i = providers(); i.hasNext();) {
CharsetProvider cp = (CharsetProvider)i.next();
Charset cs = cp.charsetForName(charsetName);
if (cs != null)
return cs;
}
return null;
}
});
} finally {
gate.set(null);
}
}
private static Charset lookupViaProviders(final String charsetName) {
// The runtime startup sequence looks up standard charsets as a
// consequence of the VM's invocation of System.initializeSystemClass
// in order to, e.g., set system properties and encode filenames. At
// that point the application class loader has not been initialized,
// however, so we can't look for providers because doing so will cause
// that loader to be prematurely initialized with incomplete
// information.
//
if (!sun.misc.VM.isBooted())
return null;
if (gate.get() != null)
// Avoid recursive provider lookups
return null;
try {
gate.set(gate);
return AccessController.doPrivileged(
new PrivilegedAction<Charset>() {
public Charset run() {
for (Iterator<CharsetProvider> i = providers();
i.hasNext();) {
CharsetProvider cp = i.next();
Charset cs = cp.charsetForName(charsetName);
if (cs != null)
return cs;
}
return null;
}
});
} finally {
gate.set(null);
}
}
private static Iterator<CharsetProvider> providers() {
return new Iterator<CharsetProvider>() {
ClassLoader cl = ClassLoader.getSystemClassLoader();
ServiceLoader<CharsetProvider> sl =
ServiceLoader.load(CharsetProvider.class, cl);
Iterator<CharsetProvider> i = sl.iterator();
CharsetProvider next = null;
private boolean getNext() {
while (next == null) {
try {
if (!i.hasNext())
return false;
next = i.next();
} catch (ServiceConfigurationError sce) {
if (sce.getCause() instanceof SecurityException) {
// Ignore security exceptions
continue;
}
throw sce;
}
}
return true;
}
public boolean hasNext() {
return getNext();
}
public CharsetProvider next() {
if (!getNext())
throw new NoSuchElementException();
CharsetProvider n = next;
next = null;
return n;
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
private static Charset lookupExtendedCharset(String charsetName) {
CharsetProvider ecp = ExtendedProviderHolder.extendedProvider;
return (ecp != null) ? ecp.charsetForName(charsetName) : null;
}
/**
* Constructs a sorted map from canonical charset names to charset objects.
*
* <p> The map returned by this method will have one entry for each charset
* for which support is available in the current Java virtual machine. If
* two or more supported charsets have the same canonical name then the
* resulting map will contain just one of them; which one it will contain
* is not specified. </p>
*
* <p> The invocation of this method, and the subsequent use of the
* resulting map, may cause time-consuming disk or network I/O operations
* to occur. This method is provided for applications that need to
* enumerate all of the available charsets, for example to allow user
* charset selection. This method is not used by the {@link #forName
* forName} method, which instead employs an efficient incremental lookup
* algorithm.
*
* <p> This method may return different results at different times if new
* charset providers are dynamically made available to the current Java
* virtual machine. In the absence of such changes, the charsets returned
* by this method are exactly those that can be retrieved via the {@link
* #forName forName} method. </p>
*
* @return An immutable, case-insensitive map from canonical charset names
* to charset objects
*/
public static SortedMap<String,Charset> availableCharsets() {
return AccessController.doPrivileged(
new PrivilegedAction<SortedMap<String,Charset>>() {
public SortedMap<String,Charset> run() {
TreeMap<String,Charset> m =
new TreeMap<String,Charset>(
ASCIICaseInsensitiveComparator.CASE_INSENSITIVE_ORDER);
put(standardProvider.charsets(), m);
CharsetProvider ecp = ExtendedProviderHolder.extendedProvider;
if (ecp != null)
put(ecp.charsets(), m);
for (Iterator<CharsetProvider> i = providers(); i.hasNext();) {
CharsetProvider cp = i.next();
put(cp.charsets(), m);
}
return Collections.unmodifiableSortedMap(m);
}
});
}
private static Iterator<CharsetProvider> providers() {
return new Iterator<CharsetProvider>() {
ClassLoader cl = ClassLoader.getSystemClassLoader();
ServiceLoader<CharsetProvider> sl =
ServiceLoader.load(CharsetProvider.class, cl);
Iterator<CharsetProvider> i = sl.iterator();
CharsetProvider next = null;
private boolean getNext() {
while (next == null) {
try {
if (!i.hasNext())
return false;
next = i.next();
} catch (ServiceConfigurationError sce) {
if (sce.getCause() instanceof SecurityException) {
// Ignore security exceptions
continue;
}
throw sce;
}
}
return true;
}
public boolean hasNext() {
return getNext();
}
public CharsetProvider next() {
if (!getNext())
throw new NoSuchElementException();
CharsetProvider n = next;
next = null;
return n;
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
private static Charset lookupExtendedCharset(String charsetName) {
CharsetProvider ecp = ExtendedProviderHolder.extendedProvider;
return (ecp != null) ? ecp.charsetForName(charsetName) : null;
}
/**
* Constructs a sorted map from canonical charset names to charset objects.
*
* <p> The map returned by this method will have one entry for each charset
* for which support is available in the current Java virtual machine. If
* two or more supported charsets have the same canonical name then the
* resulting map will contain just one of them; which one it will contain
* is not specified. </p>
*
* <p> The invocation of this method, and the subsequent use of the
* resulting map, may cause time-consuming disk or network I/O operations
* to occur. This method is provided for applications that need to
* enumerate all of the available charsets, for example to allow user
* charset selection. This method is not used by the {@link #forName
* forName} method, which instead employs an efficient incremental lookup
* algorithm.
*
* <p> This method may return different results at different times if new
* charset providers are dynamically made available to the current Java
* virtual machine. In the absence of such changes, the charsets returned
* by this method are exactly those that can be retrieved via the {@link
* #forName forName} method. </p>
*
* @return An immutable, case-insensitive map from canonical charset names
* to charset objects
*/
public static SortedMap<String,Charset> availableCharsets() {
return AccessController.doPrivileged(
new PrivilegedAction<SortedMap<String,Charset>>() {
public SortedMap<String,Charset> run() {
TreeMap<String,Charset> m =
new TreeMap<String,Charset>(
ASCIICaseInsensitiveComparator.CASE_INSENSITIVE_ORDER);
put(standardProvider.charsets(), m);
CharsetProvider ecp = ExtendedProviderHolder.extendedProvider;
if (ecp != null)
put(ecp.charsets(), m);
for (Iterator<CharsetProvider> i = providers(); i.hasNext();) {
CharsetProvider cp = i.next();
put(cp.charsets(), m);
}
return Collections.unmodifiableSortedMap(m);
}
});
}
private static Iterator<CharsetProvider> providers() {
return new Iterator<CharsetProvider>() {
ClassLoader cl = ClassLoader.getSystemClassLoader();
ServiceLoader<CharsetProvider> sl =
ServiceLoader.load(CharsetProvider.class, cl);
Iterator<CharsetProvider> i = sl.iterator();
CharsetProvider next = null;
private boolean getNext() {
while (next == null) {
try {
if (!i.hasNext())
return false;
next = i.next();
} catch (ServiceConfigurationError sce) {
if (sce.getCause() instanceof SecurityException) {
// Ignore security exceptions
continue;
}
throw sce;
}
}
return true;
}
public boolean hasNext() {
return getNext();
}
public CharsetProvider next() {
if (!getNext())
throw new NoSuchElementException();
CharsetProvider n = next;
next = null;
return n;
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
private static Charset lookupExtendedCharset(String charsetName) {
CharsetProvider ecp = ExtendedProviderHolder.extendedProvider;
return (ecp != null) ? ecp.charsetForName(charsetName) : null;
}