Skip to content

Commit 48b71b3

Browse files
authored
refactor: Replace to nio & apply CS inspection skips (#3055)
1 parent 8d222d7 commit 48b71b3

File tree

14 files changed

+60
-26
lines changed

14 files changed

+60
-26
lines changed

brut.apktool/apktool-lib/src/main/java/brut/androlib/Androlib.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import javax.xml.parsers.ParserConfigurationException;
4242
import javax.xml.transform.TransformerException;
4343
import java.io.*;
44+
import java.nio.file.Files;
4445
import java.util.*;
4546
import java.util.logging.Logger;
4647
import java.util.regex.Pattern;
@@ -98,6 +99,7 @@ public void decodeSourcesSmali(File apkFile, File outDir, String filename, boole
9899
smaliDir = new File(outDir, SMALI_DIRNAME + "_" + filename.substring(0, filename.indexOf(".")));
99100
}
100101
OS.rmdir(smaliDir);
102+
//noinspection ResultOfMethodCallIgnored
101103
smaliDir.mkdirs();
102104
LOGGER.info("Baksmaling " + filename + "...");
103105
DexFile dexFile = SmaliDecoder.decode(apkFile, smaliDir, filename, bakDeb, apiLevel);
@@ -236,6 +238,7 @@ public void writeOriginalFiles(ExtFile apkFile, File outDir)
236238
LOGGER.info("Copying original files...");
237239
File originalDir = new File(outDir, "original");
238240
if (!originalDir.exists()) {
241+
//noinspection ResultOfMethodCallIgnored
239242
originalDir.mkdirs();
240243
}
241244

@@ -313,6 +316,7 @@ public void build(ExtFile appDir, File outFile)
313316
outFile = new File(appDir, "dist" + File.separator + (outFileName == null ? "out.apk" : outFileName));
314317
}
315318

319+
//noinspection ResultOfMethodCallIgnored
316320
new File(appDir, APK_DIRNAME).mkdirs();
317321
File manifest = new File(appDir, "AndroidManifest.xml");
318322
File manifestOriginal = new File(appDir, "AndroidManifest.xml.orig");
@@ -353,6 +357,7 @@ private void buildManifestFile(File appDir, File manifest, File manifestOriginal
353357
if (manifest.isFile() && manifest.exists()) {
354358
try {
355359
if (manifestOriginal.exists()) {
360+
//noinspection ResultOfMethodCallIgnored
356361
manifestOriginal.delete();
357362
}
358363
FileUtils.copyFile(manifest, manifestOriginal);
@@ -412,7 +417,7 @@ public boolean buildSourcesRaw(File appDir, String filename)
412417
if (buildOptions.forceBuildAll || isModified(working, stored)) {
413418
LOGGER.info("Copying " + appDir.toString() + " " + filename + " file...");
414419
try {
415-
BrutIO.copyAndClose(new FileInputStream(working), new FileOutputStream(stored));
420+
BrutIO.copyAndClose(Files.newInputStream(working.toPath()), Files.newOutputStream(stored.toPath()));
416421
return true;
417422
} catch (IOException ex) {
418423
throw new AndrolibException(ex);
@@ -433,6 +438,7 @@ public boolean buildSourcesSmali(File appDir, String folder, String filename)
433438
}
434439
if (buildOptions.forceBuildAll || isModified(smaliDir, dex)) {
435440
LOGGER.info("Smaling " + folder + " folder into " + filename + "...");
441+
//noinspection ResultOfMethodCallIgnored
436442
dex.delete();
437443
SmaliBuilder.build(smaliDir, dex, buildOptions.forceApi > 0 ? buildOptions.forceApi : mMinSdkVersion);
438444
}
@@ -503,6 +509,7 @@ public boolean buildResourcesFull(File appDir, UsesFramework usesFramework)
503509
File netSecConfOrig = new File(appDir, "res/xml/network_security_config.xml");
504510
if (netSecConfOrig.exists()) {
505511
LOGGER.info("Replacing existing network_security_config.xml!");
512+
//noinspection ResultOfMethodCallIgnored
506513
netSecConfOrig.delete();
507514
}
508515
ResXmlPatcher.modNetworkSecurityConfig(netSecConfOrig);
@@ -511,7 +518,9 @@ public boolean buildResourcesFull(File appDir, UsesFramework usesFramework)
511518
}
512519

513520
File apkFile = File.createTempFile("APKTOOL", null);
521+
//noinspection ResultOfMethodCallIgnored
514522
apkFile.delete();
523+
//noinspection ResultOfMethodCallIgnored
515524
resourceFile.delete();
516525

517526
File ninePatch = new File(appDir, "9patch");
@@ -539,6 +548,7 @@ public boolean buildResourcesFull(File appDir, UsesFramework usesFramework)
539548
}
540549

541550
// delete tmpDir
551+
//noinspection ResultOfMethodCallIgnored
542552
apkFile.delete();
543553
}
544554
return true;
@@ -576,6 +586,7 @@ public boolean buildManifest(ExtFile appDir, UsesFramework usesFramework)
576586
LOGGER.info("Building AndroidManifest.xml...");
577587

578588
File apkFile = File.createTempFile("APKTOOL", null);
589+
//noinspection ResultOfMethodCallIgnored
579590
apkFile.delete();
580591

581592
File ninePatch = new File(appDir, "9patch");
@@ -590,6 +601,7 @@ public boolean buildManifest(ExtFile appDir, UsesFramework usesFramework)
590601
Directory tmpDir = new ExtFile(apkFile).getDirectory();
591602
tmpDir.copyToDir(apkDir, APK_MANIFEST_FILENAMES);
592603

604+
//noinspection ResultOfMethodCallIgnored
593605
apkFile.delete();
594606
}
595607
return true;
@@ -668,7 +680,7 @@ public void buildUnknownFiles(File appDir, File outFile, MetaInfo meta)
668680

