Merge branch 'sonarqubefixes01' into 'master'

Fixes some SonarQube detected bugs

See merge request core-developers/forge!2019
This commit is contained in:
swordshine
2019-08-10 10:32:18 +00:00
17 changed files with 67 additions and 176 deletions

View File

@@ -174,8 +174,7 @@ public class CardReaderExperiments {
}
if (updated) {
try {
PrintWriter p = new PrintWriter(file);
try (PrintWriter p = new PrintWriter(file)) {
for (int i = 0; i < lines.size(); i++) {
if (i < lines.size() - 1) {
p.println(lines.get(i));
@@ -184,7 +183,6 @@ public class CardReaderExperiments {
p.print(lines.get(i));
}
}
p.close();
output.add(rules.getName());
} catch (final Exception ex) {
}

View File

@@ -49,13 +49,11 @@ public final class CardScriptInfo {
public boolean trySetText(final String text0) {
if (file == null) { return false; }
try {
final PrintWriter p = new PrintWriter(file);
try (PrintWriter p = new PrintWriter(file)) {
p.print(text0);
if (!text0.endsWith(("\n"))){
p.print("\n");
}
p.close();
text = text0;
return true;

View File

@@ -19,33 +19,21 @@ public class CardThemedLDAIO {
public static void saveRawLDA(String format, List<Archetype> lda){
File file = getRAWLDAFile(format);
ObjectOutputStream s = null;
try {
FileOutputStream f = new FileOutputStream(file);
s = new ObjectOutputStream(f);
try (FileOutputStream f = new FileOutputStream(file);
ObjectOutputStream s = new ObjectOutputStream(f)){
s.writeObject(lda);
s.close();
} catch (IOException e) {
System.out.println("Error writing matrix data: " + e);
} finally {
if(s!=null) {
try {
s.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
public static List<Archetype> loadRawLDA(String format){
try {
FileInputStream fin = new FileInputStream(getRAWLDAFile(format));
ObjectInputStream s = new ObjectInputStream(fin);
try (FileInputStream fin = new FileInputStream(getRAWLDAFile(format));
ObjectInputStream s = new ObjectInputStream(fin)) {
List<Archetype> matrix = (List<Archetype>) s.readObject();
s.close();
return matrix;
}catch (Exception e){
} catch (Exception e){
System.out.println("Error reading LDA data: " + e);
return null;
}
@@ -54,37 +42,24 @@ public class CardThemedLDAIO {
public static void saveLDA(String format, Map<String,List<List<Pair<String, Double>>>> map){
File file = getLDAFile(format);
ObjectOutputStream s = null;
try {
FileOutputStream f = new FileOutputStream(file);
s = new ObjectOutputStream(f);
try (FileOutputStream f = new FileOutputStream(file);
ObjectOutputStream s = new ObjectOutputStream(f)){
s.writeObject(map);
s.close();
} catch (IOException e) {
System.out.println("Error writing matrix data: " + e);
} finally {
if(s!=null) {
try {
s.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
public static Map<String,List<List<Pair<String, Double>>>> loadLDA(String format){
try {
FileInputStream fin = new FileInputStream(getLDAFile(format));
ObjectInputStream s = new ObjectInputStream(fin);
try (FileInputStream fin = new FileInputStream(getLDAFile(format));
ObjectInputStream s = new ObjectInputStream(fin)) {
Map<String,List<List<Pair<String, Double>>>> matrix = (Map<String,List<List<Pair<String, Double>>>>) s.readObject();
s.close();
return matrix;
}catch (Exception e){
} catch (Exception e){
System.out.println("Error reading LDA data: " + e);
return null;
}
}
public static File getLDAFile(final String name) {

View File

@@ -24,37 +24,23 @@ public class CardThemedMatrixIO {
public static void saveMatrix(String format, HashMap<String,List<Map.Entry<PaperCard,Integer>>> map){
File file = getMatrixFile(format);
ObjectOutputStream s = null;
try {
FileOutputStream f = new FileOutputStream(file);
s = new ObjectOutputStream(f);
try (FileOutputStream f = new FileOutputStream(file);
ObjectOutputStream s = new ObjectOutputStream(f)) {
s.writeObject(map);
s.close();
} catch (IOException e) {
System.out.println("Error writing matrix data: " + e);
} finally {
if(s!=null) {
try {
s.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
public static HashMap<String,List<Map.Entry<PaperCard,Integer>>> loadMatrix(String format){
try {
FileInputStream fin = new FileInputStream(getMatrixFile(format));
ObjectInputStream s = new ObjectInputStream(fin);
try (FileInputStream fin = new FileInputStream(getMatrixFile(format));
ObjectInputStream s = new ObjectInputStream(fin)){
HashMap<String, List<Map.Entry<PaperCard,Integer>>> matrix = (HashMap<String, List<Map.Entry<PaperCard,Integer>>>) s.readObject();
s.close();
return matrix;
}catch (Exception e){
System.out.println("Error reading matrix data: " + e);
return null;
}
}
public static File getMatrixFile(final String name) {

View File

@@ -211,7 +211,7 @@ public class GuiDownloadZipService extends GuiDownloadService {
}
}
protected void copyInputStream(final InputStream in, final String outPath) throws IOException{
protected void copyInputStream(final InputStream in, final String outPath) throws IOException {
final byte[] buffer = new byte[1024];
int len;
final BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outPath));

View File

@@ -145,12 +145,9 @@ public class BugReporter {
f = GuiBase.getInterface().getSaveFile(f);
try {
final BufferedWriter bw = new BufferedWriter(new FileWriter(f));
try (BufferedWriter bw = new BufferedWriter(new FileWriter(f))){
bw.write(text);
bw.close();
}
catch (final IOException ex) {
} catch (final IOException ex) {
SOptionPane.showMessageDialog("There was an error during saving. Sorry!\n" + ex,
"Error saving file", SOptionPane.ERROR_ICON);
}

View File

@@ -109,37 +109,23 @@ public class GauntletIO {
}
public static GauntletData loadGauntlet(final File xmlSaveFile) {
GZIPInputStream zin = null;
boolean isCorrupt = false;
try {
zin = new GZIPInputStream(new FileInputStream(xmlSaveFile));
final InputStreamReader reader = new InputStreamReader(zin);
try (GZIPInputStream zin = new GZIPInputStream(new FileInputStream(xmlSaveFile));
InputStreamReader reader = new InputStreamReader(zin)) {
final GauntletData data = (GauntletData)GauntletIO.getSerializer(true).fromXML(reader);
final String filename = xmlSaveFile.getName();
data.setName(filename.substring(0, filename.length() - SUFFIX_DATA.length()));
return data;
}
catch (final IOException e) {
} catch (final IOException e) {
e.printStackTrace();
}
catch (final ConversionException e) {
} catch (final ConversionException e) {
BugReporter.reportException(e);
}
catch (final Exception e) { //if there's a non-IO exception, delete the corrupt file
} catch (final Exception e) { //if there's a non-IO exception, delete the corrupt file
e.printStackTrace();
isCorrupt = true;
}
finally {
if (zin != null) {
try {
zin.close();
} catch (final IOException e) {
System.out.println("error closing gauntlet data reader: " + e);
}
}
}
if (isCorrupt) {
try {
xmlSaveFile.delete();

View File

@@ -120,20 +120,19 @@ public class QuestDataIO {
*/
public static QuestData loadData(final File xmlSaveFile) throws IOException {
QuestData data;
final GZIPInputStream zin = new GZIPInputStream(new FileInputStream(xmlSaveFile));
final StringBuilder xml = new StringBuilder();
final char[] buf = new char[1024];
final InputStreamReader reader = new InputStreamReader(zin);
while (reader.ready()) {
final int len = reader.read(buf);
if (len == -1) {
break;
} // when end of stream was reached
xml.append(buf, 0, len);
}
zin.close();
try (GZIPInputStream zin = new GZIPInputStream(new FileInputStream(xmlSaveFile));
InputStreamReader reader = new InputStreamReader(zin)) {
final char[] buf = new char[1024];
while (reader.ready()) {
final int len = reader.read(buf);
if (len == -1) {
break;
} // when end of stream was reached
xml.append(buf, 0, len);
}
}
String bigXML = xml.toString();
try {

View File

@@ -83,34 +83,21 @@ public class TournamentIO {
}
public static TournamentData loadTournament(final File xmlSaveFile) {
GZIPInputStream zin = null;
boolean isCorrupt = false;
try {
zin = new GZIPInputStream(new FileInputStream(xmlSaveFile));
final InputStreamReader reader = new InputStreamReader(zin);
try (GZIPInputStream zin = new GZIPInputStream(new FileInputStream(xmlSaveFile));
InputStreamReader reader = new InputStreamReader(zin)) {
final TournamentData data = (TournamentData)TournamentIO.getSerializer(true).fromXML(reader);
final String filename = xmlSaveFile.getName();
data.setName(filename.substring(0, filename.length() - SUFFIX_DATA.length()));
return data;
}
catch (final IOException e) {
} catch (final IOException e) {
e.printStackTrace();
}
catch (final Exception e) { //if there's a non-IO exception, delete the corrupt file
} catch (final Exception e) { //if there's a non-IO exception, delete the corrupt file
e.printStackTrace();
isCorrupt = true;
}
finally {
if (zin != null) {
try {
zin.close();
} catch (final IOException e) {
System.out.println("error closing tournament data reader: " + e);
}
}
}
if (isCorrupt) {
try {
xmlSaveFile.delete();

View File

@@ -86,15 +86,9 @@ public class HttpUtil {
return;
}
FileInputStream uploadFileReader = null;
try {
uploadFileReader = new FileInputStream(f);
} catch (final FileNotFoundException e) {
return;
}
final int numBytesToRead = 1024;
int availableBytesToRead;
try {
try (FileInputStream uploadFileReader = new FileInputStream(f)) {
while ((availableBytesToRead = uploadFileReader.available()) > 0) {
byte[] bufferBytesRead;
bufferBytesRead = availableBytesToRead >= numBytesToRead ? new byte[numBytesToRead]
@@ -103,10 +97,10 @@ public class HttpUtil {
httpOut.write(bufferBytesRead);
httpOut.flush();
}
uploadFileReader.close();
} catch (final IOException e) {
return;
}
try {
httpOut.write(("--" + HttpUtil.BOUNDARY + "--\r\n").getBytes());
} catch (final IOException e) {
@@ -150,22 +144,17 @@ public class HttpUtil {
}
}
public static String getURL(final String sURL) {
public static String getURL(final String sURL) {
URL url = null;
try {
url = new URL(sURL);
} catch (final MalformedURLException e) {
return null;
}
InputStream is = null;
try {
is = url.openStream();
} catch (final IOException e) {
return null;
}
int ptr = 0;
final StringBuffer buffer = new StringBuffer();
try {
try (InputStream is = url.openStream()) {
while ((ptr = is.read()) != -1) {
buffer.append((char) ptr);
}