Skip to content

Commit 082125d

Browse files
author
Justin Lu
committedSep 27, 2024
8340404: CharsetProvider specification updates
Reviewed-by: alanb, naoto
1 parent a7bfced commit 082125d

File tree

4 files changed

+178
-10
lines changed

4 files changed

+178
-10
lines changed
 

‎src/java.base/share/classes/java/nio/charset/spi/CharsetProvider.java

+22-10
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,29 @@
3333
* Charset service-provider class.
3434
*
3535
* <p> A charset provider is a concrete subclass of this class that has a
36-
* zero-argument constructor and some number of associated charset
37-
* implementation classes. Charset providers may be installed in an instance
38-
* of the Java platform as extensions. Providers may also be made available by
39-
* adding them to the application class path or by some other
40-
* platform-specific means. Charset providers are looked up via the current
41-
* thread's {@link java.lang.Thread#getContextClassLoader() context class
42-
* loader}.
36+
* zero-argument constructor and some number of associated {@code Charset}
37+
* implementation classes. Charset providers are deployed on the application
38+
* module path or the application class path. In order to be looked up, charset
39+
* providers must be visible to the {@link ClassLoader#getSystemClassLoader() system
40+
* class loader}. See {@link java.util.ServiceLoader##developing-service-providers
41+
* Deploying Service Providers} for further detail on deploying a charset
42+
* provider as a module or on the class path.
4343
*
44-
* <p> A charset provider identifies itself with a provider-configuration file
45-
* named {@code java.nio.charset.spi.CharsetProvider} in the resource
46-
* directory {@code META-INF/services}. The file should contain a list of
44+
* <p> For a charset provider deployed in a module, the <i>provides</i>
45+
* directive must be specified in the module declaration. The provides directive
46+
* specifies both the service and the service provider. In this case, the service
47+
* is {@code java.nio.charset.spi.CharsetProvider}.
48+
*
49+
* <p> As an example, a charset provider deployed as a module might specify the
50+
* following directive:
51+
* <pre>{@code
52+
* provides java.nio.charset.spi.CharsetProvider with com.example.ExternalCharsetProvider;
53+
* }</pre>
54+
*
55+
* <p> For a charset provider deployed on the class path, it identifies itself
56+
* with a provider-configuration file named {@code
57+
* java.nio.charset.spi.CharsetProvider} in the resource directory
58+
* {@code META-INF/services}. The file should contain a list of
4759
* fully-qualified concrete charset-provider class names, one per line. A line
4860
* is terminated by any one of a line feed ({@code '\n'}), a carriage return
4961
* ({@code '\r'}), or a carriage return followed immediately by a line feed.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8340404
27+
* @summary Check that a CharsetProvider SPI can be deployed as a module
28+
* @build provider/*
29+
* @run main/othervm CharsetProviderAsModuleTest
30+
*/
31+
32+
import java.nio.charset.Charset;
33+
34+
public class CharsetProviderAsModuleTest {
35+
36+
// Basic test ensures that our BAZ charset is loaded via the BazProvider
37+
public static void main(String[] args) {
38+
var cs = Charset.availableCharsets();
39+
Charset bazCs;
40+
// check provider is providing BAZ via charsets()
41+
if (!cs.containsKey("BAZ")) {
42+
throw new RuntimeException("SPI BazProvider did not provide BAZ Charset");
43+
} else {
44+
bazCs = cs.get("BAZ");
45+
// check provider is in a named module
46+
if (!bazCs.getClass().getModule().isNamed()) {
47+
throw new RuntimeException("BazProvider is not a named module");
48+
}
49+
var aliases = bazCs.aliases();
50+
// check BAZ cs aliases were loaded correctly
51+
if (!aliases.contains("BAZ-1") || !aliases.contains("BAZ-2")) {
52+
throw new RuntimeException("BAZ Charset did not provide correct aliases");
53+
}
54+
// check provider implements charsetForName()
55+
if (!bazCs.equals(Charset.forName("BAZ"))) {
56+
throw new RuntimeException("SPI BazProvider provides bad charsetForName()");
57+
}
58+
}
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
module provider {
24+
provides java.nio.charset.spi.CharsetProvider with spi.BazProvider;
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
package spi;
24+
25+
import java.nio.charset.Charset;
26+
import java.nio.charset.CharsetDecoder;
27+
import java.nio.charset.CharsetEncoder;
28+
import java.nio.charset.spi.CharsetProvider;
29+
import java.util.Collections;
30+
import java.util.Iterator;
31+
32+
// Provides some simple BAZ related attributes to our provider
33+
public class BazProvider extends CharsetProvider {
34+
35+
@Override
36+
public Iterator charsets() {
37+
return Collections.singleton(new BazCharset()).iterator();
38+
}
39+
40+
@Override
41+
public Charset charsetForName(String charsetName) {
42+
if (charsetName.equals("BAZ")) {
43+
return new BazCharset();
44+
} else {
45+
return null;
46+
}
47+
}
48+
49+
public static class BazCharset extends Charset {
50+
51+
public BazCharset() {
52+
super("BAZ", new String[] { "BAZ-1", "BAZ-2" });
53+
}
54+
55+
// Overrides to satisfy Charset
56+
@Override
57+
public boolean contains(Charset cs) {
58+
return false;
59+
}
60+
61+
@Override
62+
public CharsetDecoder newDecoder() {
63+
return null;
64+
}
65+
66+
@Override
67+
public CharsetEncoder newEncoder() {
68+
return null;
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)
Please sign in to comment.