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;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.net.URL;
import java.security.MessageDigest;
import java.util.Collections;
import java.util.Comparator;
import java.util.EnumSet;
import java.util.List;
import java.util.Locale;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
@@ -75,7 +68,8 @@ public class Resource implements Comparable<Resource>
if (isPacked200Jar){
tmpJarFile = new File(target.getPath() + ".tmp");
tmpJarFile.deleteOnExit();
zip = FileUtil.unpackPacked200Jar(target, tmpJarFile);
FileUtil.unpackPacked200Jar(target, tmpJarFile);
zip = new ZipFile(tmpJarFile);
} else{
zip = new ZipFile(target);
}
@@ -107,7 +101,7 @@ public class Resource implements Comparable<Resource>
try {
zip.close();
} 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) {
@@ -128,6 +122,34 @@ public class Resource implements Comparable<Resource>
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.
*/
@@ -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 URL _remote;
protected File _local, _localNew, _marker, _unpacked;
@@ -5,12 +5,7 @@
package com.threerings.getdown.tools;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Enumeration;
@@ -190,8 +185,7 @@ public class Differ
return temp;
}
protected void jarDiff (File ofile, File nfile, ZipOutputStream jout)
throws IOException
protected void jarDiff (File ofile, File nfile, ZipOutputStream jout) throws IOException
{
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)) {
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;
/**
* JarDiff is able to create a jar file containing the delta between two jar files (old and new).
* The delta jar file can then be applied to the old jar file to reconstruct the new jar file.
* JarDiff is able to create a zip file containing the delta between two jar or zip files (old
* 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.
*
@@ -79,8 +80,8 @@ public class JarDiff implements JarDiffCodes
public static void createPatch (String oldPath, String newPath,
OutputStream os, boolean minimal) throws IOException
{
try (JarFile2 oldJar = new JarFile2(oldPath);
JarFile2 newJar = new JarFile2(newPath)) {
try (ZipFile2 oldArchive = new ZipFile2(oldPath);
ZipFile2 newArchive = new ZipFile2(newPath)) {
HashMap<String,String> moved = new HashMap<>();
HashSet<String> implicit = new HashSet<>();
@@ -88,17 +89,15 @@ public class JarDiff implements JarDiffCodes
HashSet<String> newEntries = new HashSet<>();
// FIRST PASS
// Go through the entries in new jar and
// determine which files are candidates for implicit moves
// ( files that has the same filename and same content in old.jar
// and new.jar )
// and for files that cannot be implicitly moved, we will either
// find out whether it is moved or new (modified)
for (ZipEntry newEntry : newJar) {
// Go through the entries in new archive and determine which files are candidates for
// implicit moves (files that have the same filename and same content in old and new)
// and for files that cannot be implicitly moved, we will either find out whether it is
// moved or new (modified)
for (ZipEntry newEntry : newArchive) {
String newname = newEntry.getName();
// 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) {
// New or modified entry
if (_debug) {
@@ -109,7 +108,7 @@ public class JarDiff implements JarDiffCodes
// Content already exist - need to do a move
// 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 (_debug) {
System.out.println(newname + " added to implicit set!");
@@ -125,12 +124,9 @@ public class JarDiff implements JarDiffCodes
// JarDiffPatcher also.
if (!minimal && (implicit.contains(oldname) ||
moveSrc.contains(oldname) )) {
// generate non-minimal jardiff
// for backward compatibility
if (_debug) {
System.out.println("NEW: "+ newname);
}
newEntries.add(newname);
@@ -162,7 +158,7 @@ public class JarDiff implements JarDiffCodes
// SECOND PASS: <deleted files> = <oldjarnames> - <implicitmoves> -
// <source of move commands> - <new or modified entries>
ArrayList<String> deleted = new ArrayList<>();
for (ZipEntry oldEntry : oldJar) {
for (ZipEntry oldEntry : oldArchive) {
String oldName = oldEntry.getName();
if (!implicit.contains(oldName) && !moveSrc.contains(oldName)
&& !newEntries.contains(oldName)) {
@@ -198,7 +194,7 @@ public class JarDiff implements JarDiffCodes
if (_debug) {
System.out.println("New File: " + newName);
}
writeEntry(jos, newJar.getEntryByName(newName), newJar);
writeEntry(jos, newArchive.getEntryByName(newName), newArchive);
}
jos.finish();
@@ -272,10 +268,10 @@ public class JarDiff implements JarDiffCodes
return writer;
}
private static void writeEntry (ZipOutputStream jos, ZipEntry entry, JarFile2 file)
private static void writeEntry (ZipOutputStream jos, ZipEntry entry, ZipFile2 file)
throws IOException
{
try (InputStream data = file.getJarFile().getInputStream(entry)) {
try (InputStream data = file.getArchive().getInputStream(entry)) {
jos.putNextEntry(entry);
int size = data.read(newBytes);
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 HashMap<String,ZipEntry> _nameToEntryMap;
private HashMap<Long,LinkedList<ZipEntry>> _crcToEntryMap;
public JarFile2 (String path) throws IOException {
_jar = new ZipFile(new File(path));
public ZipFile2 (String path) throws IOException {
_archive = new ZipFile(new File(path));
index();
}
public ZipFile getJarFile () {
return _jar;
public ZipFile getArchive () {
return _archive;
}
// from interface Iterable<ZipEntry>
@@ -358,7 +354,7 @@ public class JarDiff implements JarDiffCodes
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
if (contains(file, entry)) {
return (entry.getName());
@@ -368,10 +364,10 @@ public class JarDiff implements JarDiffCodes
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());
// 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)
return false;
@@ -380,16 +376,16 @@ public class JarDiff implements JarDiffCodes
return false;
// Check contents - if no match - return false
try (InputStream oldIS = getJarFile().getInputStream(thisEntry);
InputStream newIS = f.getJarFile().getInputStream(e)) {
try (InputStream oldIS = getArchive().getInputStream(thisEntry);
InputStream newIS = f.getArchive().getInputStream(e)) {
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;
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)) {
// get the Linked List with files with the crc
LinkedList<ZipEntry> ll = _crcToEntryMap.get(crcL);
@@ -398,8 +394,8 @@ public class JarDiff implements JarDiffCodes
while (li.hasNext()) {
ZipEntry thisEntry = li.next();
// check for content match
try (InputStream oldIS = getJarFile().getInputStream(thisEntry);
InputStream newIS = file.getJarFile().getInputStream(entry)) {
try (InputStream oldIS = getArchive().getInputStream(thisEntry);
InputStream newIS = file.getArchive().getInputStream(entry)) {
if (!differs(oldIS, newIS)) {
thisName = thisEntry.getName();
return thisName;
@@ -411,13 +407,13 @@ public class JarDiff implements JarDiffCodes
}
private void index () throws IOException {
Enumeration<? extends ZipEntry> entries = _jar.entries();
Enumeration<? extends ZipEntry> entries = _archive.entries();
_nameToEntryMap = new HashMap<>();
_crcToEntryMap = new HashMap<>();
_entries = new ArrayList<>();
if (_debug) {
System.out.println("indexing: " + _jar.getName());
System.out.println("indexing: " + _archive.getName());
}
if (entries != null) {
while (entries.hasMoreElements()) {
@@ -450,7 +446,7 @@ public class JarDiff implements JarDiffCodes
@Override
public void close() throws IOException {
_jar.close();
_archive.close();
}
}
}
@@ -121,8 +121,7 @@ public class Patcher
}
}
protected void patchFile (ZipFile file, ZipEntry entry,
File appdir, String path)
protected void patchFile (ZipFile file, ZipEntry entry, File appdir, String path)
{
File target = new File(appdir, path);
File patch = new File(appdir, entry.getName());
@@ -5,26 +5,10 @@
package com.threerings.getdown.util;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
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.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 java.io.*;
import java.util.*;
import java.util.jar.*;
import java.util.zip.*;
import com.threerings.getdown.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
* 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) {
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
* packedJar} has a {@code .gz} extension, it will be gunzipped first.
* Unpacks a pack200 packed jar file from {@code packedJar} into {@code target}.
* 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);
JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(target))) {
@@ -195,7 +180,6 @@ public final class FileUtil
unpacker.unpack(packJarIn2, jarOut);
}
}
return new JarFile(target);
}
/**
@@ -11,14 +11,11 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.concurrent.TimeUnit;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.*;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
import static org.junit.Assume.assumeTrue;
/**
@@ -29,10 +26,7 @@ public class GarbageCollectorTest
{
@Parameterized.Parameters(name = "{0}")
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{ ".jar" },
{ ".zip" }
});
return Arrays.asList(new Object[][] {{ ".jar" }, { ".zip" }});
}
@Parameterized.Parameter
@@ -41,7 +35,8 @@ public class GarbageCollectorTest
@Before public void setupFiles () throws IOException
{
_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 ()
@@ -11,14 +11,11 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.concurrent.TimeUnit;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.*;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
/**
* Asserts the correct functionality of the {@link ResourceCache}.
@@ -28,10 +25,7 @@ public class ResourceCacheTest
{
@Parameterized.Parameters(name = "{0}")
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{ ".jar" },
{ ".zip" }
});
return Arrays.asList(new Object[][] {{ ".jar" }, { ".zip" }});
}
@Parameterized.Parameter