package com.mbridge.msdk.foundation.tools; import android.text.TextUtils; import com.mbridge.msdk.MBridgeConstans; import com.mbridge.msdk.c.b; import com.mbridge.msdk.foundation.controller.a; import com.mbridge.msdk.foundation.same.b.c; import com.mbridge.msdk.foundation.same.c.e; import java.io.BufferedReader; import java.io.File; import java.io.FileFilter; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Enumeration; import java.util.Iterator; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; /* compiled from: SameFileTool */ public final class u extends f { /* JADX WARNING: Removed duplicated region for block: B:23:0x0038 */ /* JADX WARNING: Removed duplicated region for block: B:26:0x003e A[SYNTHETIC, Splitter:B:26:0x003e] */ private static long c(File file) throws Exception { Throwable th; Exception e; long j = 0; FileInputStream fileInputStream = null; try { if (file.exists()) { FileInputStream fileInputStream2 = new FileInputStream(file); try { j = (long) fileInputStream2.available(); fileInputStream = fileInputStream2; } catch (Exception e2) { e = e2; fileInputStream = fileInputStream2; try { e.printStackTrace(); if (fileInputStream != null) { } return j; } catch (Throwable th2) { th = th2; if (fileInputStream != null) { try { fileInputStream.close(); } catch (Exception e3) { e3.printStackTrace(); } } throw th; } } catch (Throwable th3) { th = th3; fileInputStream = fileInputStream2; if (fileInputStream != null) { } throw th; } } else { file.createNewFile(); w.d("获取文件大小", "文件不存在!"); } if (fileInputStream != null) { try { fileInputStream.close(); } catch (Exception e4) { e4.printStackTrace(); } } } catch (Exception e5) { e = e5; e.printStackTrace(); if (fileInputStream != null) { fileInputStream.close(); } return j; } return j; } private static long d(File file) throws Exception { long j; File[] listFiles = file.listFiles(); long j2 = 0; if (listFiles != null) { for (int i = 0; i < listFiles.length; i++) { if (listFiles[i].isDirectory()) { j = d(listFiles[i]); } else { j = c(listFiles[i]); } j2 += j; } } return j2; } /* JADX WARNING: Code restructure failed: missing block: B:22:0x0041, code lost: if (r1 == null) goto L_0x0044; */ /* JADX WARNING: Removed duplicated region for block: B:30:0x0050 A[SYNTHETIC, Splitter:B:30:0x0050] */ public static String a(File file) { Throwable th; StringBuffer stringBuffer; IOException e; BufferedReader bufferedReader; BufferedReader bufferedReader2 = null; if (file == null) { return null; } try { bufferedReader = new BufferedReader(new FileReader(file)); try { stringBuffer = new StringBuffer(); while (true) { try { String readLine = bufferedReader.readLine(); if (readLine != null) { stringBuffer.append(readLine + "\n"); } } catch (IOException e2) { e = e2; try { e.printStackTrace(); } catch (Throwable th2) { th = th2; bufferedReader2 = bufferedReader; if (bufferedReader2 != null) { } throw th; } } try { break; } catch (IOException unused) { } } } catch (IOException e3) { e = e3; stringBuffer = null; e.printStackTrace(); } } catch (IOException e4) { e = e4; stringBuffer = null; bufferedReader = null; e.printStackTrace(); } catch (Throwable th3) { th = th3; if (bufferedReader2 != null) { try { bufferedReader2.close(); } catch (IOException unused2) { } } throw th; } bufferedReader.close(); if (stringBuffer != null) { return stringBuffer.toString(); } return null; } public static boolean a(String str) { if (e.a(str)) { return false; } File file = new File(str); if (!file.exists() || !file.isFile()) { return false; } return true; } public static File[] b(String str) { try { File file = new File(str); if (file.exists()) { return file.listFiles(); } return null; } catch (Exception unused) { return null; } } public static void a() { new Thread(new Runnable() { /* class com.mbridge.msdk.foundation.tools.u.AnonymousClass1 */ public final void run() { try { if (a.f().j() != null) { String b = com.mbridge.msdk.foundation.same.b.e.b(c.MBRIDGE_VC); try { File file = new File(b); if (file.exists() && file.isDirectory()) { File[] b2 = u.b(b); for (File file2 : b2) { if (file2.exists() && file2.isFile()) { file2.delete(); } } } } catch (Exception e) { e.printStackTrace(); } } } catch (Exception e2) { if (MBridgeConstans.DEBUG) { e2.printStackTrace(); } } } }).start(); } public static void c(String str) { try { if (!TextUtils.isEmpty(str)) { com.mbridge.msdk.c.a b = b.a().b(a.f().k()); if (b == null) { b = b.a().b(); } long currentTimeMillis = System.currentTimeMillis() - ((long) (b.V() * 1000)); File file = new File(str); if (file.exists() && file.isDirectory()) { long currentTimeMillis2 = System.currentTimeMillis(); File[] listFiles = file.listFiles(); if (listFiles != null) { for (File file2 : listFiles) { if (file2.lastModified() + currentTimeMillis < currentTimeMillis2) { b(file2); try { File file3 = new File(str + ".zip"); if (file3.exists() && file3.isFile()) { b(file3); } } catch (Exception unused) { } } } } } } } catch (Exception unused2) { } } public static void b() { AnonymousClass2 r0 = new Runnable() { /* class com.mbridge.msdk.foundation.tools.u.AnonymousClass2 */ public final void run() { try { com.mbridge.msdk.c.a b = b.a().b(a.f().k()); if (b == null) { b = b.a().b(); } u.a(com.mbridge.msdk.foundation.same.b.e.b(c.MBRIDGE_RES_MANAGER_DIR), b.p()); u.a(System.currentTimeMillis() - ((long) (b.V() * 1000))); } catch (Exception e) { if (MBridgeConstans.DEBUG) { e.printStackTrace(); } } } }; if (com.mbridge.msdk.foundation.controller.b.a().d()) { com.mbridge.msdk.foundation.same.f.b.a().execute(r0); } else { r0.run(); } } public static String b(File file) { try { if (file.isFile()) { file.delete(); return ""; } else if (!file.isDirectory()) { return ""; } else { File[] listFiles = file.listFiles(); if (listFiles != null) { if (listFiles.length != 0) { for (File file2 : listFiles) { b(file2); } file.delete(); return ""; } } file.delete(); return ""; } } catch (Exception e) { return e.getMessage(); } } private static ArrayList<File> e(File file) { ArrayList<File> arrayList = new ArrayList<>(); File[] listFiles = file.listFiles(new FileFilter() { /* class com.mbridge.msdk.foundation.tools.u.AnonymousClass3 */ public final boolean accept(File file) { if (!file.isHidden() || file.isDirectory()) { return true; } return false; } }); for (File file2 : listFiles) { if (!file2.isDirectory()) { arrayList.add(file2); } else { arrayList.addAll(e(file2)); } } return arrayList; } private static void e(String str) { try { ArrayList<File> e = e(new File(str)); Collections.sort(e, new Comparator<File>() { /* class com.mbridge.msdk.foundation.tools.u.AnonymousClass4 */ public final boolean equals(Object obj) { return true; } /* JADX DEBUG: Method arguments types fixed to match base method, original types: [java.lang.Object, java.lang.Object] */ @Override // java.util.Comparator public final /* synthetic */ int compare(File file, File file2) { int i = ((file.lastModified() - file2.lastModified()) > 0 ? 1 : ((file.lastModified() - file2.lastModified()) == 0 ? 0 : -1)); if (i > 0) { return 1; } return i == 0 ? 0 : -1; } }); int size = (e.size() - 1) / 2; for (int i = 0; i < size; i++) { File file = e.get(i); if (file.exists() && file.isFile()) { file.delete(); } } } catch (Exception unused) { w.d("SameFileTool", "del memory failed"); } } public static void a(long j) { try { Iterator<File> it = e(new File(com.mbridge.msdk.foundation.same.b.e.b(c.MBRIDGE_RES_MANAGER_DIR))).iterator(); while (it.hasNext()) { File next = it.next(); if (next.lastModified() < j && next.exists() && next.isFile()) { next.delete(); } } } catch (Throwable th) { w.b("SameFileTool", th.getMessage(), th); } } /* JADX WARNING: Removed duplicated region for block: B:24:0x0035 A[SYNTHETIC, Splitter:B:24:0x0035] */ /* JADX WARNING: Removed duplicated region for block: B:30:0x0040 A[SYNTHETIC, Splitter:B:30:0x0040] */ public static boolean a(byte[] bArr, File file) { Throwable th; Exception e; FileOutputStream fileOutputStream = null; try { if (file.getParentFile() != null && !file.exists()) { file.getParentFile().mkdirs(); } FileOutputStream fileOutputStream2 = new FileOutputStream(file); try { fileOutputStream2.write(bArr); try { fileOutputStream2.close(); return true; } catch (IOException e2) { e2.printStackTrace(); return true; } } catch (Exception e3) { e = e3; fileOutputStream = fileOutputStream2; try { e.printStackTrace(); if (fileOutputStream != null) { } return false; } catch (Throwable th2) { th = th2; if (fileOutputStream != null) { } throw th; } } catch (Throwable th3) { th = th3; fileOutputStream = fileOutputStream2; if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e4) { e4.printStackTrace(); } } throw th; } } catch (Exception e5) { e = e5; e.printStackTrace(); if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e6) { e6.printStackTrace(); } } return false; } } public static String d(String str) { return !TextUtils.isEmpty(str) ? SameMD5.getMD5(ag.a(str.trim())) : ""; } /* JADX WARNING: Removed duplicated region for block: B:77:0x00f2 A[Catch:{ all -> 0x010e }] */ /* JADX WARNING: Removed duplicated region for block: B:80:0x00fb A[SYNTHETIC, Splitter:B:80:0x00fb] */ /* JADX WARNING: Removed duplicated region for block: B:85:0x0105 A[SYNTHETIC, Splitter:B:85:0x0105] */ /* JADX WARNING: Removed duplicated region for block: B:92:0x0111 A[SYNTHETIC, Splitter:B:92:0x0111] */ /* JADX WARNING: Removed duplicated region for block: B:97:0x011b A[SYNTHETIC, Splitter:B:97:0x011b] */ public static String a(String str, String str2) { Throwable th; FileOutputStream fileOutputStream; IOException e; if (str == null || str2 == null) { return "unzip srcFile or destDir is null "; } if (!str2.endsWith("/")) { str2 = str2 + "/"; } File file = new File(str); if (!file.exists()) { return "unzip file not exists"; } InputStream inputStream = null; try { ZipFile zipFile = new ZipFile(file); Enumeration<? extends ZipEntry> entries = zipFile.entries(); fileOutputStream = null; while (true) { try { String str3 = ""; if (entries.hasMoreElements()) { ZipEntry zipEntry = (ZipEntry) entries.nextElement(); if (zipEntry == null) { if (inputStream != null) { try { inputStream.close(); } catch (IOException e2) { e2.printStackTrace(); } } if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e3) { e3.printStackTrace(); } } return "unzip zipEntry is null"; } File file2 = new File(str2 + zipEntry.getName()); try { str3 = file2.getCanonicalPath(); } catch (IOException unused) { } if (!TextUtils.isEmpty(str3) && !str3.startsWith("..") && !str3.startsWith("../")) { if (zipEntry.isDirectory()) { file2.mkdirs(); } else { if (!file2.getParentFile().exists()) { file2.getParentFile().mkdirs(); } FileOutputStream fileOutputStream2 = new FileOutputStream(file2); try { inputStream = zipFile.getInputStream(zipEntry); byte[] bArr = new byte[1024]; while (true) { int read = inputStream.read(bArr, 0, 1024); if (read == -1) { break; } fileOutputStream2.write(bArr, 0, read); fileOutputStream2.flush(); } fileOutputStream = fileOutputStream2; } catch (IOException e4) { e = e4; fileOutputStream = fileOutputStream2; try { if (MBridgeConstans.DEBUG) { } String message = e.getMessage(); if (inputStream != null) { } if (fileOutputStream != null) { } return message; } catch (Throwable th2) { th = th2; if (inputStream != null) { try { inputStream.close(); } catch (IOException e5) { e5.printStackTrace(); } } if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e6) { e6.printStackTrace(); } } throw th; } } catch (Throwable th3) { th = th3; fileOutputStream = fileOutputStream2; if (inputStream != null) { } if (fileOutputStream != null) { } throw th; } } } } else { zipFile.close(); if (inputStream != null) { try { inputStream.close(); } catch (IOException e7) { e7.printStackTrace(); } } if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e8) { e8.printStackTrace(); } } return str3; } } catch (IOException e9) { e = e9; if (MBridgeConstans.DEBUG) { } String message2 = e.getMessage(); if (inputStream != null) { } if (fileOutputStream != null) { } return message2; } } } catch (IOException e10) { e = e10; fileOutputStream = null; if (MBridgeConstans.DEBUG) { e.printStackTrace(); } String message22 = e.getMessage(); if (inputStream != null) { try { inputStream.close(); } catch (IOException e11) { e11.printStackTrace(); } } if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e12) { e12.printStackTrace(); } } return message22; } catch (Throwable th4) { th = th4; fileOutputStream = null; if (inputStream != null) { } if (fileOutputStream != null) { } throw th; } } protected static boolean a(String str, int i, int i2, int i3) { try { Class.forName("android.os.FileUtils").getMethod("setPermissions", String.class, Integer.TYPE, Integer.TYPE, Integer.TYPE).invoke(null, str, Integer.valueOf(i), -1, -1); return true; } catch (ClassNotFoundException e) { w.a("SameFileTool", "error when set permissions:", e); return false; } catch (NoSuchMethodException e2) { w.a("SameFileTool", "error when set permissions:", e2); return false; } catch (IllegalArgumentException e3) { w.a("SameFileTool", "error when set permissions:", e3); return false; } catch (IllegalAccessException e4) { w.a("SameFileTool", "error when set permissions:", e4); return false; } catch (InvocationTargetException e5) { w.a("SameFileTool", "error when set permissions:", e5); return false; } } static /* synthetic */ void a(String str, int i) { try { if (d(new File(str)) > ((long) (i * 1048576))) { e(str); } } catch (Exception e) { e.printStackTrace(); } catch (Throwable unused) { w.d("SameFileTool", "clean memory failed"); } } }