Skip to content

Commit

Permalink
Use StandardCharsets.UTF_8 for converting String to bytes (apache#5372)
Browse files Browse the repository at this point in the history
Use StandardCharsets.UTF_8 for converting String to bytes to avoid potential encoding error.
  • Loading branch information
liketic authored and sijie committed Nov 6, 2019
1 parent e0ea6b5 commit 0053dbc
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
*/
package org.apache.spark.streaming.receiver.example;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import org.apache.pulsar.client.api.Producer;
import org.apache.pulsar.client.api.PulsarClient;

Expand All @@ -44,7 +45,7 @@ public static void main(String[] args) throws Exception {
try (PulsarClient client = PulsarClient.builder().serviceUrl(args[0]).build()) {
try (Producer<byte[]> producer = client.newProducer().topic(args[1]).create()) {
for (int i = 0; i < 100; i++) {
producer.send(("producer spark streaming msg").getBytes(Charset.forName("UTF-8")));
producer.send(("producer spark streaming msg").getBytes(StandardCharsets.UTF_8));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package org.apache.spark.streaming.receiver.example;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -74,7 +74,7 @@ public static void main(String[] args) throws Exception {

JavaReceiverInputDStream<byte[]> lineDStream = jsc.receiverStream(pulsarReceiver);
JavaPairDStream<String, Integer> result = lineDStream.flatMap(x -> {
String line = new String(x, Charset.forName("UTF-8"));
String line = new String(x, StandardCharsets.UTF_8);
List<String> list = Arrays.asList(line.split(" "));
return list.iterator();
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@
import java.io.InputStreamReader;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CmdUtility {
private static final Logger LOG = LoggerFactory.getLogger(CmdUtility.class);
private static final Charset UTF_8 = Charset.forName("UTF-8");
private static final Charset UTF_8 = StandardCharsets.UTF_8;

/**
* Executes the specified string command in a separate process. STDOUT and STDERR output will be buffered to the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import static org.testng.Assert.assertEquals;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import org.apache.pulsar.common.util.SimpleTextOutputStream;
import org.testng.annotations.BeforeMethod;
Expand Down Expand Up @@ -113,7 +113,7 @@ public void testString() {
}

public String str() {
String s = buf.toString(Charset.forName("utf-8"));
String s = buf.toString(StandardCharsets.UTF_8);
reset();
return s;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -145,7 +145,7 @@ public void testNestedObjects() {
}

public String str() {
String s = buf.toString(Charset.forName("utf-8"));
String s = buf.toString(StandardCharsets.UTF_8);
reset();
return s;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.io.Serializable;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -170,7 +171,7 @@ public void testPulsarKafkaProducerWithSerializer(int partitions) throws Excepti

byte[] bytes = new byte[payload.limit()];
payload.get(bytes);
received.add(new String(bytes, "UTF-8"));
received.add(new String(bytes, StandardCharsets.UTF_8));
}
lastOffset -= 1;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
Expand Down Expand Up @@ -127,7 +128,7 @@ public void test6() {
String data = "\n";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
System.setOut(new PrintStream(baos));
System.setIn(new ByteArrayInputStream(data.getBytes("UTF-8")));
System.setIn(new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)));
ExecutorService executor = Executors.newSingleThreadExecutor();
@SuppressWarnings("unchecked")
Future<Void> future = (Future<Void>) executor.submit(() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
package org.apache.pulsar.io.kinesis;

import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
Expand All @@ -36,7 +36,7 @@ public class KinesisRecord implements Record<byte[]> {
public static final String PARTITION_KEY = "";
public static final String SEQUENCE_NUMBER = "";

private static final CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
private static final CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder();
private final Optional<String> key;
private final byte[] value;
private final HashMap<String, String> userProperties = new HashMap<String, String> ();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import org.bson.Document;
import org.bson.json.JsonParseException;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -105,7 +105,7 @@ public void open(Map<String, Object> config, SinkContext sinkContext) throws Exc

@Override
public void write(Record<byte[]> record) {
final String recordValue = new String(record.getValue(), Charset.forName("UTF-8"));
final String recordValue = new String(record.getValue(), StandardCharsets.UTF_8);

if (log.isDebugEnabled()) {
log.debug("Received record: " + recordValue);
Expand Down Expand Up @@ -143,7 +143,7 @@ private void flush() {

try {
final byte[] docAsBytes = record.getValue();
final Document doc = Document.parse(new String(docAsBytes, Charset.forName("UTF-8")));
final Document doc = Document.parse(new String(docAsBytes, StandardCharsets.UTF_8));
docsToInsert.add(doc);
}
catch (JsonParseException | BSONException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -132,9 +132,9 @@ public void onNext(ChangeStreamDocument<Document> doc) {

consume(new DocRecord(
Optional.of(doc.getDocumentKey().toJson()),
mapper.writeValueAsString(recordValue).getBytes("UTF-8")));
mapper.writeValueAsString(recordValue).getBytes(StandardCharsets.UTF_8)));

} catch (UnsupportedEncodingException | JsonProcessingException e) {
} catch (JsonProcessingException e) {
log.error("Processing doc from mongo", e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CompletableFuture;

import org.apache.zookeeper.KeeperException;
Expand All @@ -31,7 +32,7 @@
import org.slf4j.LoggerFactory;

public class ZookeeperClientFactoryImpl implements ZooKeeperClientFactory {
public static final Charset ENCODING_SCHEME = Charset.forName("UTF-8");
public static final Charset ENCODING_SCHEME = StandardCharsets.UTF_8;

@Override
public CompletableFuture<ZooKeeper> create(String serverList, SessionType sessionType, int zkSessionTimeoutMillis) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
import static org.testng.Assert.assertEquals;

import org.apache.pulsar.common.policies.data.Policies;
import org.apache.pulsar.zookeeper.Deserializers;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.nio.charset.StandardCharsets;

@Test
public class DeserializersTest {

Expand All @@ -40,7 +41,7 @@ void teardown() throws Exception {
@Test
void testSimpleStringDeserialize() throws Exception {
String key = "test_key";
byte[] content = "test_content".getBytes("UTF-8");
byte[] content = "test_content".getBytes(StandardCharsets.UTF_8);
String result = Deserializers.STRING_DESERIALIZER.deserialize(key, content);
assertEquals(result, "test_content");
}
Expand All @@ -50,7 +51,7 @@ void testSimplePolicyDeserialize() throws Exception {
String key = "test_key";
String jsonPolicy = "{\"auth_policies\":{\"namespace_auth\":{},\"destination_auth\":{}},\"replication_clusters\":[],"
+ "\"bundles_activated\":true,\"backlog_quota_map\":{},\"persistence\":null,\"latency_stats_sample_rate\":{},\"message_ttl_in_seconds\":0}";
byte[] content = jsonPolicy.getBytes("UTF-8");
byte[] content = jsonPolicy.getBytes(StandardCharsets.UTF_8);
Policies result = Deserializers.POLICY_DESERIALIZER.deserialize(key, content);
assertEquals(result, new Policies());
}
Expand Down

0 comments on commit 0053dbc

Please sign in to comment.