Skip to content

Commit

Permalink
Manage open streams in correct way
Browse files Browse the repository at this point in the history
All open streams must be closed, we can use try-with-resources
  • Loading branch information
slawekjaranowski committed Nov 16, 2024
1 parent 602b393 commit 922e838
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 66 deletions.
2 changes: 1 addition & 1 deletion src/it/MLICENSE-44/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</includes>
<licenseName>test</licenseName>
<organizationName>CodeHaus</organizationName>
<licenseResolver>file:${basedir}/src/license</licenseResolver>
<licenseResolver>${project.baseUri}/src/license</licenseResolver>
<descriptionTemplate>${basedir}/src/license/test/descriptionTemplate.ftl</descriptionTemplate>
<inceptionYear>2010</inceptionYear>
</configuration>
Expand Down
2 changes: 1 addition & 1 deletion src/it/add-third-party-missing-file/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
<goal>add-third-party</goal>
</goals>
<configuration>
<missingFileUrl>file:///${basedir}/missing-licenses.properties</missingFileUrl>
<missingFileUrl>${project.baseUri}/missing-licenses.properties</missingFileUrl>
<thirdPartyFilename>THIRD-PARTY-by-file.txt</thirdPartyFilename>
</configuration>
</execution>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.concurrent.ConcurrentHashMap;

