1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5package org.chromium.android_webview;
6
7import android.content.Context;
8import android.content.res.AssetFileDescriptor;
9import android.content.res.AssetManager;
10import android.util.Log;
11
12import org.chromium.base.CalledByNative;
13import org.chromium.base.JNINamespace;
14
15import java.io.IOException;
16
17/**
18 * A utility class to retrieve references to uncompressed assets insides the apk. A reference is
19 * defined as tuple (file descriptor, offset, size) enabling direct mapping without deflation.
20 */
21@JNINamespace("android_webview")
22public class AwAssets {
23    private static final String LOGTAG = "AwAssets";
24
25    @CalledByNative
26    public static long[] openAsset(Context context, String fileName) {
27        AssetFileDescriptor afd = null;
28        try {
29            AssetManager manager = context.getAssets();
30            afd = manager.openFd(fileName);
31            return new long[] { afd.getParcelFileDescriptor().detachFd(),
32                                afd.getStartOffset(),
33                                afd.getLength() };
34        } catch (IOException e) {
35            Log.e(LOGTAG, "Error while loading asset " + fileName + ": " + e);
36            return new long[] {-1, -1, -1};
37        } finally {
38            try {
39                if (afd != null) {
40                    afd.close();
41                }
42            } catch (IOException e2) {
43                Log.e(LOGTAG, "Unable to close AssetFileDescriptor", e2);
44            }
45        }
46    }
47}
48