Utilize StandardCharsets
Closes gh-10972
This commit is contained in:
committed by
Stephane Nicoll
parent
e9c81bf702
commit
bd0dcfb172
@@ -19,6 +19,7 @@ package org.springframework.boot.configurationmetadata;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -32,12 +33,7 @@ import java.util.Map;
|
||||
*/
|
||||
public final class ConfigurationMetadataRepositoryJsonBuilder {
|
||||
|
||||
/**
|
||||
* UTF-8 Charset.
|
||||
*/
|
||||
public static final Charset UTF_8 = Charset.forName("UTF-8");
|
||||
|
||||
private Charset defaultCharset = UTF_8;
|
||||
private Charset defaultCharset = StandardCharsets.UTF_8;
|
||||
|
||||
private final JsonReader reader = new JsonReader();
|
||||
|
||||
@@ -159,7 +155,7 @@ public final class ConfigurationMetadataRepositoryJsonBuilder {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new builder instance using {@link #UTF_8} as the default charset and the
|
||||
* Create a new builder instance using {@link StandardCharsets#UTF_8} as the default charset and the
|
||||
* specified json resource.
|
||||
* @param inputStreams the source input streams
|
||||
* @return a new {@link ConfigurationMetadataRepositoryJsonBuilder} instance.
|
||||
@@ -175,11 +171,11 @@ public final class ConfigurationMetadataRepositoryJsonBuilder {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new builder instance using {@link #UTF_8} as the default charset.
|
||||
* Create a new builder instance using {@link StandardCharsets#UTF_8} as the default charset.
|
||||
* @return a new {@link ConfigurationMetadataRepositoryJsonBuilder} instance.
|
||||
*/
|
||||
public static ConfigurationMetadataRepositoryJsonBuilder create() {
|
||||
return create(UTF_8);
|
||||
return create(StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.boot.configurationmetadata;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
|
||||
import org.hamcrest.CoreMatchers;
|
||||
@@ -27,13 +28,13 @@ import org.junit.Test;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link JsonReader}
|
||||
* Tests for {@link JsonReader}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class JsonReaderTests extends AbstractConfigurationMetadataTests {
|
||||
|
||||
private static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
|
||||
private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
|
||||
|
||||
private final JsonReader reader = new JsonReader();
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
@@ -41,8 +41,6 @@ import org.springframework.boot.configurationprocessor.metadata.ItemMetadata.Ite
|
||||
*/
|
||||
public class JsonMarshaller {
|
||||
|
||||
private static final Charset UTF_8 = Charset.forName("UTF-8");
|
||||
|
||||
private static final int BUFFER_SIZE = 4098;
|
||||
|
||||
public void write(ConfigurationMetadata metadata, OutputStream outputStream)
|
||||
@@ -53,7 +51,7 @@ public class JsonMarshaller {
|
||||
object.put("groups", converter.toJsonArray(metadata, ItemType.GROUP));
|
||||
object.put("properties", converter.toJsonArray(metadata, ItemType.PROPERTY));
|
||||
object.put("hints", converter.toJsonArray(metadata.getHints()));
|
||||
outputStream.write(object.toString(2).getBytes(UTF_8));
|
||||
outputStream.write(object.toString(2).getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
catch (Exception ex) {
|
||||
if (ex instanceof IOException) {
|
||||
@@ -170,7 +168,7 @@ public class JsonMarshaller {
|
||||
|
||||
private String toString(InputStream inputStream) throws IOException {
|
||||
StringBuilder out = new StringBuilder();
|
||||
InputStreamReader reader = new InputStreamReader(inputStream, UTF_8);
|
||||
InputStreamReader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
|
||||
char[] buffer = new char[BUFFER_SIZE];
|
||||
int bytesRead;
|
||||
while ((bytesRead = reader.read(buffer)) != -1) {
|
||||
|
||||
@@ -22,7 +22,7 @@ import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
@@ -42,8 +42,6 @@ import java.util.regex.Pattern;
|
||||
*/
|
||||
public class DefaultLaunchScript implements LaunchScript {
|
||||
|
||||
private static final Charset UTF_8 = Charset.forName("UTF-8");
|
||||
|
||||
private static final int BUFFER_SIZE = 4096;
|
||||
|
||||
private static final Pattern PLACEHOLDER_PATTERN = Pattern
|
||||
@@ -76,7 +74,7 @@ public class DefaultLaunchScript implements LaunchScript {
|
||||
try {
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
copy(inputStream, outputStream);
|
||||
return new String(outputStream.toByteArray(), UTF_8);
|
||||
return new String(outputStream.toByteArray(), StandardCharsets.UTF_8);
|
||||
}
|
||||
finally {
|
||||
inputStream.close();
|
||||
@@ -129,7 +127,7 @@ public class DefaultLaunchScript implements LaunchScript {
|
||||
|
||||
@Override
|
||||
public byte[] toByteArray() {
|
||||
return this.content.getBytes(UTF_8);
|
||||
return this.content.getBytes(StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
package org.springframework.boot.loader.jar;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* Simple wrapper around a byte array that represents an ASCII. Used for performance
|
||||
@@ -27,8 +27,6 @@ import java.nio.charset.Charset;
|
||||
*/
|
||||
final class AsciiBytes {
|
||||
|
||||
private static final Charset UTF_8 = Charset.forName("UTF-8");
|
||||
|
||||
private final byte[] bytes;
|
||||
|
||||
private final int offset;
|
||||
@@ -44,7 +42,7 @@ final class AsciiBytes {
|
||||
* @param string the source string
|
||||
*/
|
||||
AsciiBytes(String string) {
|
||||
this(string.getBytes(UTF_8));
|
||||
this(string.getBytes(StandardCharsets.UTF_8));
|
||||
this.string = string;
|
||||
}
|
||||
|
||||
@@ -124,7 +122,7 @@ final class AsciiBytes {
|
||||
if (string == null || string.isEmpty()) {
|
||||
return this;
|
||||
}
|
||||
return append(string.getBytes(UTF_8));
|
||||
return append(string.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
public AsciiBytes append(AsciiBytes asciiBytes) {
|
||||
@@ -147,7 +145,7 @@ final class AsciiBytes {
|
||||
@Override
|
||||
public String toString() {
|
||||
if (this.string == null) {
|
||||
this.string = new String(this.bytes, this.offset, this.length, UTF_8);
|
||||
this.string = new String(this.bytes, this.offset, this.length, StandardCharsets.UTF_8);
|
||||
}
|
||||
return this.string;
|
||||
}
|
||||
@@ -215,7 +213,7 @@ final class AsciiBytes {
|
||||
}
|
||||
|
||||
static String toString(byte[] bytes) {
|
||||
return new String(bytes, UTF_8);
|
||||
return new String(bytes, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
public static int hashCode(String string) {
|
||||
|
||||
Reference in New Issue
Block a user