1 /*
2  * Copyright 2019 The Android Open Source Project
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 
17 #pragma once
18 
19 #include "SkStream.h"
20 
21 #include <android/asset_manager.h>
22 
23 // Like AssetStreamAdaptor, but operates on AAsset, a public NDK API.
24 class AAssetStreamAdaptor : public SkStreamRewindable {
25 public:
26     /**
27      * Create an SkStream that reads from an AAsset.
28      *
29      * The AAsset must remain open for the lifetime of the Adaptor. The Adaptor
30      * does *not* close the AAsset.
31      */
32     explicit AAssetStreamAdaptor(AAsset*);
33 
34     bool rewind() override;
35     size_t read(void* buffer, size_t size) override;
hasLength()36     bool hasLength() const override { return true; }
37     size_t getLength() const override;
38     bool hasPosition() const override;
39     size_t getPosition() const override;
40     bool seek(size_t position) override;
41     bool move(long offset) override;
42     bool isAtEnd() const override;
43     const void* getMemoryBase() override;
44 protected:
45     SkStreamRewindable* onDuplicate() const override;
46 
47 private:
48     AAsset* mAAsset;
49 };
50 
51