Skip to content

Commit 1e462d9

Browse files
authored
Merge pull request #672 from Tencent/dev
fix some bugs.
2 parents d0fe056 + 19f60dd commit 1e462d9

File tree

24 files changed

+342
-373
lines changed

24 files changed

+342
-373
lines changed

tinker-android/tinker-android-lib/src/main/java/com/tencent/tinker/lib/patch/BasePatchInternal.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.tencent.tinker.lib.patch;
1818

19+
import com.tencent.tinker.commons.util.StreamUtil;
1920
import com.tencent.tinker.lib.util.TinkerLog;
2021
import com.tencent.tinker.loader.shareutil.ShareConstants;
2122
import com.tencent.tinker.loader.shareutil.SharePatchFileUtil;
@@ -25,6 +26,8 @@
2526
import java.io.File;
2627
import java.io.FileOutputStream;
2728
import java.io.IOException;
29+
import java.io.InputStream;
30+
import java.io.OutputStream;
2831
import java.util.zip.ZipEntry;
2932
import java.util.zip.ZipFile;
3033

@@ -53,22 +56,22 @@ public static boolean extract(ZipFile zipFile, ZipEntry entryFile, File extractT
5356
boolean isExtractionSuccessful = false;
5457
while (numAttempts < MAX_EXTRACT_ATTEMPTS && !isExtractionSuccessful) {
5558
numAttempts++;
56-
BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entryFile));
57-
FileOutputStream fos = new FileOutputStream(extractTo);
58-
BufferedOutputStream out = new BufferedOutputStream(fos);
59+
InputStream is = null;
60+
OutputStream os = null;
5961

6062
TinkerLog.i(TAG, "try Extracting " + extractTo.getPath());
6163

