|
21 | 21 |
|
22 | 22 | import java.io.*;
|
23 | 23 | import java.nio.charset.StandardCharsets;
|
| 24 | +import java.nio.file.Files; |
24 | 25 | import java.util.HashMap;
|
25 | 26 | import java.util.Map;
|
26 | 27 | import java.util.Properties;
|
@@ -67,7 +68,7 @@ public static Properties mergePropertiesWithSystemAndPropertyFile(Properties exi
|
67 | 68 | InputStream inputStream = null;
|
68 | 69 | try {
|
69 | 70 | if (propertiesFile.exists()) {
|
70 |
| - inputStream = new FileInputStream(propertiesFile); |
| 71 | + inputStream = Files.newInputStream(propertiesFile.toPath()); |
71 | 72 | props.load(inputStream);
|
72 | 73 | }
|
73 | 74 | } catch (IOException e) {
|
@@ -147,7 +148,7 @@ public static Properties getEnvProperties(Properties startProps, Log log) {
|
147 | 148 | File propertiesFile = new File(propsFilePath);
|
148 | 149 | InputStream inputStream = null;
|
149 | 150 | try {
|
150 |
| - inputStream = new FileInputStream(propertiesFile); |
| 151 | + inputStream = Files.newInputStream(propertiesFile.toPath()); |
151 | 152 | Properties propertiesFromFile = new Properties();
|
152 | 153 | propertiesFromFile.load(inputStream);
|
153 | 154 | props.putAll(filterDynamicProperties(propertiesFromFile, ENV_PREDICATE));
|
@@ -178,37 +179,38 @@ private static JsonFactory createJsonFactory() {
|
178 | 179 | public static String buildInfoToJsonString(BuildInfo buildInfo) throws IOException {
|
179 | 180 | JsonFactory jsonFactory = createJsonFactory();
|
180 | 181 |
|
181 |
| - StringWriter writer = new StringWriter(); |
182 |
| - JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(writer); |
183 |
| - jsonGenerator.useDefaultPrettyPrinter(); |
184 |
| - |
185 |
| - jsonGenerator.writeObject(buildInfo); |
186 |
| - String result = writer.getBuffer().toString(); |
187 |
| - return result; |
| 182 | + try (StringWriter writer = new StringWriter(); |
| 183 | + JsonGenerator jsonGenerator = jsonFactory.createGenerator(writer)) { |
| 184 | + jsonGenerator.useDefaultPrettyPrinter(); |
| 185 | + jsonGenerator.writeObject(buildInfo); |
| 186 | + return writer.getBuffer().toString(); |
| 187 | + } |
188 | 188 | }
|
189 | 189 |
|
190 | 190 | public static BuildInfo jsonStringToBuildInfo(String json) throws IOException {
|
191 | 191 | JsonFactory jsonFactory = createJsonFactory();
|
192 |
| - JsonParser parser = jsonFactory.createParser(new StringReader(json)); |
193 |
| - return jsonFactory.getCodec().readValue(parser, BuildInfo.class); |
| 192 | + try (JsonParser parser = jsonFactory.createParser(new StringReader(json))) { |
| 193 | + return jsonFactory.getCodec().readValue(parser, BuildInfo.class); |
| 194 | + } |
194 | 195 | }
|
195 | 196 |
|
196 | 197 | public static <T extends Serializable> String buildInfoToJsonString(T buildComponent) throws IOException {
|
197 | 198 | JsonFactory jsonFactory = createJsonFactory();
|
198 | 199 |
|
199 |
| - StringWriter writer = new StringWriter(); |
200 |
| - JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(writer); |
201 |
| - jsonGenerator.useDefaultPrettyPrinter(); |
| 200 | + try (StringWriter writer = new StringWriter(); |
| 201 | + JsonGenerator jsonGenerator = jsonFactory.createGenerator(writer)) { |
| 202 | + jsonGenerator.useDefaultPrettyPrinter(); |
| 203 | + jsonGenerator.writeObject(buildComponent); |
202 | 204 |
|
203 |
| - jsonGenerator.writeObject(buildComponent); |
204 |
| - String result = writer.getBuffer().toString(); |
205 |
| - return result; |
| 205 | + return writer.getBuffer().toString(); |
| 206 | + } |
206 | 207 | }
|
207 | 208 |
|
208 | 209 | public static <T extends Serializable> T jsonStringToGeneric(String json, Class<T> clazz) throws IOException {
|
209 | 210 | JsonFactory jsonFactory = createJsonFactory();
|
210 |
| - JsonParser parser = jsonFactory.createParser(new StringReader(json)); |
211 |
| - return jsonFactory.getCodec().readValue(parser, clazz); |
| 211 | + try (JsonParser parser = jsonFactory.createParser(new StringReader(json))) { |
| 212 | + return jsonFactory.getCodec().readValue(parser, clazz); |
| 213 | + } |
212 | 214 | }
|
213 | 215 |
|
214 | 216 | public static void saveBuildInfoToFile(BuildInfo buildInfo, File toFile) throws IOException {
|
|
0 commit comments