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 #include "aassetstreamadaptor.h"
18
19 #include <log/log.h>
20
AAssetStreamAdaptor(AAsset * asset)21 AAssetStreamAdaptor::AAssetStreamAdaptor(AAsset* asset)
22 : mAAsset(asset)
23 {
24 }
25
rewind()26 bool AAssetStreamAdaptor::rewind() {
27 off64_t pos = AAsset_seek64(mAAsset, 0, SEEK_SET);
28 if (pos == (off64_t)-1) {
29 ALOGE("rewind failed!");
30 return false;
31 }
32 return true;
33 }
34
getLength() const35 size_t AAssetStreamAdaptor::getLength() const {
36 return AAsset_getLength64(mAAsset);
37 }
38
isAtEnd() const39 bool AAssetStreamAdaptor::isAtEnd() const {
40 return AAsset_getRemainingLength64(mAAsset) == 0;
41 }
42
onDuplicate() const43 SkStreamRewindable* AAssetStreamAdaptor::onDuplicate() const {
44 // Cannot sensibly create a duplicate, since each AAssetStreamAdaptor
45 // would be modifying the same AAsset.
46 //return new AAssetStreamAdaptor(mAAsset);
47 return nullptr;
48 }
49
hasPosition() const50 bool AAssetStreamAdaptor::hasPosition() const {
51 return AAsset_seek64(mAAsset, 0, SEEK_CUR) != -1;
52 }
53
getPosition() const54 size_t AAssetStreamAdaptor::getPosition() const {
55 const off64_t offset = AAsset_seek64(mAAsset, 0, SEEK_CUR);
56 if (offset == -1) {
57 ALOGE("getPosition failed!");
58 return 0;
59 }
60
61 return offset;
62 }
63
seek(size_t position)64 bool AAssetStreamAdaptor::seek(size_t position) {
65 if (AAsset_seek64(mAAsset, position, SEEK_SET) == -1) {
66 ALOGE("seek failed!");
67 return false;
68 }
69
70 return true;
71 }
72
move(long offset)73 bool AAssetStreamAdaptor::move(long offset) {
74 if (AAsset_seek64(mAAsset, offset, SEEK_CUR) == -1) {
75 ALOGE("move failed!");
76 return false;
77 }
78
79 return true;
80 }
81
read(void * buffer,size_t size)82 size_t AAssetStreamAdaptor::read(void* buffer, size_t size) {
83 ssize_t amount;
84
85 if (!buffer) {
86 if (!size) {
87 return 0;
88 }
89
90 // asset->seek returns new total offset
91 // we want to return amount that was skipped
92 const off64_t oldOffset = AAsset_seek64(mAAsset, 0, SEEK_CUR);
93 if (oldOffset == -1) {
94 ALOGE("seek(oldOffset) failed!");
95 return 0;
96 }
97
98 const off64_t newOffset = AAsset_seek64(mAAsset, size, SEEK_CUR);
99 if (-1 == newOffset) {
100 ALOGE("seek(%zu) failed!", size);
101 return 0;
102 }
103 amount = newOffset - oldOffset;
104 } else {
105 amount = AAsset_read(mAAsset, buffer, size);
106 }
107
108 if (amount < 0) {
109 amount = 0;
110 }
111 return amount;
112 }
113
getMemoryBase()114 const void* AAssetStreamAdaptor::getMemoryBase() {
115 return AAsset_getBuffer(mAAsset);
116 }
117
118