use of org.robolectric.res.android.Asset in project robolectric by robolectric.
the class ShadowArscAssetManager10 method nativeOpenNonAsset.
// static jlong NativeOpenNonAsset(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint jcookie,
// jstring asset_path, jint access_mode) {
@Implementation(minSdk = P)
protected static long nativeOpenNonAsset(long ptr, int jcookie, @NonNull String asset_path, int access_mode) throws FileNotFoundException {
ApkAssetsCookie cookie = JavaCookieToApkAssetsCookie(jcookie);
String asset_path_utf8 = asset_path;
if (asset_path_utf8 == null) {
// This will throw NPE.
return 0;
}
ATRACE_NAME(String.format("AssetManager::OpenNonAsset(%s)", asset_path_utf8));
if (access_mode != Asset.AccessMode.ACCESS_UNKNOWN.mode() && access_mode != Asset.AccessMode.ACCESS_RANDOM.mode() && access_mode != Asset.AccessMode.ACCESS_STREAMING.mode() && access_mode != Asset.AccessMode.ACCESS_BUFFER.mode()) {
throw new IllegalArgumentException("Bad access mode");
}
CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
Asset asset;
if (cookie.intValue() != kInvalidCookie) {
asset = assetmanager.OpenNonAsset(asset_path_utf8, cookie, Asset.AccessMode.fromInt(access_mode));
} else {
asset = assetmanager.OpenNonAsset(asset_path_utf8, Asset.AccessMode.fromInt(access_mode));
}
if (!isTruthy(asset)) {
throw new FileNotFoundException(asset_path_utf8);
}
return Registries.NATIVE_ASSET_REGISTRY.register(asset);
}
use of org.robolectric.res.android.Asset in project robolectric by robolectric.
the class ShadowArscAssetManager10 method nativeOpenAsset.
// static jlong NativeOpenAsset(JNIEnv* env, jclass /*clazz*/, jlong ptr, jstring asset_path,
// jint access_mode) {
@Implementation(minSdk = P)
protected static long nativeOpenAsset(long ptr, @NonNull String asset_path, int access_mode) throws FileNotFoundException {
String asset_path_utf8 = asset_path;
if (asset_path_utf8 == null) {
// This will throw NPE.
return 0;
}
ATRACE_NAME(String.format("AssetManager::OpenAsset(%s)", asset_path_utf8));
if (access_mode != Asset.AccessMode.ACCESS_UNKNOWN.mode() && access_mode != Asset.AccessMode.ACCESS_RANDOM.mode() && access_mode != Asset.AccessMode.ACCESS_STREAMING.mode() && access_mode != Asset.AccessMode.ACCESS_BUFFER.mode()) {
throw new IllegalArgumentException("Bad access mode");
}
CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
Asset asset = assetmanager.Open(asset_path_utf8, Asset.AccessMode.fromInt(access_mode));
if (!isTruthy(asset)) {
throw new FileNotFoundException(asset_path_utf8);
}
return Registries.NATIVE_ASSET_REGISTRY.register(asset);
}
use of org.robolectric.res.android.Asset in project robolectric by robolectric.
the class ShadowArscAssetManager10 method nativeOpenXmlAsset.
// static jlong NativeOpenXmlAsset(JNIEnv* env, jobject /*clazz*/, jlong ptr, jint jcookie,
// jstring asset_path) {
@Implementation(minSdk = P)
protected static long nativeOpenXmlAsset(long ptr, int jcookie, @NonNull String asset_path) throws FileNotFoundException {
ApkAssetsCookie cookie = JavaCookieToApkAssetsCookie(jcookie);
String asset_path_utf8 = asset_path;
if (asset_path_utf8 == null) {
// This will throw NPE.
return 0;
}
ATRACE_NAME(String.format("AssetManager::OpenXmlAsset(%s)", asset_path_utf8));
CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
Asset asset;
if (cookie.intValue() != kInvalidCookie) {
asset = assetmanager.OpenNonAsset(asset_path_utf8, cookie, Asset.AccessMode.ACCESS_RANDOM);
} else {
Ref<ApkAssetsCookie> cookieRef = new Ref<>(cookie);
asset = assetmanager.OpenNonAsset(asset_path_utf8, Asset.AccessMode.ACCESS_RANDOM, cookieRef);
cookie = cookieRef.get();
}
if (!isTruthy(asset)) {
throw new FileNotFoundException(asset_path_utf8);
}
// May be nullptr.
DynamicRefTable dynamic_ref_table = assetmanager.GetDynamicRefTableForCookie(cookie);
ResXMLTree xml_tree = new ResXMLTree(dynamic_ref_table);
int err = xml_tree.setTo(asset.getBuffer(true), (int) asset.getLength(), true);
if (err != NO_ERROR) {
throw new FileNotFoundException("Corrupt XML binary file");
}
return NATIVE_RES_XML_TREES.register(xml_tree);
}
use of org.robolectric.res.android.Asset in project robolectric by robolectric.
the class ShadowArscAssetManager10 method nativeAssetRead.
// static jint NativeAssetRead(JNIEnv* env, jclass /*clazz*/, jlong asset_ptr, jbyteArray
// java_buffer,
// jint offset, jint len) {
@Implementation(minSdk = P)
protected static int nativeAssetRead(long asset_ptr, byte[] java_buffer, int offset, int len) throws IOException {
if (len == 0) {
return 0;
}
int buffer_len = java_buffer.length;
if (offset < 0 || offset >= buffer_len || len < 0 || len > buffer_len || offset > buffer_len - len) {
throw new IndexOutOfBoundsException();
}
// ScopedByteArrayRW byte_array(env, java_buffer);
// if (byte_array.get() == null) {
// return -1;
// }
Asset asset = Registries.NATIVE_ASSET_REGISTRY.getNativeObject(asset_ptr);
// sint res = asset.read(byte_array.get() + offset, len);
int res = asset.read(java_buffer, offset, len);
if (res < 0) {
throw new IOException();
}
return res > 0 ? (int) (res) : -1;
}
use of org.robolectric.res.android.Asset in project robolectric by robolectric.
the class ShadowArscAssetManager10 method nativeOpenAssetFd.
// static jobject NativeOpenAssetFd(JNIEnv* env, jclass /*clazz*/, jlong ptr, jstring asset_path,
// jlongArray out_offsets) {
@Implementation(minSdk = P)
protected static ParcelFileDescriptor nativeOpenAssetFd(long ptr, @NonNull String asset_path, long[] out_offsets) throws IOException {
String asset_path_utf8 = asset_path;
if (asset_path_utf8 == null) {
// This will throw NPE.
return null;
}
ATRACE_NAME(String.format("AssetManager::OpenAssetFd(%s)", asset_path_utf8));
CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
Asset asset = assetmanager.Open(asset_path_utf8, Asset.AccessMode.ACCESS_RANDOM);
if (!isTruthy(asset)) {
throw new FileNotFoundException(asset_path_utf8);
}
return ReturnParcelFileDescriptor(asset, out_offsets);
}
Aggregations