Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package org.apache.beam.runners.flink;

import java.io.File;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.time.Duration;
import java.util.Arrays;
Expand Down Expand Up @@ -188,7 +188,7 @@ private void runDriverProgram() throws Exception {
String msg =
String.format(
"Failed to start job with driver program: %s %s output: %s",
executable, args, new String(output, Charset.defaultCharset()));
executable, args, new String(output, StandardCharsets.UTF_8));
throw new RuntimeException(msg, e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ private static class JulHandlerPrintStream extends PrintStream {
private int carryOverBytes;
private byte[] carryOverByteArray;

@SuppressWarnings("ForbidDefaultCharset")
private JulHandlerPrintStream(Handler handler, String loggerName, Level logLevel)
throws UnsupportedEncodingException {
super(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import static org.junit.Assert.assertThat;

import java.io.PrintStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import org.apache.beam.runners.dataflow.worker.LogSaver;
Expand Down Expand Up @@ -124,14 +124,14 @@ public void testLogOnClose() {
public void testLogRawBytes() {
PrintStream printStream = createPrintStreamAdapter();
String msg = "♠ ♡ ♢ ♣ ♤ ♥ ♦ ♧";
byte[] bytes = msg.getBytes(Charset.defaultCharset());
byte[] bytes = msg.getBytes(StandardCharsets.UTF_8);
printStream.write(bytes, 0, 1);
printStream.write(bytes, 1, 4);
printStream.write(bytes, 5, 15);
printStream.write(bytes, 20, bytes.length - 20);
assertThat(handler.getLogs(), is(empty()));
String newlineMsg = "♠ ♡ \n♦ ♧";
byte[] newlineMsgBytes = newlineMsg.getBytes(Charset.defaultCharset());
byte[] newlineMsgBytes = newlineMsg.getBytes(StandardCharsets.UTF_8);
printStream.write(newlineMsgBytes, 0, newlineMsgBytes.length);
assertThat(handler.getLogs(), hasLogItem(msg + newlineMsg));
}
Expand All @@ -154,7 +154,7 @@ public void testNoEmptyMessages() {
printStream.flush();
printStream.print("");
printStream.flush();
byte[] bytes = "a".getBytes(Charset.defaultCharset());
byte[] bytes = "a".getBytes(StandardCharsets.UTF_8);
printStream.write(bytes, 0, 0);
printStream.flush();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -130,7 +130,7 @@ public void testRedirectOutput() throws IOException, InterruptedException {
processManager.stopProcess("1");
byte[] output = Files.readAllBytes(outputFile.toPath());
assertNotNull(output);
String outputStr = new String(output, Charset.defaultCharset());
String outputStr = new String(output, StandardCharsets.UTF_8);
assertThat(outputStr, containsString("testing123"));
}

Expand Down Expand Up @@ -158,7 +158,7 @@ public void testInheritIO() throws IOException, InterruptedException {
}
// TODO: this doesn't work as inherit IO bypasses System.out/err
// the output instead appears in the console
// String outputStr = new String(baos.toByteArray(), Charset.defaultCharset());
// String outputStr = new String(baos.toByteArray(), StandardCharsets.UTF_8);
// assertThat(outputStr, containsString("testing123"));
assertFalse(ProcessManager.INHERIT_IO_FILE.exists());
}
Expand Down
8 changes: 8 additions & 0 deletions sdks/java/build-tools/src/main/resources/beam/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ page at http://checkstyle.sourceforge.net/config.html -->
<property name="message" value="You are using raw guava, please use vendored guava classes."/>
</module>

<!-- Forbid use of Charset.defaultCharset()-->
<module name="RegexpSinglelineJava">
<property name="id" value="ForbidDefaultCharset"/>
<property name="format" value="(\sCharset\.defaultCharset())"/>
<property name="severity" value="error"/>
<property name="message" value="You are using Charset.defaultCharset(), please use StandardCharsets.UTF_8."/>
</module>

<!-- Forbid non-vendored gRPC/protobuf imports. -->
<module name="RegexpSinglelineJava">
<property name="id" value="ForbidNonVendoredGrpcProtobuf"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -453,7 +453,7 @@ public Row apply(byte[] bytes) {
} catch (Exception e) {
throw new AvroRuntimeException(
"Could not decode avro record from given bytes "
+ new String(bytes, Charset.defaultCharset()),
+ new String(bytes, StandardCharsets.UTF_8),
e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import com.google.auto.value.AutoValue;
import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.apache.beam.sdk.schemas.Schema.Field;
import org.apache.beam.sdk.schemas.Schema.FieldType;
import org.apache.beam.sdk.schemas.annotations.DefaultSchema;
Expand All @@ -50,7 +50,7 @@
})
public class AutoValueSchemaTest {
static final DateTime DATE = DateTime.parse("1979-03-14");
static final byte[] BYTE_ARRAY = "bytearray".getBytes(Charset.defaultCharset());
static final byte[] BYTE_ARRAY = "bytearray".getBytes(StandardCharsets.UTF_8);
static final StringBuilder STRING_BUILDER = new StringBuilder("stringbuilder");

static final Schema SIMPLE_SCHEMA =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -77,7 +77,7 @@
})
public class JavaBeanSchemaTest {
static final DateTime DATE = DateTime.parse("1979-03-14");
static final byte[] BYTE_ARRAY = "bytearray".getBytes(Charset.defaultCharset());
static final byte[] BYTE_ARRAY = "bytearray".getBytes(StandardCharsets.UTF_8);

private SimpleBean createSimple(String name) {
return new SimpleBean(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -86,9 +86,9 @@
public class JavaFieldSchemaTest {
static final DateTime DATE = DateTime.parse("1979-03-14");
static final Instant INSTANT = DateTime.parse("1979-03-15").toInstant();
static final byte[] BYTE_ARRAY = "bytearray".getBytes(Charset.defaultCharset());
static final byte[] BYTE_ARRAY = "bytearray".getBytes(StandardCharsets.UTF_8);
static final ByteBuffer BYTE_BUFFER =
ByteBuffer.wrap("byteBuffer".getBytes(Charset.defaultCharset()));
ByteBuffer.wrap("byteBuffer".getBytes(StandardCharsets.UTF_8));

private SimplePOJO createSimple(String name) {
return new SimplePOJO(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;
import org.apache.beam.sdk.schemas.FieldValueGetter;
import org.apache.beam.sdk.schemas.FieldValueSetter;
Expand Down Expand Up @@ -131,8 +131,8 @@ public void testGeneratedSimpleGetters() {
simpleBean.setaBoolean(true);
simpleBean.setDateTime(DateTime.parse("1979-03-14"));
simpleBean.setInstant(DateTime.parse("1979-03-15").toInstant());
simpleBean.setBytes("bytes1".getBytes(Charset.defaultCharset()));
simpleBean.setByteBuffer(ByteBuffer.wrap("bytes2".getBytes(Charset.defaultCharset())));
simpleBean.setBytes("bytes1".getBytes(StandardCharsets.UTF_8));
simpleBean.setByteBuffer(ByteBuffer.wrap("bytes2".getBytes(StandardCharsets.UTF_8)));
simpleBean.setBigDecimal(new BigDecimal(42));
simpleBean.setStringBuilder(new StringBuilder("stringBuilder"));

Expand All @@ -155,11 +155,11 @@ public void testGeneratedSimpleGetters() {
assertEquals(DateTime.parse("1979-03-15").toInstant(), getters.get(7).get(simpleBean));
assertArrayEquals(
"Unexpected bytes",
"bytes1".getBytes(Charset.defaultCharset()),
"bytes1".getBytes(StandardCharsets.UTF_8),
(byte[]) getters.get(8).get(simpleBean));
assertArrayEquals(
"Unexpected bytes",
"bytes2".getBytes(Charset.defaultCharset()),
"bytes2".getBytes(StandardCharsets.UTF_8),
(byte[]) getters.get(9).get(simpleBean));
assertEquals(new BigDecimal(42), getters.get(10).get(simpleBean));
assertEquals("stringBuilder", getters.get(11).get(simpleBean).toString());
Expand All @@ -184,8 +184,8 @@ public void testGeneratedSimpleSetters() {
setters.get(5).set(simpleBean, true);
setters.get(6).set(simpleBean, DateTime.parse("1979-03-14").toInstant());
setters.get(7).set(simpleBean, DateTime.parse("1979-03-15").toInstant());
setters.get(8).set(simpleBean, "bytes1".getBytes(Charset.defaultCharset()));
setters.get(9).set(simpleBean, "bytes2".getBytes(Charset.defaultCharset()));
setters.get(8).set(simpleBean, "bytes1".getBytes(StandardCharsets.UTF_8));
setters.get(9).set(simpleBean, "bytes2".getBytes(StandardCharsets.UTF_8));
setters.get(10).set(simpleBean, new BigDecimal(42));
setters.get(11).set(simpleBean, "stringBuilder");

Expand All @@ -198,9 +198,9 @@ public void testGeneratedSimpleSetters() {
assertEquals(DateTime.parse("1979-03-14"), simpleBean.getDateTime());
assertEquals(DateTime.parse("1979-03-15").toInstant(), simpleBean.getInstant());
assertArrayEquals(
"Unexpected bytes", "bytes1".getBytes(Charset.defaultCharset()), simpleBean.getBytes());
"Unexpected bytes", "bytes1".getBytes(StandardCharsets.UTF_8), simpleBean.getBytes());
assertEquals(
ByteBuffer.wrap("bytes2".getBytes(Charset.defaultCharset())), simpleBean.getByteBuffer());
ByteBuffer.wrap("bytes2".getBytes(StandardCharsets.UTF_8)), simpleBean.getByteBuffer());
assertEquals(new BigDecimal(42), simpleBean.getBigDecimal());
assertEquals("stringBuilder", simpleBean.getStringBuilder().toString());
}
Expand Down Expand Up @@ -259,10 +259,10 @@ public void testGeneratedByteBufferSetters() {
BEAN_WITH_BYTE_ARRAY_SCHEMA,
new SetterTypeSupplier(),
new DefaultTypeConversionsFactory());
setters.get(0).set(bean, "field1".getBytes(Charset.defaultCharset()));
setters.get(1).set(bean, "field2".getBytes(Charset.defaultCharset()));
setters.get(0).set(bean, "field1".getBytes(StandardCharsets.UTF_8));
setters.get(1).set(bean, "field2".getBytes(StandardCharsets.UTF_8));

assertArrayEquals("not equal", "field1".getBytes(Charset.defaultCharset()), bean.getBytes1());
assertEquals(ByteBuffer.wrap("field2".getBytes(Charset.defaultCharset())), bean.getBytes2());
assertArrayEquals("not equal", "field1".getBytes(StandardCharsets.UTF_8), bean.getBytes1());
assertEquals(ByteBuffer.wrap("field2".getBytes(StandardCharsets.UTF_8)), bean.getBytes2());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;
import org.apache.beam.sdk.schemas.FieldValueGetter;
import org.apache.beam.sdk.schemas.FieldValueSetter;
Expand Down Expand Up @@ -62,9 +62,9 @@
public class POJOUtilsTest {
static final DateTime DATE = DateTime.parse("1979-03-14");
static final Instant INSTANT = DateTime.parse("1979-03-15").toInstant();
static final byte[] BYTE_ARRAY = "byteArray".getBytes(Charset.defaultCharset());
static final byte[] BYTE_ARRAY = "byteArray".getBytes(StandardCharsets.UTF_8);
static final ByteBuffer BYTE_BUFFER =
ByteBuffer.wrap("byteBuffer".getBytes(Charset.defaultCharset()));
ByteBuffer.wrap("byteBuffer".getBytes(StandardCharsets.UTF_8));

@Test
public void testNullables() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.io.CharStreams;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
Expand Down Expand Up @@ -50,7 +50,7 @@ static String fetchMetadata(String key) {
return "";
}
InputStream in = response.getEntity().getContent();
try (final Reader reader = new InputStreamReader(in, Charset.defaultCharset())) {
try (final Reader reader = new InputStreamReader(in, StandardCharsets.UTF_8)) {
return CharStreams.toString(reader);
}
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import javax.annotation.Nullable;
import org.apache.beam.sdk.coders.RowCoder;
import org.apache.beam.sdk.extensions.schemaio.expansion.ExternalSchemaIOTransformRegistrar.Configuration;
Expand All @@ -49,7 +49,7 @@ public class ExternalSchemaIOTransformRegistrarTest {
Row validConfigRow = Row.withSchema(validConfigSchema).addValue("value").build();

byte[] validSchemaBytes = SchemaTranslation.schemaToProto(validDataSchema, true).toByteArray();
byte[] invalidBytes = "Nice try".getBytes(Charset.defaultCharset());
byte[] invalidBytes = "Nice try".getBytes(StandardCharsets.UTF_8);

SchemaIO schemaIO = Mockito.mock(SchemaIO.class);
SchemaIOProvider schemaIOProvider = Mockito.mock(SchemaIOProvider.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.UUID;
import org.apache.beam.sdk.extensions.sorter.ExternalSorter.Options.SorterType;
Expand Down Expand Up @@ -48,8 +48,8 @@ private static void benchmark(Sorter sorter) throws IOException {
for (int i = 0; i < N; i++) {
sorter.add(
KV.of(
UUID.randomUUID().toString().getBytes(Charset.defaultCharset()),
UUID.randomUUID().toString().getBytes(Charset.defaultCharset())));
UUID.randomUUID().toString().getBytes(StandardCharsets.UTF_8),
UUID.randomUUID().toString().getBytes(StandardCharsets.UTF_8)));
}
int i = 0;
for (KV<byte[], byte[]> ignored : sorter.sort()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import com.google.datastore.v1.Key;
import com.google.datastore.v1.Value;
import com.google.protobuf.ByteString;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.UUID;
Expand Down Expand Up @@ -91,7 +91,7 @@ public class EntityToRowRowToEntityTest {
makeValue(Collections.singletonList(makeValue(NESTED_ENTITY).build())).build())
.putProperties("double", makeValue(Double.MAX_VALUE).build())
.putProperties(
"bytes", makeValue(ByteString.copyFrom("hello", Charset.defaultCharset())).build())
"bytes", makeValue(ByteString.copyFrom("hello", StandardCharsets.UTF_8)).build())
.putProperties("string", makeValue("string").build())
.putProperties("nullable", Value.newBuilder().build())
.build();
Expand All @@ -105,7 +105,7 @@ public class EntityToRowRowToEntityTest {
Arrays.asList("string1", "string2"),
Collections.singletonList(row(NESTED_ROW_SCHEMA, Long.MIN_VALUE)),
Double.MAX_VALUE,
"hello".getBytes(Charset.defaultCharset()),
"hello".getBytes(StandardCharsets.UTF_8),
"string",
null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.io.File;
import java.io.Serializable;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import org.apache.beam.sdk.io.common.HashingFn;
Expand Down Expand Up @@ -209,10 +209,10 @@ public static void beforeClass() throws Exception {
private static void replacePortsInConfFile() throws Exception {
URI uri = HadoopFormatIOCassandraTest.class.getResource("/cassandra.yaml").toURI();
Path cassandraYamlPath = new File(uri).toPath();
String content = new String(Files.readAllBytes(cassandraYamlPath), Charset.defaultCharset());
String content = new String(Files.readAllBytes(cassandraYamlPath), StandardCharsets.UTF_8);
content = content.replaceAll("9042", String.valueOf(cassandraNativePort));
content = content.replaceAll("9061", String.valueOf(cassandraPort));
Files.write(cassandraYamlPath, content.getBytes(Charset.defaultCharset()));
Files.write(cassandraYamlPath, content.getBytes(StandardCharsets.UTF_8));
}

@AfterClass
Expand Down
Loading