Skip to content

Commit 5efa7d1

Browse files
committed
Don't read Bundled models with GZIPInputStream.
1 parent 5fd3fa1 commit 5efa7d1

File tree

4 files changed

+29
-16
lines changed

4 files changed

+29
-16
lines changed
11.9 KB
Binary file not shown.

improveai-android/src/androidTest/java/ai/improve/android/DownloaderTest.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,24 @@
66
import org.junit.runner.RunWith;
77

88
import java.io.IOException;
9-
import java.net.HttpURLConnection;
109
import java.net.URL;
1110

11+
import ai.improve.DecisionModel;
12+
1213
@RunWith(AndroidJUnit4.class)
1314
public class DownloaderTest {
1415

1516
@Test
16-
public void testDownloadWithCache() {
17-
try {
18-
HttpURLConnection urlConnection = (HttpURLConnection) new URL("http://10.254.115.144:8080/dummy_v6.xgb").openConnection();
19-
urlConnection.setReadTimeout(15000);
20-
urlConnection.getInputStream();
21-
} catch (IOException e) {
22-
e.printStackTrace();
23-
}
17+
public void testLoad_asset() throws IOException {
18+
URL modelUrl = new URL("file:///android_asset/dummy_v6.xgb");
19+
DecisionModel decisionModel = new DecisionModel("dummy");
20+
decisionModel.load(modelUrl);
21+
}
22+
23+
@Test
24+
public void testLoad_asset_gzip() throws IOException {
25+
URL modelUrl = new URL("file:///android_asset/dummy_v5.xgb");
26+
DecisionModel decisionModel = new DecisionModel("dummy");
27+
decisionModel.load(modelUrl);
2428
}
2529
}

improveai/src/main/java/ai/improve/DecisionModel.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ public class DecisionModel {
5858
* It's an equivalent of DecisionModel(modelName, defaultTrackURL, defaultTrackApiKey)
5959
* We suggest to have the defaultTrackURL/defaultTrackApiKey set on startup before creating
6060
* any DecisionModel instances.
61+
* @param modelName Length of modelName must be in range [1, 64]; Only alphanumeric
62+
* characters([a-zA-Z0-9]), '-', '.' and '_' are allowed in the modelName
63+
* and the first character must be an alphanumeric one.
6164
* */
6265
public DecisionModel(String modelName) {
6366
this(modelName, sDefaultTrackURL, sDefaultTrackApiKey);
@@ -66,7 +69,7 @@ public DecisionModel(String modelName) {
6669
/**
6770
* @param modelName Length of modelName must be in range [1, 64]; Only alphanumeric
6871
* characters([a-zA-Z0-9]), '-', '.' and '_' are allowed in the modelName
69-
* and the first character must be an alphanumeric one;
72+
* and the first character must be an alphanumeric one.
7073
* @param trackURL url for tracking decisions. If trackURL is null, no decisions would be
7174
* tracked. If trackURL is not a valid URL, an exception would be thrown.
7275
* @param trackApiKey will be attached to the header fields of all the post request for tracking
@@ -137,6 +140,12 @@ public void loadAsync(URL modelUrl) {
137140
* may cause leaks. Before we add a cancel method to allow aborting downloading tasks, you may
138141
* have to call loadAsync() in something like a Singleton class.
139142
* @deprecated The callback method signature will likely have to change for multiple URLs
143+
* @param modelUrl A url that can be a local file path, a remote http url that points to a
144+
* model file, or a bundled asset. Urls that ends with '.gz' are considered gzip
145+
* compressed, and will be handled appropriately. Bundled model asset urls
146+
* appears a bit different. Suppose that you have a bundled model file in folder
147+
* "assets/models/my_model.xgb.gz", then modelUrl should be
148+
* new URL("file:///android_asset/models/my_model.xgb").
140149
* */
141150
@Deprecated
142151
public void loadAsync(URL modelUrl, LoadListener listener) {

improveai/src/main/java/ai/improve/downloader/ModelDownloader.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,7 @@ public void run() {
5858
// When running in pure Java, new URL("file:///android_asset/") is interpreted
5959
// as new URL("file:/android_asset").
6060
String path = urlString.substring("file:///android_asset/".length());
61-
if(urlString.endsWith(".gz")) {
62-
inputStream = new GZIPInputStream(assetModelLoader.load(path));
63-
} else {
64-
inputStream = assetModelLoader.load(path);
65-
}
61+
inputStream = assetModelLoader.load(path);
6662
ImprovePredictor predictor = new ImprovePredictor(inputStream);
6763
if(listener != null) {
6864
listener.onFinish(predictor, null);
@@ -87,7 +83,11 @@ public void run() {
8783
e.printStackTrace();
8884
IMPLog.e(Tag, url + ", model download exception: " + e.getMessage());
8985
if(listener != null) {
90-
listener.onFinish(null, new IOException(e.getMessage()));
86+
if(e instanceof IOException) {
87+
listener.onFinish(null, (IOException) e);
88+
} else {
89+
listener.onFinish(null, new IOException(e.getMessage()));
90+
}
9191
}
9292
} finally {
9393
if(inputStream != null) {

0 commit comments

Comments
 (0)