From bfd4b6a1881047eeb75cb83f206fba53c0e8ecb2 Mon Sep 17 00:00:00 2001 From: Googler Date: Wed, 20 Sep 2023 11:19:36 -0700 Subject: [PATCH] Move GlProgram.loadAsset to Util and make it public. PiperOrigin-RevId: 567025091 --- .../media3/common/util/GlProgram.java | 23 +++--------------- .../androidx/media3/common/util/Util.java | 24 +++++++++++++++++++ 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/libraries/common/src/main/java/androidx/media3/common/util/GlProgram.java b/libraries/common/src/main/java/androidx/media3/common/util/GlProgram.java index e343a07e20..34667faf3e 100644 --- a/libraries/common/src/main/java/androidx/media3/common/util/GlProgram.java +++ b/libraries/common/src/main/java/androidx/media3/common/util/GlProgram.java @@ -22,7 +22,6 @@ import android.opengl.GLES11Ext; import android.opengl.GLES20; import androidx.annotation.Nullable; import java.io.IOException; -import java.io.InputStream; import java.nio.Buffer; import java.util.HashMap; import java.util.Map; @@ -57,25 +56,9 @@ public final class GlProgram { */ public GlProgram(Context context, String vertexShaderFilePath, String fragmentShaderFilePath) throws IOException, GlUtil.GlException { - this(loadAsset(context, vertexShaderFilePath), loadAsset(context, fragmentShaderFilePath)); - } - - /** - * Loads a file from the assets folder. - * - * @param context The {@link Context}. - * @param assetPath The path to the file to load, from the assets folder. - * @return The content of the file to load. - * @throws IOException If the file couldn't be read. - */ - private static String loadAsset(Context context, String assetPath) throws IOException { - @Nullable InputStream inputStream = null; - try { - inputStream = context.getAssets().open(assetPath); - return Util.fromUtf8Bytes(Util.toByteArray(inputStream)); - } finally { - Util.closeQuietly(inputStream); - } + this( + Util.loadAsset(context, vertexShaderFilePath), + Util.loadAsset(context, fragmentShaderFilePath)); } /** diff --git a/libraries/common/src/main/java/androidx/media3/common/util/Util.java b/libraries/common/src/main/java/androidx/media3/common/util/Util.java index d0703b76e4..fa0e776ce1 100644 --- a/libraries/common/src/main/java/androidx/media3/common/util/Util.java +++ b/libraries/common/src/main/java/androidx/media3/common/util/Util.java @@ -969,6 +969,30 @@ public final class Util { return normalizedTag; } + /** + * Loads a file from the assets folder. + * + *

This should only be used for known-small files. Generally, loading assets should be done + * with {@code AssetDataSource}. + * + *

The file is assumed to be encoded in UTF-8. + * + * @param context The {@link Context}. + * @param assetPath The path to the file to load, from the assets folder. + * @return The content of the file to load. + * @throws IOException If the file couldn't be read. + */ + @UnstableApi + public static String loadAsset(Context context, String assetPath) throws IOException { + @Nullable InputStream inputStream = null; + try { + inputStream = context.getAssets().open(assetPath); + return Util.fromUtf8Bytes(Util.toByteArray(inputStream)); + } finally { + Util.closeQuietly(inputStream); + } + } + /** * Returns a new {@link String} constructed by decoding UTF-8 encoded bytes. *