From 407ffdc5feb42ca477c0d97e0467a219c87b7549 Mon Sep 17 00:00:00 2001 From: Rover12421 Date: Mon, 30 Mar 2015 13:30:52 +0800 Subject: [PATCH] remove BrutIO copy method `copy` method already in IOUtils --- .../src/main/java/brut/androlib/Androlib.java | 29 ++++++++++++------- .../src/main/java/brut/util/BrutIO.java | 17 ++++------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/brut.apktool/apktool-lib/src/main/java/brut/androlib/Androlib.java b/brut.apktool/apktool-lib/src/main/java/brut/androlib/Androlib.java index 05377560..fd4a22ed 100644 --- a/brut.apktool/apktool-lib/src/main/java/brut/androlib/Androlib.java +++ b/brut.apktool/apktool-lib/src/main/java/brut/androlib/Androlib.java @@ -36,6 +36,7 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipOutputStream; +import org.apache.commons.io.IOUtils; import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; @@ -230,7 +231,7 @@ public class Androlib { try ( Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream( - new File(mOutDir, "apktool.yml")), "UTF-8")); + new File(mOutDir, "apktool.yml")), "UTF-8")) ) { yaml.dump(meta, writer); } catch (IOException ex) { @@ -241,7 +242,7 @@ public class Androlib { public Map readMetaFile(ExtFile appDir) throws AndrolibException { try( - InputStream in = appDir.getDirectory().getFileInput("apktool.yml"); + InputStream in = appDir.getDirectory().getFileInput("apktool.yml") ) { Yaml yaml = new Yaml(); return (Map) yaml.load(in); @@ -557,9 +558,8 @@ public class Androlib { ZipFile inputFile = new ZipFile(tempFile); ZipOutputStream actualOutput = new ZipOutputStream(new FileOutputStream(outFile)); ) { - byte[] buffer = new byte[4096 * 1024]; - copyExistingFiles(inputFile, actualOutput, buffer); - copyUnknownFiles(appDir, actualOutput, files, buffer); + copyExistingFiles(inputFile, actualOutput); + copyUnknownFiles(appDir, actualOutput, files); } catch (IOException ex) { throw new AndrolibException(ex); } @@ -569,7 +569,7 @@ public class Androlib { } } - private void copyExistingFiles(ZipFile inputFile, ZipOutputStream outputFile, byte[] buffer) throws IOException { + private void copyExistingFiles(ZipFile inputFile, ZipOutputStream outputFile) throws IOException { // First, copy the contents from the existing outFile: Enumeration entries = inputFile.entries(); while (entries.hasMoreElements()) { @@ -580,14 +580,18 @@ public class Androlib { // No need to create directory entries in the final apk if (!entry.isDirectory()) { - BrutIO.copy(inputFile.getInputStream(entry), outputFile, buffer); + try ( + InputStream is = inputFile.getInputStream(entry) + ){ + IOUtils.copy(is, outputFile); + } } outputFile.closeEntry(); } } - private void copyUnknownFiles(File appDir, ZipOutputStream outputFile, Map files, byte[] buffer) + private void copyUnknownFiles(File appDir, ZipOutputStream outputFile, Map files) throws IOException { File unknownFileDir = new File(appDir, UNK_DIRNAME); @@ -606,7 +610,7 @@ public class Androlib { newEntry.setSize(inputFile.length()); newEntry.setCompressedSize(-1); BufferedInputStream unknownFile = new BufferedInputStream(new FileInputStream(inputFile)); - CRC32 crc = BrutIO.calculateCrc(unknownFile, buffer); + CRC32 crc = BrutIO.calculateCrc(unknownFile); newEntry.setCrc(crc.getValue()); LOGGER.fine("\tsize: " + newEntry.getSize()); @@ -615,8 +619,11 @@ public class Androlib { } outputFile.putNextEntry(newEntry); - BufferedInputStream unknownFile = new BufferedInputStream(new FileInputStream(inputFile)); - BrutIO.copy(unknownFile, outputFile, buffer); + try ( + FileInputStream fis = new FileInputStream(inputFile) + ){ + IOUtils.copy(fis, outputFile); + } outputFile.closeEntry(); } } diff --git a/brut.j.util/src/main/java/brut/util/BrutIO.java b/brut.j.util/src/main/java/brut/util/BrutIO.java index a5ea49fd..70a72122 100644 --- a/brut.j.util/src/main/java/brut/util/BrutIO.java +++ b/brut.j.util/src/main/java/brut/util/BrutIO.java @@ -30,10 +30,8 @@ public class BrutIO { try { IOUtils.copy(in, out); } finally { - try { - in.close(); - out.close(); - } catch (IOException ex) {} + IOUtils.closeQuietly(in); + IOUtils.closeQuietly(out); } } @@ -62,19 +60,14 @@ public class BrutIO { return modified; } - public static CRC32 calculateCrc(InputStream input, byte[] buffer) throws IOException { + public static CRC32 calculateCrc(InputStream input) throws IOException { CRC32 crc = new CRC32(); - int bytesRead = 0; + int bytesRead; + byte[] buffer = new byte[8192]; while((bytesRead = input.read(buffer)) != -1) { crc.update(buffer, 0, bytesRead); } return crc; } - public static void copy(InputStream input, OutputStream output, byte[] buffer) throws IOException { - int bytesRead; - while((bytesRead = input.read(buffer)) != -1) { - output.write(buffer, 0, bytesRead); - } - } }