Skip to content

Commit 68bac42

Browse files
committed
Be sure that resources are closed
1 parent 78a4369 commit 68bac42

File tree

2 files changed

+18
-23
lines changed

2 files changed

+18
-23
lines changed

exist-ant/src/main/java/org/exist/ant/XMLDBExtractTask.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,10 @@ private void writeBinaryResource(final Resource res, File dest) throws XMLDBExce
297297
final String fname = res.getId();
298298
dest = new File(dest, fname);
299299
}
300-
final FileOutputStream os;
301-
os = new FileOutputStream(dest);
302-
303-
((ExtendedResource) res).getContentIntoAStream(os);
304300

301+
try(final OutputStream os = Files.newOutputStream(dest.toPath())) {
302+
((ExtendedResource) res).getContentIntoAStream(os);
303+
}
305304

306305
} else {
307306
final String msg = "Dest binary file " + ((dest != null) ? (dest.getAbsolutePath() + " ") : "") + "exists. Use " + "overwrite property to overwrite file.";

exist-core/src/main/java/org/exist/client/DocumentView.java

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,11 @@
3030
import java.awt.event.KeyEvent;
3131
import java.awt.event.WindowAdapter;
3232
import java.awt.event.WindowEvent;
33-
import java.io.File;
34-
import java.io.FileOutputStream;
35-
import java.io.IOException;
36-
import java.io.OutputStreamWriter;
37-
import java.io.PrintWriter;
38-
import java.io.StringWriter;
33+
import java.io.*;
3934
import java.net.URISyntaxException;
4035
import java.net.URL;
4136
import java.nio.charset.Charset;
37+
import java.nio.file.Files;
4238
import java.nio.file.Paths;
4339
import java.util.Observable;
4440
import java.util.Observer;
@@ -79,6 +75,8 @@
7975
import org.xmldb.api.base.XMLDBException;
8076
import org.xmldb.api.modules.XMLResource;
8177

78+
import static java.nio.charset.StandardCharsets.UTF_8;
79+
8280
class DocumentView extends JFrame {
8381

8482
private static final long serialVersionUID = 1L;
@@ -410,33 +408,31 @@ private void saveAs() {
410408
}
411409

412410
private void export() throws XMLDBException {
413-
final String workDir = properties.getProperty("working-dir", System //$NON-NLS-1$
414-
.getProperty("user.dir")); //$NON-NLS-1$
411+
final String workDir = properties.getProperty("working-dir", System.getProperty("user.dir"));
415412
final JFileChooser chooser = new JFileChooser(workDir);
416413
chooser.setMultiSelectionEnabled(false);
417414
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
418415
chooser.setSelectedFile(Paths.get(resource.getId()).toFile());
419-
if (chooser.showDialog(this, Messages.getString("DocumentView.44")) == JFileChooser.APPROVE_OPTION) { //$NON-NLS-1$
416+
417+
if (chooser.showDialog(this, Messages.getString("DocumentView.44")) == JFileChooser.APPROVE_OPTION) {
420418
final File file = chooser.getSelectedFile();
421419
if (file.exists()
422420
&& JOptionPane.showConfirmDialog(this,
423-
Messages.getString("DocumentView.45"), Messages.getString("DocumentView.46"), //$NON-NLS-1$ //$NON-NLS-2$
421+
Messages.getString("DocumentView.45"), Messages.getString("DocumentView.46"),
424422
JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
425423
return;
426424
}
427-
try {
428-
final OutputStreamWriter writer = new OutputStreamWriter(
429-
new FileOutputStream(file), Charset.forName(properties
430-
.getProperty("encoding"))); //$NON-NLS-1$
425+
426+
final Charset encoding = Charset.forName(properties.getProperty("encoding"));
427+
try (final Writer writer = Files.newBufferedWriter(file.toPath(), encoding)) {
431428
writer.write(text.getText());
432-
writer.close();
429+
433430
} catch (final IOException e) {
434-
ClientFrame.showErrorMessage(Messages.getString("DocumentView.48") //$NON-NLS-1$
435-
+ e.getMessage(), e);
431+
ClientFrame.showErrorMessage(Messages.getString("DocumentView.48") + e.getMessage(), e);
436432
}
433+
437434
final File selectedDir = chooser.getCurrentDirectory();
438-
properties
439-
.setProperty("working-dir", selectedDir.getAbsolutePath()); //$NON-NLS-1$
435+
properties.setProperty("working-dir", selectedDir.getAbsolutePath());
440436
}
441437
}
442438

0 commit comments

Comments
 (0)