Skip to content

Commit

Permalink
HADOOP-19041. Use StandardCharsets in more places (#6449)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjfanning authored Mar 29, 2024
1 parent 347521c commit 97c5a6e
Show file tree
Hide file tree
Showing 62 changed files with 166 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
Expand Down Expand Up @@ -426,12 +425,8 @@ DER get(int... tags) {
}

String getAsString() {
try {
return new String(bb.array(), bb.arrayOffset() + bb.position(),
bb.remaining(), "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new IllegalCharsetNameException("UTF-8"); // won't happen.
}
return new String(bb.array(), bb.arrayOffset() + bb.position(),
bb.remaining(), StandardCharsets.UTF_8);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3565,7 +3565,7 @@ private void checkForOverride(Properties properties, String name, String attr, S
* @throws IOException raised on errors performing I/O.
*/
public void writeXml(OutputStream out) throws IOException {
writeXml(new OutputStreamWriter(out, "UTF-8"));
writeXml(new OutputStreamWriter(out, StandardCharsets.UTF_8));
}

public void writeXml(Writer out) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.hadoop.fs;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Base64;
Expand Down Expand Up @@ -76,7 +77,7 @@ public static byte[] decodeValue(String value) throws IOException {
String en = value.substring(0, 2);
if (value.startsWith("\"") && value.endsWith("\"")) {
value = value.substring(1, value.length()-1);
result = value.getBytes("utf-8");
result = value.getBytes(StandardCharsets.UTF_8);
} else if (en.equalsIgnoreCase(HEX_PREFIX)) {
value = value.substring(2, value.length());
try {
Expand All @@ -90,7 +91,7 @@ public static byte[] decodeValue(String value) throws IOException {
}
}
if (result == null) {
result = value.getBytes("utf-8");
result = value.getBytes(StandardCharsets.UTF_8);
}
}
return result;
Expand All @@ -114,7 +115,7 @@ public static String encodeValue(byte[] value, XAttrCodec encoding)
} else if (encoding == BASE64) {
return BASE64_PREFIX + base64.encodeToString(value);
} else {
return "\"" + new String(value, "utf-8") + "\"";
return "\"" + new String(value, StandardCharsets.UTF_8) + "\"";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ public void testMultiByteCharacters() throws IOException {
String name = "multi_byte_\u611b_name";
String value = "multi_byte_\u0641_value";
out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(CONFIG_MULTI_BYTE), "UTF-8"));
new FileOutputStream(CONFIG_MULTI_BYTE), StandardCharsets.UTF_8));
startConfig();
declareProperty(name, value, value);
endConfig();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import java.io.*;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collection;
import java.util.EnumSet;
Expand Down Expand Up @@ -673,7 +674,7 @@ public void testFSOutputStreamBuilder() throws Exception {
fileSys.createFile(path).recursive();
FSDataOutputStream out = builder.build();
String content = "Create with a generic type of createFile!";
byte[] contentOrigin = content.getBytes("UTF8");
byte[] contentOrigin = content.getBytes(StandardCharsets.UTF_8);
out.write(contentOrigin);
out.close();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ static URL createURL(Path path, Map<String, String> params, Map<String,
if (multiValuedParams != null) {
for (Map.Entry<String, List<String>> multiValuedEntry :
multiValuedParams.entrySet()) {
String name = URLEncoder.encode(multiValuedEntry.getKey(), "UTF8");
String name = URLEncoder.encode(multiValuedEntry.getKey(), "UTF-8");
List<String> values = multiValuedEntry.getValue();
for (String value : values) {
sb.append(separator).append(name).append("=").
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Iterator;
Expand Down Expand Up @@ -148,7 +149,7 @@ TextWriter createWriter(Path file, CompressionCodec codec, String delim,
}
OutputStream tmp = fs.create(file);
java.io.Writer out = new BufferedWriter(new OutputStreamWriter(
(null == codec) ? tmp : codec.createOutputStream(tmp), "UTF-8"));
(null == codec) ? tmp : codec.createOutputStream(tmp), StandardCharsets.UTF_8));
return new TextWriter(out, delim);
}

Expand Down Expand Up @@ -379,7 +380,7 @@ public Iterator<FileRegion> iterator() {
FRIterator i = new FRIterator();
try {
BufferedReader r =
new BufferedReader(new InputStreamReader(createStream(), "UTF-8"));
new BufferedReader(new InputStreamReader(createStream(), StandardCharsets.UTF_8));
iterators.put(i, r);
i.pending = nextInternal(i);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Queue;
Expand Down Expand Up @@ -1063,7 +1064,7 @@ void receiveBlock(
// send a special ack upstream.
if (datanode.isRestarting() && isClient && !isTransfer) {
try (Writer out = new OutputStreamWriter(
replicaInfo.createRestartMetaStream(), "UTF-8")) {
replicaInfo.createRestartMetaStream(), StandardCharsets.UTF_8)) {
// write out the current time.
out.write(Long.toString(Time.now() + restartBudget));
out.flush();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -399,7 +400,7 @@ void saveDfsUsed() {
try {
long used = getDfsUsed();
try (Writer out = new OutputStreamWriter(
Files.newOutputStream(outFile.toPath()), "UTF-8")) {
Files.newOutputStream(outFile.toPath()), StandardCharsets.UTF_8)) {
// mtime is written last, so that truncated writes won't be valid.
out.write(Long.toString(used) + " " + Long.toString(timer.now()));
// This is only called as part of the volume shutdown.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.RandomAccessFile;
import java.net.URI;
import java.nio.channels.ClosedChannelException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Collection;
Expand Down Expand Up @@ -929,7 +930,7 @@ public void save() throws IOException {
boolean success = false;
try (BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(fileIoProvider.getFileOutputStream(
FsVolumeImpl.this, getTempSaveFile()), "UTF-8"))) {
FsVolumeImpl.this, getTempSaveFile()), StandardCharsets.UTF_8))) {
WRITER.writeValue(writer, state);
success = true;
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -740,7 +741,7 @@ private static Options buildCliOptions() {
private static String[] readPathFile(String file) throws IOException {
List<String> list = Lists.newArrayList();
BufferedReader reader = new BufferedReader(
new InputStreamReader(new FileInputStream(file), "UTF-8"));
new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8));
try {
String line;
while ((line = reader.readLine()) != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.PrivilegedExceptionAction;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -207,7 +208,7 @@ private Integer listCorruptFileBlocks(String dir, String baseUrl)
}
InputStream stream = connection.getInputStream();
BufferedReader input = new BufferedReader(new InputStreamReader(
stream, "UTF-8"));
stream, StandardCharsets.UTF_8));
try {
String line = null;
while ((line = input.readLine()) != null) {
Expand Down Expand Up @@ -376,7 +377,7 @@ else if (args[idx].equals("-replicaDetails")) {
}
InputStream stream = connection.getInputStream();
BufferedReader input = new BufferedReader(new InputStreamReader(
stream, "UTF-8"));
stream, StandardCharsets.UTF_8));
String line = null;
String lastLine = NamenodeFsck.CORRUPT_STATUS;
int errCode = -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ private INodeSection.INode.Builder processINodeXml(Node node)
inodeBld.setId(id);
String name = node.removeChildStr(SECTION_NAME);
if (name != null) {
inodeBld.setName(ByteString.copyFrom(name, "UTF8"));
inodeBld.setName(ByteString.copyFrom(name, StandardCharsets.UTF_8));
}
switch (type) {
case "FILE":
Expand Down Expand Up @@ -838,7 +838,7 @@ private void processSymlinkXml(Node node,
}
String target = node.removeChildStr(INODE_SECTION_TARGET);
if (target != null) {
bld.setTarget(ByteString.copyFrom(target, "UTF8"));
bld.setTarget(ByteString.copyFrom(target, StandardCharsets.UTF_8));
}
Long lval = node.removeChildLong(INODE_SECTION_MTIME);
if (lval != null) {
Expand Down Expand Up @@ -900,7 +900,7 @@ private INodeSection.XAttrFeatureProto.Builder xattrsXmlToProto(Node xattrs)
}
val = new HexBinaryAdapter().unmarshal(valHex);
} else {
val = valStr.getBytes("UTF8");
val = valStr.getBytes(StandardCharsets.UTF_8);
}
b.setValue(ByteString.copyFrom(val));

Expand Down Expand Up @@ -1232,7 +1232,7 @@ public void process() throws IOException {
}
String name = inodeRef.removeChildStr("name");
if (name != null) {
bld.setName(ByteString.copyFrom(name, "UTF8"));
bld.setName(ByteString.copyFrom(name, StandardCharsets.UTF_8));
}
Integer dstSnapshotId = inodeRef.removeChildInt(
INODE_REFERENCE_SECTION_DST_SNAPSHOT_ID);
Expand Down Expand Up @@ -1468,7 +1468,7 @@ private void processDirDiffEntry() throws IOException {
bld.setChildrenSize(childrenSize);
String name = dirDiff.removeChildStr(SECTION_NAME);
if (name != null) {
bld.setName(ByteString.copyFrom(name, "UTF8"));
bld.setName(ByteString.copyFrom(name, StandardCharsets.UTF_8));
}
Node snapshotCopy = dirDiff.removeChild(
SNAPSHOT_DIFF_SECTION_SNAPSHOT_COPY);
Expand Down Expand Up @@ -1514,7 +1514,7 @@ private void processDirDiffEntry() throws IOException {
}
created.verifyNoRemainingKeys("created");
FsImageProto.SnapshotDiffSection.CreatedListEntry.newBuilder().
setName(ByteString.copyFrom(cleName, "UTF8")).
setName(ByteString.copyFrom(cleName, StandardCharsets.UTF_8)).
build().writeDelimitedTo(out);
actualCreatedListSize++;
}
Expand Down Expand Up @@ -1571,7 +1571,7 @@ private void processFileDiffEntry() throws IOException {
}
String name = fileDiff.removeChildStr(SECTION_NAME);
if (name != null) {
bld.setName(ByteString.copyFrom(name, "UTF8"));
bld.setName(ByteString.copyFrom(name, StandardCharsets.UTF_8));
}
Node snapshotCopy = fileDiff.removeChild(
SNAPSHOT_DIFF_SECTION_SNAPSHOT_COPY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.hadoop.hdfs;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.security.Permission;
import java.security.PrivilegedExceptionAction;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -1917,7 +1918,7 @@ private static void corrupt(
char c = content.charAt(0);
sb.setCharAt(0, ++c);
for(MaterializedReplica replica : replicas) {
replica.corruptData(sb.toString().getBytes("UTF8"));
replica.corruptData(sb.toString().getBytes(StandardCharsets.UTF_8));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import java.net.ServerSocket;
import java.net.SocketTimeoutException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException;
import java.security.PrivilegedExceptionAction;
import java.util.ArrayList;
Expand Down Expand Up @@ -1885,7 +1886,7 @@ public void testDFSDataOutputStreamBuilderForCreation() throws Exception {
.replication((short) 1)
.blockSize(4096)
.build()) {
byte[] contentOrigin = content.getBytes("UTF8");
byte[] contentOrigin = content.getBytes(StandardCharsets.UTF_8);
out1.write(contentOrigin);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,10 @@ private static void writeFile(FileSystem fs, Path path, byte[] buffer,
*/
public static Configuration initializeStorageStateConf(int numDirs,
Configuration conf) {
StringBuffer nameNodeDirs =
new StringBuffer(new File(TEST_ROOT_DIR, "name1").toString());
StringBuffer dataNodeDirs =
new StringBuffer(new File(TEST_ROOT_DIR, "data1").toString());
StringBuilder nameNodeDirs =
new StringBuilder(new File(TEST_ROOT_DIR, "name1").toString());
StringBuilder dataNodeDirs =
new StringBuilder(new File(TEST_ROOT_DIR, "data1").toString());
for (int i = 2; i <= numDirs; i++) {
nameNodeDirs.append("," + new File(TEST_ROOT_DIR, "name"+i));
dataNodeDirs.append("," + new File(TEST_ROOT_DIR, "data"+i));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.io.Writer;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
Expand Down Expand Up @@ -147,7 +148,7 @@ public FileRegion next() {
newFile.getAbsolutePath());
newFile.createNewFile();
Writer writer = new OutputStreamWriter(
new FileOutputStream(newFile.getAbsolutePath()), "utf-8");
new FileOutputStream(newFile.getAbsolutePath()), StandardCharsets.UTF_8);
for(int i=0; i< BLK_LEN/(Integer.SIZE/8); i++) {
writer.write(currentCount);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import static org.mockito.Mockito.mock;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.function.LongFunction;

Expand Down Expand Up @@ -433,7 +434,7 @@ private static INodeDirectory createINodeDirectory(INodeDirectory parent,
PermissionStatus permStatus = PermissionStatus.createImmutable(owner, group,
FsPermission.createImmutable(perm));
INodeDirectory inodeDirectory = new INodeDirectory(
HdfsConstants.GRANDFATHER_INODE_ID, name.getBytes("UTF-8"), permStatus, 0L);
HdfsConstants.GRANDFATHER_INODE_ID, name.getBytes(StandardCharsets.UTF_8), permStatus, 0L);
parent.addChild(inodeDirectory);
return inodeDirectory;
}
Expand All @@ -443,8 +444,8 @@ private static INodeFile createINodeFile(INodeDirectory parent, String name,
PermissionStatus permStatus = PermissionStatus.createImmutable(owner, group,
FsPermission.createImmutable(perm));
INodeFile inodeFile = new INodeFile(HdfsConstants.GRANDFATHER_INODE_ID,
name.getBytes("UTF-8"), permStatus, 0L, 0L, null, REPLICATION,
PREFERRED_BLOCK_SIZE);
name.getBytes(StandardCharsets.UTF_8), permStatus, 0L, 0L, null,
REPLICATION, PREFERRED_BLOCK_SIZE);
parent.addChild(inodeFile);
return inodeFile;
}
Expand Down
Loading

0 comments on commit 97c5a6e

Please sign in to comment.