1/*
2 * Copyright (C) 2010 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 "Utils.h"
18#include "SkUtils.h"
19#include "SkData.h"
20
21using namespace android;
22
23AssetStreamAdaptor::AssetStreamAdaptor(Asset* asset)
24    : fAsset(asset)
25{
26}
27
28bool AssetStreamAdaptor::rewind() {
29    off64_t pos = fAsset->seek(0, SEEK_SET);
30    if (pos == (off64_t)-1) {
31        SkDebugf("----- fAsset->seek(rewind) failed\n");
32        return false;
33    }
34    return true;
35}
36
37size_t AssetStreamAdaptor::getLength() const {
38    return fAsset->getLength();
39}
40
41bool AssetStreamAdaptor::isAtEnd() const {
42    return fAsset->getRemainingLength() == 0;
43}
44
45SkStreamRewindable* AssetStreamAdaptor::duplicate() const {
46    // Cannot create a duplicate, since each AssetStreamAdaptor
47    // would be modifying the Asset.
48    //return new AssetStreamAdaptor(fAsset);
49    return NULL;
50}
51
52size_t AssetStreamAdaptor::read(void* buffer, size_t size) {
53    ssize_t amount;
54
55    if (NULL == buffer) {
56        if (0 == size) {
57            return 0;
58        }
59        // asset->seek returns new total offset
60        // we want to return amount that was skipped
61
62        off64_t oldOffset = fAsset->seek(0, SEEK_CUR);
63        if (-1 == oldOffset) {
64            SkDebugf("---- fAsset->seek(oldOffset) failed\n");
65            return 0;
66        }
67        off64_t newOffset = fAsset->seek(size, SEEK_CUR);
68        if (-1 == newOffset) {
69            SkDebugf("---- fAsset->seek(%d) failed\n", size);
70            return 0;
71        }
72        amount = newOffset - oldOffset;
73    } else {
74        amount = fAsset->read(buffer, size);
75    }
76
77    if (amount < 0) {
78        amount = 0;
79    }
80    return amount;
81}
82
83SkMemoryStream* android::CopyAssetToStream(Asset* asset) {
84    if (NULL == asset) {
85        return NULL;
86    }
87
88    const off64_t seekReturnVal = asset->seek(0, SEEK_SET);
89    if ((off64_t)-1 == seekReturnVal) {
90        SkDebugf("---- copyAsset: asset rewind failed\n");
91        return NULL;
92    }
93
94    const off64_t size = asset->getLength();
95    if (size <= 0) {
96        SkDebugf("---- copyAsset: asset->getLength() returned %d\n", size);
97        return NULL;
98    }
99
100    sk_sp<SkData> data(SkData::MakeUninitialized(size));
101    const off64_t len = asset->read(data->writable_data(), size);
102    if (len != size) {
103        SkDebugf("---- copyAsset: asset->read(%d) returned %d\n", size, len);
104        return NULL;
105    }
106
107    return new SkMemoryStream(std::move(data));
108}
109
110jobject android::nullObjectReturn(const char msg[]) {
111    if (msg) {
112        SkDebugf("--- %s\n", msg);
113    }
114    return NULL;
115}
116
117bool android::isSeekable(int descriptor) {
118    return ::lseek64(descriptor, 0, SEEK_CUR) != -1;
119}
120