Search in sources :

Example 16 with Asset

use of org.robolectric.res.android.Asset in project robolectric by robolectric.

the class ShadowArscAssetManager method openNonAssetNative.

@HiddenApi
@Implementation
protected final Number openNonAssetNative(int cookie, String fileName, int accessMode) throws FileNotFoundException {
    CppAssetManager am = assetManagerForJavaObject();
    if (am == null) {
        return RuntimeEnvironment.castNativePtr(0);
    }
    ALOGV("openNonAssetNative in %s (Java object %s)\n", am, AssetManager.class);
    String fileName8 = fileName;
    if (fileName8 == null) {
        return RuntimeEnvironment.castNativePtr(-1);
    }
    AccessMode mode = AccessMode.fromInt(accessMode);
    if (mode != Asset.AccessMode.ACCESS_UNKNOWN && mode != Asset.AccessMode.ACCESS_RANDOM && mode != Asset.AccessMode.ACCESS_STREAMING && mode != Asset.AccessMode.ACCESS_BUFFER) {
        throw new IllegalArgumentException("Bad access mode");
    }
    Asset a = isTruthy(cookie) ? am.openNonAsset(cookie, fileName8, mode) : am.openNonAsset(fileName8, mode, null);
    if (a == null) {
        throw new FileNotFoundException(fileName8);
    }
    long assetId = Registries.NATIVE_ASSET_REGISTRY.register(a);
    // todo: something better than this [xw]
    a.onClose = () -> destroyAsset(assetId);
    // printf("Created Asset Stream: %p\n", a);
    return RuntimeEnvironment.castNativePtr(assetId);
}
Also used : CppAssetManager(org.robolectric.res.android.CppAssetManager) FileNotFoundException(java.io.FileNotFoundException) Asset(org.robolectric.res.android.Asset) AccessMode(org.robolectric.res.android.Asset.AccessMode) HiddenApi(org.robolectric.annotation.HiddenApi) Implementation(org.robolectric.annotation.Implementation)

Example 17 with Asset

use of org.robolectric.res.android.Asset in project robolectric by robolectric.

the class ShadowArscAssetManager9 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);
}
Also used : CppAssetManager2(org.robolectric.res.android.CppAssetManager2) FileNotFoundException(java.io.FileNotFoundException) Asset(org.robolectric.res.android.Asset) Implementation(org.robolectric.annotation.Implementation)

Example 18 with Asset

use of org.robolectric.res.android.Asset in project robolectric by robolectric.

the class ShadowArscAssetManager9 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;
}
Also used : Asset(org.robolectric.res.android.Asset) IOException(java.io.IOException) Implementation(org.robolectric.annotation.Implementation)

Example 19 with Asset

use of org.robolectric.res.android.Asset in project robolectric by robolectric.

the class ShadowArscAssetManager9 method nativeOpenNonAssetFd.

// static jobject NativeOpenNonAssetFd(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint jcookie,
// jstring asset_path, jlongArray out_offsets) {
@Implementation(minSdk = P)
@Nullable
protected static ParcelFileDescriptor nativeOpenNonAssetFd(long ptr, int jcookie, @NonNull String asset_path, @NonNull long[] out_offsets) throws IOException {
    ApkAssetsCookie cookie = JavaCookieToApkAssetsCookie(jcookie);
    String asset_path_utf8 = asset_path;
    if (asset_path_utf8 == null) {
        // This will throw NPE.
        return null;
    }
    ATRACE_NAME(String.format("AssetManager::OpenNonAssetFd(%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 {
        asset = assetmanager.OpenNonAsset(asset_path_utf8, Asset.AccessMode.ACCESS_RANDOM);
    }
    if (!isTruthy(asset)) {
        throw new FileNotFoundException(asset_path_utf8);
    }
    return ReturnParcelFileDescriptor(asset, out_offsets);
}
Also used : ApkAssetsCookie(org.robolectric.res.android.ApkAssetsCookie) CppAssetManager2(org.robolectric.res.android.CppAssetManager2) FileNotFoundException(java.io.FileNotFoundException) Asset(org.robolectric.res.android.Asset) Implementation(org.robolectric.annotation.Implementation) Nullable(android.annotation.Nullable)

Example 20 with Asset

use of org.robolectric.res.android.Asset in project robolectric by robolectric.

the class ShadowLegacyAssetManager method openNonAsset.

@HiddenApi
@Implementation
public final InputStream openNonAsset(int cookie, String fileName, int accessMode) throws IOException {
    final ResName resName = qualifyFromNonAssetFileName(fileName);
    final FileTypedResource typedResource = (FileTypedResource) getResourceTable().getValue(resName, config);
    if (typedResource == null) {
        throw new IOException("Unable to find resource for " + fileName);
    }
    InputStream stream;
    if (accessMode == AssetManager.ACCESS_STREAMING) {
        stream = Fs.getInputStream(typedResource.getPath());
    } else {
        stream = new ByteArrayInputStream(Fs.getBytes(typedResource.getPath()));
    }
    if (RuntimeEnvironment.getApiLevel() >= P) {
        Asset asset = Asset.newFileAsset(typedResource);
        long assetPtr = Registries.NATIVE_ASSET_REGISTRY.register(asset);
        // Camouflage the InputStream as an AssetInputStream so subsequent instanceof checks pass.
        stream = ShadowAssetInputStream.createAssetInputStream(stream, assetPtr, realObject);
    }
    return stream;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Asset(org.robolectric.res.android.Asset) ResName(org.robolectric.res.ResName) FileTypedResource(org.robolectric.res.FileTypedResource) IOException(java.io.IOException) HiddenApi(org.robolectric.annotation.HiddenApi) Implementation(org.robolectric.annotation.Implementation)

Aggregations

Asset (org.robolectric.res.android.Asset)25 Implementation (org.robolectric.annotation.Implementation)24 FileNotFoundException (java.io.FileNotFoundException)16 CppAssetManager2 (org.robolectric.res.android.CppAssetManager2)11 HiddenApi (org.robolectric.annotation.HiddenApi)8 ApkAssetsCookie (org.robolectric.res.android.ApkAssetsCookie)6 CppAssetManager (org.robolectric.res.android.CppAssetManager)5 IOException (java.io.IOException)4 ResXMLTree (org.robolectric.res.android.ResXMLTree)4 DynamicRefTable (org.robolectric.res.android.DynamicRefTable)3 Ref (org.robolectric.res.android.Ref)3 Nullable (android.annotation.Nullable)2 AssetManager (android.content.res.AssetManager)1 AssetInputStream (android.content.res.AssetManager.AssetInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 FileTypedResource (org.robolectric.res.FileTypedResource)1 ResName (org.robolectric.res.ResName)1 AccessMode (org.robolectric.res.android.Asset.AccessMode)1 CppApkAssets (org.robolectric.res.android.CppApkAssets)1