• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.google.android.libraries.mobiledatadownload.file.openers;
17 
18 import android.content.res.AssetFileDescriptor;
19 import android.os.ParcelFileDescriptor;
20 import com.google.android.libraries.mobiledatadownload.file.OpenContext;
21 import com.google.android.libraries.mobiledatadownload.file.Opener;
22 import java.io.IOException;
23 
24 /**
25  * Opener that returns an AssetFileDescriptor. Caller must close the AssetFileDescriptor when done
26  * reading from it. Does not support Monitors or Transforms.
27  *
28  * <p>TODO: Support offset and length.
29  */
30 public final class AssetFileDescriptorOpener implements Opener<AssetFileDescriptor> {
31 
AssetFileDescriptorOpener()32   private AssetFileDescriptorOpener() {}
33 
create()34   public static AssetFileDescriptorOpener create() {
35     return new AssetFileDescriptorOpener();
36   }
37 
38   @Override
open(OpenContext openContext)39   public AssetFileDescriptor open(OpenContext openContext) throws IOException {
40     ParcelFileDescriptorOpener pfdOpener = ParcelFileDescriptorOpener.create();
41     ParcelFileDescriptor pfd = pfdOpener.open(openContext);
42     // TODO(b/115933017): consider wrapping the AFD to force it to implement Closeable on all sdks
43     return new AssetFileDescriptor(pfd, 0, pfd.getStatSize());
44   }
45 }
46