diff --git a/code/src/java/pcgen/gui2/dialog/DataInstaller.java b/code/src/java/pcgen/gui2/dialog/DataInstaller.java index 1055753bee2..844416eb803 100644 --- a/code/src/java/pcgen/gui2/dialog/DataInstaller.java +++ b/code/src/java/pcgen/gui2/dialog/DataInstaller.java @@ -32,10 +32,12 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collection; import java.util.Enumeration; import java.util.List; +import java.util.stream.Collectors; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; @@ -261,23 +263,16 @@ private boolean readDataSet(File dataSet) ShowMessageDelegate.showMessageDialog( LanguageBundle.getFormattedString("in_diNoInstallFile", dataSet.getName()), TITLE, MessageType.WARNING); - in.close(); return false; } // Parse the install file InputStream inStream = in.getInputStream(installEntry); - BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "UTF-8")); //$NON-NLS-1$ - - StringBuilder installInfo = new StringBuilder(); - String line; - while ((line = reader.readLine()) != null) - { - installInfo.append(line).append("\n"); - } + BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, StandardCharsets.UTF_8)); //$NON-NLS-1$ + String installInfo = reader.lines().collect(Collectors.joining("\n")); final InstallLoader loader = new InstallLoader(); - loader.loadLstString(null, dataSet.toURI(), installInfo.toString()); + loader.loadLstString(null, dataSet.toURI(), installInfo); campaign = loader.getCampaign(); } catch (IOException e) @@ -520,15 +515,9 @@ private boolean checkOverwriteOK(Collection files, File destDir) * * @throws IOException Signals that an I/O exception has occurred. */ - private void copyInputStream(InputStream in, OutputStream out) throws IOException + private static void copyInputStream(InputStream in, OutputStream out) throws IOException { - byte[] buffer = new byte[1024]; - int len; - - while ((len = in.read(buffer)) >= 0) - { - out.write(buffer, 0, len); - } + in.transferTo(out); in.close(); out.close(); @@ -543,7 +532,7 @@ private void copyInputStream(InputStream in, OutputStream out) throws IOExceptio * * @return the corrected file name. */ - private String correctFileName(File destDir, String fileName) + private static String correctFileName(File destDir, String fileName) { if (fileName.toLowerCase().startsWith(DATA_FOLDER)) { @@ -565,7 +554,7 @@ else if (fileName.toLowerCase().startsWith(OUTPUTSHEETS_FOLDER)) * * @return true, if successful */ - private boolean createDirectories(Iterable directories, File destDir) + private static boolean createDirectories(Iterable directories, File destDir) { for (String dirname : directories) { @@ -595,7 +584,7 @@ private boolean createDirectories(Iterable directories, File destDir) * * @return true, if all files created ok */ - private boolean createFiles(File dataSet, File destDir, Iterable files) + private static boolean createFiles(File dataSet, File destDir, Iterable files) { String corrFilename = ""; try (ZipFile in = new ZipFile(dataSet)) @@ -724,7 +713,9 @@ private void initComponents() * @return true, if populate file and dir lists */ @SuppressWarnings("rawtypes") - private boolean populateFileAndDirLists(File dataSet, Collection directories, Collection files) + private static boolean populateFileAndDirLists(File dataSet, + Collection directories, + Collection files) { // Navigate through the zip file, processing each file // Open the ZIP file diff --git a/code/src/java/plugin/notes/gui/NotesView.java b/code/src/java/plugin/notes/gui/NotesView.java index 06c2b2c9ae5..dbed405abcf 100644 --- a/code/src/java/plugin/notes/gui/NotesView.java +++ b/code/src/java/plugin/notes/gui/NotesView.java @@ -628,8 +628,6 @@ else if (align == StyleConstants.ALIGN_CENTER) private int writeNotesDir(ZipOutputStream out, File parentDir, File currentDir, ProgressMonitor pm, int progress) throws IOException { - byte[] buffer = new byte[4096]; - int bytes_read; int returnValue = progress; for (File f : currentDir.listFiles()) @@ -645,31 +643,13 @@ private int writeNotesDir(ZipOutputStream out, File parentDir, File currentDir, } else { - FileInputStream in = new FileInputStream(f); - - try + try (InputStream in = new FileInputStream(f)) { String parentPath = parentDir.getParentFile().getAbsolutePath(); ZipEntry entry = new ZipEntry(f.getAbsolutePath().substring(parentPath.length() + 1)); out.putNextEntry(entry); - - while ((bytes_read = in.read(buffer)) != -1) - { - out.write(buffer, 0, bytes_read); - } - } - finally - { - try - { - in.close(); - } - catch (IOException e) - { - //TODO: Should this really be ignored? - } + out.write(in.readAllBytes()); } - returnValue++; } } @@ -690,29 +670,25 @@ private void writeNotesFile(File exportFile, NotesTreeNode node) throws IOExcept { File dir = node.getDir(); - ZipOutputStream out = new ZipOutputStream(new FileOutputStream(exportFile)); - int max = fileCount(dir); - ProgressMonitor pm = new ProgressMonitor(GMGenSystem.inst, "Writing out Notes Export", "Writing", 0, max); - - try + ProgressMonitor pm = null; + try (ZipOutputStream out = new ZipOutputStream(new FileOutputStream(exportFile));) { + int max = fileCount(dir); + pm = new ProgressMonitor(GMGenSystem.inst, "Writing out Notes Export", "Writing", 0, max); writeNotesDir(out, dir, dir, pm, 0); } - - // Always close the streams, even if exceptions were thrown + catch (IOException e) + { + Logging.debugPrint("error writing notes", e); + //TODO: Should this really be ignored? + } finally { - try - { - out.close(); - } - catch (IOException e) + if (pm != null) { - //TODO: Should this really be ignored? + pm.close(); } } - - pm.close(); } //CoreUtility methods