Skip to content

Commit

Permalink
[*] Use modern Java APIs for reading buffers instead of homebrewing (#…
Browse files Browse the repository at this point in the history
…5440)

Using the standard APIs is likely to be faster and/or more
understandable in most cases. In the event we need custom stuff we can
implement it in a proper utility method.

p.s. why is file reading/writing happening inside of "gui2" code?
  • Loading branch information
grimreaper authored and thpr committed May 5, 2019
1 parent 247510b commit d2e6b93
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 59 deletions.
35 changes: 13 additions & 22 deletions code/src/java/pcgen/gui2/dialog/DataInstaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -520,15 +515,9 @@ private boolean checkOverwriteOK(Collection<String> 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();
Expand All @@ -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))
{
Expand All @@ -565,7 +554,7 @@ else if (fileName.toLowerCase().startsWith(OUTPUTSHEETS_FOLDER))
*
* @return true, if successful
*/
private boolean createDirectories(Iterable<String> directories, File destDir)
private static boolean createDirectories(Iterable<String> directories, File destDir)
{
for (String dirname : directories)
{
Expand Down Expand Up @@ -595,7 +584,7 @@ private boolean createDirectories(Iterable<String> directories, File destDir)
*
* @return true, if all files created ok
*/
private boolean createFiles(File dataSet, File destDir, Iterable<String> files)
private static boolean createFiles(File dataSet, File destDir, Iterable<String> files)
{
String corrFilename = "";
try (ZipFile in = new ZipFile(dataSet))
Expand Down Expand Up @@ -724,7 +713,9 @@ private void initComponents()
* @return true, if populate file and dir lists
*/
@SuppressWarnings("rawtypes")
private boolean populateFileAndDirLists(File dataSet, Collection<String> directories, Collection<String> files)
private static boolean populateFileAndDirLists(File dataSet,
Collection<String> directories,
Collection<String> files)
{
// Navigate through the zip file, processing each file
// Open the ZIP file
Expand Down
50 changes: 13 additions & 37 deletions code/src/java/plugin/notes/gui/NotesView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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++;
}
}
Expand All @@ -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
Expand Down

0 comments on commit d2e6b93

Please sign in to comment.