Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(317)

Unified Diff: base/android/java/src/org/chromium/base/FileUtils.java

Issue 2006703004: Merge code which extracts assets from APK to file (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/android/java/src/org/chromium/base/FileUtils.java
diff --git a/base/android/java/src/org/chromium/base/FileUtils.java b/base/android/java/src/org/chromium/base/FileUtils.java
index fbaec8c69e31044084a0a14920a38f0ca3b391a8..2c0d8f59fac9ae329a3a803d7ce66f7361a3f07d 100644
--- a/base/android/java/src/org/chromium/base/FileUtils.java
+++ b/base/android/java/src/org/chromium/base/FileUtils.java
@@ -4,7 +4,14 @@
package org.chromium.base;
+import android.content.Context;
+
+import java.io.BufferedOutputStream;
import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
/**
* Helper methods for dealing with Files.
@@ -28,4 +35,42 @@ public class FileUtils {
if (!currentFile.delete()) Log.e(TAG, "Failed to delete: " + currentFile);
}
+
+ /**
+ * Extracts an asset from the app's APK to a file.
+ * @param context
+ * @param assetName Name of the asset to extract.
+ * @param dest File to extract the asset to.
+ * @return true on success.
+ */
+ public static boolean extractAsset(Context context, String assetName, File dest) {
+ InputStream inputStream = null;
+ OutputStream outputStream = null;
+ try {
+ inputStream = context.getAssets().open(assetName);
+ outputStream = new BufferedOutputStream(new FileOutputStream(dest));
+ byte[] buffer = new byte[8192];
+ int c;
+ while ((c = inputStream.read(buffer)) != -1) {
+ outputStream.write(buffer, 0, c);
+ }
+ inputStream.close();
+ outputStream.close();
+ return true;
+ } catch (IOException e) {
+ if (inputStream != null) {
+ try {
+ inputStream.close();
+ } catch (IOException ex) {
+ }
+ }
+ if (outputStream != null) {
+ try {
+ outputStream.close();
+ } catch (IOException ex) {
+ }
+ }
+ }
+ return false;
+ }
}

Powered by Google App Engine
This is Rietveld 408576698