Skip to content

Commit

Permalink
Use StandardCharsets instead of Strings
Browse files Browse the repository at this point in the history
  • Loading branch information
dadoonet committed Oct 13, 2018
1 parent c8aa579 commit 9215b40
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.nio.charset.StandardCharsets;

/**
* This class checks at startup if everything is correctly set.
* We are using a class for it so it's easier to track checks within a single place
Expand All @@ -36,8 +38,8 @@ public static void check() {

private static void checkUTF8() {
String encoding = System.getProperty("file.encoding");
if (!encoding.equals("UTF-8")) {
logger.warn("[file.encoding] should be [{}] but is [{}]", "UTF-8", encoding);
if (!encoding.equals(StandardCharsets.UTF_8.name())) {
logger.warn("[file.encoding] should be [{}] but is [{}]", StandardCharsets.UTF_8.name(), encoding);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.CopyOption;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.FileSystem;
Expand Down Expand Up @@ -98,7 +99,7 @@ public static String readDefaultJsonVersionedFile(Path config, String version, S
*/
private static String readJsonVersionedFile(Path dir, String version, String type) throws IOException {
Path file = dir.resolve(version).resolve(type + ".json");
return new String(Files.readAllBytes(file), "UTF-8");
return new String(Files.readAllBytes(file), StandardCharsets.UTF_8);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected String readFile(String subdir, String filename) throws IOException {
if (subdir != null) {
dir = dir.resolve(subdir);
}
return new String(Files.readAllBytes(dir.resolve(filename)), "UTF-8");
return new String(Files.readAllBytes(dir.resolve(filename)), StandardCharsets.UTF_8);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import static com.carrotsearch.randomizedtesting.RandomizedTest.randomAsciiLettersOfLengthBetween;
import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -36,11 +36,11 @@ public class StreamsUtilTest extends AbstractFSCrawlerTestCase {
@Test
public void copyStream() throws IOException {
String text = randomAsciiLettersOfLengthBetween(10, 1000);
ByteArrayInputStream bis = new ByteArrayInputStream(text.getBytes(Charset.forName("UTF-8")));
ByteArrayInputStream bis = new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
StreamsUtil.copy(bis, bos);

String copiedText = bos.toString("UTF-8");
String copiedText = bos.toString(StandardCharsets.UTF_8.name());
assertThat(copiedText, is(text));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import javax.ws.rs.core.MediaType;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -84,7 +85,7 @@ public UploadResponse post(
// Create the Doc object
Doc doc = new Doc();

String filename = new String(d.getFileName().getBytes("ISO-8859-1"), "UTF-8");
String filename = new String(d.getFileName().getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
long filesize = d.getSize();

// File
Expand Down

0 comments on commit 9215b40

Please sign in to comment.