Merge "Fix mmap errors when reading font files via the AssetManager." into rvc-dev am: 8cf381064f am: a161f3f17d

Change-Id: I2d553ac422b19bbbe1ee2d76e5f8c3009ecbad7f
This commit is contained in:
Automerger Merge Worker
2020-02-28 15:47:04 +00:00

View File

@@ -19,6 +19,7 @@ package android.graphics.fonts;
import android.annotation.IntRange; import android.annotation.IntRange;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.annotation.Nullable; import android.annotation.Nullable;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager; import android.content.res.AssetManager;
import android.content.res.Resources; import android.content.res.Resources;
import android.os.LocaleList; import android.os.LocaleList;
@@ -219,6 +220,27 @@ public final class Font {
Preconditions.checkNotNull(am, "assetManager can not be null"); Preconditions.checkNotNull(am, "assetManager can not be null");
Preconditions.checkNotNull(path, "path can not be null"); Preconditions.checkNotNull(path, "path can not be null");
// Attempt to open as FD, which should work unless the asset is compressed
AssetFileDescriptor assetFD;
try {
if (isAsset) {
assetFD = am.openFd(path);
} else if (cookie > 0) {
assetFD = am.openNonAssetFd(cookie, path);
} else {
assetFD = am.openNonAssetFd(path);
}
try (FileInputStream fis = assetFD.createInputStream()) {
final FileChannel fc = fis.getChannel();
long startOffset = assetFD.getStartOffset();
long declaredLength = assetFD.getDeclaredLength();
return fc.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength);
}
} catch (IOException e) {
// failed to open as FD so now we will attempt to open as an input stream
}
try (InputStream assetStream = isAsset ? am.open(path, AssetManager.ACCESS_BUFFER) try (InputStream assetStream = isAsset ? am.open(path, AssetManager.ACCESS_BUFFER)
: am.openNonAsset(cookie, path, AssetManager.ACCESS_BUFFER)) { : am.openNonAsset(cookie, path, AssetManager.ACCESS_BUFFER)) {