Skip to content

Commit 85a4ee6

Browse files
authored
Using StandartCharsets and removing UnsupportedEncodingException (helidon-io#3479)
* Using StandartCharsets and removing UnsupportedEncodingException * fix checkstyle and copyright warnings
1 parent 5cc5c9d commit 85a4ee6

5 files changed

Lines changed: 24 additions & 46 deletions

File tree

media/common/src/main/java/io/helidon/media/common/ContentReaders.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.io.ByteArrayOutputStream;
2020
import java.io.IOException;
2121
import java.io.InputStream;
22-
import java.io.UnsupportedEncodingException;
2322
import java.net.URLDecoder;
2423
import java.nio.ByteBuffer;
2524
import java.nio.charset.Charset;
@@ -168,15 +167,7 @@ private static final class StringToDecodedString implements Mapper<String, Strin
168167

169168
@Override
170169
public String map(String s) {
171-
try {
172-
return URLDecoder.decode(s, charset.name());
173-
} catch (UnsupportedEncodingException e) {
174-
/*
175-
* Convert the encoding exception into an unchecked one to simplify the mapper's use
176-
* in lambdas.
177-
*/
178-
throw new RuntimeException(e);
179-
}
170+
return URLDecoder.decode(s, charset);
180171
}
181172
}
182173
/**

media/multipart/src/main/java/io/helidon/media/multipart/MimeParser.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020 Oracle and/or its affiliates.
2+
* Copyright (c) 2020, 2021 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,9 +15,9 @@
1515
*/
1616
package io.helidon.media.multipart;
1717

18-
import java.io.UnsupportedEncodingException;
1918
import java.nio.ByteBuffer;
2019
import java.nio.charset.Charset;
20+
import java.nio.charset.StandardCharsets;
2121
import java.util.Collections;
2222
import java.util.Iterator;
2323
import java.util.List;
@@ -245,7 +245,7 @@ private ParsingException(Throwable cause) {
245245
}
246246

247247
private static final Logger LOGGER = Logger.getLogger(MimeParser.class.getName());
248-
private static final Charset HEADER_ENCODING = Charset.forName("ISO8859-1");
248+
private static final Charset HEADER_ENCODING = StandardCharsets.ISO_8859_1;
249249

