aboutsummaryrefslogtreecommitdiff
path: root/tensorflow_lite_support/java/src/javatests/org/tensorflow/lite/task/core/TestUtils.java
diff options
context:
space:
mode:
Diffstat (limited to 'tensorflow_lite_support/java/src/javatests/org/tensorflow/lite/task/core/TestUtils.java')
-rw-r--r--tensorflow_lite_support/java/src/javatests/org/tensorflow/lite/task/core/TestUtils.java50
1 files changed, 50 insertions, 0 deletions
diff --git a/tensorflow_lite_support/java/src/javatests/org/tensorflow/lite/task/core/TestUtils.java b/tensorflow_lite_support/java/src/javatests/org/tensorflow/lite/task/core/TestUtils.java
new file mode 100644
index 00000000..4c6f369d
--- /dev/null
+++ b/tensorflow_lite_support/java/src/javatests/org/tensorflow/lite/task/core/TestUtils.java
@@ -0,0 +1,50 @@
+package org.tensorflow.lite.task.core;
+
+import android.content.Context;
+import android.content.res.AssetManager;
+
+import com.google.common.io.ByteStreams;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+
+/** Helper class for the Java test in Task Libary. */
+public final class TestUtils {
+
+ /**
+ * Loads the file and create a {@link File} object by reading a file from the asset directory.
+ * Simulates downloading or reading a file that's not precompiled with the app.
+ *
+ * @return a {@link File} object for the model.
+ */
+ public static File loadFile(Context context, String fileName) {
+ File target = new File(context.getFilesDir(), fileName);
+ try (InputStream is = context.getAssets().open(fileName);
+ FileOutputStream os = new FileOutputStream(target)) {
+ ByteStreams.copy(is, os);
+ } catch (IOException e) {
+ throw new AssertionError("Failed to load model file at " + fileName, e);
+ }
+ return target;
+ }
+
+ /**
+ * Reads a file into a direct {@link ByteBuffer} object from the asset directory.
+ *
+ * @return a {@link ByteBuffer} object for the file.
+ */
+ public static ByteBuffer loadToDirectByteBuffer(Context context, String fileName)
+ throws IOException {
+ AssetManager assetManager = context.getAssets();
+ InputStream inputStream = assetManager.open(fileName);
+ byte[] bytes = ByteStreams.toByteArray(inputStream);
+
+ ByteBuffer buffer = ByteBuffer.allocateDirect(bytes.length).order(ByteOrder.nativeOrder());
+ buffer.put(bytes);
+ return buffer;
+ }
+} \ No newline at end of file