forked from External/mage
Blocker and Critical level bugfixes throughout the project (#4648)
* fixed https://sonarcloud.io/project/issues?id=org.xmage%3Amage-root&issues=AWIlv32RgrzAwlaaQ7rP&open=AWIlv32RgrzAwlaaQ7rP * ensure closing of scanner if it was opened * Refactored method in EmpyrialArchAngel to not always return same value. * Refactored method in FalkenrathAristocrat to not always return same value. * Refactored method in GilderBairn to not always return the same value. * fixed left open resources, ensured quiet closing of the streams * Refactored method in IceCave to not always return same value. * Refactored method in KjeldoranRoyalGuard to not always return same value. * Refactored method in LegionsInitiative to not always return same value. * Refactored method in NaturesWill to not always return same value. * added quiet closing method in new streamutils class, used to clean up the connectdialog * Fix small typo * added quiet closing to saveobjectutil * closed resources in savegame method of gamecontroller * properly close resources in loadGame method of GameReplay class * further proper resource closing in ServerMessagesUtil * fixed unclosed resources in copy method in mage framework Copier * closed unclosed resources in copyCompressed method in Copier * ensure closing of filewriter in manasymbols * ensure proper closing of Stream in arcane UI * ensure closing of datagram socket in arcane Util * ensure resource closing in deckimport from clipboard * ensure closing of plugin classloader * ensured closing of zipinputstream resource * ensure closing of fileoutputstream in ScryfallSymbolsSource * ensure closing resources after finishing/canceling download of pictures * remove commented code * move locks to try block to ensure unlocking along all execution paths * remove dangerous instance of double-checked locking * removed dangerous instance of double checked locking in settingsmanager * Removed dangerous instance of double-checked locking in ThemePluginImpl * close resource which did not happen certainly * close another stream * ensure closing of inputstream
This commit is contained in:
parent
ec77cecbf6
commit
5ac975c52e
31 changed files with 288 additions and 276 deletions
|
|
@ -9,6 +9,7 @@ import java.util.*;
|
|||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import mage.util.StreamUtils;
|
||||
import org.mage.plugins.card.dl.DownloadJob;
|
||||
|
||||
import static org.mage.card.arcane.ManaSymbols.getSymbolFileNameAsSVG;
|
||||
|
|
@ -106,20 +107,21 @@ public class ScryfallSymbolsSource implements Iterable<DownloadJob> {
|
|||
if (destFile.exists() && (destFile.length() > 0)){
|
||||
continue;
|
||||
}
|
||||
|
||||
FileOutputStream stream = null;
|
||||
try {
|
||||
// base64 transform
|
||||
String data64 = foundedData.get(searchCode);
|
||||
Base64.Decoder dec = Base64.getDecoder();
|
||||
byte[] fileData = dec.decode(data64);
|
||||
|
||||
FileOutputStream stream = new FileOutputStream(destFile);
|
||||
stream = new FileOutputStream(destFile);
|
||||
stream.write(fileData);
|
||||
stream.close();
|
||||
|
||||
LOGGER.info("New svg symbol downloaded: " + needCode);
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("Can't decode svg icon and save to file: " + destFile.getPath() + ", reason: " + e.getMessage());
|
||||
} finally {
|
||||
StreamUtils.closeQuietly(stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ import mage.client.MageFrame;
|
|||
import mage.client.dialog.PreferencesDialog;
|
||||
import mage.client.util.sets.ConstructedFormats;
|
||||
import mage.remote.Connection;
|
||||
import mage.util.StreamUtils;
|
||||
import net.java.truevfs.access.TFile;
|
||||
import net.java.truevfs.access.TFileOutputStream;
|
||||
import net.java.truevfs.access.TVFS;
|
||||
|
|
@ -745,34 +746,6 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
|
|||
}
|
||||
}
|
||||
|
||||
/*
|
||||
if(!destFile.getParentFile().exists()){
|
||||
destFile.getParentFile().mkdirs();
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
// WTF start?! TODO: wtf
|
||||
File existingFile = new File(imagePath.replaceFirst("\\w{3}.zip", ""));
|
||||
if (existingFile.exists()) {
|
||||
try {
|
||||
new TFile(existingFile).cp_rp(outputFile);
|
||||
} catch (IOException e) {
|
||||
logger.error("Error while copying file " + card.getName(), e);
|
||||
}
|
||||
synchronized (sync) {
|
||||
update(cardIndex + 1, count);
|
||||
}
|
||||
existingFile.delete();
|
||||
File parent = existingFile.getParentFile();
|
||||
if (parent != null && parent.isDirectory() && parent.list().length == 0) {
|
||||
parent.delete();
|
||||
}
|
||||
return;
|
||||
}
|
||||
// WTF end?!
|
||||
*/
|
||||
// START to download
|
||||
cardImageSource.doPause(url.getPath());
|
||||
URLConnection httpConn = url.openConnection(p);
|
||||
httpConn.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
|
||||
|
|
@ -782,18 +755,18 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
|
|||
if (responseCode == 200) {
|
||||
// download OK
|
||||
// save data to temp
|
||||
BufferedOutputStream out;
|
||||
try (BufferedInputStream in = new BufferedInputStream(httpConn.getInputStream())) {
|
||||
out = new BufferedOutputStream(new TFileOutputStream(fileTempImage));
|
||||
OutputStream out = null;
|
||||
OutputStream tfileout = null;
|
||||
InputStream in = null;
|
||||
try {
|
||||
in = new BufferedInputStream(httpConn.getInputStream());
|
||||
tfileout = new TFileOutputStream(fileTempImage);
|
||||
out = new BufferedOutputStream(tfileout);
|
||||
byte[] buf = new byte[1024];
|
||||
int len;
|
||||
while ((len = in.read(buf)) != -1) {
|
||||
// user cancelled
|
||||
if (cancel) {
|
||||
in.close();
|
||||
out.flush();
|
||||
out.close();
|
||||
|
||||
// stop download, save current state and exit
|
||||
TFile archive = destFile.getTopLevelArchive();
|
||||
///* not need to unmout/close - it's auto action
|
||||
|
|
@ -804,8 +777,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
|
|||
} catch (Exception e) {
|
||||
logger.error("Can't close archive file: " + e.getMessage(), e);
|
||||
}
|
||||
|
||||
}//*/
|
||||
}
|
||||
try {
|
||||
TFile.rm(fileTempImage);
|
||||
} catch (Exception e) {
|
||||
|
|
@ -816,9 +788,12 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
|
|||
out.write(buf, 0, len);
|
||||
}
|
||||
}
|
||||
// TODO: remove to finnaly section?
|
||||
out.flush();
|
||||
out.close();
|
||||
finally {
|
||||
StreamUtils.closeQuietly(in);
|
||||
StreamUtils.closeQuietly(out);
|
||||
StreamUtils.closeQuietly(tfileout);
|
||||
}
|
||||
|
||||
|
||||
// TODO: add two faces card correction? (WTF)
|
||||
// SAVE final data
|
||||
|
|
@ -847,81 +822,6 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
|
|||
}
|
||||
}
|
||||
|
||||
/*
|
||||
// Logger.getLogger(this.getClass()).info(url.toString());
|
||||
boolean useTempFile = false;
|
||||
int responseCode = 0;
|
||||
URLConnection httpConn = null;
|
||||
|
||||
if (temporaryFile != null && temporaryFile.length() > 100) {
|
||||
useTempFile = true;
|
||||
} else {
|
||||
cardImageSource.doPause(url.getPath());
|
||||
httpConn = url.openConnection(p);
|
||||
httpConn.connect();
|
||||
responseCode = ((HttpURLConnection) httpConn).getResponseCode();
|
||||
}
|
||||
|
||||
if (responseCode == 200 || useTempFile) {
|
||||
if (!useTempFile) {
|
||||
BufferedOutputStream out;
|
||||
try (BufferedInputStream in = new BufferedInputStream(httpConn.getInputStream())) {
|
||||
//try (BufferedInputStream in = new BufferedInputStream(url.openConnection(p).getInputStream())) {
|
||||
out = new BufferedOutputStream(new TFileOutputStream(temporaryFile));
|
||||
byte[] buf = new byte[1024];
|
||||
int len;
|
||||
while ((len = in.read(buf)) != -1) {
|
||||
// user cancelled
|
||||
if (cancel) {
|
||||
in.close();
|
||||
out.flush();
|
||||
out.close();
|
||||
temporaryFile.delete();
|
||||
return;
|
||||
}
|
||||
out.write(buf, 0, len);
|
||||
}
|
||||
|
||||
}
|
||||
out.flush();
|
||||
out.close();
|
||||
}
|
||||
|
||||
// TODO: WTF?! start
|
||||
if (card != null && card.isTwoFacedCard()) {
|
||||
BufferedImage image = ImageIO.read(temporaryFile);
|
||||
if (image.getHeight() == 470) {
|
||||
BufferedImage renderedImage = new BufferedImage(265, 370, BufferedImage.TYPE_INT_RGB);
|
||||
renderedImage.getGraphics();
|
||||
Graphics2D graphics2D = renderedImage.createGraphics();
|
||||
if (card.isTwoFacedCard() && card.isSecondSide()) {
|
||||
graphics2D.drawImage(image, 0, 0, 265, 370, 313, 62, 578, 432, null);
|
||||
} else {
|
||||
graphics2D.drawImage(image, 0, 0, 265, 370, 41, 62, 306, 432, null);
|
||||
}
|
||||
graphics2D.dispose();
|
||||
writeImageToFile(renderedImage, outputFile);
|
||||
} else {
|
||||
outputFile.getParentFile().mkdirs();
|
||||
new TFile(temporaryFile).cp_rp(outputFile);
|
||||
}
|
||||
//temporaryFile.delete();
|
||||
} else {
|
||||
outputFile.getParentFile().mkdirs();
|
||||
new TFile(temporaryFile).cp_rp(outputFile);
|
||||
}
|
||||
// WTF?! end
|
||||
} else {
|
||||
if (card != null && !useSpecifiedPaths) {
|
||||
logger.warn("Image download for " + card.getName()
|
||||
+ (!card.getDownloadName().equals(card.getName()) ? " downloadname: " + card.getDownloadName() : "")
|
||||
+ '(' + card.getSet() + ") failed - responseCode: " + responseCode + " url: " + url.toString());
|
||||
}
|
||||
if (logger.isDebugEnabled()) { // Shows the returned html from the request to the web server
|
||||
logger.debug("Returned HTML ERROR:\n" + convertStreamToString(((HttpURLConnection) httpConn).getErrorStream()));
|
||||
}
|
||||
}
|
||||
*/
|
||||
} catch (AccessDeniedException e) {
|
||||
logger.error("Can't access to files: " + card.getName() + "(" + card.getSet() + "). Try rebooting your system to remove the file lock.");
|
||||
} catch (Exception e) {
|
||||
|
|
@ -933,26 +833,6 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
|
|||
update(cardIndex + 1, count);
|
||||
}
|
||||
}
|
||||
|
||||
// private void writeImageToFile(BufferedImage image, TFile file) throws IOException {
|
||||
// Iterator iter = ImageIO.getImageWritersByFormatName("jpg");
|
||||
//
|
||||
// ImageWriter writer = (ImageWriter) iter.next();
|
||||
// ImageWriteParam iwp = writer.getDefaultWriteParam();
|
||||
// iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
|
||||
// iwp.setCompressionQuality(0.96f);
|
||||
//
|
||||
// File tempFile = new File(getImagesDir() + File.separator + image.hashCode() + file.getName());
|
||||
// FileImageOutputStream output = new FileImageOutputStream(tempFile);
|
||||
// writer.setOutput(output);
|
||||
// IIOImage image2 = new IIOImage(image, null, null);
|
||||
// writer.write(null, image2, iwp);
|
||||
// writer.dispose();
|
||||
// output.close();
|
||||
//
|
||||
// new TFile(tempFile).cp_rp(file);
|
||||
// tempFile.delete();
|
||||
// }
|
||||
}
|
||||
|
||||
private void update(int card, int count) {
|
||||
|
|
|
|||
|
|
@ -12,13 +12,9 @@ public class SettingsManager {
|
|||
|
||||
private static SettingsManager settingsManager = null;
|
||||
|
||||
public static SettingsManager getIntance() {
|
||||
public static synchronized SettingsManager getIntance() {
|
||||
if (settingsManager == null) {
|
||||
synchronized (SettingsManager.class) {
|
||||
if (settingsManager == null) {
|
||||
settingsManager = new SettingsManager();
|
||||
}
|
||||
}
|
||||
settingsManager = new SettingsManager();
|
||||
}
|
||||
return settingsManager;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -150,47 +150,43 @@ public class ThemePluginImpl implements ThemePlugin {
|
|||
return bgPanel;
|
||||
}
|
||||
|
||||
private ImagePanel createImagePanelInstance() {
|
||||
private synchronized ImagePanel createImagePanelInstance() {
|
||||
if (background == null) {
|
||||
synchronized (ThemePluginImpl.class) {
|
||||
if (background == null) {
|
||||
String filename = "/background.png";
|
||||
try {
|
||||
if (PreferencesDialog.getCachedValue(PreferencesDialog.KEY_BACKGROUND_IMAGE_DEFAULT, "true").equals("true")) {
|
||||
InputStream is = this.getClass().getResourceAsStream(filename);
|
||||
if (is == null) {
|
||||
throw new FileNotFoundException("Couldn't find " + filename + " in resources.");
|
||||
}
|
||||
background = ImageIO.read(is);
|
||||
} else {
|
||||
String path = PreferencesDialog.getCachedValue(PreferencesDialog.KEY_BACKGROUND_IMAGE, "");
|
||||
if (path != null && !path.isEmpty()) {
|
||||
try {
|
||||
File f = new File(path);
|
||||
if (f != null) {
|
||||
background = ImageIO.read(f);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
background = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (background == null) {
|
||||
InputStream is = this.getClass().getResourceAsStream(filename);
|
||||
if (is == null) {
|
||||
throw new FileNotFoundException("Couldn't find " + filename + " in resources.");
|
||||
}
|
||||
background = ImageIO.read(is);
|
||||
}
|
||||
if (background == null) {
|
||||
String filename = "/background.png";
|
||||
try {
|
||||
if (PreferencesDialog.getCachedValue(PreferencesDialog.KEY_BACKGROUND_IMAGE_DEFAULT, "true").equals("true")) {
|
||||
InputStream is = this.getClass().getResourceAsStream(filename);
|
||||
if (is == null) {
|
||||
throw new FileNotFoundException("Couldn't find " + filename + " in resources.");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
return null;
|
||||
background = ImageIO.read(is);
|
||||
} else {
|
||||
String path = PreferencesDialog.getCachedValue(PreferencesDialog.KEY_BACKGROUND_IMAGE, "");
|
||||
if (path != null && !path.isEmpty()) {
|
||||
try {
|
||||
File f = new File(path);
|
||||
if (f != null) {
|
||||
background = ImageIO.read(f);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
background = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (background == null) {
|
||||
InputStream is = this.getClass().getResourceAsStream(filename);
|
||||
if (is == null) {
|
||||
throw new FileNotFoundException("Couldn't find " + filename + " in resources.");
|
||||
}
|
||||
background = ImageIO.read(is);
|
||||
}
|
||||
if (background == null) {
|
||||
throw new FileNotFoundException("Couldn't find " + filename + " in resources.");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
return new ImagePanel(background, ImagePanelStyle.SCALED);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue