/docs/weixin_7.0.4_source/反编译源码/未反混淆/src/main/java/com/tencent/tmassistantsdk/storage/TMAssistantFile.java

https://github.com/HelloHuDi/AndroidReverseNotes · Java · 315 lines · 298 code · 16 blank · 1 comment · 67 complexity · f53cd88c8a2ec055cff1742bacb4771c MD5 · raw file

  1. package com.tencent.tmassistantsdk.storage;
  2. import android.content.Context;
  3. import android.os.Environment;
  4. import android.text.TextUtils;
  5. import com.tencent.matrix.trace.core.AppMethodBeat;
  6. import com.tencent.mm.sdk.platformtools.ab;
  7. import com.tencent.tmassistantsdk.util.GlobalUtil;
  8. import com.tencent.tmassistantsdk.util.TMLog;
  9. import java.io.File;
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12. import java.security.InvalidParameterException;
  13. public class TMAssistantFile {
  14. protected static final int DataBufferMaxLen = 16384;
  15. protected static final String TAG = "TMAssistantFile";
  16. protected int mDataBufferDataLen = 0;
  17. protected long mFileDataLen = 0;
  18. protected FileOutputStream mFileOutputStream = null;
  19. protected String mFinalFileName = null;
  20. protected String mTempFileName = null;
  21. protected byte[] mWriteDataBuffer = null;
  22. public TMAssistantFile(String str, String str2) {
  23. AppMethodBeat.i(76168);
  24. this.mTempFileName = str;
  25. this.mFinalFileName = str2;
  26. this.mFileDataLen = length();
  27. TMLog.i(TAG, "mFileDataLen = " + this.mFileDataLen);
  28. AppMethodBeat.o(76168);
  29. }
  30. public void deleteTempFile() {
  31. AppMethodBeat.i(76169);
  32. String tmpFilePath = getTmpFilePath(this.mTempFileName);
  33. TMLog.i(TAG, "deleteFile 1 tmpFilePathString: ".concat(String.valueOf(tmpFilePath)));
  34. if (!TextUtils.isEmpty(tmpFilePath)) {
  35. File file = new File(tmpFilePath);
  36. TMLog.i(TAG, "deleteFile 2 file: ".concat(String.valueOf(file)));
  37. if (file.exists()) {
  38. TMLog.i(TAG, "deleteFile result:" + file.delete() + ",filename:" + tmpFilePath);
  39. AppMethodBeat.o(76169);
  40. return;
  41. }
  42. TMLog.i(TAG, "deleteFile 3");
  43. }
  44. AppMethodBeat.o(76169);
  45. }
  46. public long length() {
  47. AppMethodBeat.i(76170);
  48. String saveFilePath = getSaveFilePath(this.mFinalFileName);
  49. if (saveFilePath != null) {
  50. File file = new File(saveFilePath);
  51. if (file.exists()) {
  52. this.mFileDataLen = file.length();
  53. } else {
  54. saveFilePath = getTmpFilePath(this.mTempFileName);
  55. if (saveFilePath != null) {
  56. file = new File(saveFilePath);
  57. if (file.exists()) {
  58. TMLog.i(TAG, saveFilePath + " exists");
  59. this.mFileDataLen = file.length();
  60. } else {
  61. this.mFileDataLen = 0;
  62. }
  63. }
  64. }
  65. long j = this.mFileDataLen;
  66. AppMethodBeat.o(76170);
  67. return j;
  68. }
  69. AppMethodBeat.o(76170);
  70. return 0;
  71. }
  72. /* Access modifiers changed, original: 0000 */
  73. public void ensureFilePath(String str) {
  74. boolean z = true;
  75. AppMethodBeat.i(76171);
  76. InvalidParameterException invalidParameterException;
  77. if (TextUtils.isEmpty(str)) {
  78. TMLog.i(TAG, "fileFullPath is null or the filename.size is zero.");
  79. invalidParameterException = new InvalidParameterException("fileFullPath is null or the filename.size is zero.");
  80. AppMethodBeat.o(76171);
  81. throw invalidParameterException;
  82. }
  83. int lastIndexOf = str.lastIndexOf("/");
  84. if (lastIndexOf == -1 || str.length() == 1) {
  85. TMLog.i(TAG, "fileFullPath is not a valid full path. fileName: ".concat(String.valueOf(str)));
  86. invalidParameterException = new InvalidParameterException("fileFullPath is not a valid full path. fileName: ".concat(String.valueOf(str)));
  87. AppMethodBeat.o(76171);
  88. throw invalidParameterException;
  89. }
  90. if (lastIndexOf > 0) {
  91. str = str.substring(0, lastIndexOf);
  92. }
  93. File file = new File(str);
  94. if (!file.exists()) {
  95. z = file.mkdirs();
  96. }
  97. if (z) {
  98. AppMethodBeat.o(76171);
  99. return;
  100. }
  101. TMLog.i(TAG, "Failed to create directory. dir: ".concat(String.valueOf(str)));
  102. IOException iOException = new IOException("Failed to create directory. dir: ".concat(String.valueOf(str)));
  103. AppMethodBeat.o(76171);
  104. throw iOException;
  105. }
  106. public synchronized boolean write(byte[] bArr, int i, int i2, long j, boolean z) {
  107. boolean z2 = false;
  108. synchronized (this) {
  109. AppMethodBeat.i(76172);
  110. if (this.mFileOutputStream == null) {
  111. String tmpFilePath = getTmpFilePath(this.mTempFileName);
  112. if (tmpFilePath != null) {
  113. try {
  114. ensureFilePath(tmpFilePath);
  115. this.mFileOutputStream = new FileOutputStream(tmpFilePath, true);
  116. } catch (Exception e) {
  117. ab.printErrStackTrace(TAG, e, "", new Object[0]);
  118. TMLog.w(TAG, "write failed" + e.getMessage());
  119. AppMethodBeat.o(76172);
  120. }
  121. } else {
  122. TMLog.w(TAG, "write failed tmpFilePathString is null");
  123. AppMethodBeat.o(76172);
  124. }
  125. }
  126. if (this.mWriteDataBuffer == null) {
  127. this.mWriteDataBuffer = new byte[DataBufferMaxLen];
  128. this.mDataBufferDataLen = 0;
  129. }
  130. z2 = writeData(this.mFileOutputStream, bArr, i, i2, j);
  131. if (!z2) {
  132. AppMethodBeat.o(76172);
  133. } else if (z && flush()) {
  134. z2 = moveFileFromTmpToSavaPath(getTmpFilePath(this.mTempFileName), getSaveFilePath(this.mFinalFileName));
  135. AppMethodBeat.o(76172);
  136. } else {
  137. AppMethodBeat.o(76172);
  138. z2 = true;
  139. }
  140. }
  141. return z2;
  142. }
  143. public synchronized boolean flush() {
  144. boolean z = false;
  145. synchronized (this) {
  146. AppMethodBeat.i(76173);
  147. if (this.mFileOutputStream != null && this.mDataBufferDataLen > 0) {
  148. try {
  149. this.mFileOutputStream.write(this.mWriteDataBuffer, 0, this.mDataBufferDataLen);
  150. this.mDataBufferDataLen = 0;
  151. z = true;
  152. AppMethodBeat.o(76173);
  153. } catch (IOException e) {
  154. ab.printErrStackTrace(TAG, e, "", new Object[0]);
  155. TMLog.w(TAG, "flush failed ".concat(String.valueOf(e)));
  156. }
  157. }
  158. AppMethodBeat.o(76173);
  159. }
  160. return z;
  161. }
  162. public synchronized void close() {
  163. AppMethodBeat.i(76174);
  164. flush();
  165. try {
  166. if (this.mFileOutputStream != null) {
  167. this.mFileOutputStream.close();
  168. }
  169. } catch (IOException e) {
  170. ab.printErrStackTrace(TAG, e, "", new Object[0]);
  171. }
  172. this.mFileOutputStream = null;
  173. this.mWriteDataBuffer = null;
  174. this.mFileDataLen = 0;
  175. AppMethodBeat.o(76174);
  176. return;
  177. }
  178. public static String getSaveFilePath(String str) {
  179. AppMethodBeat.i(76175);
  180. if (str == null) {
  181. AppMethodBeat.o(76175);
  182. return null;
  183. }
  184. String savePathRootDir = getSavePathRootDir();
  185. if (savePathRootDir == null) {
  186. AppMethodBeat.o(76175);
  187. return null;
  188. }
  189. String str2 = savePathRootDir + File.separator + str;
  190. AppMethodBeat.o(76175);
  191. return str2;
  192. }
  193. public String getTmpFilePath(String str) {
  194. AppMethodBeat.i(76176);
  195. if (str == null) {
  196. AppMethodBeat.o(76176);
  197. return null;
  198. }
  199. String savePathRootDir = getSavePathRootDir();
  200. if (savePathRootDir == null) {
  201. AppMethodBeat.o(76176);
  202. return null;
  203. }
  204. String str2 = savePathRootDir + "/.tmp/" + str + ".tmp";
  205. AppMethodBeat.o(76176);
  206. return str2;
  207. }
  208. public static String getSavePathRootDir() {
  209. AppMethodBeat.i(76177);
  210. Context context = GlobalUtil.getInstance().getContext();
  211. if (context == null) {
  212. AppMethodBeat.o(76177);
  213. return null;
  214. }
  215. String str;
  216. if (isSDCardExistAndCanWrite()) {
  217. str = Environment.getExternalStorageDirectory().getAbsolutePath() + "/tencent/TMAssistantSDK/Download/".concat(String.valueOf(context.getPackageName()));
  218. } else {
  219. str = context.getFilesDir().getAbsolutePath() + "/TMAssistantSDK/Download";
  220. }
  221. AppMethodBeat.o(76177);
  222. return str;
  223. }
  224. public void moveFileToSavaPath() {
  225. AppMethodBeat.i(76178);
  226. moveFileFromTmpToSavaPath(getTmpFilePath(this.mTempFileName), getSaveFilePath(this.mFinalFileName));
  227. AppMethodBeat.o(76178);
  228. }
  229. public static boolean isSDCardExistAndCanWrite() {
  230. AppMethodBeat.i(76179);
  231. if ("mounted".equals(Environment.getExternalStorageState()) && Environment.getExternalStorageDirectory().canWrite()) {
  232. AppMethodBeat.o(76179);
  233. return true;
  234. }
  235. AppMethodBeat.o(76179);
  236. return false;
  237. }
  238. private boolean writeData(FileOutputStream fileOutputStream, byte[] bArr, int i, int i2, long j) {
  239. AppMethodBeat.i(76180);
  240. if (fileOutputStream == null || bArr == null) {
  241. AppMethodBeat.o(76180);
  242. return false;
  243. } else if (j != this.mFileDataLen) {
  244. TMLog.i(TAG, "writeData0 failed,filelen:" + this.mFileDataLen + ",offset:" + j + ",filename:" + this.mTempFileName);
  245. AppMethodBeat.o(76180);
  246. return false;
  247. } else {
  248. if (i2 >= DataBufferMaxLen) {
  249. try {
  250. if (this.mDataBufferDataLen > 0) {
  251. fileOutputStream.write(this.mWriteDataBuffer, 0, this.mDataBufferDataLen);
  252. this.mDataBufferDataLen = 0;
  253. }
  254. fileOutputStream.write(bArr, i, i2);
  255. this.mFileDataLen += (long) i2;
  256. } catch (IOException e) {
  257. ab.printErrStackTrace(TAG, e, "", new Object[0]);
  258. TMLog.w(TAG, "writeData1 failed " + e.getMessage());
  259. AppMethodBeat.o(76180);
  260. return false;
  261. }
  262. }
  263. if (this.mDataBufferDataLen + i2 > DataBufferMaxLen && this.mDataBufferDataLen > 0) {
  264. try {
  265. fileOutputStream.write(this.mWriteDataBuffer, 0, this.mDataBufferDataLen);
  266. this.mDataBufferDataLen = 0;
  267. } catch (IOException e2) {
  268. ab.printErrStackTrace(TAG, e2, "", new Object[0]);
  269. TMLog.w(TAG, "writeData2 failed " + e2.getMessage());
  270. AppMethodBeat.o(76180);
  271. return false;
  272. }
  273. }
  274. System.arraycopy(bArr, i, this.mWriteDataBuffer, this.mDataBufferDataLen, i2);
  275. this.mDataBufferDataLen += i2;
  276. this.mFileDataLen += (long) i2;
  277. AppMethodBeat.o(76180);
  278. return true;
  279. }
  280. }
  281. private boolean moveFileFromTmpToSavaPath(String str, String str2) {
  282. AppMethodBeat.i(76181);
  283. TMLog.i(TAG, "moveFileFromTmpToSavaPath, tmpFilePath = " + str + ", saveFilePath = " + str2);
  284. if (!(str == null || str2 == null)) {
  285. File file = new File(str);
  286. if (file.exists()) {
  287. boolean renameTo = file.renameTo(new File(str2));
  288. if (renameTo) {
  289. GlobalUtil.updateFilePathAuthorized(str2);
  290. }
  291. AppMethodBeat.o(76181);
  292. return renameTo;
  293. }
  294. }
  295. TMLog.w(TAG, "moveFileFromTmpToSavaPath failed ");
  296. AppMethodBeat.o(76181);
  297. return false;
  298. }
  299. }