Skip to content

Commit

Permalink
Using StandartCharsets and removing UnsupportedEncodingException (#3479)
Browse files Browse the repository at this point in the history
* Using StandartCharsets and removing UnsupportedEncodingException

* fix checkstyle and copyright warnings
  • Loading branch information
Captain1653 authored Oct 8, 2021
1 parent 5cc5c9d commit 85a4ee6
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -168,15 +167,7 @@ private static final class StringToDecodedString implements Mapper<String, Strin

@Override
public String map(String s) {
try {
return URLDecoder.decode(s, charset.name());
} catch (UnsupportedEncodingException e) {
/*
* Convert the encoding exception into an unchecked one to simplify the mapper's use
* in lambdas.
*/
throw new RuntimeException(e);
}
return URLDecoder.decode(s, charset);
}
}
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates.
* Copyright (c) 2020, 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,9 +15,9 @@
*/
package io.helidon.media.multipart;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -245,7 +245,7 @@ private ParsingException(Throwable cause) {
}

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

/**
* All states.
Expand Down Expand Up @@ -696,8 +696,6 @@ private void skipPreamble() {
* @return a header line or an empty string if the blank line separating the
* header from the body has been reached, or {@code null} if the there is
* no more data in the buffer
* @throws UnsupportedEncodingException if an error occurs while decoding
* from the buffer
*/
private String readHeaderLine() {
// FIXME: what about multi-line headers?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
Expand Down Expand Up @@ -213,16 +212,16 @@ private List<URL> findIndexFiles(String... indexPaths) throws IOException {
return result;
}

private static void dumpIndex(Level level, Index index) throws UnsupportedEncodingException {
private static void dumpIndex(Level level, Index index) {
if (LOGGER.isLoggable(level)) {
LOGGER.log(level, "Dump of internal Jandex index:");
PrintStream oldStdout = System.out;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (PrintStream newPS = new PrintStream(baos, true, Charset.defaultCharset().name())) {
try (PrintStream newPS = new PrintStream(baos, true, Charset.defaultCharset())) {
System.setOut(newPS);
index.printAnnotations();
index.printSubclasses();
LOGGER.log(level, baos.toString(Charset.defaultCharset().name()));
LOGGER.log(level, baos.toString(Charset.defaultCharset()));
} finally {
System.setOut(oldStdout);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2020 Oracle and/or its affiliates.
* Copyright (c) 2017, 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,7 +16,6 @@

package io.helidon.webserver.testsupport;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -96,7 +95,7 @@ public TestRequest queryParameter(String name, String value) {
public TestRequest header(String name, String value) {
Objects.requireNonNull(name, "Parameter 'name' is null!");
Objects.requireNonNull(name, "Parameter 'value' is null!");
headers.computeIfAbsent(name, k -> new ArrayList()).add(value);
headers.computeIfAbsent(name, k -> new ArrayList<>()).add(value);
return this;
}

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

private String encode(String str) {
try {
return URLEncoder.encode(str, StandardCharsets.UTF_8.toString());
} catch (UnsupportedEncodingException e) {
// Rare case. Should never happen in java
throw new IllegalStateException("UTF-8 is not supported!", e);
}
return URLEncoder.encode(str, StandardCharsets.UTF_8);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,12 +16,13 @@

package io.helidon.webserver;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import io.helidon.common.http.HashParameters;
import io.helidon.common.http.Parameters;

import static java.nio.charset.StandardCharsets.UTF_8;

/**
* Extracted from Jersey
* <p>
Expand Down Expand Up @@ -86,22 +87,17 @@ static Parameters decodeQuery(String query, boolean decodeNames, boolean decodeV
}

private static void decodeQueryParam(Parameters params, String param, boolean decodeNames, boolean decodeValues) {
try {
int equals = param.indexOf('=');
if (equals > 0) {
params.add((decodeNames) ? URLDecoder.decode(param.substring(0, equals), "UTF-8") : param.substring(0, equals),
(decodeValues)
? URLDecoder.decode(param.substring(equals + 1), "UTF-8")
: param.substring(equals + 1));
} else if (equals == 0) {
// no key declared, ignore
return;
} else if (!param.isEmpty()) {
params.add((decodeNames) ? URLDecoder.decode(param, "UTF-8") : param, "");
}
} catch (UnsupportedEncodingException ex) {
// This should never occur
throw new IllegalArgumentException("Should never occur!", ex);
int equals = param.indexOf('=');
if (equals > 0) {
params.add((decodeNames) ? URLDecoder.decode(param.substring(0, equals), UTF_8) : param.substring(0, equals),
(decodeValues)
? URLDecoder.decode(param.substring(equals + 1), UTF_8)
: param.substring(equals + 1));
} else if (equals == 0) {
// no key declared, ignore
return;
} else if (!param.isEmpty()) {
params.add((decodeNames) ? URLDecoder.decode(param, UTF_8) : param, "");
}
}
}

0 comments on commit 85a4ee6

Please sign in to comment.