Skip to content

Commit

Permalink
[JENKINS-43845] - Use Java 7 Files API and make FindBugs happy
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-nenashev committed Apr 26, 2017
1 parent 4c063a5 commit 5719305
Showing 1 changed file with 19 additions and 37 deletions.
@@ -1,6 +1,9 @@
package org.jenkinsci.plugins.envinjectapi.util;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
Expand All @@ -25,54 +28,33 @@ private EnvInjectVarsIO() {

@CheckForNull
public static Map<String, String> getEnvironment(@Nonnull File envInjectBaseDir) throws EnvInjectException {
FileReader fileReader = null;
try {
File f = new File(envInjectBaseDir, ENVINJECT_TXT_FILENAME);
if (!f.exists()) {
return null;
}
fileReader = new FileReader(f);
File f = new File(envInjectBaseDir, ENVINJECT_TXT_FILENAME);
if (!f.exists()) {
return null;
}

try (Reader fileReader = Files.newBufferedReader(f.toPath(), Charset.defaultCharset())) {
Map<String, String> result = new HashMap<>();
fromTxt(fileReader, result);
return result;
} catch (FileNotFoundException fne) {
} catch (IOException | InvalidPathException fne) {
throw new EnvInjectException(fne);
} finally {
if (fileReader != null) {
try {
fileReader.close();
} catch (IOException ioe) {
throw new EnvInjectException(ioe);
}
}
}
}

public static void saveEnvironment(@Nonnull File rootDir, @Nonnull Map<String, String> envMap) throws EnvInjectException {
FileWriter fileWriter = null;
try {
File f = new File(rootDir, ENVINJECT_TXT_FILENAME);
fileWriter = new FileWriter(f);
public static void saveEnvironment(@Nonnull File rootDir, @Nonnull Map<String, String> envMap) throws EnvInjectException {
File f = new File(rootDir, ENVINJECT_TXT_FILENAME);
try (Writer wr = Files.newBufferedWriter(f.toPath(), Charset.defaultCharset())) {
Map<String, String> map2Write = new TreeMap<>();
map2Write.putAll(envMap);
toTxt(map2Write, fileWriter);
} catch (FileNotFoundException fne) {
throw new EnvInjectException(fne);
} catch (IOException ioe) {
throw new EnvInjectException(ioe);
} finally {
if (fileWriter != null) {
try {
fileWriter.close();
} catch (IOException ioe) {
throw new EnvInjectException(ioe);
}
}
toTxt(map2Write, wr);
} catch (IOException | InvalidPathException ex) {
throw new EnvInjectException(ex);
}
}

private static void fromTxt(@Nonnull FileReader fileReader, @Nonnull Map<String, String> result) throws EnvInjectException {
BufferedReader bufferedReader = new BufferedReader(fileReader);
private static void fromTxt(@Nonnull Reader reader, @Nonnull Map<String, String> result) throws EnvInjectException {
BufferedReader bufferedReader = new BufferedReader(reader);
String line;
try {
while ((line = bufferedReader.readLine()) != null) {
Expand All @@ -93,7 +75,7 @@ private static void fromTxt(@Nonnull FileReader fileReader, @Nonnull Map<String,
}
}

private static void toTxt(@Nonnull Map<String, String> envMap, @Nonnull FileWriter fw) throws IOException {
private static void toTxt(@Nonnull Map<String, String> envMap, @Nonnull Writer fw) throws IOException {
for (Map.Entry<String, String> entry : envMap.entrySet()) {
fw.write(String.format("%s%s%s%n", entry.getKey(), TOKEN, entry.getValue()));
}
Expand Down

0 comments on commit 5719305

Please sign in to comment.