Merge pull request #2 from stephenneal/dev
Clean up; close streams, readers (potential memory leaks)
This commit is contained in:
@@ -102,7 +102,9 @@ public class Digest
|
|||||||
{
|
{
|
||||||
MessageDigest md = getMessageDigest();
|
MessageDigest md = getMessageDigest();
|
||||||
StringBuilder data = new StringBuilder();
|
StringBuilder data = new StringBuilder();
|
||||||
PrintWriter pout = new PrintWriter(
|
PrintWriter pout = null;
|
||||||
|
try {
|
||||||
|
pout = new PrintWriter(
|
||||||
new OutputStreamWriter(new FileOutputStream(output), "UTF-8"));
|
new OutputStreamWriter(new FileOutputStream(output), "UTF-8"));
|
||||||
|
|
||||||
// compute and append the MD5 digest of each resource in the list
|
// compute and append the MD5 digest of each resource in the list
|
||||||
@@ -123,8 +125,12 @@ public class Digest
|
|||||||
byte[] contents = data.toString().getBytes("UTF-8");
|
byte[] contents = data.toString().getBytes("UTF-8");
|
||||||
pout.println(DIGEST_FILE + " = " + StringUtil.hexlate(md.digest(contents)));
|
pout.println(DIGEST_FILE + " = " + StringUtil.hexlate(md.digest(contents)));
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
if (pout != null) {
|
||||||
pout.close();
|
pout.close();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Obtains an appropriate message digest instance for use by the Getdown system.
|
* Obtains an appropriate message digest instance for use by the Getdown system.
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ import java.util.List;
|
|||||||
|
|
||||||
import org.apache.commons.codec.binary.Base64;
|
import org.apache.commons.codec.binary.Base64;
|
||||||
|
|
||||||
|
import com.samskivert.io.StreamUtil;
|
||||||
import com.threerings.getdown.data.Application;
|
import com.threerings.getdown.data.Application;
|
||||||
import com.threerings.getdown.data.Digest;
|
import com.threerings.getdown.data.Digest;
|
||||||
import com.threerings.getdown.data.Resource;
|
import com.threerings.getdown.data.Resource;
|
||||||
@@ -81,15 +82,19 @@ public class Digester
|
|||||||
File inputFile = new File(appdir, Digest.DIGEST_FILE);
|
File inputFile = new File(appdir, Digest.DIGEST_FILE);
|
||||||
File signatureFile = new File(appdir, Digest.DIGEST_FILE + Application.SIGNATURE_SUFFIX);
|
File signatureFile = new File(appdir, Digest.DIGEST_FILE + Application.SIGNATURE_SUFFIX);
|
||||||
|
|
||||||
|
FileInputStream storeInput = null;
|
||||||
|
FileInputStream dataInput = null;
|
||||||
|
FileOutputStream signatureOutput = null;
|
||||||
|
try {
|
||||||
// initialize the keystore
|
// initialize the keystore
|
||||||
KeyStore store = KeyStore.getInstance("JKS");
|
KeyStore store = KeyStore.getInstance("JKS");
|
||||||
FileInputStream storeInput = new FileInputStream(storePath);
|
storeInput = new FileInputStream(storePath);
|
||||||
store.load(storeInput, storePass.toCharArray());
|
store.load(storeInput, storePass.toCharArray());
|
||||||
PrivateKey key = (PrivateKey)store.getKey(storeAlias, storePass.toCharArray());
|
PrivateKey key = (PrivateKey)store.getKey(storeAlias, storePass.toCharArray());
|
||||||
|
|
||||||
// sign the digest file
|
// sign the digest file
|
||||||
Signature sig = Signature.getInstance("SHA1withRSA");
|
Signature sig = Signature.getInstance("SHA1withRSA");
|
||||||
FileInputStream dataInput = new FileInputStream(inputFile);
|
dataInput = new FileInputStream(inputFile);
|
||||||
byte[] buffer = new byte[8192];
|
byte[] buffer = new byte[8192];
|
||||||
int length;
|
int length;
|
||||||
|
|
||||||
@@ -99,8 +104,14 @@ public class Digester
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Write out the signature
|
// Write out the signature
|
||||||
FileOutputStream signatureOutput = new FileOutputStream(signatureFile);
|
signatureOutput = new FileOutputStream(signatureFile);
|
||||||
String signed = new String(Base64.encodeBase64(sig.sign()));
|
String signed = new String(Base64.encodeBase64(sig.sign()));
|
||||||
signatureOutput.write(signed.getBytes("utf8"));
|
signatureOutput.write(signed.getBytes("utf8"));
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
StreamUtil.close(storeInput);
|
||||||
|
StreamUtil.close(dataInput);
|
||||||
|
StreamUtil.close(signatureOutput);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,25 +5,25 @@
|
|||||||
|
|
||||||
package com.threerings.getdown.tools;
|
package com.threerings.getdown.tools;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.LineNumberReader;
|
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.LineNumberReader;
|
||||||
|
|
||||||
import java.util.Set;
|
import java.util.ArrayList;
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.ArrayList;
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import java.util.jar.JarOutputStream;
|
|
||||||
import java.util.jar.JarFile;
|
|
||||||
import java.util.jar.JarEntry;
|
import java.util.jar.JarEntry;
|
||||||
|
import java.util.jar.JarFile;
|
||||||
|
import java.util.jar.JarOutputStream;
|
||||||
|
|
||||||
import com.threerings.getdown.util.ProgressObserver;
|
import com.threerings.getdown.util.ProgressObserver;
|
||||||
|
|
||||||
@@ -43,15 +43,19 @@ public class JarDiffPatcher implements JarDiffCodes
|
|||||||
*
|
*
|
||||||
* @throws IOException if any problem occurs during patching.
|
* @throws IOException if any problem occurs during patching.
|
||||||
*/
|
*/
|
||||||
public void patchJar (String jarPath, String diffPath, OutputStream target,
|
public void patchJar (String jarPath, String diffPath, File target,
|
||||||
ProgressObserver observer)
|
ProgressObserver observer)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
File oldFile = new File(jarPath);
|
File oldFile = new File(jarPath);
|
||||||
File diffFile = new File(diffPath);
|
File diffFile = new File(diffPath);
|
||||||
JarOutputStream jos = new JarOutputStream(target);
|
JarOutputStream jos = null;
|
||||||
JarFile oldJar = new JarFile(oldFile);
|
JarFile oldJar = null;
|
||||||
JarFile jarDiff = new JarFile(diffFile);
|
JarFile jarDiff = null;
|
||||||
|
try {
|
||||||
|
jos = new JarOutputStream(new FileOutputStream(target));
|
||||||
|
oldJar = new JarFile(oldFile);
|
||||||
|
jarDiff = new JarFile(diffFile);
|
||||||
Set<String> ignoreSet = new HashSet<String>();
|
Set<String> ignoreSet = new HashSet<String>();
|
||||||
|
|
||||||
Map<String, String> renameMap = new HashMap<String, String>();
|
Map<String, String> renameMap = new HashMap<String, String>();
|
||||||
@@ -158,7 +162,11 @@ public class JarDiffPatcher implements JarDiffCodes
|
|||||||
}
|
}
|
||||||
updateObserver(observer, currentEntry, size);
|
updateObserver(observer, currentEntry, size);
|
||||||
|
|
||||||
jos.finish();
|
} finally {
|
||||||
|
jos = closeStream(jos);
|
||||||
|
oldJar = closeFile(oldJar);
|
||||||
|
jarDiff = closeFile(jarDiff);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void updateObserver (ProgressObserver observer,
|
protected void updateObserver (ProgressObserver observer,
|
||||||
@@ -281,6 +289,26 @@ public class JarDiffPatcher implements JarDiffCodes
|
|||||||
data.close();
|
data.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static JarFile closeFile(JarFile jar) {
|
||||||
|
if (jar != null) {
|
||||||
|
try {
|
||||||
|
jar.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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[] newBytes = new byte[DEFAULT_READ_SIZE];
|
||||||
|
|||||||
@@ -149,8 +149,6 @@ public class Patcher
|
|||||||
FileOutputStream fout = null;
|
FileOutputStream fout = null;
|
||||||
try {
|
try {
|
||||||
StreamUtil.copy(in = file.getInputStream(entry), fout = new FileOutputStream(patch));
|
StreamUtil.copy(in = file.getInputStream(entry), fout = new FileOutputStream(patch));
|
||||||
StreamUtil.close(fout);
|
|
||||||
fout = null;
|
|
||||||
|
|
||||||
// move the current version of the jar to .old
|
// move the current version of the jar to .old
|
||||||
if (!FileUtil.renameTo(target, otarget)) {
|
if (!FileUtil.renameTo(target, otarget)) {
|
||||||
@@ -168,8 +166,7 @@ public class Patcher
|
|||||||
|
|
||||||
// now apply the patch to create the new target file
|
// now apply the patch to create the new target file
|
||||||
patcher = new JarDiffPatcher();
|
patcher = new JarDiffPatcher();
|
||||||
fout = new FileOutputStream(target);
|
patcher.patchJar(otarget.getPath(), patch.getPath(), target, obs);
|
||||||
patcher.patchJar(otarget.getPath(), patch.getPath(), fout, obs);
|
|
||||||
|
|
||||||
} catch (IOException ioe) {
|
} catch (IOException ioe) {
|
||||||
if (patcher == null) {
|
if (patcher == null) {
|
||||||
|
|||||||
@@ -32,11 +32,10 @@ public class VersionUtil
|
|||||||
*/
|
*/
|
||||||
public static long readVersion (File vfile)
|
public static long readVersion (File vfile)
|
||||||
{
|
{
|
||||||
FileInputStream fin = null;
|
|
||||||
long fileVersion = -1;
|
long fileVersion = -1;
|
||||||
|
BufferedReader bin = null;
|
||||||
try {
|
try {
|
||||||
fin = new FileInputStream(vfile);
|
bin = new BufferedReader(new InputStreamReader(new FileInputStream(vfile)));
|
||||||
BufferedReader bin = new BufferedReader(new InputStreamReader(fin));
|
|
||||||
String vstr = bin.readLine();
|
String vstr = bin.readLine();
|
||||||
if (!StringUtil.isBlank(vstr)) {
|
if (!StringUtil.isBlank(vstr)) {
|
||||||
fileVersion = Long.parseLong(vstr);
|
fileVersion = Long.parseLong(vstr);
|
||||||
@@ -44,7 +43,7 @@ public class VersionUtil
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.info("Unable to read version file: " + e.getMessage());
|
log.info("Unable to read version file: " + e.getMessage());
|
||||||
} finally {
|
} finally {
|
||||||
StreamUtil.close(fin);
|
StreamUtil.close(bin);
|
||||||
}
|
}
|
||||||
|
|
||||||
return fileVersion;
|
return fileVersion;
|
||||||
@@ -89,15 +88,15 @@ public class VersionUtil
|
|||||||
*/
|
*/
|
||||||
public static long readReleaseVersion (File relfile, String versRegex)
|
public static long readReleaseVersion (File relfile, String versRegex)
|
||||||
{
|
{
|
||||||
|
BufferedReader in = null;
|
||||||
try {
|
try {
|
||||||
BufferedReader in = new BufferedReader(new FileReader(relfile));
|
in = new BufferedReader(new FileReader(relfile));
|
||||||
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=")) {
|
||||||
relvers = line.substring("JAVA_VERSION=".length()).replace('"', ' ').trim();
|
relvers = line.substring("JAVA_VERSION=".length()).replace('"', ' ').trim();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
in.close();
|
|
||||||
|
|
||||||
if (relvers == null) {
|
if (relvers == null) {
|
||||||
log.warning("No JAVA_VERSION line in 'release' file", "file", relfile);
|
log.warning("No JAVA_VERSION line in 'release' file", "file", relfile);
|
||||||
@@ -108,6 +107,8 @@ public class VersionUtil
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.warning("Failed to read version from 'release' file", "file", relfile, e);
|
log.warning("Failed to read version from 'release' file", "file", relfile, e);
|
||||||
return 0L;
|
return 0L;
|
||||||
|
} finally {
|
||||||
|
StreamUtil.close(in);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user