669681
try (
670682
ZipFile inputFile = new ZipFile(tempFile);
671-
ZipOutputStream actualOutput = new ZipOutputStream(new FileOutputStream(outFile))
683+
ZipOutputStream actualOutput = new ZipOutputStream(Files.newOutputStream(outFile.toPath()))
672684
) {
673685
copyExistingFiles(inputFile, actualOutput);
674686
copyUnknownFiles(appDir, actualOutput, files);
@@ -677,6 +689,7 @@ public void buildUnknownFiles(File appDir, File outFile, MetaInfo meta)
677689
}
678690

679691
// Remove our temporary file.
692+
//noinspection ResultOfMethodCallIgnored
680693
tempFile.delete();
681694
}
682695
}
@@ -726,7 +739,7 @@ private void copyUnknownFiles(File appDir, ZipOutputStream outputFile, Map<Strin
726739
newEntry.setMethod(ZipEntry.STORED);
727740
newEntry.setSize(inputFile.length());
728741
newEntry.setCompressedSize(-1);
729-
BufferedInputStream unknownFile = new BufferedInputStream(new FileInputStream(inputFile));
742+
BufferedInputStream unknownFile = new BufferedInputStream(Files.newInputStream(inputFile.toPath()));
730743
CRC32 crc = BrutIO.calculateCrc(unknownFile);
731744
newEntry.setCrc(crc.getValue());
732745
unknownFile.close();
@@ -743,10 +756,12 @@ private void copyUnknownFiles(File appDir, ZipOutputStream outputFile, Map<Strin
743756
public void buildApk(File appDir, File outApk) throws AndrolibException {
744757
LOGGER.info("Building apk file...");
745758
if (outApk.exists()) {
759+
//noinspection ResultOfMethodCallIgnored
746760
outApk.delete();
747761
} else {
748762
File outDir = outApk.getParentFile();
749763
if (outDir != null && !outDir.exists()) {
764+
//noinspection ResultOfMethodCallIgnored
750765
outDir.mkdirs();
751766
}
752767
}

brut.apktool/apktool-lib/src/main/java/brut/androlib/mod/SmaliMod.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import java.io.*;
3030
import java.nio.charset.StandardCharsets;
31+
import java.nio.file.Files;
3132

3233
public class SmaliMod {
3334
public static boolean assembleSmaliFile(File smaliFile, DexBuilder dexBuilder, int apiLevel, boolean verboseErrors,
@@ -36,7 +37,7 @@ public static boolean assembleSmaliFile(File smaliFile, DexBuilder dexBuilder, i
3637
CommonTokenStream tokens;
3738
smaliFlexLexer lexer;
3839

39-
InputStream is = new FileInputStream(smaliFile);
40+
InputStream is = Files.newInputStream(smaliFile.toPath());
4041
InputStreamReader reader = new InputStreamReader(is, StandardCharsets.UTF_8);
4142

4243
lexer = new smaliFlexLexer(reader, apiLevel);

brut.apktool/apktool-lib/src/main/java/brut/androlib/res/AndrolibResources.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.xmlpull.v1.XmlSerializer;
3838

3939
import java.io.*;
40+
import java.nio.file.Files;
4041
import java.util.*;
4142
import java.util.logging.Logger;
4243
import java.util.zip.CRC32;
@@ -185,7 +186,7 @@ public void decodeManifestWithResources(ResTable resTable, ExtFile apkFile, File
185186

186187
attrDecoder.setCurrentPackage(resTable.listMainPackages().iterator().next());
187188

188-
Directory inApk, in = null, out;
189+
Directory inApk, out;
189190
try {
190191
inApk = apkFile.getDirectory();
191192
out = new FileDirectory(outDir);
@@ -433,7 +434,8 @@ private void aapt2Package(File apkFile, File manifest, File resDir, File rawDir,
433434
if (buildOptions.doNotCompress != null && !customAapt) {
434435
// Use custom -e option to avoid limits on commandline length.
435436
// Can only be used when custom aapt binary is not used.
436-
String extensionsFilePath = createDoNotCompressExtensionsFile(buildOptions).getAbsolutePath();
437+
String extensionsFilePath =
438+
Objects.requireNonNull(createDoNotCompressExtensionsFile(buildOptions)).getAbsolutePath();
437439
cmd.add("-e");
438440
cmd.add(extensionsFilePath);
439441
} else if (buildOptions.doNotCompress != null) {
@@ -555,7 +557,8 @@ private void aapt1Package(File apkFile, File manifest, File resDir, File rawDir,
555557
if (buildOptions.doNotCompress != null && !customAapt) {
556558
// Use custom -e option to avoid limits on commandline length.
557559
// Can only be used when custom aapt binary is not used.
558-
String extensionsFilePath = createDoNotCompressExtensionsFile(buildOptions).getAbsolutePath();
560+
String extensionsFilePath =
561+
Objects.requireNonNull(createDoNotCompressExtensionsFile(buildOptions)).getAbsolutePath();
559562
cmd.add("-e");
560563
cmd.add(extensionsFilePath);
561564
} else if (buildOptions.doNotCompress != null) {
@@ -801,7 +804,7 @@ public File getFrameworkApk(int id, String frameTag)
801804

802805
if (id == 1) {
803806
try (InputStream in = getAndroidFrameworkResourcesAsStream();
804-
OutputStream out = new FileOutputStream(apk)) {
807+
OutputStream out = Files.newOutputStream(apk.toPath())) {
805808
IOUtils.copy(in, out);
806809
return apk;
807810
} catch (IOException ex) {
@@ -822,12 +825,13 @@ public void emptyFrameworkDirectory() throws AndrolibException {
822825
LOGGER.warning("Can't empty framework directory, no file found at: " + apk.getAbsolutePath());
823826
} else {
824827
try {
825-
if (apk.exists() && dir.listFiles().length > 1 && ! buildOptions.forceDeleteFramework) {
828+
if (apk.exists() && Objects.requireNonNull(dir.listFiles()).length > 1 && ! buildOptions.forceDeleteFramework) {
826829
LOGGER.warning("More than default framework detected. Please run command with `--force` parameter to wipe framework directory.");
827830
} else {
828-
for (File file : dir.listFiles()) {
831+
for (File file : Objects.requireNonNull(dir.listFiles())) {
829832
if (file.isFile() && file.getName().endsWith(".apk")) {
830833
LOGGER.info("Removing " + file.getName() + " framework file...");
834+
//noinspection ResultOfMethodCallIgnored
831835
file.delete();
832836
}
833837
}
@@ -879,7 +883,7 @@ public void installFramework(File frameFile, String tag)
879883
+ (tag == null ? "" : '-' + tag)
880884
+ ".apk");
881885

882-
out = new ZipOutputStream(new FileOutputStream(outFile));
886+
out = new ZipOutputStream(Files.newOutputStream(outFile.toPath()));
883887
out.setMethod(ZipOutputStream.STORED);
884888
CRC32 crc = new CRC32();
885889
crc.update(data);
@@ -919,8 +923,9 @@ public void installFramework(File frameFile, String tag)
919923
public void publicizeResources(File arscFile) throws AndrolibException {
920924
byte[] data = new byte[(int) arscFile.length()];
921925

922-
try(InputStream in = new FileInputStream(arscFile);
923-
OutputStream out = new FileOutputStream(arscFile)) {
926+
try(InputStream in = Files.newInputStream(arscFile.toPath());
927+
OutputStream out = Files.newOutputStream(arscFile.toPath())) {
928+
//noinspection ResultOfMethodCallIgnored
924929
in.read(data);
925930
publicizeResources(data);
926931
out.write(data);
@@ -1034,7 +1039,7 @@ public void close() throws IOException {
10341039

10351040
public BuildOptions buildOptions;
10361041

1037-
public Map<String, String> mResFileMapping = new HashMap();
1042+
public Map<String, String> mResFileMapping = new HashMap<>();
10381043

10391044
// TODO: dirty static hack. I have to refactor decoding mechanisms.
10401045
public static boolean sKeepBroken = false;

brut.apktool/apktool-lib/src/main/java/brut/androlib/res/xml/ResXmlPatcher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public static void modNetworkSecurityConfig(File file)
168168
* build, thus preventing the application from installing. This is from a bug/error
169169
* in AOSP where public resources cannot be part of an authorities attribute within
170170
* a provider tag.
171-
*
171+
* <p>
172172
* This finds any reference and replaces it with the literal value found in the
173173
* res/values/strings.xml file.
174174
*

brut.apktool/apktool-lib/src/main/java/brut/androlib/src/SmaliBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
import com.android.tools.smali.dexlib2.writer.builder.DexBuilder;
2626
import com.android.tools.smali.dexlib2.writer.io.FileDataStore;
2727
import java.io.File;
28-
import java.io.FileInputStream;
2928
import java.io.IOException;
3029
import java.io.InputStream;
30+
import java.nio.file.Files;
3131
import java.util.logging.Logger;
3232

3333
public class SmaliBuilder {
@@ -63,7 +63,7 @@ private void build() throws AndrolibException {
6363
private void buildFile(String fileName, DexBuilder dexBuilder)
6464
throws AndrolibException, IOException {
6565
File inFile = new File(mSmaliDir, fileName);
66-
InputStream inStream = new FileInputStream(inFile);
66+
InputStream inStream = Files.newInputStream(inFile.toPath());
6767

6868
if (fileName.endsWith(".smali")) {
6969
try {

brut.apktool/apktool-lib/src/main/java/brut/androlib/src/SmaliDecoder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ private DexFile decode() throws AndrolibException {
8080
dexEntry = container.getEntry(mDexFile);
8181
}
8282

83-
// Double check the passed param exists
83+
// Double-check the passed param exists
8484
if (dexEntry == null) {
8585
dexEntry = container.getEntry(container.getDexEntryNames().get(0));
8686
}

brut.apktool/apktool-lib/src/main/java/org/xmlpull/renamed/MXSerializer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.OutputStream;
2323
import java.io.OutputStreamWriter;
2424
import java.io.Writer;
25+
import java.util.Objects;
2526

2627
/**
2728
* Implementation of XmlSerializer interface from XmlPull V1 API. This
@@ -106,7 +107,7 @@ public class MXSerializer implements XmlSerializer {
106107
private final boolean checkNamesInterned = false;
107108

108109
private void checkInterning(String name) {
109-
if (namesInterned && name != name.intern()) {
110+
if (namesInterned && !Objects.equals(name, name.intern())) {
110111
throw new IllegalArgumentException("all names passed as arguments must be interned"
111112
+ "when NAMES INTERNED feature is enabled");
112113
}

brut.apktool/apktool-lib/src/test/java/brut/androlib/TestUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public static void cleanFrameworkFile() throws BrutException {
127127

128128
public static byte[] readHeaderOfFile(File file, int size) throws IOException {
129129
byte[] buffer = new byte[size];
130-
InputStream inputStream = new FileInputStream(file);
130+
InputStream inputStream = Files.newInputStream(file.toPath());
131131
if (inputStream.read(buffer) != buffer.length) {
132132
throw new IOException("File size too small for buffer length: " + size);
133133
}

brut.j.dir/src/main/java/brut/directory/DirUtil.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public static void copyToDir(Directory in, File out, String fileName)
9090
} else {
9191
String cleanedFilename = BrutIO.sanitizeUnknownFile(out, fileName);
9292
File outFile = new File(out, cleanedFilename);
93+
//noinspection ResultOfMethodCallIgnored
9394
outFile.getParentFile().mkdirs();
9495
BrutIO.copyAndClose(in.getFileInput(fileName), Files.newOutputStream(outFile.toPath()));
9596
}

brut.j.dir/src/main/java/brut/directory/FileDirectory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public long getCompressedSize(String fileName)
5959
@Override
6060
protected AbstractDirectory createDirLocal(String name) throws DirectoryException {
6161
File dir = new File(generatePath(name));
62+
//noinspection ResultOfMethodCallIgnored
6263
dir.mkdir();
6364
return new FileDirectory(dir);
6465
}
@@ -93,6 +94,7 @@ protected void loadFiles() {
9394

9495
@Override
9596
protected void removeFileLocal(String name) {
97+
//noinspection ResultOfMethodCallIgnored
9698
new File(generatePath(name)).delete();
9799
}
98100

brut.j.dir/src/main/java/brut/directory/ZipUtils.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.commons.io.IOUtils;
2323

2424
import java.io.*;
25+
import java.nio.file.Files;
2526
import java.util.Collection;
2627
import java.util.zip.CRC32;
2728
import java.util.zip.ZipEntry;
@@ -39,7 +40,7 @@ public static void zipFolders(final File folder, final File zip, final File asse
3940
throws BrutException, IOException {
4041

4142
mDoNotCompress = doNotCompress;
42-
ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zip));
43+
ZipOutputStream zipOutputStream = new ZipOutputStream(Files.newOutputStream(zip.toPath()));
4344
zipFolders(folder, zipOutputStream);
4445

4546
// We manually set the assets because we need to retain the folder structure
@@ -67,7 +68,7 @@ private static void processFolder(final File folder, final ZipOutputStream zipOu
6768
if (mDoNotCompress != null && (mDoNotCompress.contains(extension) || mDoNotCompress.contains(zipEntry.getName()))) {
6869
zipEntry.setMethod(ZipEntry.STORED);
6970
zipEntry.setSize(file.length());
70-
BufferedInputStream unknownFile = new BufferedInputStream(new FileInputStream(file));
71+
BufferedInputStream unknownFile = new BufferedInputStream(Files.newInputStream(file.toPath()));
7172
CRC32 crc = BrutIO.calculateCrc(unknownFile);
7273
zipEntry.setCrc(crc.getValue());
7374
unknownFile.close();

brut.j.util/src/main/java/brut/util/AaptManager.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public static String getAaptExecutionCommand(String aaptPath, File aapt) throws
6767
if (! aaptPath.isEmpty()) {
6868
File aaptFile = new File(aaptPath);
6969
if (aaptFile.canRead() && aaptFile.exists()) {
70+
//noinspection ResultOfMethodCallIgnored
7071
aaptFile.setExecutable(true);
7172
return aaptFile.getPath();
7273
} else {
@@ -101,6 +102,7 @@ public static int getAaptVersion(File aapt) throws BrutException {
101102
if (!aapt.isFile()) {
102103
throw new BrutException("Could not identify aapt binary as executable.");
103104
}
105+
//noinspection ResultOfMethodCallIgnored
104106
aapt.setExecutable(true);
105107

106108
List<String> cmd = new ArrayList<>();

brut.j.util/src/main/java/brut/util/Jar.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.apache.commons.io.IOUtils;
2121

2222
import java.io.*;
23+
import java.nio.file.Files;
2324
import java.util.HashMap;
2425
import java.util.Map;
2526
import java.util.concurrent.ThreadLocalRandom;
@@ -51,7 +52,7 @@ public static File extractToTmp(String resourcePath, String tmpPrefix, Class<?>
5152
File fileOut = File.createTempFile(tmpPrefix, suffix + ".tmp");
5253
fileOut.deleteOnExit();
5354

54-
OutputStream out = new FileOutputStream(fileOut);
55+
OutputStream out = Files.newOutputStream(fileOut.toPath());
5556
IOUtils.copy(in, out);
5657
in.close();
5758
out.close();

0 commit comments

Comments
 (0)