From e54de94e5ab40405d837ee7664e90921f5bc38e4 Mon Sep 17 00:00:00 2001 From: Akira Ajisaka Date: Thu, 4 Aug 2016 20:13:11 +0900 Subject: [PATCH] MAPREDUCE-6730. Use StandardCharsets instead of String overload in TextOutputFormat. Contributed by Sahil Kang. This closes #114 (cherry picked from commit 70c278115249898132490a89a548fd936c09f54b) --- .../hadoop/mapred/TextOutputFormat.java | 24 ++++++------------- .../lib/output/TextOutputFormat.java | 24 ++++++------------- 2 files changed, 14 insertions(+), 34 deletions(-) diff --git a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapred/TextOutputFormat.java b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapred/TextOutputFormat.java index ca499e49a5a..bf2f44733d2 100644 --- a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapred/TextOutputFormat.java +++ b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapred/TextOutputFormat.java @@ -20,7 +20,7 @@ import java.io.DataOutputStream; import java.io.IOException; -import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceStability; @@ -43,26 +43,16 @@ public class TextOutputFormat extends FileOutputFormat { protected static class LineRecordWriter implements RecordWriter { - private static final String utf8 = "UTF-8"; - private static final byte[] newline; - static { - try { - newline = "\n".getBytes(utf8); - } catch (UnsupportedEncodingException uee) { - throw new IllegalArgumentException("can't find " + utf8 + " encoding"); - } - } + private static final byte[] NEWLINE = + "\n".getBytes(StandardCharsets.UTF_8); protected DataOutputStream out; private final byte[] keyValueSeparator; public LineRecordWriter(DataOutputStream out, String keyValueSeparator) { this.out = out; - try { - this.keyValueSeparator = keyValueSeparator.getBytes(utf8); - } catch (UnsupportedEncodingException uee) { - throw new IllegalArgumentException("can't find " + utf8 + " encoding"); - } + this.keyValueSeparator = + keyValueSeparator.getBytes(StandardCharsets.UTF_8); } public LineRecordWriter(DataOutputStream out) { @@ -80,7 +70,7 @@ private void writeObject(Object o) throws IOException { Text to = (Text) o; out.write(to.getBytes(), 0, to.getLength()); } else { - out.write(o.toString().getBytes(utf8)); + out.write(o.toString().getBytes(StandardCharsets.UTF_8)); } } @@ -101,7 +91,7 @@ public synchronized void write(K key, V value) if (!nullValue) { writeObject(value); } - out.write(newline); + out.write(NEWLINE); } public synchronized void close(Reporter reporter) throws IOException { diff --git a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/lib/output/TextOutputFormat.java b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/lib/output/TextOutputFormat.java index 1522c4c47f0..1c8ea720065 100644 --- a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/lib/output/TextOutputFormat.java +++ b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/lib/output/TextOutputFormat.java @@ -20,7 +20,7 @@ import java.io.DataOutputStream; import java.io.IOException; -import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceStability; @@ -45,26 +45,16 @@ public class TextOutputFormat extends FileOutputFormat { public static String SEPERATOR = "mapreduce.output.textoutputformat.separator"; protected static class LineRecordWriter extends RecordWriter { - private static final String utf8 = "UTF-8"; - private static final byte[] newline; - static { - try { - newline = "\n".getBytes(utf8); - } catch (UnsupportedEncodingException uee) { - throw new IllegalArgumentException("can't find " + utf8 + " encoding"); - } - } + private static final byte[] NEWLINE = + "\n".getBytes(StandardCharsets.UTF_8); protected DataOutputStream out; private final byte[] keyValueSeparator; public LineRecordWriter(DataOutputStream out, String keyValueSeparator) { this.out = out; - try { - this.keyValueSeparator = keyValueSeparator.getBytes(utf8); - } catch (UnsupportedEncodingException uee) { - throw new IllegalArgumentException("can't find " + utf8 + " encoding"); - } + this.keyValueSeparator = + keyValueSeparator.getBytes(StandardCharsets.UTF_8); } public LineRecordWriter(DataOutputStream out) { @@ -82,7 +72,7 @@ private void writeObject(Object o) throws IOException { Text to = (Text) o; out.write(to.getBytes(), 0, to.getLength()); } else { - out.write(o.toString().getBytes(utf8)); + out.write(o.toString().getBytes(StandardCharsets.UTF_8)); } } @@ -103,7 +93,7 @@ public synchronized void write(K key, V value) if (!nullValue) { writeObject(value); } - out.write(newline); + out.write(NEWLINE); } public synchronized