6264
try {
65+
is = new BufferedInputStream(zipFile.getInputStream(entryFile));
66+
os = new BufferedOutputStream(new FileOutputStream(extractTo));
6367
byte[] buffer = new byte[ShareConstants.BUFFER_SIZE];
64-
int length = bis.read(buffer);
65-
while (length != -1) {
66-
out.write(buffer, 0, length);
67-
length = bis.read(buffer);
68+
int length = 0;
69+
while ((length = is.read(buffer)) > 0) {
70+
os.write(buffer, 0, length);
6871
}
6972
} finally {
70-
SharePatchFileUtil.closeQuietly(out);
71-
SharePatchFileUtil.closeQuietly(bis);
73+
StreamUtil.closeQuietly(os);
74+
StreamUtil.closeQuietly(is);
7275
}
7376
if (targetMd5 != null) {
7477
if (isDex) {

tinker-android/tinker-android-lib/src/main/java/com/tencent/tinker/lib/patch/BsDiffPatchInternal.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import android.os.SystemClock;
2222

2323
import com.tencent.tinker.bsdiff.BSPatch;
24+
import com.tencent.tinker.commons.util.StreamUtil;
2425
import com.tencent.tinker.lib.tinker.Tinker;
2526
import com.tencent.tinker.lib.util.TinkerLog;
2627
import com.tencent.tinker.loader.TinkerRuntimeException;
@@ -182,8 +183,8 @@ private static boolean extractBsDiffInternals(Context context, String dir, Strin
182183
newStream = patch.getInputStream(patchFileEntry);
183184
BSPatch.patchFast(oldStream, newStream, extractedFile);
184185
} finally {
185-
SharePatchFileUtil.closeQuietly(oldStream);
186-
SharePatchFileUtil.closeQuietly(newStream);
186+
StreamUtil.closeQuietly(oldStream);
187+
StreamUtil.closeQuietly(newStream);
187188
}
188189

189190
//go go go bsdiff get the

tinker-android/tinker-android-lib/src/main/java/com/tencent/tinker/lib/patch/DexDiffPatchInternal.java

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import android.os.SystemClock;
2323

2424
import com.tencent.tinker.commons.dexpatcher.DexPatchApplier;
25+
import com.tencent.tinker.commons.util.StreamUtil;
2526
import com.tencent.tinker.lib.tinker.Tinker;
2627
import com.tencent.tinker.lib.util.TinkerLog;
2728
import com.tencent.tinker.loader.TinkerDexOptimizer;
@@ -142,13 +143,7 @@ protected static boolean waitAndCheckDexOptFile(File patchFile, Tinker manager)
142143
failDexFiles.add(file);
143144
lastThrowable = e;
144145
} finally {
145-
if (elfFile != null) {
146-
try {
147-
elfFile.close();
148-
} catch (IOException ignore) {
149-
150-
}
151-
}
146+
StreamUtil.closeQuietly(elfFile);
152147
}
153148
}
154149
}
@@ -265,11 +260,12 @@ private static boolean mergeClassNDexFiles(final Context context, final File pat
265260
TinkerZipFile dexZipFile = new TinkerZipFile(dexFile);
266261
TinkerZipEntry rawDexZipEntry = dexZipFile.getEntry(ShareConstants.DEX_IN_JAR);
267262
TinkerZipEntry newDexZipEntry = new TinkerZipEntry(rawDexZipEntry, info.rawName);
268-
InputStream inputStream = dexZipFile.getInputStream(rawDexZipEntry);
263+
InputStream inputStream = null;
269264
try {
265+
inputStream = dexZipFile.getInputStream(rawDexZipEntry);
270266
TinkerZipUtil.extractTinkerEntry(newDexZipEntry, inputStream, out);
271267
} finally {
272-
SharePatchFileUtil.closeQuietly(inputStream);
268+
StreamUtil.closeQuietly(inputStream);
273269
}
274270
} else {
275271
TinkerZipEntry dexZipEntry = new TinkerZipEntry(info.rawName);
@@ -281,7 +277,7 @@ private static boolean mergeClassNDexFiles(final Context context, final File pat
281277
TinkerLog.printErrStackTrace(TAG, throwable, "merge classN file");
282278
result = false;
283279
} finally {
284-
SharePatchFileUtil.closeQuietly(out);
280+
StreamUtil.closeQuietly(out);
285281
}
286282

287283
if (result) {
@@ -574,17 +570,14 @@ private static boolean extractDexToJar(ZipFile zipFile, ZipEntry entryFile, File
574570
while (numAttempts < MAX_EXTRACT_ATTEMPTS && !isExtractionSuccessful) {
575571
numAttempts++;
576572

577-
FileOutputStream fos = new FileOutputStream(extractTo);
578-
InputStream in = zipFile.getInputStream(entryFile);
579-
580573
ZipOutputStream zos = null;
581574
BufferedInputStream bis = null;
582575

583576
TinkerLog.i(TAG, "try Extracting " + extractTo.getPath());
584577
try {
585578
zos = new ZipOutputStream(new
586-
BufferedOutputStream(fos));
587-
bis = new BufferedInputStream(in);
579+
BufferedOutputStream(new FileOutputStream(extractTo)));
580+
bis = new BufferedInputStream(zipFile.getInputStream(entryFile));
588581

589582
byte[] buffer = new byte[ShareConstants.BUFFER_SIZE];
590583
ZipEntry entry = new ZipEntry(ShareConstants.DEX_IN_JAR);
@@ -596,8 +589,8 @@ private static boolean extractDexToJar(ZipFile zipFile, ZipEntry entryFile, File
596589
}
597590
zos.closeEntry();
598591
} finally {
599-
SharePatchFileUtil.closeQuietly(bis);
600-
SharePatchFileUtil.closeQuietly(zos);
592+
StreamUtil.closeQuietly(bis);
593+
StreamUtil.closeQuietly(zos);
601594
}
602595

603596
isExtractionSuccessful = SharePatchFileUtil.verifyDexFileMd5(extractTo, targetMd5);
@@ -676,21 +669,21 @@ private static void patchDexFile(
676669
}
677670
new DexPatchApplier(zis, patchFileStream).executeAndSaveTo(zos);
678671
} finally {
679-
SharePatchFileUtil.closeQuietly(zis);
672+
StreamUtil.closeQuietly(zis);
680673
}
681674
} else {
682675
new DexPatchApplier(oldDexStream, patchFileStream).executeAndSaveTo(zos);
683676
}
684677
zos.closeEntry();
685678
} finally {
686-
SharePatchFileUtil.closeQuietly(zos);
679+
StreamUtil.closeQuietly(zos);
687680
}
688681
} else {
689682
new DexPatchApplier(oldDexStream, patchFileStream).executeAndSaveTo(patchedDexFile);
690683
}
691684
} finally {
692-
SharePatchFileUtil.closeQuietly(oldDexStream);
693-
SharePatchFileUtil.closeQuietly(patchFileStream);
685+
StreamUtil.closeQuietly(oldDexStream);
686+
StreamUtil.closeQuietly(patchFileStream);
694687
}
695688
}
696689

tinker-android/tinker-android-lib/src/main/java/com/tencent/tinker/lib/patch/ResDiffPatchInternal.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import android.os.SystemClock;
2222

