OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.net; | 5 package org.chromium.net; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.content.res.AssetManager; | 8 import android.content.res.AssetManager; |
9 import android.util.Log; | 9 import android.util.Log; |
10 | 10 |
| 11 import org.chromium.base.FileUtils; |
11 import org.chromium.base.PathUtils; | 12 import org.chromium.base.PathUtils; |
12 import org.chromium.base.annotations.SuppressFBWarnings; | 13 import org.chromium.base.annotations.SuppressFBWarnings; |
13 | 14 |
14 import java.io.File; | 15 import java.io.File; |
15 import java.io.FileOutputStream; | |
16 import java.io.IOException; | 16 import java.io.IOException; |
17 import java.io.InputStream; | |
18 import java.io.OutputStream; | |
19 | 17 |
20 /** | 18 /** |
21 * Helper class to install test files. | 19 * Helper class to install test files. |
22 */ | 20 */ |
23 public final class TestFilesInstaller { | 21 public final class TestFilesInstaller { |
24 private static final String TAG = "TestFilesInstaller"; | 22 private static final String TAG = "TestFilesInstaller"; |
25 // Name of the asset directory in which test files are stored. | 23 // Name of the asset directory in which test files are stored. |
26 private static final String TEST_FILE_ASSET_PATH = "test"; | 24 private static final String TEST_FILE_ASSET_PATH = "test"; |
27 | 25 |
28 /** | 26 /** |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 * @params context Application context | 60 * @params context Application context |
63 * @params path | 61 * @params path |
64 */ | 62 */ |
65 private static void install(Context context, String path) throws IOException
{ | 63 private static void install(Context context, String path) throws IOException
{ |
66 AssetManager assetManager = context.getAssets(); | 64 AssetManager assetManager = context.getAssets(); |
67 String files[] = assetManager.list(path); | 65 String files[] = assetManager.list(path); |
68 Log.i(TAG, "Loading " + path + " ..."); | 66 Log.i(TAG, "Loading " + path + " ..."); |
69 String root = PathUtils.getDataDirectory(context); | 67 String root = PathUtils.getDataDirectory(context); |
70 if (files.length == 0) { | 68 if (files.length == 0) { |
71 // The path is a file, so copy the file now. | 69 // The path is a file, so copy the file now. |
72 copyTestFile(assetManager, path, root + "/" + path); | 70 copyTestFile(context, path, root + "/" + path); |
73 } else { | 71 } else { |
74 // The path is a directory, so recursively handle its files, since | 72 // The path is a directory, so recursively handle its files, since |
75 // the directory can contain subdirectories. | 73 // the directory can contain subdirectories. |
76 String fullPath = root + "/" + path; | 74 String fullPath = root + "/" + path; |
77 File dir = new File(fullPath); | 75 File dir = new File(fullPath); |
78 if (!dir.exists()) { | 76 if (!dir.exists()) { |
79 Log.i(TAG, "Creating directory " + fullPath + " ..."); | 77 Log.i(TAG, "Creating directory " + fullPath + " ..."); |
80 if (!dir.mkdir()) { | 78 if (!dir.mkdir()) { |
81 throw new IOException("Directory not created."); | 79 throw new IOException("Directory not created."); |
82 } | 80 } |
83 } | 81 } |
84 for (int i = 0; i < files.length; i++) { | 82 for (int i = 0; i < files.length; i++) { |
85 install(context, path + "/" + files[i]); | 83 install(context, path + "/" + files[i]); |
86 } | 84 } |
87 } | 85 } |
88 } | 86 } |
89 /** | 87 /** |
90 * Copies a file from assets to the device's file system. | 88 * Copies a file from assets to the device's file system. |
91 * @param assetManager AssetManager of the application. | 89 * @param context |
92 * @param srcFilePath the source file path in assets. | 90 * @param srcFilePath the source file path in assets. |
93 * @param destFilePath the destination file path. | 91 * @param destFilePath the destination file path. |
94 * @throws IllegalStateException if the destination file already exists. | 92 * @throws IllegalStateException if the destination file already exists. |
95 */ | 93 */ |
96 @SuppressFBWarnings("OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE") | 94 @SuppressFBWarnings("OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE") |
97 private static void copyTestFile(AssetManager assetManager, | 95 private static void copyTestFile(Context context, String srcFilePath, String
destFilePath) |
98 String srcFilePath, | 96 throws IOException { |
99 String destFilePath) throws IOException { | |
100 File destFile = new File(destFilePath); | 97 File destFile = new File(destFilePath); |
101 if (destFile.exists()) { | 98 if (destFile.exists()) { |
102 throw new IllegalStateException(srcFilePath + " already exists"); | 99 throw new IllegalStateException(srcFilePath + " already exists."); |
103 } | 100 } |
104 OutputStream out = new FileOutputStream(destFilePath); | |
105 InputStream in = assetManager.open(srcFilePath); | |
106 | 101 |
107 byte[] buffer = new byte[1024]; | 102 if (!FileUtils.extractAsset(context, srcFilePath, destFile)) { |
108 int read; | 103 throw new IOException("Could not extract asset " + srcFilePath + "."
); |
109 while ((read = in.read(buffer)) != -1) { | |
110 out.write(buffer, 0, read); | |
111 } | 104 } |
112 in.close(); | |
113 out.flush(); | |
114 out.close(); | |
115 } | 105 } |
116 } | 106 } |
OLD | NEW |