250250
/**
251251
* All states.
@@ -696,8 +696,6 @@ private void skipPreamble() {
696696
* @return a header line or an empty string if the blank line separating the
697697
* header from the body has been reached, or {@code null} if the there is
698698
* no more data in the buffer
699-
* @throws UnsupportedEncodingException if an error occurs while decoding
700-
* from the buffer
701699
*/
702700
private String readHeaderLine() {
703701
// FIXME: what about multi-line headers?

microprofile/openapi/src/main/java/io/helidon/microprofile/openapi/OpenApiCdiExtension.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.io.IOException;
2121
import java.io.InputStream;
2222
import java.io.PrintStream;
23-
import java.io.UnsupportedEncodingException;
2423
import java.net.URL;
2524
import java.nio.charset.Charset;
2625
import java.util.ArrayList;
@@ -213,16 +212,16 @@ private List<URL> findIndexFiles(String... indexPaths) throws IOException {
213212
return result;
214213
}
215214

216-
private static void dumpIndex(Level level, Index index) throws UnsupportedEncodingException {
215+
private static void dumpIndex(Level level, Index index) {
217216
if (LOGGER.isLoggable(level)) {
218217
LOGGER.log(level, "Dump of internal Jandex index:");
219218
PrintStream oldStdout = System.out;
220219
ByteArrayOutputStream baos = new ByteArrayOutputStream();
221-
try (PrintStream newPS = new PrintStream(baos, true, Charset.defaultCharset().name())) {
220+
try (PrintStream newPS = new PrintStream(baos, true, Charset.defaultCharset())) {
222221
System.setOut(newPS);
223222
index.printAnnotations();
224223
index.printSubclasses();
225-
LOGGER.log(level, baos.toString(Charset.defaultCharset().name()));
224+
LOGGER.log(level, baos.toString(Charset.defaultCharset()));
226225
} finally {
227226
System.setOut(oldStdout);
228227
}

webserver/test-support/src/main/java/io/helidon/webserver/testsupport/TestRequest.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2020 Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2021 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,6 @@
1616

1717
package io.helidon.webserver.testsupport;
1818

19-
import java.io.UnsupportedEncodingException;
2019
import java.net.URI;
2120
import java.net.URLEncoder;
2221
import java.nio.charset.StandardCharsets;
@@ -96,7 +95,7 @@ public TestRequest queryParameter(String name, String value) {
9695
public TestRequest header(String name, String value) {
9796
Objects.requireNonNull(name, "Parameter 'name' is null!");
9897
Objects.requireNonNull(name, "Parameter 'value' is null!");
99-
headers.computeIfAbsent(name, k -> new ArrayList()).add(value);
98+
headers.computeIfAbsent(name, k -> new ArrayList<>()).add(value);
10099
return this;
101100
}
102101

@@ -310,11 +309,6 @@ public TestResponse call(Http.RequestMethod method) throws InterruptedException,
310309
}
311310

312311
private String encode(String str) {
313-
try {
314-
return URLEncoder.encode(str, StandardCharsets.UTF_8.toString());
315-
} catch (UnsupportedEncodingException e) {
316-
// Rare case. Should never happen in java
317-
throw new IllegalStateException("UTF-8 is not supported!", e);
318-
}
312+
return URLEncoder.encode(str, StandardCharsets.UTF_8);
319313
}
320314
}

webserver/webserver/src/main/java/io/helidon/webserver/UriComponent.java

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2020 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2021 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,12 +16,13 @@
1616

1717
package io.helidon.webserver;
1818

19-
import java.io.UnsupportedEncodingException;
2019
import java.net.URLDecoder;
2120

2221
import io.helidon.common.http.HashParameters;
2322
import io.helidon.common.http.Parameters;
2423

24+
import static java.nio.charset.StandardCharsets.UTF_8;
25+
2526
/**
2627
* Extracted from Jersey
2728
* <p>
@@ -86,22 +87,17 @@ static Parameters decodeQuery(String query, boolean decodeNames, boolean decodeV
8687
}
8788

8889
private static void decodeQueryParam(Parameters params, String param, boolean decodeNames, boolean decodeValues) {
89-
try {
90-
int equals = param.indexOf('=');
91-
if (equals > 0) {
92-
params.add((decodeNames) ? URLDecoder.decode(param.substring(0, equals), "UTF-8") : param.substring(0, equals),
93-
(decodeValues)
94-
? URLDecoder.decode(param.substring(equals + 1), "UTF-8")
95-
: param.substring(equals + 1));
96-
} else if (equals == 0) {
97-
// no key declared, ignore
98-
return;
99-
} else if (!param.isEmpty()) {
100-
params.add((decodeNames) ? URLDecoder.decode(param, "UTF-8") : param, "");
101-
}
102-
} catch (UnsupportedEncodingException ex) {
103-
// This should never occur
104-
throw new IllegalArgumentException("Should never occur!", ex);
90+
int equals = param.indexOf('=');
91+
if (equals > 0) {
92+
params.add((decodeNames) ? URLDecoder.decode(param.substring(0, equals), UTF_8) : param.substring(0, equals),
93+
(decodeValues)
94+
? URLDecoder.decode(param.substring(equals + 1), UTF_8)
95+
: param.substring(equals + 1));
96+
} else if (equals == 0) {
97+
// no key declared, ignore
98+
return;
99+
} else if (!param.isEmpty()) {
100+
params.add((decodeNames) ? URLDecoder.decode(param, UTF_8) : param, "");
105101
}
106102
}
107103
}

0 commit comments

Comments
 (0)