Various formatting & style blah blah.

This commit is contained in:
Michael Bayne
2019-05-09 10:34:22 -07:00
parent 4065acd77e
commit dca5d69f9f
7 changed files with 93 additions and 128 deletions
@@ -5,17 +5,10 @@
package com.threerings.getdown.data; package com.threerings.getdown.data;
import java.io.File; import java.io.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL; import java.net.URL;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.util.Collections; import java.util.*;
import java.util.Comparator;
import java.util.EnumSet;
import java.util.List;
import java.util.Locale;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipFile; import java.util.zip.ZipFile;
@@ -75,7 +68,8 @@ public class Resource implements Comparable<Resource>
if (isPacked200Jar){ if (isPacked200Jar){
tmpJarFile = new File(target.getPath() + ".tmp"); tmpJarFile = new File(target.getPath() + ".tmp");
tmpJarFile.deleteOnExit(); tmpJarFile.deleteOnExit();
zip = FileUtil.unpackPacked200Jar(target, tmpJarFile); FileUtil.unpackPacked200Jar(target, tmpJarFile);
zip = new ZipFile(tmpJarFile);
} else{ } else{
zip = new ZipFile(target); zip = new ZipFile(target);
} }
@@ -107,7 +101,7 @@ public class Resource implements Comparable<Resource>
try { try {
zip.close(); zip.close();
} catch (IOException ioe) { } catch (IOException ioe) {
log.warning("Error closing zip", "path", target, "zip", zip, "error", ioe); log.warning("Error closing", "path", target, "zip", zip, "error", ioe);
} }
} }
if (tmpJarFile != null) { if (tmpJarFile != null) {
@@ -128,6 +122,34 @@ public class Resource implements Comparable<Resource>
return StringUtil.hexlate(md.digest()); return StringUtil.hexlate(md.digest());
} }
/**
* Returns whether {@code file} is a {@code zip} file.
*/
public static boolean isZip (File file)
{
String path = file.getName();
return path.endsWith(".zip") || path.endsWith(".zip_new");
}
/**
* Returns whether {@code file} is a {@code jar} file.
*/
public static boolean isJar (File file)
{
String path = file.getName();
return path.endsWith(".jar") || path.endsWith(".jar_new");
}
/**
* Returns whether {@code file} is a {@code jar.pack} file.
*/
public static boolean isPacked200Jar (File file)
{
String path = file.getName();
return path.endsWith(".jar.pack") || path.endsWith(".jar.pack_new") ||
path.endsWith(".jar.pack.gz") || path.endsWith(".jar.pack.gz_new");
}
/** /**
* Creates a resource with the supplied remote URL and local path. * Creates a resource with the supplied remote URL and local path.
*/ */
@@ -367,25 +389,6 @@ public class Resource implements Comparable<Resource>
} }
} }
public static boolean isJar (File file)
{
String path = file.getName();
return path.endsWith(".jar") || path.endsWith(".jar_new");
}
public static boolean isPacked200Jar (File file)
{
String path = file.getName();
return path.endsWith(".jar.pack") || path.endsWith(".jar.pack_new") ||
path.endsWith(".jar.pack.gz")|| path.endsWith(".jar.pack.gz_new");
}
public static boolean isZip (File file)
{
String path = file.getName();
return path.endsWith(".zip") || path.endsWith(".zip_new");
}
protected String _path; protected String _path;
protected URL _remote; protected URL _remote;
protected File _local, _localNew, _marker, _unpacked; protected File _local, _localNew, _marker, _unpacked;
@@ -5,12 +5,7 @@
package com.threerings.getdown.tools; package com.threerings.getdown.tools;
import java.io.BufferedOutputStream; import java.io.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Enumeration; import java.util.Enumeration;
@@ -170,9 +165,9 @@ public class Differ
{ {
File temp = File.createTempFile("differ", "jar"); File temp = File.createTempFile("differ", "jar");
try (ZipFile jar = new ZipFile(target); try (ZipFile jar = new ZipFile(target);
FileOutputStream tempFos = new FileOutputStream(temp); FileOutputStream tempFos = new FileOutputStream(temp);
BufferedOutputStream tempBos = new BufferedOutputStream(tempFos); BufferedOutputStream tempBos = new BufferedOutputStream(tempFos);
ZipOutputStream jout = new ZipOutputStream(tempBos)) { ZipOutputStream jout = new ZipOutputStream(tempBos)) {
byte[] buffer = new byte[4096]; byte[] buffer = new byte[4096];
for (Enumeration<? extends ZipEntry> iter = jar.entries(); iter.hasMoreElements();) { for (Enumeration<? extends ZipEntry> iter = jar.entries(); iter.hasMoreElements();) {
ZipEntry entry = iter.nextElement(); ZipEntry entry = iter.nextElement();
@@ -190,8 +185,7 @@ public class Differ
return temp; return temp;
} }
protected void jarDiff (File ofile, File nfile, ZipOutputStream jout) protected void jarDiff (File ofile, File nfile, ZipOutputStream jout) throws IOException
throws IOException
{ {
JarDiff.createPatch(ofile.getPath(), nfile.getPath(), jout, false); JarDiff.createPatch(ofile.getPath(), nfile.getPath(), jout, false);
} }
@@ -219,10 +213,10 @@ public class Differ
} }
} }
protected static void pipe (File file, ZipOutputStream jout) throws IOException protected static void pipe (File file, OutputStream out) throws IOException
{ {
try (FileInputStream fin = new FileInputStream(file)) { try (FileInputStream fin = new FileInputStream(file)) {
StreamUtil.copy(fin, jout); StreamUtil.copy(fin, out);
} }
} }
} }
@@ -56,8 +56,9 @@ import java.util.zip.ZipOutputStream;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
/** /**
* JarDiff is able to create a jar file containing the delta between two jar files (old and new). * JarDiff is able to create a zip file containing the delta between two jar or zip files (old
* The delta jar file can then be applied to the old jar file to reconstruct the new jar file. * and new). The delta file can then be applied to the old archive file to reconstruct the new
* archive file.
* *
* <p> Refer to the JNLP spec for details on how this is done. * <p> Refer to the JNLP spec for details on how this is done.
* *
@@ -79,8 +80,8 @@ public class JarDiff implements JarDiffCodes
public static void createPatch (String oldPath, String newPath, public static void createPatch (String oldPath, String newPath,
OutputStream os, boolean minimal) throws IOException OutputStream os, boolean minimal) throws IOException
{ {
try (JarFile2 oldJar = new JarFile2(oldPath); try (ZipFile2 oldArchive = new ZipFile2(oldPath);
JarFile2 newJar = new JarFile2(newPath)) { ZipFile2 newArchive = new ZipFile2(newPath)) {
HashMap<String,String> moved = new HashMap<>(); HashMap<String,String> moved = new HashMap<>();
HashSet<String> implicit = new HashSet<>(); HashSet<String> implicit = new HashSet<>();
@@ -88,17 +89,15 @@ public class JarDiff implements JarDiffCodes
HashSet<String> newEntries = new HashSet<>(); HashSet<String> newEntries = new HashSet<>();
// FIRST PASS // FIRST PASS
// Go through the entries in new jar and // Go through the entries in new archive and determine which files are candidates for
// determine which files are candidates for implicit moves // implicit moves (files that have the same filename and same content in old and new)
// ( files that has the same filename and same content in old.jar // and for files that cannot be implicitly moved, we will either find out whether it is
// and new.jar ) // moved or new (modified)
// and for files that cannot be implicitly moved, we will either for (ZipEntry newEntry : newArchive) {
// find out whether it is moved or new (modified)
for (ZipEntry newEntry : newJar) {
String newname = newEntry.getName(); String newname = newEntry.getName();
// Return best match of contents, will return a name match if possible // Return best match of contents, will return a name match if possible
String oldname = oldJar.getBestMatch(newJar, newEntry); String oldname = oldArchive.getBestMatch(newArchive, newEntry);
if (oldname == null) { if (oldname == null) {
// New or modified entry // New or modified entry
if (_debug) { if (_debug) {
@@ -109,7 +108,7 @@ public class JarDiff implements JarDiffCodes
// Content already exist - need to do a move // Content already exist - need to do a move
// Should do implicit move? Yes, if names are the same, and // Should do implicit move? Yes, if names are the same, and
// no move command already exist from oldJar // no move command already exist from oldArchive
if (oldname.equals(newname) && !moveSrc.contains(oldname)) { if (oldname.equals(newname) && !moveSrc.contains(oldname)) {
if (_debug) { if (_debug) {
System.out.println(newname + " added to implicit set!"); System.out.println(newname + " added to implicit set!");
@@ -125,12 +124,9 @@ public class JarDiff implements JarDiffCodes
// JarDiffPatcher also. // JarDiffPatcher also.
if (!minimal && (implicit.contains(oldname) || if (!minimal && (implicit.contains(oldname) ||
moveSrc.contains(oldname) )) { moveSrc.contains(oldname) )) {
// generate non-minimal jardiff // generate non-minimal jardiff
// for backward compatibility // for backward compatibility
if (_debug) { if (_debug) {
System.out.println("NEW: "+ newname); System.out.println("NEW: "+ newname);
} }
newEntries.add(newname); newEntries.add(newname);
@@ -162,7 +158,7 @@ public class JarDiff implements JarDiffCodes
// SECOND PASS: <deleted files> = <oldjarnames> - <implicitmoves> - // SECOND PASS: <deleted files> = <oldjarnames> - <implicitmoves> -
// <source of move commands> - <new or modified entries> // <source of move commands> - <new or modified entries>
ArrayList<String> deleted = new ArrayList<>(); ArrayList<String> deleted = new ArrayList<>();
for (ZipEntry oldEntry : oldJar) { for (ZipEntry oldEntry : oldArchive) {
String oldName = oldEntry.getName(); String oldName = oldEntry.getName();
if (!implicit.contains(oldName) && !moveSrc.contains(oldName) if (!implicit.contains(oldName) && !moveSrc.contains(oldName)
&& !newEntries.contains(oldName)) { && !newEntries.contains(oldName)) {
@@ -198,7 +194,7 @@ public class JarDiff implements JarDiffCodes
if (_debug) { if (_debug) {
System.out.println("New File: " + newName); System.out.println("New File: " + newName);
} }
writeEntry(jos, newJar.getEntryByName(newName), newJar); writeEntry(jos, newArchive.getEntryByName(newName), newArchive);
} }
jos.finish(); jos.finish();
@@ -272,10 +268,10 @@ public class JarDiff implements JarDiffCodes
return writer; return writer;
} }
private static void writeEntry (ZipOutputStream jos, ZipEntry entry, JarFile2 file) private static void writeEntry (ZipOutputStream jos, ZipEntry entry, ZipFile2 file)
throws IOException throws IOException
{ {
try (InputStream data = file.getJarFile().getInputStream(entry)) { try (InputStream data = file.getArchive().getInputStream(entry)) {
jos.putNextEntry(entry); jos.putNextEntry(entry);
int size = data.read(newBytes); int size = data.read(newBytes);
while (size != -1) { while (size != -1) {
@@ -286,22 +282,22 @@ public class JarDiff implements JarDiffCodes
} }
/** /**
* JarFile2 wraps a ZipFile providing some convenience methods. * ZipFile2 wraps a ZipFile providing some convenience methods.
*/ */
private static class JarFile2 implements Iterable<ZipEntry>, Closeable private static class ZipFile2 implements Iterable<ZipEntry>, Closeable
{ {
private ZipFile _jar; private ZipFile _archive;
private List<ZipEntry> _entries; private List<ZipEntry> _entries;
private HashMap<String,ZipEntry> _nameToEntryMap; private HashMap<String,ZipEntry> _nameToEntryMap;
private HashMap<Long,LinkedList<ZipEntry>> _crcToEntryMap; private HashMap<Long,LinkedList<ZipEntry>> _crcToEntryMap;
public JarFile2 (String path) throws IOException { public ZipFile2 (String path) throws IOException {
_jar = new ZipFile(new File(path)); _archive = new ZipFile(new File(path));
index(); index();
} }
public ZipFile getJarFile () { public ZipFile getArchive () {
return _jar; return _archive;
} }
// from interface Iterable<ZipEntry> // from interface Iterable<ZipEntry>
@@ -358,7 +354,7 @@ public class JarDiff implements JarDiffCodes
return retVal; return retVal;
} }
public String getBestMatch (JarFile2 file, ZipEntry entry) throws IOException { public String getBestMatch (ZipFile2 file, ZipEntry entry) throws IOException {
// check for same name and same content, return name if found // check for same name and same content, return name if found
if (contains(file, entry)) { if (contains(file, entry)) {
return (entry.getName()); return (entry.getName());
@@ -368,10 +364,10 @@ public class JarDiff implements JarDiffCodes
return (hasSameContent(file,entry)); return (hasSameContent(file,entry));
} }
public boolean contains (JarFile2 f, ZipEntry e) throws IOException { public boolean contains (ZipFile2 f, ZipEntry e) throws IOException {
ZipEntry thisEntry = getEntryByName(e.getName()); ZipEntry thisEntry = getEntryByName(e.getName());
// Look up name in 'this' Jar2File - if not exist return false // Look up name in 'this' ZipFile2 - if not exist return false
if (thisEntry == null) if (thisEntry == null)
return false; return false;
@@ -380,16 +376,16 @@ public class JarDiff implements JarDiffCodes
return false; return false;
// Check contents - if no match - return false // Check contents - if no match - return false
try (InputStream oldIS = getJarFile().getInputStream(thisEntry); try (InputStream oldIS = getArchive().getInputStream(thisEntry);
InputStream newIS = f.getJarFile().getInputStream(e)) { InputStream newIS = f.getArchive().getInputStream(e)) {
return !differs(oldIS, newIS); return !differs(oldIS, newIS);
} }
} }
public String hasSameContent (JarFile2 file, ZipEntry entry) throws IOException { public String hasSameContent (ZipFile2 file, ZipEntry entry) throws IOException {
String thisName = null; String thisName = null;
Long crcL = Long.valueOf(entry.getCrc()); Long crcL = Long.valueOf(entry.getCrc());
// check if this jar contains files with the passed in entry's crc // check if this archive contains files with the passed in entry's crc
if (_crcToEntryMap.containsKey(crcL)) { if (_crcToEntryMap.containsKey(crcL)) {
// get the Linked List with files with the crc // get the Linked List with files with the crc
LinkedList<ZipEntry> ll = _crcToEntryMap.get(crcL); LinkedList<ZipEntry> ll = _crcToEntryMap.get(crcL);
@@ -398,8 +394,8 @@ public class JarDiff implements JarDiffCodes
while (li.hasNext()) { while (li.hasNext()) {
ZipEntry thisEntry = li.next(); ZipEntry thisEntry = li.next();
// check for content match // check for content match
try (InputStream oldIS = getJarFile().getInputStream(thisEntry); try (InputStream oldIS = getArchive().getInputStream(thisEntry);
InputStream newIS = file.getJarFile().getInputStream(entry)) { InputStream newIS = file.getArchive().getInputStream(entry)) {
if (!differs(oldIS, newIS)) { if (!differs(oldIS, newIS)) {
thisName = thisEntry.getName(); thisName = thisEntry.getName();
return thisName; return thisName;
@@ -411,13 +407,13 @@ public class JarDiff implements JarDiffCodes
} }
private void index () throws IOException { private void index () throws IOException {
Enumeration<? extends ZipEntry> entries = _jar.entries(); Enumeration<? extends ZipEntry> entries = _archive.entries();
_nameToEntryMap = new HashMap<>(); _nameToEntryMap = new HashMap<>();
_crcToEntryMap = new HashMap<>(); _crcToEntryMap = new HashMap<>();
_entries = new ArrayList<>(); _entries = new ArrayList<>();
if (_debug) { if (_debug) {
System.out.println("indexing: " + _jar.getName()); System.out.println("indexing: " + _archive.getName());
} }
if (entries != null) { if (entries != null) {
while (entries.hasMoreElements()) { while (entries.hasMoreElements()) {
@@ -450,7 +446,7 @@ public class JarDiff implements JarDiffCodes
@Override @Override
public void close() throws IOException { public void close() throws IOException {
_jar.close(); _archive.close();
} }
} }
} }
@@ -121,8 +121,7 @@ public class Patcher
} }
} }
protected void patchFile (ZipFile file, ZipEntry entry, protected void patchFile (ZipFile file, ZipEntry entry, File appdir, String path)
File appdir, String path)
{ {
File target = new File(appdir, path); File target = new File(appdir, path);
File patch = new File(appdir, entry.getName()); File patch = new File(appdir, entry.getName());
@@ -5,26 +5,10 @@
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.util.zip.*;
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.JarFile;
import java.util.jar.JarOutputStream;
import java.util.jar.Pack200;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import com.threerings.getdown.Log; import com.threerings.getdown.Log;
import static com.threerings.getdown.Log.log; import static com.threerings.getdown.Log.log;
@@ -130,7 +114,8 @@ public final class FileUtil
* @param cleanExistingDirs if true, all files in all directories contained in {@code jar} will * @param cleanExistingDirs if true, all files in all directories contained in {@code jar} will
* be deleted prior to unpacking the jar. * be deleted prior to unpacking the jar.
*/ */
public static void unpackJar (ZipFile jar, File target, boolean cleanExistingDirs) throws IOException public static void unpackJar (ZipFile jar, File target, boolean cleanExistingDirs)
throws IOException
{ {
if (cleanExistingDirs) { if (cleanExistingDirs) {
Enumeration<? extends ZipEntry> entries = jar.entries(); Enumeration<? extends ZipEntry> entries = jar.entries();
@@ -181,10 +166,10 @@ public final class FileUtil
} }
/** /**
* Unpacks a pack200 packed jar file from {@code packedJar} into {@code target}. If {@code * Unpacks a pack200 packed jar file from {@code packedJar} into {@code target}.
* packedJar} has a {@code .gz} extension, it will be gunzipped first. * If {@code packedJar} has a {@code .gz} extension, it will be gunzipped first.
*/ */
public static JarFile unpackPacked200Jar (File packedJar, File target) throws IOException public static void unpackPacked200Jar (File packedJar, File target) throws IOException
{ {
try (InputStream packJarIn = new FileInputStream(packedJar); try (InputStream packJarIn = new FileInputStream(packedJar);
JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(target))) { JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(target))) {
@@ -195,7 +180,6 @@ public final class FileUtil
unpacker.unpack(packJarIn2, jarOut); unpacker.unpack(packJarIn2, jarOut);
} }
} }
return new JarFile(target);
} }
/** /**
@@ -11,14 +11,11 @@ import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.junit.Before; import org.junit.*;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder; import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.Parameterized; import org.junit.runners.Parameterized;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.*;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue; import static org.junit.Assume.assumeTrue;
/** /**
@@ -29,10 +26,7 @@ public class GarbageCollectorTest
{ {
@Parameterized.Parameters(name = "{0}") @Parameterized.Parameters(name = "{0}")
public static Collection<Object[]> data() { public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] { return Arrays.asList(new Object[][] {{ ".jar" }, { ".zip" }});
{ ".jar" },
{ ".zip" }
});
} }
@Parameterized.Parameter @Parameterized.Parameter
@@ -41,7 +35,8 @@ public class GarbageCollectorTest
@Before public void setupFiles () throws IOException @Before public void setupFiles () throws IOException
{ {
_cachedFile = _folder.newFile("abc123" + extension); _cachedFile = _folder.newFile("abc123" + extension);
_lastAccessedFile = _folder.newFile("abc123" + extension + ResourceCache.LAST_ACCESSED_FILE_SUFFIX); _lastAccessedFile = _folder.newFile(
"abc123" + extension + ResourceCache.LAST_ACCESSED_FILE_SUFFIX);
} }
@Test public void shouldDeleteCacheEntryIfRetentionPeriodIsReached () @Test public void shouldDeleteCacheEntryIfRetentionPeriodIsReached ()
@@ -11,14 +11,11 @@ import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.junit.Before; import org.junit.*;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder; import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.Parameterized; import org.junit.runners.Parameterized;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.*;
import static org.junit.Assert.assertTrue;
/** /**
* Asserts the correct functionality of the {@link ResourceCache}. * Asserts the correct functionality of the {@link ResourceCache}.
@@ -28,10 +25,7 @@ public class ResourceCacheTest
{ {
@Parameterized.Parameters(name = "{0}") @Parameterized.Parameters(name = "{0}")
public static Collection<Object[]> data() { public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] { return Arrays.asList(new Object[][] {{ ".jar" }, { ".zip" }});
{ ".jar" },
{ ".zip" }
});
} }
@Parameterized.Parameter @Parameterized.Parameter