Autoclose FileOutputStreams (#5753)

This commit is contained in:
Chris H
2024-08-01 14:22:40 -04:00
committed by GitHub
parent e6379daf91
commit 5fc6d38954
9 changed files with 120 additions and 195 deletions

View File

@@ -1,31 +1,5 @@
package forge.gui.framework;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.swing.border.EmptyBorder;
import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import forge.Singletons;
import forge.gui.FThreads;
import forge.gui.SOverlayUtils;
@@ -42,6 +16,19 @@ import forge.util.maps.MapOfLists;
import forge.view.FFrame;
import forge.view.FView;
import javax.swing.border.EmptyBorder;
import javax.xml.stream.*;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import java.awt.*;
import java.io.*;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* Handles layout saving and loading.
*
@@ -297,12 +284,9 @@ public final class SLayoutIO {
}
final XMLOutputFactory out = XMLOutputFactory.newInstance();
FileOutputStream fos = null;
XMLEventWriter writer = null;
try {
try(FileOutputStream fos = new FileOutputStream(fWriteTo);) {
String layoutSerial = getLayoutSerial(file.defaultLoc);
fos = new FileOutputStream(fWriteTo);
writer = out.createXMLEventWriter(fos);
final List<DragCell> cells = FView.SINGLETON_INSTANCE.getDragCells();
@@ -337,15 +321,12 @@ public final class SLayoutIO {
}
writer.flush();
writer.add(EF.createEndDocument());
} catch (FileNotFoundException | XMLStreamException e) {
} catch (XMLStreamException | IOException e) {
// TODO Auto-generated catch block ignores the exception, but sends it to System.err and probably forge.log.
e.printStackTrace();
} finally {
if ( writer != null )
try { writer.close(); } catch (XMLStreamException e) {}
if ( fos != null )
try { fos.close(); } catch (IOException e) {}
}
}