Skip to content

Commit 08e9e78

Browse files
committed
Replace String UTF-8 with a charset
Signed-off-by: jansupol <jan.supol@oracle.com>
1 parent e128f30 commit 08e9e78

File tree

9 files changed

+41
-52
lines changed

9 files changed

+41
-52
lines changed

core-common/src/main/java/org/glassfish/jersey/internal/OsgiRegistry.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2024 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2025 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -57,6 +57,8 @@
5757
import org.osgi.framework.FrameworkUtil;
5858
import org.osgi.framework.SynchronousBundleListener;
5959

60+
import static java.nio.charset.StandardCharsets.UTF_8;
61+
6062
/**
6163
* Utility class to deal with OSGi runtime specific behavior.
6264
* This is mainly to handle META-INF/services lookup
@@ -210,7 +212,7 @@ public List<Class<?>> call() throws Exception {
210212
if (LOGGER.isLoggable(Level.FINEST)) {
211213
LOGGER.log(Level.FINEST, "Loading providers for SPI: {0}", spi);
212214
}
213-
reader = new BufferedReader(new InputStreamReader(spiRegistryUrl.openStream(), "UTF-8"));
215+
reader = new BufferedReader(new InputStreamReader(spiRegistryUrl.openStream(), UTF_8));
214216
String providerClassName;
215217

216218
final List<Class<?>> providerClasses = new ArrayList<Class<?>>();

core-common/src/main/java/org/glassfish/jersey/uri/UriComponent.java

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2023 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2025 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -16,13 +16,11 @@
1616

1717
package org.glassfish.jersey.uri;
1818

19-
import java.io.UnsupportedEncodingException;
2019
import java.net.URI;
2120
import java.net.URLDecoder;
2221
import java.nio.ByteBuffer;
2322
import java.nio.Buffer;
2423
import java.nio.CharBuffer;
25-
import java.nio.charset.Charset;
2624
import java.util.ArrayList;
2725
import java.util.Arrays;
2826
import java.util.LinkedList;
@@ -34,6 +32,8 @@
3432
import org.glassfish.jersey.internal.LocalizationMessages;
3533
import org.glassfish.jersey.internal.util.collection.MultivaluedStringMap;
3634

35+
import static java.nio.charset.StandardCharsets.UTF_8;
36+
3737
/**
3838
* Utility class for validating, encoding and decoding components
3939
* of a URI.
@@ -333,7 +333,7 @@ private static void appendPercentEncodedOctet(final StringBuilder sb, final int
333333

334334
private static void appendUTF8EncodedCharacter(final StringBuilder sb, final int codePoint) {
335335
final CharBuffer chars = CharBuffer.wrap(Character.toChars(codePoint));
336-
final ByteBuffer bytes = UTF_8_CHARSET.encode(chars);
336+
final ByteBuffer bytes = UTF_8.encode(chars);
337337

338338
while (bytes.hasRemaining()) {
339339
appendPercentEncodedOctet(sb, bytes.get() & 0xFF);
@@ -424,8 +424,6 @@ private static boolean[] initEncodingTable(final List<String> allowed) {
424424
return table;
425425
}
426426

427-
private static final Charset UTF_8_CHARSET = Charset.forName("UTF-8");
428-
429427
/**
430428
* Decodes characters of a string that are percent-encoded octets using
431429
* UTF-8 decoding (if needed).
@@ -565,19 +563,14 @@ public static MultivaluedMap<String, String> decodeQuery(final String q, final b
565563
@SuppressWarnings("StatementWithEmptyBody")
566564
private static void decodeQueryParam(final MultivaluedMap<String, String> params, final String param,
567565
final boolean decodeNames, final boolean decodeValues) {
568-
try {
569-
final int equals = param.indexOf('=');
570-
if (equals > 0) {
571-
params.add((decodeNames) ? URLDecoder.decode(param.substring(0, equals), "UTF-8") : param.substring(0, equals),
572-
(decodeValues) ? URLDecoder.decode(param.substring(equals + 1), "UTF-8") : param.substring(equals + 1));
573-
} else if (equals == 0) {
574-
// no key declared, ignore
575-
} else if (param.length() > 0) {
576-
params.add((decodeNames) ? URLDecoder.decode(param, "UTF-8") : param, "");
577-
}
578-
} catch (final UnsupportedEncodingException ex) {
579-
// This should never occur
580-
throw new IllegalArgumentException(ex);
566+
final int equals = param.indexOf('=');
567+
if (equals > 0) {
568+
params.add((decodeNames) ? URLDecoder.decode(param.substring(0, equals), UTF_8) : param.substring(0, equals),
569+
(decodeValues) ? URLDecoder.decode(param.substring(equals + 1), UTF_8) : param.substring(equals + 1));
570+
} else if (equals == 0) {
571+
// no key declared, ignore
572+
} else if (param.length() > 0) {
573+
params.add((decodeNames) ? URLDecoder.decode(param, UTF_8) : param, "");
581574
}
582575
}
583576

@@ -853,7 +846,7 @@ private static int decodeOctets(final int i, final ByteBuffer bb, final StringBu
853846
return i + 2;
854847
} else {
855848
//
856-
final CharBuffer cb = UTF_8_CHARSET.decode(bb);
849+
final CharBuffer cb = UTF_8.decode(bb);
857850
sb.append(cb.toString());
858851
return i + ((Buffer) bb).limit() * 3 - 1;
859852
}

core-server/src/main/java/org/glassfish/jersey/server/wadl/internal/generators/WadlGeneratorJAXBGrammarGenerator.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2020 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2025 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -22,6 +22,7 @@
2222
import java.lang.reflect.InvocationTargetException;
2323
import java.lang.reflect.ParameterizedType;
2424
import java.lang.reflect.Type;
25+
import java.nio.charset.StandardCharsets;
2526
import java.security.AccessController;
2627
import java.security.PrivilegedActionException;
2728
import java.security.PrivilegedExceptionAction;
@@ -360,7 +361,7 @@ public Result createOutput(final String namespaceUri, final String suggestedFile
360361

361362
for (final StreamResult result : results) {
362363
final CharArrayWriter writer = (CharArrayWriter) result.getWriter();
363-
final byte[] contents = writer.toString().getBytes("UTF8");
364+
final byte[] contents = writer.toString().getBytes(StandardCharsets.UTF_8);
364365
extraFiles.put(
365366
result.getSystemId(),
366367
new ApplicationDescription.ExternalGrammar(

ext/mvc/src/main/java/org/glassfish/jersey/server/mvc/internal/TemplateHelper.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2020 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2025 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -18,6 +18,7 @@
1818

1919
import java.lang.annotation.Annotation;
2020
import java.nio.charset.Charset;
21+
import java.nio.charset.StandardCharsets;
2122
import java.util.Arrays;
2223
import java.util.List;
2324
import java.util.stream.Collectors;
@@ -44,8 +45,6 @@
4445
*/
4546
public final class TemplateHelper {
4647

47-
private static final Charset DEFAULT_ENCODING = Charset.forName("UTF-8");
48-
4948
/**
5049
* Return an absolute path to the given class where segments are separated using {@code delim} character and {@code path}
5150
* is appended to this path.
@@ -138,7 +137,7 @@ public static Charset getTemplateOutputEncoding(Configuration configuration, Str
138137
final String enc = PropertiesHelper.getValue(configuration.getProperties(), MvcFeature.ENCODING + suffix,
139138
String.class, null);
140139
if (enc == null) {
141-
return DEFAULT_ENCODING;
140+
return StandardCharsets.UTF_8;
142141
} else {
143142
return Charset.forName(enc);
144143
}

incubator/html-json/src/main/java/org/glassfish/jersey/media/htmljson/HtmlJsonProvider.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2020 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2025 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -23,6 +23,7 @@
2323
import java.lang.reflect.Array;
2424
import java.lang.reflect.ParameterizedType;
2525
import java.lang.reflect.Type;
26+
import java.nio.charset.StandardCharsets;
2627
import java.util.ArrayList;
2728
import java.util.List;
2829

@@ -126,7 +127,7 @@ private void dump(Object t, OutputStream out) throws IOException {
126127
}
127128
out.write(']');
128129
} else {
129-
out.write(t.toString().getBytes("UTF-8"));
130+
out.write(t.toString().getBytes(StandardCharsets.UTF_8));
130131
}
131132
}
132133

media/json-jettison/src/main/java/org/glassfish/jersey/jettison/internal/BaseJsonMarshaller.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2020 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2025 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -20,7 +20,7 @@
2020
import java.io.OutputStream;
2121
import java.io.OutputStreamWriter;
2222
import java.io.Writer;
23-
import java.nio.charset.Charset;
23+
import java.nio.charset.StandardCharsets;
2424

2525
import jakarta.xml.bind.JAXBContext;
2626
import jakarta.xml.bind.JAXBException;
@@ -40,8 +40,6 @@
4040
*/
4141
public class BaseJsonMarshaller implements JettisonMarshaller, JettisonConfigured {
4242

43-
private static final Charset UTF8 = Charset.forName("UTF-8");
44-
4543
protected final Marshaller jaxbMarshaller;
4644
protected JettisonConfig jsonConfig;
4745

@@ -67,7 +65,7 @@ public void marshallToJSON(Object o, OutputStream outputStream) throws JAXBExcep
6765
throw new IllegalArgumentException("The output stream is null");
6866
}
6967

70-
marshallToJSON(o, new OutputStreamWriter(outputStream, UTF8));
68+
marshallToJSON(o, new OutputStreamWriter(outputStream, StandardCharsets.UTF_8));
7169
}
7270

7371
public void marshallToJSON(Object o, Writer writer) throws JAXBException {

media/json-jettison/src/main/java/org/glassfish/jersey/jettison/internal/BaseJsonUnmarshaller.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2020 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2025 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -19,7 +19,7 @@
1919
import java.io.InputStream;
2020
import java.io.InputStreamReader;
2121
import java.io.Reader;
22-
import java.nio.charset.Charset;
22+
import java.nio.charset.StandardCharsets;
2323

2424
import jakarta.xml.bind.JAXBContext;
2525
import jakarta.xml.bind.JAXBElement;
@@ -41,8 +41,6 @@
4141
*/
4242
public class BaseJsonUnmarshaller implements JettisonUnmarshaller, JettisonConfigured {
4343

44-
private static final Charset UTF8 = Charset.forName("UTF-8");
45-
4644
protected final Unmarshaller jaxbUnmarshaller;
4745
protected final JettisonConfig jsonConfig;
4846

@@ -62,7 +60,7 @@ public JettisonConfig getJSONConfiguration() {
6260

6361
// JsonUnmarshaller
6462
public <T> T unmarshalFromJSON(InputStream inputStream, Class<T> expectedType) throws JAXBException {
65-
return unmarshalFromJSON(new InputStreamReader(inputStream, UTF8), expectedType);
63+
return unmarshalFromJSON(new InputStreamReader(inputStream, StandardCharsets.UTF_8), expectedType);
6664
}
6765

6866
@SuppressWarnings("unchecked")
@@ -75,7 +73,7 @@ public <T> T unmarshalFromJSON(Reader reader, Class<T> expectedType) throws JAXB
7573
}
7674

7775
public <T> JAXBElement<T> unmarshalJAXBElementFromJSON(InputStream inputStream, Class<T> declaredType) throws JAXBException {
78-
return unmarshalJAXBElementFromJSON(new InputStreamReader(inputStream, UTF8), declaredType);
76+
return unmarshalJAXBElementFromJSON(new InputStreamReader(inputStream, StandardCharsets.UTF_8), declaredType);
7977
}
8078

8179
public <T> JAXBElement<T> unmarshalJAXBElementFromJSON(Reader reader, Class<T> declaredType) throws JAXBException {

media/sse/src/main/java/org/glassfish/jersey/media/sse/EventOutput.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2019 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2025 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -16,10 +16,10 @@
1616

1717
package org.glassfish.jersey.media.sse;
1818

19-
import java.nio.charset.Charset;
20-
2119
import org.glassfish.jersey.server.ChunkedOutput;
2220

21+
import java.nio.charset.StandardCharsets;
22+
2323
/**
2424
* Outbound Server-Sent Events channel.
2525
*
@@ -31,7 +31,7 @@
3131
*/
3232
public class EventOutput extends ChunkedOutput<OutboundEvent> {
3333
// encoding does not matter for lower ASCII characters
34-
private static final byte[] SSE_EVENT_DELIMITER = "\n".getBytes(Charset.forName("UTF-8"));
34+
private static final byte[] SSE_EVENT_DELIMITER = "\n".getBytes(StandardCharsets.UTF_8);
3535

3636
/**
3737
* Create new outbound Server-Sent Events channel.

security/oauth1-signature/src/main/java/org/glassfish/jersey/oauth1/signature/HmaSha1Method.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2018 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2025 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -16,7 +16,6 @@
1616

1717
package org.glassfish.jersey.oauth1.signature;
1818

19-
import java.io.UnsupportedEncodingException;
2019
import java.security.InvalidKeyException;
2120
import java.security.NoSuchAlgorithmException;
2221

@@ -25,6 +24,8 @@
2524

2625
import org.glassfish.jersey.uri.UriComponent;
2726

27+
import static java.nio.charset.StandardCharsets.UTF_8;
28+
2829

2930
/**
3031
* An OAuth signature method that implements HMAC-SHA1.
@@ -78,11 +79,7 @@ public String sign(String baseString, OAuth1Secrets secrets) {
7879

7980
byte[] key;
8081

81-
try {
82-
key = buf.toString().getBytes("UTF-8");
83-
} catch (UnsupportedEncodingException uee) {
84-
throw new IllegalStateException(uee);
85-
}
82+
key = buf.toString().getBytes(UTF_8);
8683

8784
SecretKeySpec spec = new SecretKeySpec(key, SIGNATURE_ALGORITHM);
8885

0 commit comments

Comments
 (0)