Utils.cpp revision c1d7b7f71ce1a55548d7e8bb32d728190e2ffd47
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        if (amount <= 0) {
76            SkDebugf("---- fAsset->read(%d) returned %d\n", size, amount);
77        }
78    }
79
80    if (amount < 0) {
81        amount = 0;
82    }
83    return amount;
84}
85
86SkMemoryStream* android::CopyAssetToStream(Asset* asset) {
87    if (NULL == asset) {
88        return NULL;
89    }
90
91    const off64_t seekReturnVal = asset->seek(0, SEEK_SET);
92    if ((off64_t)-1 == seekReturnVal) {
93        SkDebugf("---- copyAsset: asset rewind failed\n");
94        return NULL;
95    }
96
97    const off64_t size = asset->getLength();
98    if (size <= 0) {
99        SkDebugf("---- copyAsset: asset->getLength() returned %d\n", size);
100        return NULL;
101    }
102
103    SkAutoTUnref<SkData> data(SkData::NewUninitialized(size));
104    const off64_t len = asset->read(data->writable_data(), size);
105    if (len != size) {
106        SkDebugf("---- copyAsset: asset->read(%d) returned %d\n", size, len);
107        return NULL;
108    }
109
110    return new SkMemoryStream(data);
111}
112
113jobject android::nullObjectReturn(const char msg[]) {
114    if (msg) {
115        SkDebugf("--- %s\n", msg);
116    }
117    return NULL;
118}
119
120bool android::isSeekable(int descriptor) {
121    return ::lseek64(descriptor, 0, SEEK_CUR) != -1;
122}
123