From 7a10d9a786e1a4eae916fe964bbf7e4cb3ea1b2b Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Tue, 23 Jun 2015 07:14:28 -0700 Subject: [PATCH] A bit of tidying here and there. --- .../com/threerings/getdown/data/Digest.java | 10 +-- .../threerings/getdown/tools/Digester.java | 9 +-- .../getdown/tools/JarDiffPatcher.java | 73 ++++++++----------- .../threerings/getdown/util/VersionUtil.java | 1 - 4 files changed, 38 insertions(+), 55 deletions(-) diff --git a/src/main/java/com/threerings/getdown/data/Digest.java b/src/main/java/com/threerings/getdown/data/Digest.java index 1ed9cad..51fd471 100644 --- a/src/main/java/com/threerings/getdown/data/Digest.java +++ b/src/main/java/com/threerings/getdown/data/Digest.java @@ -17,6 +17,7 @@ import java.security.NoSuchAlgorithmException; import java.util.HashMap; import java.util.List; +import com.samskivert.io.StreamUtil; import com.samskivert.text.MessageUtil; import com.samskivert.util.StringUtil; @@ -104,8 +105,7 @@ public class Digest StringBuilder data = new StringBuilder(); PrintWriter pout = null; try { - pout = new PrintWriter( - new OutputStreamWriter(new FileOutputStream(output), "UTF-8")); + pout = new PrintWriter(new OutputStreamWriter(new FileOutputStream(output), "UTF-8")); // compute and append the MD5 digest of each resource in the list for (Resource rsrc : resources) { @@ -119,16 +119,14 @@ public class Digest "Error computing digest for: " + rsrc).initCause(t); } } - + // finally compute and append the digest for the file contents md.reset(); byte[] contents = data.toString().getBytes("UTF-8"); pout.println(DIGEST_FILE + " = " + StringUtil.hexlate(md.digest(contents))); } finally { - if (pout != null) { - pout.close(); - } + StreamUtil.close(pout); } } diff --git a/src/main/java/com/threerings/getdown/tools/Digester.java b/src/main/java/com/threerings/getdown/tools/Digester.java index b932de1..254e3a5 100644 --- a/src/main/java/com/threerings/getdown/tools/Digester.java +++ b/src/main/java/com/threerings/getdown/tools/Digester.java @@ -82,8 +82,7 @@ public class Digester File inputFile = new File(appdir, Digest.DIGEST_FILE); File signatureFile = new File(appdir, Digest.DIGEST_FILE + Application.SIGNATURE_SUFFIX); - FileInputStream storeInput = null; - FileInputStream dataInput = null; + FileInputStream storeInput = null, dataInput = null; FileOutputStream signatureOutput = null; try { // initialize the keystore @@ -97,7 +96,7 @@ public class Digester dataInput = new FileInputStream(inputFile); byte[] buffer = new byte[8192]; int length; - + sig.initSign(key); while ((length = dataInput.read(buffer)) != -1) { sig.update(buffer, 0, length); @@ -109,9 +108,9 @@ public class Digester signatureOutput.write(signed.getBytes("utf8")); } finally { - StreamUtil.close(storeInput); - StreamUtil.close(dataInput); StreamUtil.close(signatureOutput); + StreamUtil.close(dataInput); + StreamUtil.close(storeInput); } } } diff --git a/src/main/java/com/threerings/getdown/tools/JarDiffPatcher.java b/src/main/java/com/threerings/getdown/tools/JarDiffPatcher.java index 3806c9e..b37d714 100644 --- a/src/main/java/com/threerings/getdown/tools/JarDiffPatcher.java +++ b/src/main/java/com/threerings/getdown/tools/JarDiffPatcher.java @@ -25,6 +25,7 @@ import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.jar.JarOutputStream; +import com.samskivert.io.StreamUtil; import com.threerings.getdown.util.ProgressObserver; /** @@ -43,15 +44,12 @@ public class JarDiffPatcher implements JarDiffCodes * * @throws IOException if any problem occurs during patching. */ - public void patchJar (String jarPath, String diffPath, File target, - ProgressObserver observer) + public void patchJar (String jarPath, String diffPath, File target, ProgressObserver observer) throws IOException { - File oldFile = new File(jarPath); - File diffFile = new File(diffPath); + File oldFile = new File(jarPath), diffFile = new File(diffPath); JarOutputStream jos = null; - JarFile oldJar = null; - JarFile jarDiff = null; + JarFile oldJar = null, jarDiff = null; try { jos = new JarOutputStream(new FileOutputStream(target)); oldJar = new JarFile(oldFile); @@ -60,10 +58,10 @@ public class JarDiffPatcher implements JarDiffCodes Map renameMap = new HashMap(); determineNameMapping(jarDiff, ignoreSet, renameMap); - + // get all keys in renameMap String[] keys = renameMap.keySet().toArray(new String[renameMap.size()]); - + // Files to implicit move Set oldjarNames = new HashSet(); Enumeration oldEntries = oldJar.entries(); @@ -72,7 +70,7 @@ public class JarDiffPatcher implements JarDiffCodes oldjarNames.add(oldEntries.nextElement().getName()); } } - + // size depends on the three parameters below, which is basically the // counter for each loop that do the actual writes to the output file // since oldjarNames.size() changes in the first two loop below, we @@ -80,11 +78,11 @@ public class JarDiffPatcher implements JarDiffCodes // changes double size = oldjarNames.size() + keys.length + jarDiff.size(); double currentEntry = 0; - + // Handle all remove commands oldjarNames.removeAll(ignoreSet); size -= ignoreSet.size(); - + // Add content from JARDiff Enumeration entries = jarDiff.entries(); if (entries != null) { @@ -94,36 +92,36 @@ public class JarDiffPatcher implements JarDiffCodes updateObserver(observer, currentEntry, size); currentEntry++; writeEntry(jos, entry, jarDiff); - + // Remove entry from oldjarNames since no implicit move is // needed boolean wasInOld = oldjarNames.remove(entry.getName()); - + // Update progress counters. If it was in old, we do not // need an implicit move, so adjust total size. if (wasInOld) { size--; } - + } else { // no write is done, decrement size size--; } } } - + // go through the renameMap and apply move for each entry for (String newName : keys) { // Apply move command String oldName = renameMap.get(newName); - + // Get source JarEntry JarEntry oldEntry = oldJar.getJarEntry(oldName); if (oldEntry == null) { String moveCmd = MOVE_COMMAND + oldName + " " + newName; throw new IOException("error.badmove: " + moveCmd); } - + // Create dest JarEntry JarEntry newEntry = new JarEntry(newName); newEntry.setTime(oldEntry.getTime()); @@ -133,22 +131,22 @@ public class JarDiffPatcher implements JarDiffCodes newEntry.setMethod(oldEntry.getMethod()); newEntry.setExtra(oldEntry.getExtra()); newEntry.setComment(oldEntry.getComment()); - + updateObserver(observer, currentEntry, size); currentEntry++; - + writeEntry(jos, newEntry, oldJar.getInputStream(oldEntry)); - + // Remove entry from oldjarNames since no implicit move is needed boolean wasInOld = oldjarNames.remove(oldName); - + // Update progress counters. If it was in old, we do not need an // implicit move, so adjust total size. if (wasInOld) { size--; } } - + // implicit move Iterator iEntries = oldjarNames.iterator(); if (iEntries != null) { @@ -163,18 +161,17 @@ public class JarDiffPatcher implements JarDiffCodes updateObserver(observer, currentEntry, size); } finally { - jos = closeStream(jos); - oldJar = closeFile(oldJar); - jarDiff = closeFile(jarDiff); + StreamUtil.close(jos); + closeFile(oldJar); + closeFile(jarDiff); } } - protected void updateObserver (ProgressObserver observer, - double currentSize, double size) + protected void updateObserver (ProgressObserver observer, double currentSize, double size) { - if (observer != null) { - observer.progress((int)(100*currentSize/size)); - } + if (observer != null) { + observer.progress((int)(100*currentSize/size)); + } } protected void determineNameMapping ( @@ -289,27 +286,17 @@ public class JarDiffPatcher implements JarDiffCodes data.close(); } - private static JarFile closeFile(JarFile jar) { + private static void closeFile (JarFile jar) { if (jar != null) { try { jar.close(); } catch (IOException e) { + e.printStackTrace(System.err); } } - return null; } - private static JarOutputStream closeStream(JarOutputStream jar) { - if (jar != null) { - try { - jar.close(); - } catch (IOException e) { - } - } - return null; - } - - protected static final int DEFAULT_READ_SIZE = 2048; + protected static final int DEFAULT_READ_SIZE = 2048; protected static byte[] newBytes = new byte[DEFAULT_READ_SIZE]; protected static byte[] oldBytes = new byte[DEFAULT_READ_SIZE]; diff --git a/src/main/java/com/threerings/getdown/util/VersionUtil.java b/src/main/java/com/threerings/getdown/util/VersionUtil.java index 2afd160..324cdeb 100644 --- a/src/main/java/com/threerings/getdown/util/VersionUtil.java +++ b/src/main/java/com/threerings/getdown/util/VersionUtil.java @@ -110,7 +110,6 @@ public class VersionUtil } finally { StreamUtil.close(in); } - } private static int parseInt (String str) {