2323
import com.tencent.tinker.bsdiff.BSPatch;
24+
import com.tencent.tinker.commons.util.StreamUtil;
2425
import com.tencent.tinker.lib.tinker.Tinker;
2526
import com.tencent.tinker.lib.util.TinkerLog;
2627
import com.tencent.tinker.loader.TinkerRuntimeException;
@@ -211,15 +212,10 @@ private static boolean extractResourceDiffInternals(Context context, String dir,
211212
// set comment back
212213
out.setComment(oldApk.getComment());
213214
} finally {
214-
if (out != null) {
215-
out.close();
216-
}
217-
if (oldApk != null) {
218-
oldApk.close();
219-
}
220-
if (newApk != null) {
221-
newApk.close();
222-
}
215+
StreamUtil.closeQuietly(out);
216+
StreamUtil.closeQuietly(oldApk);
217+
StreamUtil.closeQuietly(newApk);
218+
223219
//delete temp files
224220
SharePatchFileUtil.deleteDir(tempResFileDirectory);
225221
}
@@ -332,8 +328,8 @@ private static boolean checkAndExtractResourceLargeFile(Context context, String
332328
newStream = patchZipFile.getInputStream(patchEntry);
333329
BSPatch.patchFast(oldStream, newStream, largeModeInfo.file);
334330
} finally {
335-
SharePatchFileUtil.closeQuietly(oldStream);
336-
SharePatchFileUtil.closeQuietly(newStream);
331+
StreamUtil.closeQuietly(oldStream);
332+
StreamUtil.closeQuietly(newStream);
337333
}
338334
//go go go bsdiff get the
339335
if (!SharePatchFileUtil.verifyFileMd5(largeModeInfo.file, largeModeInfo.md5)) {

tinker-android/tinker-android-lib/src/main/java/com/tencent/tinker/lib/tinker/Tinker.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,9 @@ public static Tinker with(Context context) {
102102
if (!sInstalled) {
103103
throw new TinkerRuntimeException("you must install tinker before get tinker sInstance");
104104
}
105-
if (sInstance == null) {
106-
synchronized (Tinker.class) {
107-
if (sInstance == null) {
108-
sInstance = new Builder(context).build();
109-
}
105+
synchronized (Tinker.class) {
106+
if (sInstance == null) {
107+
sInstance = new Builder(context).build();
110108
}
111109
}
112110
return sInstance;

tinker-android/tinker-android-lib/src/main/java/com/tencent/tinker/lib/util/UpgradePatchRetry.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import android.content.Context;
2020
import android.content.Intent;
2121

22+
import com.tencent.tinker.commons.util.StreamUtil;
2223
import com.tencent.tinker.lib.service.TinkerPatchService;
2324
import com.tencent.tinker.lib.tinker.Tinker;
2425
import com.tencent.tinker.lib.tinker.TinkerInstaller;
@@ -266,7 +267,7 @@ static RetryInfo readRetryProperty(File infoFile) {
266267
} catch (IOException e) {
267268
TinkerLog.e(TAG, "fail to readRetryProperty:" + e);
268269
} finally {
269-
SharePatchFileUtil.closeQuietly(inputStream);
270+
StreamUtil.closeQuietly(inputStream);
270271
}
271272

272273
return new RetryInfo(md5, times);
@@ -293,7 +294,7 @@ static void writeRetryProperty(File infoFile, RetryInfo info) {
293294
// e.printStackTrace();
294295
TinkerLog.printErrStackTrace(TAG, e, "retry write property fail");
295296
} finally {
296-
SharePatchFileUtil.closeQuietly(outputStream);
297+
StreamUtil.closeQuietly(outputStream);
297298
}
298299

299300
}

tinker-android/tinker-android-loader/src/main/java/com/tencent/tinker/loader/shareutil/SharePatchFileUtil.java

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616

1717
package com.tencent.tinker.loader.shareutil;
1818

19+
import android.annotation.SuppressLint;
1920
import android.content.Context;
2021
import android.content.pm.ApplicationInfo;
22+
import android.os.Build;
2123
import android.util.Log;
2224

2325
import com.tencent.tinker.loader.TinkerRuntimeException;
@@ -132,6 +134,25 @@ public static String checkTinkerLastUncaughtCrash(Context context) {
132134

133135
}
134136

137+
/**
138+
* Closes the given {@code obj}. Suppresses any exceptions.
139+
*/
140+
@SuppressLint("NewApi")
141+
public static void closeQuietly(Object obj) {
142+
if (obj == null) return;
143+
try {
144+
if (obj instanceof Closeable) {
145+
((Closeable) obj).close();
146+
} else if (Build.VERSION.SDK_INT >= 19 && obj instanceof AutoCloseable) {
147+
((AutoCloseable) obj).close();
148+
} else if (obj instanceof ZipFile) {
149+
((ZipFile) obj).close();
150+
}
151+
} catch (Throwable ignored) {
152+
// ignored.
153+
}
154+
}
155+
135156
public static final boolean isLegalFile(File file) {
136157
return file != null && file.exists() && file.canRead() && file.isFile() && file.length() > 0;
137158
}
@@ -451,19 +472,6 @@ public static String optimizedPathFor(File path, File optimizedDirectory) {
451472
return result.getPath();
452473
}
453474

454-
/**
455-
* Closes the given {@code Closeable}. Suppresses any IO exceptions.
456-
*/
457-
public static void closeQuietly(Closeable closeable) {
458-
try {
459-
if (closeable != null) {
460-
closeable.close();
461-
}
462-
} catch (IOException e) {
463-
Log.w(TAG, "Failed to close resource", e);
464-
}
465-
}
466-
467475
public static void closeZip(ZipFile zipFile) {
468476
try {
469477
if (zipFile != null) {
@@ -491,7 +499,7 @@ public static boolean checkResourceArscMd5(File resOutput, String destMd5) {
491499
return true;
492500
}
493501
} finally {
494-
SharePatchFileUtil.closeQuietly(inputStream);
502+
closeQuietly(inputStream);
495503
}
496504

497505
} catch (Throwable e) {

0 commit comments

Comments
 (0)