Skip to content

Commit

Permalink
Portable String <--> bytes conversion (#841)
Browse files Browse the repository at this point in the history
  • Loading branch information
chanseokoh authored Sep 14, 2021
1 parent 92f7c1c commit 556e7dd
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.common.base.Strings;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
Expand Down Expand Up @@ -69,7 +70,9 @@ static Optional<List<GapicBatchingSettings>> parse(String gapicYamlConfigFilePat
String fileContents = null;

try {
fileContents = new String(Files.readAllBytes(Paths.get(gapicYamlConfigFilePath)));
fileContents =
new String(
Files.readAllBytes(Paths.get(gapicYamlConfigFilePath)), StandardCharsets.UTF_8);
} catch (IOException e) {
return Optional.empty();
}
Expand Down Expand Up @@ -104,7 +107,7 @@ private static Optional<List<GapicBatchingSettings>> parseFromMap(Map<String, Ob
batchingOuterYamlConfig.containsKey(YAML_KEY_DESCRIPTOR),
String.format(
"%s key expected but not found for method %s",
YAML_KEY_DESCRIPTOR, (String) methodYamlConfig.get(YAML_KEY_NAME)));
YAML_KEY_DESCRIPTOR, methodYamlConfig.get(YAML_KEY_NAME)));

// Parse the threshold values first.
Map<String, Object> batchingYamlConfig =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.common.base.Strings;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;
Expand Down Expand Up @@ -48,7 +49,9 @@ static Optional<GapicLanguageSettings> parse(String gapicYamlConfigFilePath) {
String fileContents = null;

try {
fileContents = new String(Files.readAllBytes(Paths.get(gapicYamlConfigFilePath)));
fileContents =
new String(
Files.readAllBytes(Paths.get(gapicYamlConfigFilePath)), StandardCharsets.UTF_8);
} catch (IOException e) {
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.common.base.Strings;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
Expand Down Expand Up @@ -56,7 +57,9 @@ static Optional<List<GapicLroRetrySettings>> parse(String gapicYamlConfigFilePat
String fileContents = null;

try {
fileContents = new String(Files.readAllBytes(Paths.get(gapicYamlConfigFilePath)));
fileContents =
new String(
Files.readAllBytes(Paths.get(gapicYamlConfigFilePath)), StandardCharsets.UTF_8);
} catch (IOException e) {
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.protobuf.util.JsonFormat;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.LinkedHashMap;
Expand All @@ -37,7 +38,8 @@ public static Optional<com.google.api.Service> parse(String serviceYamlFilePath)

String fileContents = null;
try {
fileContents = new String(Files.readAllBytes(Paths.get(serviceYamlFilePath)));
fileContents =
new String(Files.readAllBytes(Paths.get(serviceYamlFilePath)), StandardCharsets.UTF_8);
} catch (IOException e) {
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.protobuf.compiler.PluginProtos.CodeGeneratorResponse;
import com.google.protobuf.util.JsonFormat;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
Expand Down Expand Up @@ -61,7 +62,7 @@ public static CodeGeneratorResponse write(
JarEntry jarEntry = new JarEntry(String.format("%s/%s.java", path, className));
try {
jos.putNextEntry(jarEntry);
jos.write(code.getBytes());
jos.write(code.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
throw new GapicWriterException(
String.format(
Expand All @@ -80,7 +81,7 @@ public static CodeGeneratorResponse write(
JarEntry jarEntry = new JarEntry(String.format("%s/package-info.java", path));
try {
jos.putNextEntry(jarEntry);
jos.write(code.getBytes());
jos.write(code.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
throw new GapicWriterException("Could not write code for package-info.java");
}
Expand All @@ -90,7 +91,8 @@ public static CodeGeneratorResponse write(
jarEntry = new JarEntry(String.format("%s/gapic_metadata.json", path));
try {
jos.putNextEntry(jarEntry);
jos.write(JsonFormat.printer().print(context.gapicMetadata()).getBytes());
jos.write(
JsonFormat.printer().print(context.gapicMetadata()).getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
throw new GapicWriterException("Could not write gapic_metadata.json");
}
Expand Down

0 comments on commit 556e7dd

Please sign in to comment.