Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
import java.net.Socket;
import java.net.URI;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -98,29 +97,6 @@ public class DFSUtilClient {
private static final Logger LOG = LoggerFactory.getLogger(
DFSUtilClient.class);

// Using the charset canonical name for String/byte[] conversions is much
// more efficient due to use of cached encoders/decoders.
private static final String UTF8_CSN = StandardCharsets.UTF_8.name();

/**
* Converts a string to a byte array using UTF8 encoding.
*/
public static byte[] string2Bytes(String str) {
try {
return str.getBytes(UTF8_CSN);
} catch (UnsupportedEncodingException e) {
// should never happen!
throw new IllegalArgumentException("UTF8 decoding is not supported", e);
}
}

/**
* Converts a byte array to a string using UTF8 encoding.
*/
public static String bytes2String(byte[] bytes) {
return bytes2String(bytes, 0, bytes.length);
}

/** Return used as percentage of capacity */
public static float getPercentUsed(long used, long capacity) {
return capacity <= 0 ? 100 : (used * 100.0f)/capacity;
Expand Down Expand Up @@ -288,24 +264,6 @@ public static byte[] byteArray2bytes(byte[][] pathComponents) {
return path;
}

/**
* Decode a specific range of bytes of the given byte array to a string
* using UTF8.
*
* @param bytes The bytes to be decoded into characters
* @param offset The index of the first byte to decode
* @param length The number of bytes to decode
* @return The decoded string
*/
static String bytes2String(byte[] bytes, int offset, int length) {
try {
return new String(bytes, offset, length, UTF8_CSN);
} catch (UnsupportedEncodingException e) {
// should never happen!
throw new IllegalArgumentException("UTF8 encoding is not supported", e);
}
}

/**
* @return <code>coll</code> if it is non-null and non-empty. Otherwise,
* returns a list with a single null value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package org.apache.hadoop.hdfs.protocol;

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

import org.apache.hadoop.classification.InterfaceAudience;
Expand All @@ -25,7 +26,6 @@
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.permission.FsPermission;
import org.apache.hadoop.hdfs.DFSUtilClient;

/** Interface that represents the over the wire information for a file.
*/
Expand Down Expand Up @@ -192,7 +192,7 @@ public final boolean isEmptyLocalName() {
* @return the local name in string
*/
public final String getLocalName() {
return DFSUtilClient.bytes2String(path);
return new String(path, UTF_8);
}

/**
Expand Down Expand Up @@ -239,7 +239,7 @@ public final Path getFullPath(final Path parent) {
* @return the symlink as a string.
*/
public final String getSymlink() {
return DFSUtilClient.bytes2String(symlink);
return new String(symlink, UTF_8);
}

public final byte[] getSymlinkInBytes() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package org.apache.hadoop.hdfs.protocol;

import static java.nio.charset.StandardCharsets.UTF_8;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -121,7 +122,7 @@ public DiffType getType() {
}

static String getPathString(byte[] path) {
String pathStr = DFSUtilClient.bytes2String(path);
String pathStr = new String(path, UTF_8);
if (pathStr.isEmpty()) {
return Path.CUR_DIR;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.hadoop.hdfs.protocol;

import java.io.PrintStream;
import static java.nio.charset.StandardCharsets.UTF_8;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;
Expand Down Expand Up @@ -103,7 +104,7 @@ public HdfsFileStatus getDirStatus() {
public Path getFullPath() {
String parentFullPathStr =
(parentFullPath == null || parentFullPath.length == 0) ?
null : DFSUtilClient.bytes2String(parentFullPath);
null : new String(parentFullPath, UTF_8);
if (parentFullPathStr == null
&& dirStatus.getLocalNameInBytes().length == 0) {
// root
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import org.apache.hadoop.fs.permission.AclEntry;
import org.apache.hadoop.fs.permission.AclStatus;
import org.apache.hadoop.fs.permission.FsPermission;
import org.apache.hadoop.hdfs.DFSUtilClient;
import org.apache.hadoop.hdfs.protocol.DatanodeInfo;
import org.apache.hadoop.hdfs.protocol.DirectoryListing;
import org.apache.hadoop.hdfs.protocol.ExtendedBlock;
Expand All @@ -54,6 +53,7 @@
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import static java.nio.charset.StandardCharsets.UTF_8;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -117,7 +117,7 @@ static HdfsFileStatus toFileStatus(final Map<?, ?> json,
final WebHdfsConstants.PathType type =
WebHdfsConstants.PathType.valueOf((String) m.get("type"));
final byte[] symlink = type != WebHdfsConstants.PathType.SYMLINK? null
: DFSUtilClient.string2Bytes((String) m.get("symlink"));
: ((String) m.get("symlink")).getBytes(UTF_8);

final long len = ((Number) m.get("length")).longValue();
final String owner = (String) m.get("owner");
Expand All @@ -138,7 +138,7 @@ static HdfsFileStatus toFileStatus(final Map<?, ?> json,
HdfsConstants.BLOCK_STORAGE_POLICY_ID_UNSPECIFIED;
return new HdfsFileStatus(len, type == WebHdfsConstants.PathType.DIRECTORY,
replication, blockSize, mTime, aTime, permission, owner, group,
symlink, DFSUtilClient.string2Bytes(localName),
symlink, localName.getBytes(UTF_8),
fileId, childrenNum, null,
storagePolicy, null);
}
Expand Down
Loading