import freemarker.template.Template;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
Expand Down Expand Up @@ -758,7 +759,7 @@ private boolean processFile(FileHeaderProcessor processor, File file, File proce
// this is a costy operation
// TODO-TC-20100411 We should process always from the read content not reading again from file

content = FileUtil.readAsString(file, getEncoding());
content = IOUtils.toString(file.toURI(), getEncoding());

} catch (IOException e) {
throw new IOException("Could not obtain content of file " + file);
Expand Down Expand Up @@ -859,7 +860,7 @@ private void finalizeFile(File file, File processFile) throws IOException {
} else {
try {
// replace file with the updated one
String updatedContent = FileUtil.readAsString(processFile, getEncoding());
String updatedContent = IOUtils.toString(processFile.toURI(), getEncoding());
FileUtil.printString(file, updatedContent, getEncoding());
Files.deleteIfExists(processFile.toPath());
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.TreeMap;

import freemarker.template.Template;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Mojo;
Expand Down Expand Up @@ -408,7 +409,7 @@ private boolean processFile(FileHeaderTransformer transformer, File file, File p
String content;

try {
content = FileUtil.readAsString(file, getEncoding());
content = IOUtils.toString(file.toURI(), getEncoding());
} catch (IOException e) {
throw new IOException("Could not obtain content of file " + file);
}
Expand Down
21 changes: 7 additions & 14 deletions src/main/java/org/codehaus/mojo/license/model/License.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@
* #L%
*/

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.InputStream;
import java.net.URL;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.codehaus.mojo.license.utils.MojoHelper;
import org.codehaus.plexus.util.IOUtil;

/**
* The model of a license.
Expand Down Expand Up @@ -113,23 +111,18 @@ public String getLicenseContent(String encoding) throws IOException {
throw new IllegalStateException("no baseURL defined, can not obtain license content in " + this);
}

Reader r = new BufferedReader(new InputStreamReader(getLicenseURL().openStream(), encoding));
try {
return IOUtil.toString(r);
} finally {
r.close();
try (InputStream in = getLicenseURL().openStream()) {
return IOUtils.toString(in, encoding);
}
}

public String getHeaderContent(String encoding) throws IOException {
if (baseURL == null) {
throw new IllegalStateException("no baseURL defined, can not obtain header content in " + this);
}
Reader r = new BufferedReader(new InputStreamReader(getHeaderURL().openStream(), encoding));
try {
return IOUtil.toString(r);
} finally {
r.close();

try (InputStream in = getHeaderURL().openStream()) {
return IOUtils.toString(in, encoding);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
Expand Down Expand Up @@ -106,7 +107,9 @@ public void load() throws IOException {
"no licenses.properties found with url [" + definitionURL + "] for resolver " + this);
}
Properties p = new Properties();
p.load(definitionURL.openStream());
try (InputStream in = definitionURL.openStream()) {
p.load(in);
}

for (Entry<Object, Object> entry : p.entrySet()) {
String licenseName = (String) entry.getKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/

import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
Expand Down Expand Up @@ -160,7 +161,7 @@ public void addRepository(String extraResolver) throws IOException {
URL baseURL = getClass().getClassLoader().getResource(extraResolver);
addRepository(baseURL);
} else {
URL baseURL = new URL(extraResolver);
URL baseURL = URI.create(extraResolver).toURL();
addRepository(baseURL);
}
}
Expand Down
35 changes: 6 additions & 29 deletions src/main/java/org/codehaus/mojo/license/utils/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringReader;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.MessageDigest;
Expand All @@ -44,8 +43,7 @@
import java.util.Locale;

import org.apache.commons.codec.binary.Hex;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.IOUtil;
import org.apache.commons.io.FileUtils;

/**
* Some basic file io utilities
Expand All @@ -66,12 +64,12 @@ public class FileUtil {
public static void renameFile(File file, File destination) throws IOException {
try {
try {
org.apache.commons.io.FileUtils.forceDelete(destination);
FileUtils.forceDelete(destination);
} catch (FileNotFoundException ex) {
// Just do nothing
}

org.apache.commons.io.FileUtils.moveFile(file, destination);
FileUtils.moveFile(file, destination);
} catch (IOException ex) {
throw new IOException(String.format("could not rename '%s' to '%s'", file, destination));
}
Expand Down Expand Up @@ -109,22 +107,6 @@ public static void backupFile(File f) throws IOException {
copyFile(f, dst);
}

/**
* Permet de lire un fichier et de retourner sont contenu sous forme d'une
* chaine de carateres.
*
* @param file le fichier a lire
* @param encoding encoding to read file
* @return the content of the file
* @throws IOException if IO pb
*/
public static String readAsString(File file, String encoding) throws IOException {

try (BufferedReader in = Files.newBufferedReader(file.toPath(), Charset.forName(encoding))) {
return IOUtil.toString(in);
}
}

/**
* Print content to file. This method ensures that a platform specific line ending is used.
*
Expand All @@ -136,18 +118,13 @@ public static String readAsString(File file, String encoding) throws IOException
public static void printString(File file, String content, String encoding) throws IOException {
Files.createDirectories(file.getParentFile().toPath());

BufferedReader in;
PrintWriter out;
in = new BufferedReader(new StringReader(content));
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), encoding)));
try {
try (BufferedReader in = new BufferedReader(new StringReader(content));
PrintWriter out = new PrintWriter(
new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), encoding)))) {
String line;
while ((line = in.readLine()) != null) {
out.println(line);
}
} finally {
in.close();
out.close();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.text.MessageFormat;
import java.util.Arrays;
Expand Down Expand Up @@ -164,7 +165,7 @@ public static String convert(long value, double[] factors, String[] unites) {
public static URL getUrl(URL baseUrl, String suffix) {
String url = baseUrl.toString() + "/" + suffix;
try {
return new URL(url);
return URI.create(url).toURL();
} catch (MalformedURLException ex) {
throw new IllegalArgumentException("could not obtain url " + url, ex);
}
Expand Down
24 changes: 9 additions & 15 deletions src/main/java/org/codehaus/mojo/license/utils/UrlRequester.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
import java.io.BufferedReader;
import java.io.CharArrayReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
Expand Down Expand Up @@ -64,9 +66,9 @@ public static boolean isStringUrl(String data) {
return true;
}
try {
new URL(data);
URI.create(data).toURL();
return true;
} catch (MalformedURLException e) {
} catch (IllegalArgumentException | MalformedURLException e) {
return false;
}
}
Expand Down Expand Up @@ -153,7 +155,9 @@ public static String getFromUrl(String url, String encoding) throws IOException
}
}
} else {
result = IOUtils.toString(new URL(url).openStream(), charset);
try (InputStream in = URI.create(url).toURL().openStream()) {
result = IOUtils.toString(in, charset);
}
}
return result;
}
Expand All @@ -172,10 +176,8 @@ public static String getFromUrl(String url, String encoding) throws IOException
*/
public static List<String> downloadList(String url) throws MojoExecutionException {
List<String> list = new ArrayList<>();
BufferedReader bufferedReader = null;
try {
bufferedReader =
new BufferedReader(new CharArrayReader(getFromUrl(url).toCharArray()));
try (BufferedReader bufferedReader =
new BufferedReader(new CharArrayReader(getFromUrl(url).toCharArray()))) {
String line;
while ((line = bufferedReader.readLine()) != null) {
if (StringUtils.isNotBlank(line) && !StringUtils.startsWith(line, "#") && !list.contains(line)) {
Expand All @@ -184,14 +186,6 @@ public static List<String> downloadList(String url) throws MojoExecutionExceptio
}
} catch (IOException e) {
throw new MojoExecutionException("Could not open connection to URL: " + url, e);
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
throw new MojoExecutionException(e.getMessage(), e);
}
}
}
return list;
}
Expand Down

0 comments on commit 922e838

Please sign in to comment.