Search in sources :

Example 1 with FileTypedResource

use of org.robolectric.res.FileTypedResource in project robolectric by robolectric.

the class ShadowAssetManager 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) resourceTable.getValue(resName, RuntimeEnvironment.getQualifiers());
    if (typedResource == null) {
        throw new IOException("Unable to find resource for " + fileName);
    }
    if (accessMode == AssetManager.ACCESS_STREAMING) {
        return typedResource.getFsFile().getInputStream();
    } else {
        return new ByteArrayInputStream(typedResource.getFsFile().getBytes());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ResName(org.robolectric.res.ResName) FileTypedResource(org.robolectric.res.FileTypedResource) IOException(java.io.IOException) HiddenApi(org.robolectric.annotation.HiddenApi) Implementation(org.robolectric.annotation.Implementation)

Example 2 with FileTypedResource

use of org.robolectric.res.FileTypedResource 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

ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 HiddenApi (org.robolectric.annotation.HiddenApi)2 Implementation (org.robolectric.annotation.Implementation)2 FileTypedResource (org.robolectric.res.FileTypedResource)2 ResName (org.robolectric.res.ResName)2 InputStream (java.io.InputStream)1 Asset (org.robolectric.res.android.Asset)1