Merge pull request #119 from sdgx/close-resources
Use try-with-resources
This commit is contained in:
@@ -1241,27 +1241,24 @@ public class Application
|
||||
}
|
||||
|
||||
if (_latest != null) {
|
||||
InputStream in = null;
|
||||
PrintStream out = null;
|
||||
try {
|
||||
in = ConnectionUtil.open(_latest, 0, 0).getInputStream();
|
||||
BufferedReader bin = new BufferedReader(new InputStreamReader(in));
|
||||
try (InputStream in = ConnectionUtil.open(_latest, 0, 0).getInputStream();
|
||||
InputStreamReader reader = new InputStreamReader(in);
|
||||
BufferedReader bin = new BufferedReader(reader)) {
|
||||
for (String[] pair : Config.parsePairs(bin, Config.createOpts(false))) {
|
||||
if (pair[0].equals("version")) {
|
||||
_targetVersion = Math.max(Long.parseLong(pair[1]), _targetVersion);
|
||||
if (fileVersion != -1 && _targetVersion > fileVersion) {
|
||||
// replace the file with the newest version
|
||||
out = new PrintStream(new FileOutputStream(vfile));
|
||||
try (FileOutputStream fos = new FileOutputStream(vfile);
|
||||
PrintStream out = new PrintStream(fos)) {
|
||||
out.println(_targetVersion);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warning("Unable to retrieve version from latest config file.", e);
|
||||
} finally {
|
||||
StreamUtil.close(in);
|
||||
StreamUtil.close(out);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1567,21 +1564,16 @@ public class Application
|
||||
} else {
|
||||
File signatureFile = downloadFile(path + SIGNATURE_SUFFIX);
|
||||
byte[] signature = null;
|
||||
FileReader reader = null;
|
||||
try {
|
||||
reader = new FileReader(signatureFile);
|
||||
try (FileReader reader = new FileReader(signatureFile)) {
|
||||
signature = StreamUtil.toByteArray(new FileInputStream(signatureFile));
|
||||
} finally {
|
||||
StreamUtil.close(reader);
|
||||
FileUtil.deleteHarder(signatureFile); // delete the file regardless
|
||||
}
|
||||
|
||||
byte[] buffer = new byte[8192];
|
||||
int length, validated = 0;
|
||||
for (Certificate cert : _signers) {
|
||||
FileInputStream dataInput = null;
|
||||
try {
|
||||
dataInput = new FileInputStream(target);
|
||||
try (FileInputStream dataInput = new FileInputStream(target)) {
|
||||
Signature sig = Signature.getInstance(Digest.sigAlgorithm(sigVersion));
|
||||
sig.initVerify(cert);
|
||||
while ((length = dataInput.read(buffer)) != -1) {
|
||||
@@ -1602,9 +1594,6 @@ public class Application
|
||||
} catch (GeneralSecurityException gse) {
|
||||
// no problem!
|
||||
|
||||
} finally {
|
||||
StreamUtil.close(dataInput);
|
||||
dataInput = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1645,9 +1634,6 @@ public class Application
|
||||
log.info("Attempting to refetch '" + path + "' from '" + targetURL + "'.");
|
||||
|
||||
// stream the URL into our temporary file
|
||||
InputStream fin = null;
|
||||
FileOutputStream fout = null;
|
||||
try {
|
||||
URLConnection uconn = ConnectionUtil.open(targetURL, 0, 0);
|
||||
// we have to tell Java not to use caches here, otherwise it will cache any request for
|
||||
// same URL for the lifetime of this JVM (based on the URL string, not the URL object);
|
||||
@@ -1656,16 +1642,14 @@ public class Application
|
||||
// to download a file, it expects it to come over the wire, not from a cache
|
||||
uconn.setUseCaches(false);
|
||||
uconn.setRequestProperty("Accept-Encoding", "gzip");
|
||||
fin = uconn.getInputStream();
|
||||
try (InputStream fin = uconn.getInputStream()) {
|
||||
String encoding = uconn.getContentEncoding();
|
||||
if ("gzip".equalsIgnoreCase(encoding)) {
|
||||
fin = new GZIPInputStream(fin);
|
||||
boolean gzip = "gzip".equalsIgnoreCase(encoding);
|
||||
try (InputStream fin2 = (gzip ? new GZIPInputStream(fin) : fin)) {
|
||||
try (FileOutputStream fout = new FileOutputStream(target)) {
|
||||
StreamUtil.copy(fin2, fout);
|
||||
}
|
||||
}
|
||||
fout = new FileOutputStream(target);
|
||||
StreamUtil.copy(fin, fout);
|
||||
} finally {
|
||||
StreamUtil.close(fin);
|
||||
StreamUtil.close(fout);
|
||||
}
|
||||
|
||||
return target;
|
||||
|
||||
@@ -6,12 +6,12 @@
|
||||
package com.threerings.getdown.data;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
import com.samskivert.io.StreamUtil;
|
||||
import com.samskivert.text.MessageUtil;
|
||||
import com.samskivert.util.StringUtil;
|
||||
|
||||
@@ -99,9 +99,9 @@ public class Digest
|
||||
}
|
||||
|
||||
StringBuilder data = new StringBuilder();
|
||||
PrintWriter pout = null;
|
||||
try {
|
||||
pout = new PrintWriter(new OutputStreamWriter(new FileOutputStream(output), "UTF-8"));
|
||||
try (FileOutputStream fos = new FileOutputStream(output);
|
||||
OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
|
||||
PrintWriter pout = new PrintWriter(osw)) {
|
||||
// compute and append the digest of each resource in the list
|
||||
for (Resource rsrc : resources) {
|
||||
String path = rsrc.getPath();
|
||||
@@ -114,9 +114,6 @@ public class Digest
|
||||
byte[] contents = data.toString().getBytes("UTF-8");
|
||||
String filename = digestFile(version);
|
||||
pout.println(filename + " = " + StringUtil.hexlate(md.digest(contents)));
|
||||
|
||||
} finally {
|
||||
StreamUtil.close(pout);
|
||||
}
|
||||
|
||||
long elapsed = System.currentTimeMillis() - start;
|
||||
|
||||
@@ -15,7 +15,6 @@ import java.util.List;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarFile;
|
||||
|
||||
import com.samskivert.io.StreamUtil;
|
||||
import com.samskivert.util.StringUtil;
|
||||
|
||||
import com.threerings.getdown.util.FileUtil;
|
||||
@@ -83,15 +82,12 @@ public class Resource implements Comparable<Resource>
|
||||
}
|
||||
}
|
||||
|
||||
InputStream in = null;
|
||||
try {
|
||||
in = jar.getInputStream(entry);
|
||||
try (InputStream in = jar.getInputStream(entry)) {
|
||||
while ((read = in.read(buffer)) != -1) {
|
||||
md.update(buffer, 0, read);
|
||||
}
|
||||
} finally {
|
||||
StreamUtil.close(in);
|
||||
}
|
||||
|
||||
updateProgress(obs, eidx, entries.size());
|
||||
}
|
||||
|
||||
@@ -110,16 +106,12 @@ public class Resource implements Comparable<Resource>
|
||||
|
||||
} else {
|
||||
long totalSize = target.length(), position = 0L;
|
||||
FileInputStream fin = null;
|
||||
try {
|
||||
fin = new FileInputStream(target);
|
||||
try (FileInputStream fin = new FileInputStream(target)) {
|
||||
while ((read = fin.read(buffer)) != -1) {
|
||||
md.update(buffer, 0, read);
|
||||
position += read;
|
||||
updateProgress(obs, position, totalSize);
|
||||
}
|
||||
} finally {
|
||||
StreamUtil.close(fin);
|
||||
}
|
||||
}
|
||||
return StringUtil.hexlate(md.digest());
|
||||
@@ -282,7 +274,9 @@ public class Resource implements Comparable<Resource>
|
||||
throw new IOException("Requested to unpack non-jar file '" + _local + "'.");
|
||||
}
|
||||
if (_isJar) {
|
||||
FileUtil.unpackJar(new JarFile(_local), _unpacked);
|
||||
try (JarFile jar = new JarFile(_local)) {
|
||||
FileUtil.unpackJar(jar, _unpacked);
|
||||
}
|
||||
} else {
|
||||
FileUtil.unpackPacked200Jar(_local, _unpacked);
|
||||
}
|
||||
|
||||
@@ -229,13 +229,11 @@ public abstract class Getdown extends Thread
|
||||
// if we're provided with valid values, create a proxy.txt file
|
||||
if (!StringUtil.isBlank(host)) {
|
||||
File pfile = _app.getLocalPath("proxy.txt");
|
||||
try {
|
||||
PrintStream pout = new PrintStream(new FileOutputStream(pfile));
|
||||
try (PrintStream pout = new PrintStream(new FileOutputStream(pfile))) {
|
||||
pout.println("host = " + host);
|
||||
if (!StringUtil.isBlank(port)) {
|
||||
pout.println("port = " + port);
|
||||
}
|
||||
pout.close();
|
||||
} catch (IOException ioe) {
|
||||
log.warning("Error creating proxy file '" + pfile + "': " + ioe);
|
||||
}
|
||||
@@ -570,6 +568,7 @@ public abstract class Getdown extends Thread
|
||||
}
|
||||
|
||||
// documentation inherited from interface
|
||||
@Override
|
||||
public void updateStatus (String message)
|
||||
{
|
||||
setStatusAsync(message, -1, -1L, true);
|
||||
@@ -580,6 +579,7 @@ public abstract class Getdown extends Thread
|
||||
* if we can find a localized version by sticking a {@code _<language>} in front of the "." in
|
||||
* the filename.
|
||||
*/
|
||||
@Override
|
||||
public BufferedImage loadImage (String path)
|
||||
{
|
||||
if (StringUtil.isBlank(path)) {
|
||||
|
||||
@@ -92,11 +92,9 @@ public class GetdownApp
|
||||
File crtFile = new File(appDir, Digest.digestFile(Digest.VERSION) + ".crt");
|
||||
List<Certificate> crts = new ArrayList<>();
|
||||
if (crtFile.exists()) {
|
||||
try {
|
||||
FileInputStream fis = new FileInputStream(crtFile);
|
||||
try (FileInputStream fis = new FileInputStream(crtFile)) {
|
||||
X509Certificate certificate = (X509Certificate)
|
||||
CertificateFactory.getInstance("X.509").generateCertificate(fis);
|
||||
fis.close();
|
||||
crts.add(certificate);
|
||||
} catch (Exception e) {
|
||||
log.warning("Certificate error: " + e.getMessage());
|
||||
|
||||
@@ -233,6 +233,7 @@ public class GetdownApplet extends JApplet
|
||||
}
|
||||
|
||||
// implemented from ImageLoader
|
||||
@Override
|
||||
public Image loadImage (String path)
|
||||
{
|
||||
try {
|
||||
@@ -293,10 +294,9 @@ public class GetdownApplet extends JApplet
|
||||
*/
|
||||
protected boolean writeToFile (File tofile, String contents)
|
||||
{
|
||||
try {
|
||||
PrintStream out = new PrintStream(new FileOutputStream(tofile));
|
||||
try (FileOutputStream fos = new FileOutputStream(tofile);
|
||||
PrintStream out = new PrintStream(fos)) {
|
||||
out.println(contents);
|
||||
out.close();
|
||||
return true;
|
||||
} catch (IOException ioe) {
|
||||
log.warning("Failed to create '" + tofile + "'.", ioe);
|
||||
@@ -311,11 +311,8 @@ public class GetdownApplet extends JApplet
|
||||
if (keyUrl == null) {
|
||||
return null;
|
||||
}
|
||||
InputStream is = keyUrl.openStream();
|
||||
try {
|
||||
try (InputStream is = keyUrl.openStream()) {
|
||||
return CertificateFactory.getInstance("X.509").generateCertificate(is);
|
||||
} finally {
|
||||
is.close();
|
||||
}
|
||||
} catch (CertificateException e) {
|
||||
throw new RuntimeException(e);
|
||||
|
||||
@@ -394,10 +394,9 @@ public class GetdownAppletConfig
|
||||
*/
|
||||
protected boolean writeToFile (File tofile, String contents)
|
||||
{
|
||||
try {
|
||||
PrintStream out = new PrintStream(new FileOutputStream(tofile));
|
||||
try (FileOutputStream fos = new FileOutputStream(tofile);
|
||||
PrintStream out = new PrintStream(fos)) {
|
||||
out.println(contents);
|
||||
out.close();
|
||||
return true;
|
||||
} catch (IOException ioe) {
|
||||
log.warning("Failed to create '" + tofile + "'.", ioe);
|
||||
|
||||
@@ -12,10 +12,7 @@ import java.net.HttpURLConnection;
|
||||
import java.net.URLConnection;
|
||||
import java.util.Collection;
|
||||
|
||||
import com.samskivert.io.StreamUtil;
|
||||
|
||||
import com.threerings.getdown.data.Resource;
|
||||
import com.threerings.getdown.data.SysProps;
|
||||
import com.threerings.getdown.util.ConnectionUtil;
|
||||
|
||||
import static com.threerings.getdown.Log.log;
|
||||
@@ -74,19 +71,16 @@ public class HTTPDownloader extends Downloader
|
||||
|
||||
long actualSize = conn.getContentLength();
|
||||
log.info("Downloading resource", "url", rsrc.getRemote(), "size", actualSize);
|
||||
InputStream in = null;
|
||||
FileOutputStream out = null;
|
||||
long currentSize = 0L;
|
||||
try {
|
||||
in = conn.getInputStream();
|
||||
out = new FileOutputStream(rsrc.getLocalNew());
|
||||
int read;
|
||||
try (InputStream in = conn.getInputStream();
|
||||
FileOutputStream out = new FileOutputStream(rsrc.getLocalNew())) {
|
||||
|
||||
// TODO: look to see if we have a download info file
|
||||
// containing info on potentially partially downloaded data;
|
||||
// if so, use a "Range: bytes=HAVE-" header.
|
||||
|
||||
// read in the file data
|
||||
int read;
|
||||
while ((read = in.read(_buffer)) != -1) {
|
||||
// write it out to our local copy
|
||||
out.write(_buffer, 0, read);
|
||||
@@ -100,9 +94,6 @@ public class HTTPDownloader extends Downloader
|
||||
currentSize += read;
|
||||
updateObserver(rsrc, currentSize, actualSize);
|
||||
}
|
||||
} finally {
|
||||
StreamUtil.close(in);
|
||||
StreamUtil.close(out);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,6 @@ public class AppletParamSigner
|
||||
{
|
||||
public static void main (String[] args)
|
||||
{
|
||||
try {
|
||||
if (args.length != 7) {
|
||||
System.err.println("AppletParamSigner keystore storepass alias keypass " +
|
||||
"appbase appname imgpath");
|
||||
@@ -38,9 +37,11 @@ public class AppletParamSigner
|
||||
String imgpath = args[6];
|
||||
String params = appbase + appname + imgpath;
|
||||
|
||||
try (FileInputStream fis = new FileInputStream(keystore);
|
||||
BufferedInputStream bis = new BufferedInputStream(fis)) {
|
||||
|
||||
KeyStore store = KeyStore.getInstance("JKS");
|
||||
store.load(new BufferedInputStream(new FileInputStream(keystore)),
|
||||
storepass.toCharArray());
|
||||
store.load(bis, storepass.toCharArray());
|
||||
PrivateKey key = (PrivateKey)store.getKey(alias, keypass.toCharArray());
|
||||
Signature sig = Signature.getInstance(Digest.sigAlgorithm(Digest.VERSION));
|
||||
sig.initSign(key);
|
||||
|
||||
@@ -96,10 +96,9 @@ public class Differ
|
||||
{
|
||||
int version = Digest.VERSION;
|
||||
MessageDigest md = Digest.getMessageDigest(version);
|
||||
JarOutputStream jout = null;
|
||||
try {
|
||||
jout = new JarOutputStream(
|
||||
new BufferedOutputStream(new FileOutputStream(patch)));
|
||||
try (FileOutputStream fos = new FileOutputStream(patch);
|
||||
BufferedOutputStream buffered = new BufferedOutputStream(fos);
|
||||
JarOutputStream jout = new JarOutputStream(buffered)) {
|
||||
|
||||
// for each file in the new application, it either already exists
|
||||
// in the old application, or it is new
|
||||
@@ -160,11 +159,9 @@ public class Differ
|
||||
jout.putNextEntry(new ZipEntry(rsrc.getPath() + Patcher.DELETE));
|
||||
}
|
||||
|
||||
StreamUtil.close(jout);
|
||||
System.out.println("Created patch file: " + patch);
|
||||
|
||||
} catch (IOException ioe) {
|
||||
StreamUtil.close(jout);
|
||||
patch.delete();
|
||||
throw ioe;
|
||||
}
|
||||
@@ -173,25 +170,25 @@ public class Differ
|
||||
protected File rebuildJar (File target)
|
||||
throws IOException
|
||||
{
|
||||
JarFile jar = new JarFile(target);
|
||||
File temp = File.createTempFile("differ", "jar");
|
||||
JarOutputStream jout = new JarOutputStream(
|
||||
new BufferedOutputStream(new FileOutputStream(temp)));
|
||||
try (JarFile jar = new JarFile(target);
|
||||
FileOutputStream tempFos = new FileOutputStream(temp);
|
||||
BufferedOutputStream tempBos = new BufferedOutputStream(tempFos);
|
||||
JarOutputStream jout = new JarOutputStream(tempBos)) {
|
||||
byte[] buffer = new byte[4096];
|
||||
for (Enumeration< JarEntry > iter = jar.entries(); iter.hasMoreElements();) {
|
||||
JarEntry entry = iter.nextElement();
|
||||
entry.setCompressedSize(-1);
|
||||
jout.putNextEntry(entry);
|
||||
InputStream in = jar.getInputStream(entry);
|
||||
try (InputStream in = jar.getInputStream(entry)) {
|
||||
int size = in.read(buffer);
|
||||
while (size != -1) {
|
||||
jout.write(buffer, 0, size);
|
||||
size = in.read(buffer);
|
||||
}
|
||||
in.close();
|
||||
}
|
||||
jout.close();
|
||||
jar.close();
|
||||
}
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
|
||||
@@ -227,11 +224,8 @@ public class Differ
|
||||
protected static void pipe (File file, JarOutputStream jout)
|
||||
throws IOException
|
||||
{
|
||||
FileInputStream fin = null;
|
||||
try {
|
||||
StreamUtil.copy(fin = new FileInputStream(file), jout);
|
||||
} finally {
|
||||
StreamUtil.close(fin);
|
||||
try (FileInputStream fin = new FileInputStream(file)) {
|
||||
StreamUtil.copy(fin, jout);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@ import java.security.Signature;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.samskivert.io.StreamUtil;
|
||||
import com.threerings.getdown.data.Application;
|
||||
import com.threerings.getdown.data.Digest;
|
||||
import com.threerings.getdown.data.Resource;
|
||||
@@ -99,19 +98,18 @@ public class Digester
|
||||
File inputFile = new File(appdir, filename);
|
||||
File signatureFile = new File(appdir, filename + Application.SIGNATURE_SUFFIX);
|
||||
|
||||
FileInputStream storeInput = null, dataInput = null;
|
||||
FileOutputStream signatureOutput = null;
|
||||
try {
|
||||
try (FileInputStream storeInput = new FileInputStream(storePath);
|
||||
FileInputStream dataInput = new FileInputStream(inputFile);
|
||||
FileOutputStream signatureOutput = new FileOutputStream(signatureFile)) {
|
||||
|
||||
// initialize the keystore
|
||||
KeyStore store = KeyStore.getInstance("JKS");
|
||||
storeInput = new FileInputStream(storePath);
|
||||
store.load(storeInput, storePass.toCharArray());
|
||||
PrivateKey key = (PrivateKey)store.getKey(storeAlias, storePass.toCharArray());
|
||||
|
||||
// sign the digest file
|
||||
String algo = Digest.sigAlgorithm(version);
|
||||
Signature sig = Signature.getInstance(algo);
|
||||
dataInput = new FileInputStream(inputFile);
|
||||
byte[] buffer = new byte[8192];
|
||||
int length;
|
||||
|
||||
@@ -121,14 +119,8 @@ public class Digester
|
||||
}
|
||||
|
||||
// Write out the signature
|
||||
signatureOutput = new FileOutputStream(signatureFile);
|
||||
String signed = Base64.encodeToString(sig.sign(), Base64.DEFAULT);
|
||||
signatureOutput.write(signed.getBytes("utf8"));
|
||||
|
||||
} finally {
|
||||
StreamUtil.close(signatureOutput);
|
||||
StreamUtil.close(dataInput);
|
||||
StreamUtil.close(storeInput);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,9 +41,25 @@
|
||||
|
||||
package com.threerings.getdown.tools;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
import java.util.jar.*;
|
||||
import java.io.Closeable;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
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).
|
||||
@@ -69,10 +85,9 @@ public class JarDiff implements JarDiffCodes
|
||||
public static void createPatch (String oldPath, String newPath,
|
||||
OutputStream os, boolean minimal) throws IOException
|
||||
{
|
||||
JarFile2 oldJar = new JarFile2(oldPath);
|
||||
JarFile2 newJar = new JarFile2(newPath);
|
||||
try (JarFile2 oldJar = new JarFile2(oldPath);
|
||||
JarFile2 newJar = new JarFile2(newPath)) {
|
||||
|
||||
try {
|
||||
HashMap<String,String> moved = new HashMap<>();
|
||||
HashSet<String> implicit = new HashSet<>();
|
||||
HashSet<String> moveSrc = new HashSet<>();
|
||||
@@ -194,21 +209,7 @@ public class JarDiff implements JarDiffCodes
|
||||
|
||||
jos.finish();
|
||||
// jos.close();
|
||||
|
||||
} catch (IOException ioE){
|
||||
throw ioE;
|
||||
} finally {
|
||||
try {
|
||||
oldJar.getJarFile().close();
|
||||
} catch (IOException e1) {
|
||||
//ignore
|
||||
}
|
||||
try {
|
||||
newJar.getJarFile().close();
|
||||
} catch (IOException e1) {
|
||||
//ignore
|
||||
}
|
||||
} // finally
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -220,7 +221,7 @@ public class JarDiff implements JarDiffCodes
|
||||
Map<String,String> movedMap)
|
||||
throws IOException
|
||||
{
|
||||
StringWriter writer = new StringWriter();
|
||||
try (StringWriter writer = new StringWriter()) {
|
||||
|
||||
writer.write(VERSION_HEADER);
|
||||
writer.write("\r\n");
|
||||
@@ -247,10 +248,10 @@ public class JarDiff implements JarDiffCodes
|
||||
JarEntry je = new JarEntry(INDEX_NAME);
|
||||
byte[] bytes = writer.toString().getBytes("UTF-8");
|
||||
|
||||
writer.close();
|
||||
jos.putNextEntry(je);
|
||||
jos.write(bytes, 0, bytes.length);
|
||||
}
|
||||
}
|
||||
|
||||
private static void writeEscapedString (Writer writer, String string)
|
||||
throws IOException
|
||||
@@ -282,7 +283,9 @@ public class JarDiff implements JarDiffCodes
|
||||
private static void writeEntry (JarOutputStream jos, JarEntry entry, JarFile2 file)
|
||||
throws IOException
|
||||
{
|
||||
writeEntry(jos, entry, file.getJarFile().getInputStream(entry));
|
||||
try (InputStream data = file.getJarFile().getInputStream(entry)) {
|
||||
writeEntry(jos, entry, data);
|
||||
}
|
||||
}
|
||||
|
||||
private static void writeEntry (JarOutputStream jos, JarEntry entry, InputStream data)
|
||||
@@ -290,7 +293,6 @@ public class JarDiff implements JarDiffCodes
|
||||
{
|
||||
jos.putNextEntry(entry);
|
||||
|
||||
try {
|
||||
// Read the entry
|
||||
int size = data.read(newBytes);
|
||||
|
||||
@@ -298,22 +300,12 @@ public class JarDiff implements JarDiffCodes
|
||||
jos.write(newBytes, 0, size);
|
||||
size = data.read(newBytes);
|
||||
}
|
||||
} catch(IOException ioE) {
|
||||
throw ioE;
|
||||
} finally {
|
||||
try {
|
||||
data.close();
|
||||
} catch(IOException e){
|
||||
//Ignore
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* JarFile2 wraps a JarFile providing some convenience methods.
|
||||
*/
|
||||
private static class JarFile2 implements Iterable<JarEntry>
|
||||
private static class JarFile2 implements Iterable<JarEntry>, Closeable
|
||||
{
|
||||
private JarFile _jar;
|
||||
private List<JarEntry> _entries;
|
||||
@@ -330,6 +322,7 @@ public class JarDiff implements JarDiffCodes
|
||||
}
|
||||
|
||||
// from interface Iterable<JarEntry>
|
||||
@Override
|
||||
public Iterator<JarEntry> iterator () {
|
||||
return _entries.iterator();
|
||||
}
|
||||
@@ -347,7 +340,6 @@ public class JarDiff implements JarDiffCodes
|
||||
int total = 0;
|
||||
boolean retVal = false;
|
||||
|
||||
try{
|
||||
while (newSize != -1) {
|
||||
newSize = newIS.read(newBytes);
|
||||
oldSize = oldIS.read(oldBytes);
|
||||
@@ -379,20 +371,7 @@ public class JarDiff implements JarDiffCodes
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch(IOException ioE){
|
||||
throw ioE;
|
||||
} finally {
|
||||
try {
|
||||
oldIS.close();
|
||||
} catch(IOException e){
|
||||
//Ignore
|
||||
}
|
||||
try {
|
||||
newIS.close();
|
||||
} catch(IOException e){
|
||||
//Ignore
|
||||
}
|
||||
}
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
@@ -419,11 +398,10 @@ public class JarDiff implements JarDiffCodes
|
||||
return false;
|
||||
|
||||
// Check contents - if no match - return false
|
||||
InputStream oldIS = getJarFile().getInputStream(thisEntry);
|
||||
InputStream newIS = f.getJarFile().getInputStream(e);
|
||||
boolean retValue = differs(oldIS, newIS);
|
||||
|
||||
return !retValue;
|
||||
try (InputStream oldIS = getJarFile().getInputStream(thisEntry);
|
||||
InputStream newIS = f.getJarFile().getInputStream(e)) {
|
||||
return !differs(oldIS, newIS);
|
||||
}
|
||||
}
|
||||
|
||||
public String hasSameContent (JarFile2 file, JarEntry entry) throws IOException {
|
||||
@@ -438,14 +416,15 @@ public class JarDiff implements JarDiffCodes
|
||||
while (li.hasNext()) {
|
||||
JarEntry thisEntry = li.next();
|
||||
// check for content match
|
||||
InputStream oldIS = getJarFile().getInputStream(thisEntry);
|
||||
InputStream newIS = file.getJarFile().getInputStream(entry);
|
||||
try (InputStream oldIS = getJarFile().getInputStream(thisEntry);
|
||||
InputStream newIS = file.getJarFile().getInputStream(entry)) {
|
||||
if (!differs(oldIS, newIS)) {
|
||||
thisName = thisEntry.getName();
|
||||
return thisName;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return thisName;
|
||||
}
|
||||
|
||||
@@ -486,5 +465,10 @@ public class JarDiff implements JarDiffCodes
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
_jar.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,7 +25,6 @@ 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;
|
||||
|
||||
/**
|
||||
@@ -48,14 +47,13 @@ public class JarDiffPatcher implements JarDiffCodes
|
||||
throws IOException
|
||||
{
|
||||
File oldFile = new File(jarPath), diffFile = new File(diffPath);
|
||||
JarOutputStream jos = null;
|
||||
JarFile oldJar = null, jarDiff = null;
|
||||
try {
|
||||
jos = new JarOutputStream(new FileOutputStream(target));
|
||||
oldJar = new JarFile(oldFile);
|
||||
jarDiff = new JarFile(diffFile);
|
||||
Set<String> ignoreSet = new HashSet<>();
|
||||
|
||||
try (JarFile oldJar = new JarFile(oldFile);
|
||||
JarFile jarDiff = new JarFile(diffFile);
|
||||
FileOutputStream fos = new FileOutputStream(target);
|
||||
JarOutputStream jos = new JarOutputStream(fos)) {
|
||||
|
||||
Set<String> ignoreSet = new HashSet<>();
|
||||
Map<String, String> renameMap = new HashMap<>();
|
||||
determineNameMapping(jarDiff, ignoreSet, renameMap);
|
||||
|
||||
@@ -135,7 +133,9 @@ public class JarDiffPatcher implements JarDiffCodes
|
||||
updateObserver(observer, currentEntry, size);
|
||||
currentEntry++;
|
||||
|
||||
writeEntry(jos, newEntry, oldJar.getInputStream(oldEntry));
|
||||
try (InputStream data = oldJar.getInputStream(oldEntry)) {
|
||||
writeEntry(jos, newEntry, data);
|
||||
}
|
||||
|
||||
// Remove entry from oldjarNames since no implicit move is needed
|
||||
boolean wasInOld = oldjarNames.remove(oldName);
|
||||
@@ -159,11 +159,6 @@ public class JarDiffPatcher implements JarDiffCodes
|
||||
}
|
||||
}
|
||||
updateObserver(observer, currentEntry, size);
|
||||
|
||||
} finally {
|
||||
StreamUtil.close(jos);
|
||||
closeFile(oldJar);
|
||||
closeFile(jarDiff);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -268,7 +263,9 @@ public class JarDiffPatcher implements JarDiffCodes
|
||||
JarOutputStream jos, JarEntry entry, JarFile file)
|
||||
throws IOException
|
||||
{
|
||||
writeEntry(jos, entry, file.getInputStream(entry));
|
||||
try (InputStream data = file.getInputStream(entry)) {
|
||||
writeEntry(jos, entry, data);
|
||||
}
|
||||
}
|
||||
|
||||
protected void writeEntry (
|
||||
@@ -283,17 +280,6 @@ public class JarDiffPatcher implements JarDiffCodes
|
||||
jos.write(newBytes, 0, size);
|
||||
size = data.read(newBytes);
|
||||
}
|
||||
data.close();
|
||||
}
|
||||
|
||||
private static void closeFile (JarFile jar) {
|
||||
if (jar != null) {
|
||||
try {
|
||||
jar.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace(System.err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected static final int DEFAULT_READ_SIZE = 2048;
|
||||
|
||||
@@ -56,7 +56,7 @@ public class Patcher
|
||||
_obs = obs;
|
||||
_plength = patch.length();
|
||||
|
||||
JarFile file = new JarFile(patch);
|
||||
try (JarFile file = new JarFile(patch)) {
|
||||
Enumeration<JarEntry> entries = file.entries(); // old skool!
|
||||
while (entries.hasMoreElements()) {
|
||||
JarEntry entry = entries.nextElement();
|
||||
@@ -89,7 +89,7 @@ public class Patcher
|
||||
// note that we've completed this entry
|
||||
_complete += elength;
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
|
||||
protected String strip (String path, String suffix)
|
||||
@@ -110,11 +110,9 @@ public class Patcher
|
||||
log.warning("Failed to create parent for '" + target + "'.");
|
||||
}
|
||||
|
||||
InputStream in = null;
|
||||
FileOutputStream fout = null;
|
||||
try {
|
||||
in = file.getInputStream(entry);
|
||||
fout = new FileOutputStream(target);
|
||||
try (InputStream in = file.getInputStream(entry);
|
||||
FileOutputStream fout = new FileOutputStream(target)) {
|
||||
|
||||
int total = 0, read;
|
||||
while ((read = in.read(_buffer)) != -1) {
|
||||
total += read;
|
||||
@@ -124,10 +122,6 @@ public class Patcher
|
||||
|
||||
} catch (IOException ioe) {
|
||||
System.err.println("Error creating '" + target + "': " + ioe);
|
||||
|
||||
} finally {
|
||||
StreamUtil.close(in);
|
||||
StreamUtil.close(fout);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -143,12 +137,11 @@ public class Patcher
|
||||
otarget.delete();
|
||||
|
||||
// pipe the contents of the patch into a file
|
||||
InputStream in = null;
|
||||
FileOutputStream fout = null;
|
||||
try {
|
||||
StreamUtil.copy(in = file.getInputStream(entry), fout = new FileOutputStream(patch));
|
||||
try (InputStream in = file.getInputStream(entry);
|
||||
FileOutputStream fout = new FileOutputStream(patch)) {
|
||||
|
||||
StreamUtil.copy(in, fout);
|
||||
StreamUtil.close(fout);
|
||||
fout = null;
|
||||
|
||||
// move the current version of the jar to .old
|
||||
if (!FileUtil.renameTo(target, otarget)) {
|
||||
@@ -176,8 +169,6 @@ public class Patcher
|
||||
}
|
||||
|
||||
} finally {
|
||||
StreamUtil.close(fout);
|
||||
StreamUtil.close(in);
|
||||
// clean up our temporary files
|
||||
if (!patch.delete()) {
|
||||
patch.deleteOnExit();
|
||||
|
||||
@@ -13,7 +13,7 @@ import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -70,9 +70,11 @@ public class Config
|
||||
throws IOException
|
||||
{
|
||||
// annoyingly FileReader does not allow encoding to be specified (uses platform default)
|
||||
InputStreamReader input = new InputStreamReader(new FileInputStream(source), "UTF-8");
|
||||
try (FileInputStream fis = new FileInputStream(source);
|
||||
InputStreamReader input = new InputStreamReader(fis, StandardCharsets.UTF_8)) {
|
||||
return parsePairs(input, opts);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* See {@link #parsePairs(File,ParseOpts)}.
|
||||
|
||||
@@ -5,7 +5,14 @@
|
||||
|
||||
package com.threerings.getdown.util;
|
||||
|
||||
import java.io.*;
|
||||
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;
|
||||
@@ -58,15 +65,13 @@ public class FileUtil
|
||||
}
|
||||
|
||||
// as a last resort, try copying the old data over the new
|
||||
FileInputStream fin = null;
|
||||
FileOutputStream fout = null;
|
||||
try {
|
||||
fin = new FileInputStream(source);
|
||||
fout = new FileOutputStream(dest);
|
||||
try (FileInputStream fin = new FileInputStream(source);
|
||||
FileOutputStream fout = new FileOutputStream(dest)) {
|
||||
|
||||
StreamUtil.copy(fin, fout);
|
||||
|
||||
// close the input stream now so we can delete 'source'
|
||||
fin.close();
|
||||
fin = null;
|
||||
if (!deleteHarder(source)) {
|
||||
log.warning("Failed to delete " + source +
|
||||
" after brute force copy to " + dest + ".");
|
||||
@@ -76,10 +81,6 @@ public class FileUtil
|
||||
} catch (IOException ioe) {
|
||||
log.warning("Failed to copy " + source + " to " + dest + ": " + ioe);
|
||||
return false;
|
||||
|
||||
} finally {
|
||||
StreamUtil.close(fin);
|
||||
StreamUtil.close(fout);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,21 +104,17 @@ public class FileUtil
|
||||
throws IOException
|
||||
{
|
||||
List<String> lines = new ArrayList<>();
|
||||
try {
|
||||
BufferedReader bin = new BufferedReader(in);
|
||||
try (BufferedReader bin = new BufferedReader(in)) {
|
||||
for (String line = null; (line = bin.readLine()) != null; lines.add(line)) {}
|
||||
} finally {
|
||||
StreamUtil.close(in);
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unpacks the specified jar file intto the specified target directory.
|
||||
* Unpacks the specified jar file into the specified target directory.
|
||||
*/
|
||||
public static void unpackJar (JarFile jar, File target) throws IOException
|
||||
{
|
||||
try {
|
||||
Enumeration<?> entries = jar.entries();
|
||||
while (entries.hasMoreElements()) {
|
||||
JarEntry entry = (JarEntry)entries.nextElement();
|
||||
@@ -140,26 +137,12 @@ public class FileUtil
|
||||
continue;
|
||||
}
|
||||
|
||||
BufferedOutputStream fout = null;
|
||||
InputStream jin = null;
|
||||
try {
|
||||
fout = new BufferedOutputStream(new FileOutputStream(efile));
|
||||
jin = jar.getInputStream(entry);
|
||||
try (BufferedOutputStream fout = new BufferedOutputStream(new FileOutputStream(efile));
|
||||
InputStream jin = jar.getInputStream(entry)) {
|
||||
StreamUtil.copy(jin, fout);
|
||||
} catch (Exception e) {
|
||||
throw new IOException(
|
||||
Logger.format("Failure unpacking", "jar", jar, "entry", efile), e);
|
||||
} finally {
|
||||
StreamUtil.close(jin);
|
||||
StreamUtil.close(fout);
|
||||
}
|
||||
}
|
||||
|
||||
} finally {
|
||||
try {
|
||||
jar.close();
|
||||
} catch (Exception e) {
|
||||
log.warning("Failed to close jar file", "jar", jar, "error", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -170,23 +153,14 @@ public class FileUtil
|
||||
*/
|
||||
public static void unpackPacked200Jar (File packedJar, File target) throws IOException
|
||||
{
|
||||
InputStream packedJarIn = null;
|
||||
FileOutputStream extractedJarFileOut = null;
|
||||
JarOutputStream jarOutputStream = null;
|
||||
try {
|
||||
extractedJarFileOut = new FileOutputStream(target);
|
||||
jarOutputStream = new JarOutputStream(extractedJarFileOut);
|
||||
packedJarIn = new FileInputStream(packedJar);
|
||||
if (packedJar.getName().endsWith(".gz") || packedJar.getName().endsWith(".gz_new")) {
|
||||
packedJarIn = new GZIPInputStream(packedJarIn);
|
||||
}
|
||||
try (InputStream packedJarIn = new FileInputStream(packedJar);
|
||||
FileOutputStream extractedJarFileOut = new FileOutputStream(target);
|
||||
JarOutputStream jarOutputStream = new JarOutputStream(extractedJarFileOut)) {
|
||||
boolean gz = (packedJar.getName().endsWith(".gz") || packedJar.getName().endsWith(".gz_new"));
|
||||
try (InputStream packedJarIn2 = (gz ? new GZIPInputStream(packedJarIn) : packedJarIn)) {
|
||||
Pack200.Unpacker unpacker = Pack200.newUnpacker();
|
||||
unpacker.unpack(packedJarIn, jarOutputStream);
|
||||
|
||||
} finally {
|
||||
StreamUtil.close(jarOutputStream);
|
||||
StreamUtil.close(extractedJarFileOut);
|
||||
StreamUtil.close(packedJarIn);
|
||||
unpacker.unpack(packedJarIn2, jarOutputStream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -194,15 +168,9 @@ public class FileUtil
|
||||
* Copies the given {@code source} file to the given {@code target}.
|
||||
*/
|
||||
public static void copy (File source, File target) throws IOException {
|
||||
FileInputStream in = null;
|
||||
FileOutputStream out = null;
|
||||
try {
|
||||
in = new FileInputStream(source);
|
||||
out = new FileOutputStream(target);
|
||||
try (FileInputStream in = new FileInputStream(source);
|
||||
FileOutputStream out = new FileOutputStream(target)) {
|
||||
StreamUtil.copy(in, out);
|
||||
} finally {
|
||||
StreamUtil.close(in);
|
||||
StreamUtil.close(out);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
package com.threerings.getdown.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
@@ -52,9 +51,10 @@ public class LaunchUtil
|
||||
{
|
||||
// create the file that instructs Getdown to upgrade
|
||||
File vfile = new File(appdir, "version.txt");
|
||||
PrintStream ps = new PrintStream(new FileOutputStream(vfile));
|
||||
try (FileOutputStream fos = new FileOutputStream(vfile);
|
||||
PrintStream ps = new PrintStream(fos)) {
|
||||
ps.println(newVersion);
|
||||
ps.close();
|
||||
}
|
||||
|
||||
// make sure that we can find our getdown.jar file and can safely launch children
|
||||
File pro = new File(appdir, getdownJarName);
|
||||
|
||||
@@ -16,7 +16,6 @@ import java.io.PrintStream;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import com.samskivert.io.StreamUtil;
|
||||
import com.samskivert.util.StringUtil;
|
||||
|
||||
import com.threerings.getdown.data.SysProps;
|
||||
@@ -33,17 +32,15 @@ public class VersionUtil
|
||||
public static long readVersion (File vfile)
|
||||
{
|
||||
long fileVersion = -1;
|
||||
BufferedReader bin = null;
|
||||
try {
|
||||
bin = new BufferedReader(new InputStreamReader(new FileInputStream(vfile)));
|
||||
try (FileInputStream fis = new FileInputStream(vfile);
|
||||
InputStreamReader reader = new InputStreamReader(fis);
|
||||
BufferedReader bin = new BufferedReader(reader)) {
|
||||
String vstr = bin.readLine();
|
||||
if (!StringUtil.isBlank(vstr)) {
|
||||
fileVersion = Long.parseLong(vstr);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.info("Unable to read version file: " + e.getMessage());
|
||||
} finally {
|
||||
StreamUtil.close(bin);
|
||||
}
|
||||
|
||||
return fileVersion;
|
||||
@@ -54,13 +51,11 @@ public class VersionUtil
|
||||
*/
|
||||
public static void writeVersion (File vfile, long version) throws IOException
|
||||
{
|
||||
PrintStream out = new PrintStream(new FileOutputStream(vfile));
|
||||
try {
|
||||
try (FileOutputStream fos = new FileOutputStream(vfile);
|
||||
PrintStream out = new PrintStream(fos)) {
|
||||
out.println(version);
|
||||
} catch (Exception e) {
|
||||
log.warning("Unable to write version file: " + e.getMessage());
|
||||
} finally {
|
||||
StreamUtil.close(out);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,9 +83,9 @@ public class VersionUtil
|
||||
*/
|
||||
public static long readReleaseVersion (File relfile, String versRegex)
|
||||
{
|
||||
BufferedReader in = null;
|
||||
try {
|
||||
in = new BufferedReader(new FileReader(relfile));
|
||||
try (FileReader reader = new FileReader(relfile);
|
||||
BufferedReader in = new BufferedReader(reader)) {
|
||||
|
||||
String line = null, relvers = null;
|
||||
while ((line = in.readLine()) != null) {
|
||||
if (line.startsWith("JAVA_VERSION=")) {
|
||||
@@ -107,8 +102,6 @@ public class VersionUtil
|
||||
} catch (Exception e) {
|
||||
log.warning("Failed to read version from 'release' file", "file", relfile, e);
|
||||
return 0L;
|
||||
} finally {
|
||||
StreamUtil.close(in);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user