Move GlProgram.loadAsset to Util and make it public.

PiperOrigin-RevId: 567025091
This commit is contained in:
Googler 2023-09-20 11:19:36 -07:00 committed by Copybara-Service
parent 7b580d3cf8
commit bfd4b6a188
2 changed files with 27 additions and 20 deletions

View file

@ -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));
}
/**

View file

@ -969,6 +969,30 @@ public final class Util {
return normalizedTag;
}
/**
* Loads a file from the assets folder.
*
* <p>This should only be used for known-small files. Generally, loading assets should be done
* with {@code AssetDataSource}.
*
* <p>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.
*