Skip to content

Commit

Permalink
Switch to modern I/O (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
elharo authored Dec 18, 2024
1 parent 9022926 commit bf4a3d2
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Locale;

import org.apache.maven.artifact.Artifact;
Expand All @@ -54,7 +55,6 @@
import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolver;
import org.codehaus.plexus.i18n.I18N;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.ReaderFactory;

/**
* Generates the Project Dependencies report.
Expand Down Expand Up @@ -223,45 +223,27 @@ private DependencyNode resolveProject() {
* @throws IOException if any
*/
private void copyResources(File outputDirectory) throws IOException {
InputStream resourceList = null;
InputStream in = null;
BufferedReader reader = null;
OutputStream out = null;
try {
resourceList = getClass().getClassLoader().getResourceAsStream(RESOURCES_DIR + "/resources.txt");

if (resourceList != null) {
reader = new LineNumberReader(new InputStreamReader(resourceList, ReaderFactory.US_ASCII));

InputStream resourceList = getClass().getClassLoader().getResourceAsStream(RESOURCES_DIR + "/resources.txt");
if (resourceList != null) {
try (BufferedReader reader =
new LineNumberReader(new InputStreamReader(resourceList, StandardCharsets.US_ASCII))) {
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
in = getClass().getClassLoader().getResourceAsStream(RESOURCES_DIR + "/" + line);

if (in == null) {
throw new IOException("The resource " + line + " doesn't exist.");
try (InputStream in = getClass().getClassLoader().getResourceAsStream(RESOURCES_DIR + "/" + line)) {
if (in == null) {
throw new IOException("The resource " + line + " doesn't exist.");
}

File outputFile = new File(outputDirectory, line);
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
}

try (OutputStream out = new FileOutputStream(outputFile)) {
IOUtil.copy(in, out);
}
}

File outputFile = new File(outputDirectory, line);

if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
}

out = new FileOutputStream(outputFile);
IOUtil.copy(in, out);
out.close();
out = null;
in.close();
in = null;
}

reader.close();
reader = null;
}
} finally {
IOUtil.close(out);
IOUtil.close(reader);
IOUtil.close(in);
IOUtil.close(resourceList);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,56 +131,54 @@ protected String getI18Nsection() {

@Override
protected void executeReport(Locale locale) throws MavenReportException {
Sink sink = getSink();
try (Sink sink = getSink()) {
sink.head();
sink.title();

sink.head();
sink.title();
if (isReactorBuild()) {
sink.text(getI18nString(locale, "reactor.title"));
} else {
sink.text(getI18nString(locale, "title"));
}

if (isReactorBuild()) {
sink.text(getI18nString(locale, "reactor.title"));
} else {
sink.text(getI18nString(locale, "title"));
}
sink.title_();
sink.head_();

sink.title_();
sink.head_();
sink.body();

sink.body();
sink.section1();

sink.section1();
sink.sectionTitle1();

sink.sectionTitle1();
if (isReactorBuild()) {
sink.text(getI18nString(locale, "reactor.title"));
} else {
sink.text(getI18nString(locale, "title"));
}

if (isReactorBuild()) {
sink.text(getI18nString(locale, "reactor.title"));
} else {
sink.text(getI18nString(locale, "title"));
}
sink.sectionTitle1_();

sink.sectionTitle1_();
DependencyAnalyzeResult dependencyResult = analyzeDependencyTree();
int convergence = calculateConvergence(dependencyResult);

DependencyAnalyzeResult dependencyResult = analyzeDependencyTree();
int convergence = calculateConvergence(dependencyResult);
if (convergence < FULL_CONVERGENCE) {
// legend
generateLegend(locale, sink);
sink.lineBreak();
}

if (convergence < FULL_CONVERGENCE) {
// legend
generateLegend(locale, sink);
sink.lineBreak();
}
// stats
generateStats(locale, sink, dependencyResult);

// stats
generateStats(locale, sink, dependencyResult);
sink.section1_();

sink.section1_();
if (convergence < FULL_CONVERGENCE) {
// convergence
generateConvergence(locale, sink, dependencyResult);
}

if (convergence < FULL_CONVERGENCE) {
// convergence
generateConvergence(locale, sink, dependencyResult);
sink.body_();
}

sink.body_();
sink.flush();
sink.close();
}

// ----------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@ public class ProjectInfoReportUtils {
/** The timeout when getting the url input stream */
private static final int TIMEOUT = 1000 * 5;

/** The default encoding used to transform bytes to characters */
private static final String DEFAULT_ENCODING = "UTF-8";

/**
* Get the input stream using UTF-8 as character encoding from a URL.
*
Expand All @@ -83,7 +80,7 @@ public class ProjectInfoReportUtils {
* @see #getContent(URL, Settings, String)
*/
public static String getContent(URL url, Settings settings) throws IOException {
return getContent(url, settings, DEFAULT_ENCODING);
return getContent(url, settings, "UTF-8");
}

/**
Expand Down Expand Up @@ -115,23 +112,13 @@ public static String getContent(URL url, MavenProject project, Settings settings
String scheme = url.getProtocol();

if (encoding == null || encoding.isEmpty()) {
encoding = DEFAULT_ENCODING;
encoding = "UTF-8";
}

if ("file".equals(scheme)) {
InputStream in = null;
try {
URLConnection conn = url.openConnection();
in = conn.getInputStream();

try (InputStream in = url.openConnection().getInputStream()) {
final String content = IOUtil.toString(in, encoding);

in.close();
in = null;

return content;
} finally {
IOUtil.close(in);
}
}

Expand Down Expand Up @@ -167,19 +154,8 @@ protected PasswordAuthentication getPasswordAuthentication() {
}
}

InputStream in = null;
try {
URLConnection conn = getURLConnection(url, project, settings);
in = conn.getInputStream();

final String string = IOUtil.toString(in, encoding);

in.close();
in = null;

return string;
} finally {
IOUtil.close(in);
try (InputStream in = getURLConnection(url, project, settings).getInputStream()) {
return IOUtil.toString(in, encoding);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package org.apache.maven.report.projectinfo.stubs;

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -40,9 +42,8 @@
import org.apache.maven.model.PluginManagement;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
import org.apache.maven.shared.utils.io.IOUtil;
import org.codehaus.plexus.util.ReaderFactory;
import org.codehaus.plexus.util.xml.XmlStreamReader;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.eclipse.aether.repository.RemoteRepository;

/**
Expand All @@ -61,17 +62,11 @@ public abstract class ProjectInfoProjectStub extends MavenProjectStub {
*/
public ProjectInfoProjectStub() {
MavenXpp3Reader pomReader = new MavenXpp3Reader();
XmlStreamReader reader = null;
try {
reader = ReaderFactory.newXmlReader(new File(getBasedir(), getPOM()));
try (Reader reader = ReaderFactory.newXmlReader(new File(getBasedir(), getPOM()))) {
model = pomReader.read(reader);
reader.close();
reader = null;
setModel(model);
} catch (Exception e) {
} catch (IOException | XmlPullParserException e) {
throw new RuntimeException(e);
} finally {
IOUtil.close(reader);
}

setGroupId(model.getGroupId());
Expand Down

0 comments on commit bf4a3d2

Please sign in to comment.