Skip to content

Commit

Permalink
Fixed sonarcloud warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
depryf committed Jul 24, 2023
1 parent c3b4ba4 commit b033efb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 39 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/imsweb/seerutils/SeerMath.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public final class SeerMath {

/**
* No instanciation for this class!
* No instantiation for this class!
* <p/>
* Created on Feb 9, 2011 by depryf
*/
Expand Down Expand Up @@ -112,6 +112,7 @@ public static RegressionResult calculateRegressionResult(List<? extends Number>
* Created on Aug 21, 2011 by Fabian
* @author Fabian
*/
@SuppressWarnings("unused")
public static class RegressionResult {

/**
Expand Down
67 changes: 37 additions & 30 deletions src/main/java/com/imsweb/seerutils/SeerUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.io.Writer;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -35,14 +36,14 @@
/**
* This class provides shared functionality that can be used by all the other modules in SEER*Utils.
*/
@SuppressWarnings("unused")
public final class SeerUtils {

// cached pattern for the versions
private static final Pattern _VERSION_CLEANUP_PATTERN = Pattern.compile("^v|-(snapshot|beta)$");
private static final Pattern _VERSIONS_PATTERN = Pattern.compile("^\\d+(\\.\\d+){0,3}$");

/**
* Private constructor, no instanciation.
* Private constructor, no instantiation.
* <p/>
* Created on Feb 7, 2011 by Fabian
*/
Expand All @@ -63,8 +64,14 @@ public static int compareSeerVersions(String version1, String version2) {
if (version2 == null)
return 1;

String v1 = _VERSION_CLEANUP_PATTERN.matcher(version1.toLowerCase()).replaceAll("");
String v2 = _VERSION_CLEANUP_PATTERN.matcher(version2.toLowerCase()).replaceAll("");
String v1 = version1.toLowerCase();
if (v1.startsWith("v"))
v1 = v1.substring(1);
v1 = v1.replace("-snapshot", "").replace("-beta", "");
String v2 = version2.toLowerCase();
if (v2.startsWith("v"))
v2 = v2.substring(1);
v2 = v2.replace("-snapshot", "").replace("-beta", "");

if (!_VERSIONS_PATTERN.matcher(v1).matches())
throw new IllegalArgumentException("Invalid version format: " + v1);
Expand Down Expand Up @@ -101,7 +108,7 @@ public static int compareSeerVersions(String version1, String version2) {
/**
* Returns true if the provided string contains only printable ASCII characters, false otherwise.
* <p/>
* See http://www.asciitable.com/
* See <a href="http://www.asciitable.com/">http://www.asciitable.com/</a>
* <p/>
* Considering byte values from -127 to 128, only the range 32-126 is considered printable ASCII with the following exceptions:
* <ul>
Expand All @@ -123,7 +130,7 @@ public static boolean isPureAscii(String s) {
/**
* Returns true if the provided array of bytes contains only printable ASCII characters, false otherwise.
* <p/>
* See http://www.asciitable.com/
* See <a href="http://www.asciitable.com/">http://www.asciitable.com/</a>
* <p/>
* Considering byte values from -127 to 128, only the range 32-126 is considered printable ASCII with the following exceptions:
* <ul>
Expand All @@ -145,7 +152,7 @@ public static boolean isPureAscii(byte[] bytes) {
/**
* Returns true if the provided array of bytes contains only printable pure-ASCII characters, false otherwise.
* <p/>
* see <url>http://www.asciitable.com/<url>
* see <url><a href="http://www.asciitable.com/">http://www.asciitable.com/</a><url>
* <p/>
* Considering byte values from -127 to 128, only the range 32-126 is considered printable ASCII with the following exceptions:
* <ul>
Expand All @@ -160,7 +167,7 @@ public static boolean isPureAscii(byte[] bytes) {
* @return boolean true if the provided array of bytes contains only printable ASCII characters, false otherwise
*/
public static boolean isPureAscii(byte[] bytes, byte[] exceptions) {
if (bytes == null || bytes.length == 0)
if (bytes == null)
return true;

for (byte b : bytes)
Expand All @@ -172,7 +179,7 @@ public static boolean isPureAscii(byte[] bytes, byte[] exceptions) {
}

/**
* Copies the content of the given input stream to the the given output stream
* Copies the content of the given input stream to the given output stream
* <p/>
* Both the input stream and the output stream will be closed when this method returns.
* <p/>
Expand All @@ -193,7 +200,7 @@ public static void copyInputStreamToOutputStream(InputStream input, OutputStream
* Created on May 27, 2004 by Fabian Depry
* @param input where to take the data from
* @param output where to send the data to
* @param closeOutput whether or not the output stream should be closed
* @param closeOutput whether the output stream should be closed
* @throws IOException if data cannot be copied from input to output
*/
public static void copyInputStreamToOutputStream(InputStream input, OutputStream output, boolean closeOutput) throws IOException {
Expand All @@ -215,7 +222,7 @@ public static void copyInputStreamToOutputStream(InputStream input, OutputStream
}

/**
* Copies the content of the given reader to the the given writer.
* Copies the content of the given reader to the given writer.
* <p/>
* Both readers will be closed when this method returns.
* <p/>
Expand All @@ -229,14 +236,14 @@ public static void copyReaderToWriter(Reader reader, Writer writer) throws IOExc
}

/**
* Copies the content of the given reader to the the given writer making no assumption on the encoding.
* Copies the content of the given reader to the given writer making no assumption on the encoding.
* <p/>
* The input reader will be closed when this method returns; the output writer will be closed only if closeOutput is set to true
* <p/>
* Created on May 27, 2004 by Fabian Depry
* @param input where to take the data from
* @param output where to send the data to
* @param closeOutput whether or not the output writer should be closed
* @param closeOutput whether the output writer should be closed
* @throws IOException if data cannot be copied from input to output
*/
public static void copyReaderToWriter(Reader input, Writer output, boolean closeOutput) throws IOException {
Expand Down Expand Up @@ -387,7 +394,7 @@ public static InputStream createInputStream(File file, String zipEntryToUse) thr

InputStream is;
if (name.endsWith(".gz") || name.endsWith(".gzip"))
is = new GZIPInputStream(new FileInputStream(file));
is = new GZIPInputStream(Files.newInputStream(file.toPath()));
else if (name.endsWith(".zip")) {
ZipFile zipFile = new ZipFile(file);
Enumeration<? extends ZipEntry> entries = zipFile.entries();
Expand Down Expand Up @@ -417,7 +424,7 @@ else if (zipEntryToUse.endsWith(".zip"))
is = tmp;
}
else
is = new FileInputStream(file);
is = Files.newInputStream(file.toPath());

return is;
}
Expand All @@ -440,11 +447,11 @@ public static OutputStream createOutputStream(File file) throws IOException {
String name = file.getName().toLowerCase();

if (name.endsWith(".gz") || name.endsWith(".gzip"))
os = new GZIPOutputStream(new FileOutputStream(file));
os = new GZIPOutputStream(Files.newOutputStream(file.toPath()));
else if (name.endsWith(".zip"))
os = new ZipOutputStream(new FileOutputStream(file));
os = new ZipOutputStream(Files.newOutputStream(file.toPath()));
else
os = new FileOutputStream(file);
os = Files.newOutputStream(file.toPath());

return os;
}
Expand Down Expand Up @@ -615,8 +622,11 @@ public static void copyDirectory(File from, File to) throws IOException {
throw new IOException("Unable to read from source directory");

for (File f : files) {
if (f.isFile())
copyInputStreamToOutputStream(new FileInputStream(f), new FileOutputStream(new File(to, f.getName())));
if (f.isFile()) {
try (InputStream is = Files.newInputStream(f.toPath())) {
copyInputStreamToOutputStream(is, Files.newOutputStream(new File(to, f.getName()).toPath()));
}
}
else
copyDirectory(f, new File(to, f.getName()));
}
Expand Down Expand Up @@ -649,17 +659,14 @@ private static void deleteDirectory(File dir, boolean deleteRoot) throws IOExcep
throw new IOException("Unable to read from source directory");

for (File f : files) {
if (f.isFile()) {
if (!f.delete())
throw new IOException("Unable to delete '" + f.getPath() + "'");
}
if (f.isFile())
Files.delete(f.toPath());
else
deleteDirectory(f, true);
}

if (deleteRoot)
if (!dir.delete())
throw new IOException("Unable to delete '" + dir.getPath() + "'");
Files.delete(dir.toPath());
}

/**
Expand Down Expand Up @@ -695,7 +702,7 @@ private static void internalZip(File file, ZipOutputStream zipOutput, int topDir
relative += "/";
zipOutput.putNextEntry(new ZipEntry(relative));
if (file.isFile())
copyInputStreamToOutputStream(new FileInputStream(file), zipOutput, false);
copyInputStreamToOutputStream(Files.newInputStream(file.toPath()), zipOutput, false);
else {
File[] files = file.listFiles();
if (files != null)
Expand Down Expand Up @@ -733,9 +740,9 @@ public static void unzipFile(File from, File to) throws IOException {
throw new IOException("Unable to create '" + target.getPath() + "'");
}
else {
FileOutputStream fos = new FileOutputStream(target);
copyInputStreamToOutputStream(file.getInputStream(entry), fos);
fos.close();
try (FileOutputStream fos = new FileOutputStream(target)) {
copyInputStreamToOutputStream(file.getInputStream(entry), fos);
}
}

entry = zipInput.getNextEntry();
Expand Down
15 changes: 7 additions & 8 deletions src/test/java/com/imsweb/seerutils/SeerUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
package com.imsweb.seerutils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -174,8 +173,8 @@ public void testZipFiles() throws IOException {
File testFile2 = new File(tempDir, "testFile2.txt");
String file1Txt = "This is test file 1.";
String file2Txt = "This is test file 2.";
try (Writer writer1 = new OutputStreamWriter(new FileOutputStream(testFile1), StandardCharsets.US_ASCII);
Writer writer2 = new OutputStreamWriter(new FileOutputStream(testFile2), StandardCharsets.US_ASCII)) {
try (Writer writer1 = new OutputStreamWriter(Files.newOutputStream(testFile1.toPath()), StandardCharsets.US_ASCII);
Writer writer2 = new OutputStreamWriter(Files.newOutputStream(testFile2.toPath()), StandardCharsets.US_ASCII)) {
writer1.write(file1Txt);
writer2.write(file2Txt);
}
Expand All @@ -185,7 +184,7 @@ public void testZipFiles() throws IOException {

List<String> fileTxt = new ArrayList<>();
List<String> fileNames = new ArrayList<>();
try (ZipInputStream is = new ZipInputStream(new FileInputStream(zipFile)); LineNumberReader reader = new LineNumberReader(new InputStreamReader(is, StandardCharsets.US_ASCII))) {
try (ZipInputStream is = new ZipInputStream(Files.newInputStream(zipFile.toPath())); LineNumberReader reader = new LineNumberReader(new InputStreamReader(is, StandardCharsets.US_ASCII))) {
ZipEntry entry;
while ((entry = is.getNextEntry()) != null) {
fileNames.add(entry.getName());
Expand All @@ -207,8 +206,8 @@ public void testZipFiles() throws IOException {
File testFile4 = new File(tempTestDir, "testFile4.txt");
String file3Txt = "This is test file 3.";
String file4Txt = "This is test file 4.";
try (Writer writer3 = new OutputStreamWriter(new FileOutputStream(testFile3), StandardCharsets.US_ASCII);
Writer writer4 = new OutputStreamWriter(new FileOutputStream(testFile4), StandardCharsets.US_ASCII)) {
try (Writer writer3 = new OutputStreamWriter(Files.newOutputStream(testFile3.toPath()), StandardCharsets.US_ASCII);
Writer writer4 = new OutputStreamWriter(Files.newOutputStream(testFile4.toPath()), StandardCharsets.US_ASCII)) {
writer3.write(file3Txt);
writer4.write(file4Txt);
}
Expand All @@ -218,7 +217,7 @@ public void testZipFiles() throws IOException {

fileTxt = new ArrayList<>();
fileNames = new ArrayList<>();
try (ZipInputStream is = new ZipInputStream(new FileInputStream(zipFileDir)); LineNumberReader reader = new LineNumberReader(new InputStreamReader(is, StandardCharsets.US_ASCII))) {
try (ZipInputStream is = new ZipInputStream(Files.newInputStream(zipFileDir.toPath())); LineNumberReader reader = new LineNumberReader(new InputStreamReader(is, StandardCharsets.US_ASCII))) {
ZipEntry entry;
while ((entry = is.getNextEntry()) != null) {
fileNames.add(entry.getName());
Expand Down

0 comments on commit b033efb

Please sign in to comment.