Reel in the verbosity a bit.

Other than to disambiguate for the compiler, I don't think clarity is enhanced
with twenty plus individual imports from the most commonly used packages in the
JDK (java.io + java.util). We all know where HashMap and InputStream are
defined.

Also some of the try-with-resources expanded multiple stream wrappers into
individual declarations. This doesn't make things any safer, close one and
you've closed them all. I reverted them to their more compact form.
This commit is contained in:
Michael Bayne
2018-08-27 10:56:26 -07:00
parent 1c11ddf7f3
commit cdde1c773c
5 changed files with 49 additions and 101 deletions
@@ -41,25 +41,9 @@
package com.threerings.getdown.tools; package com.threerings.getdown.tools;
import java.io.Closeable; import java.io.*;
import java.io.File; import java.util.*;
import java.io.IOException; import java.util.jar.*;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
/** /**
* JarDiff is able to create a jar file containing the delta between two jar files (old and new). * JarDiff is able to create a jar file containing the delta between two jar files (old and new).
@@ -221,36 +205,32 @@ public class JarDiff implements JarDiffCodes
Map<String,String> movedMap) Map<String,String> movedMap)
throws IOException throws IOException
{ {
try (StringWriter writer = new StringWriter()) { StringWriter writer = new StringWriter();
writer.write(VERSION_HEADER);
writer.write("\r\n");
writer.write(VERSION_HEADER); // Write out entries that have been removed
for (String name : oldEntries) {
writer.write(REMOVE_COMMAND);
writer.write(" ");
writeEscapedString(writer, name);
writer.write("\r\n"); writer.write("\r\n");
// Write out entries that have been removed
for (String name : oldEntries) {
writer.write(REMOVE_COMMAND);
writer.write(" ");
writeEscapedString(writer, name);
writer.write("\r\n");
}
// And those that have moved
for (String newName : movedMap.keySet()) {
String oldName = movedMap.get(newName);
writer.write(MOVE_COMMAND);
writer.write(" ");
writeEscapedString(writer, oldName);
writer.write(" ");
writeEscapedString(writer, newName);
writer.write("\r\n");
}
JarEntry je = new JarEntry(INDEX_NAME);
byte[] bytes = writer.toString().getBytes("UTF-8");
jos.putNextEntry(je);
jos.write(bytes, 0, bytes.length);
} }
// And those that have moved
for (String newName : movedMap.keySet()) {
String oldName = movedMap.get(newName);
writer.write(MOVE_COMMAND);
writer.write(" ");
writeEscapedString(writer, oldName);
writer.write(" ");
writeEscapedString(writer, newName);
writer.write("\r\n");
}
jos.putNextEntry(new JarEntry(INDEX_NAME));
byte[] bytes = writer.toString().getBytes("UTF-8");
jos.write(bytes, 0, bytes.length);
} }
private static void writeEscapedString (Writer writer, String string) private static void writeEscapedString (Writer writer, String string)
@@ -284,21 +264,12 @@ public class JarDiff implements JarDiffCodes
throws IOException throws IOException
{ {
try (InputStream data = file.getJarFile().getInputStream(entry)) { try (InputStream data = file.getJarFile().getInputStream(entry)) {
writeEntry(jos, entry, data); jos.putNextEntry(entry);
} int size = data.read(newBytes);
} while (size != -1) {
jos.write(newBytes, 0, size);
private static void writeEntry (JarOutputStream jos, JarEntry entry, InputStream data) size = data.read(newBytes);
throws IOException }
{
jos.putNextEntry(entry);
// Read the entry
int size = data.read(newBytes);
while (size != -1) {
jos.write(newBytes, 0, size);
size = data.read(newBytes);
} }
} }
@@ -50,8 +50,7 @@ public class JarDiffPatcher implements JarDiffCodes
try (JarFile oldJar = new JarFile(oldFile); try (JarFile oldJar = new JarFile(oldFile);
JarFile jarDiff = new JarFile(diffFile); JarFile jarDiff = new JarFile(diffFile);
FileOutputStream fos = new FileOutputStream(target); JarOutputStream jos = new JarOutputStream(new FileOutputStream(target))) {
JarOutputStream jos = new JarOutputStream(fos)) {
Set<String> ignoreSet = new HashSet<>(); Set<String> ignoreSet = new HashSet<>();
Map<String, String> renameMap = new HashMap<>(); Map<String, String> renameMap = new HashMap<>();
@@ -259,8 +258,7 @@ public class JarDiffPatcher implements JarDiffCodes
return sub; return sub;
} }
protected void writeEntry ( protected void writeEntry (JarOutputStream jos, JarEntry entry, JarFile file)
JarOutputStream jos, JarEntry entry, JarFile file)
throws IOException throws IOException
{ {
try (InputStream data = file.getInputStream(entry)) { try (InputStream data = file.getInputStream(entry)) {
@@ -268,8 +266,7 @@ public class JarDiffPatcher implements JarDiffCodes
} }
} }
protected void writeEntry ( protected void writeEntry (JarOutputStream jos, JarEntry entry, InputStream data)
JarOutputStream jos, JarEntry entry, InputStream data)
throws IOException throws IOException
{ {
jos.putNextEntry(new JarEntry(entry.getName())); jos.putNextEntry(new JarEntry(entry.getName()));
@@ -5,24 +5,9 @@
package com.threerings.getdown.util; package com.threerings.getdown.util;
import java.io.BufferedOutputStream; import java.io.*;
import java.io.BufferedReader; import java.util.*;
import java.io.File; import java.util.jar.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Deque;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
import java.util.jar.Pack200;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
import com.samskivert.io.StreamUtil; import com.samskivert.io.StreamUtil;
@@ -153,13 +138,13 @@ public class FileUtil
*/ */
public static void unpackPacked200Jar (File packedJar, File target) throws IOException public static void unpackPacked200Jar (File packedJar, File target) throws IOException
{ {
try (InputStream packedJarIn = new FileInputStream(packedJar); try (InputStream packJarIn = new FileInputStream(packedJar);
FileOutputStream extractedJarFileOut = new FileOutputStream(target); JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(target))) {
JarOutputStream jarOutputStream = new JarOutputStream(extractedJarFileOut)) { boolean gz = (packedJar.getName().endsWith(".gz") ||
boolean gz = (packedJar.getName().endsWith(".gz") || packedJar.getName().endsWith(".gz_new")); packedJar.getName().endsWith(".gz_new"));
try (InputStream packedJarIn2 = (gz ? new GZIPInputStream(packedJarIn) : packedJarIn)) { try (InputStream packJarIn2 = (gz ? new GZIPInputStream(packJarIn) : packJarIn)) {
Pack200.Unpacker unpacker = Pack200.newUnpacker(); Pack200.Unpacker unpacker = Pack200.newUnpacker();
unpacker.unpack(packedJarIn2, jarOutputStream); unpacker.unpack(packJarIn2, jarOut);
} }
} }
} }
@@ -51,8 +51,7 @@ public class LaunchUtil
{ {
// create the file that instructs Getdown to upgrade // create the file that instructs Getdown to upgrade
File vfile = new File(appdir, "version.txt"); File vfile = new File(appdir, "version.txt");
try (FileOutputStream fos = new FileOutputStream(vfile); try (PrintStream ps = new PrintStream(new FileOutputStream(vfile))) {
PrintStream ps = new PrintStream(fos)) {
ps.println(newVersion); ps.println(newVersion);
} }
@@ -32,9 +32,8 @@ public class VersionUtil
public static long readVersion (File vfile) public static long readVersion (File vfile)
{ {
long fileVersion = -1; long fileVersion = -1;
try (FileInputStream fis = new FileInputStream(vfile); try (BufferedReader bin =
InputStreamReader reader = new InputStreamReader(fis); new BufferedReader(new InputStreamReader(new FileInputStream(vfile)))) {
BufferedReader bin = new BufferedReader(reader)) {
String vstr = bin.readLine(); String vstr = bin.readLine();
if (!StringUtil.isBlank(vstr)) { if (!StringUtil.isBlank(vstr)) {
fileVersion = Long.parseLong(vstr); fileVersion = Long.parseLong(vstr);
@@ -51,8 +50,7 @@ public class VersionUtil
*/ */
public static void writeVersion (File vfile, long version) throws IOException public static void writeVersion (File vfile, long version) throws IOException
{ {
try (FileOutputStream fos = new FileOutputStream(vfile); try (PrintStream out = new PrintStream(new FileOutputStream(vfile))) {
PrintStream out = new PrintStream(fos)) {
out.println(version); out.println(version);
} catch (Exception e) { } catch (Exception e) {
log.warning("Unable to write version file: " + e.getMessage()); log.warning("Unable to write version file: " + e.getMessage());
@@ -83,9 +81,7 @@ public class VersionUtil
*/ */
public static long readReleaseVersion (File relfile, String versRegex) public static long readReleaseVersion (File relfile, String versRegex)
{ {
try (FileReader reader = new FileReader(relfile); try (BufferedReader in = new BufferedReader(new FileReader(relfile))) {
BufferedReader in = new BufferedReader(reader)) {
String line = null, relvers = null; String line = null, relvers = null;
while ((line = in.readLine()) != null) { while ((line = in.readLine()) != null) {
if (line.startsWith("JAVA_VERSION=")) { if (line.startsWith("JAVA_VERSION=")) {