Skip to content

Commit

Permalink
Code Improvements
Browse files Browse the repository at this point in the history
 * using Files.newBufferedWriter(), Files.lines()
 * using reader.lines().forEach()
 * Removed inverse logic.
 * Using StandardCharsets.UTF_8 instead of literal
   texts.
  • Loading branch information
khmarbaise committed Aug 15, 2023
1 parent 0062083 commit 213d2a4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,12 @@
*/
package org.apache.maven.plugins.dependency.fromDependencies;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
Expand All @@ -35,6 +32,8 @@
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.lang3.StringUtils;
import org.apache.maven.artifact.Artifact;
Expand Down Expand Up @@ -307,9 +306,8 @@ private void storeClasspathFile(String cpString, File out) throws MojoExecutionE
// make sure the parent path exists.
out.getParentFile().mkdirs();

String encoding = Objects.toString(outputEncoding, "UTF-8");

try (Writer w = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(out), encoding))) {
String encoding = Objects.toString(outputEncoding, StandardCharsets.UTF_8.name());
try (Writer w = Files.newBufferedWriter(out.toPath(), Charset.forName(encoding))) {
w.write(cpString);
getLog().info("Wrote classpath file '" + out + "'.");
} catch (IOException ex) {
Expand All @@ -333,15 +331,11 @@ protected String readClasspathFile() throws IOException {
if (!outputFile.isFile()) {
return null;
}
StringBuilder sb = new StringBuilder();
String encoding = Objects.toString(outputEncoding, "UTF-8");

try (BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(outputFile), encoding))) {
for (String line = r.readLine(); line != null; line = r.readLine()) {
sb.append(line);
}
String encoding = Objects.toString(outputEncoding, StandardCharsets.UTF_8.name());

return sb.toString();
try (Stream<String> lines = Files.lines(outputFile.toPath(), Charset.forName(encoding))) {
return lines.collect(Collectors.joining());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -106,15 +106,13 @@ public static String getFormattedFileName(
destFileName.append(artifact.getGroupId()).append(".");
}

String versionString;
String versionString = "";
if (!removeVersion) {
if (useBaseVersion) {
versionString = "-" + ArtifactUtils.toSnapshotVersion(artifact.getVersion());
} else {
versionString = "-" + artifact.getVersion();
}
} else {
versionString = "";
}

String classifierString = "";
Expand Down Expand Up @@ -227,9 +225,9 @@ public static synchronized void write(String string, File file, boolean append,
*/
public static synchronized void write(String string, File file, boolean append, String encoding)
throws IOException {
file.getParentFile().mkdirs();
Files.createDirectories(file.getParentFile().toPath());

try (Writer writer = new OutputStreamWriter(new FileOutputStream(file, append), encoding)) {
try (Writer writer = Files.newBufferedWriter(file.toPath(), Charset.forName(encoding))) {
writer.write(string);
}
}
Expand All @@ -242,15 +240,9 @@ public static synchronized void write(String string, File file, boolean append,
* @throws IOException if an I/O error occurs
*/
public static synchronized void log(String string, Log log) throws IOException {
BufferedReader reader = new BufferedReader(new StringReader(string));

String line;

while ((line = reader.readLine()) != null) {
log.info(line);
try (BufferedReader reader = new BufferedReader(new StringReader(string))) {
reader.lines().forEach(log::info);
}

reader.close();
}

/**
Expand All @@ -273,7 +265,7 @@ public static String cleanToBeTokenizedString(String str) {
String ret = "";
if (!(str == null || str.isEmpty())) {
// remove initial and ending spaces, plus all spaces next to commas
ret = str.trim().replaceAll("[\\s]*,[\\s]*", ",");
ret = str.trim().replaceAll("\\s*,\\s*", ",");
}

return ret;
Expand Down

0 comments on commit 213d2a4

Please sign in to comment.