Utils.cpp revision 6b849e2123be98eb2a1a25b8abf0b13a279ce952
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
20using namespace android;
21
22bool AssetStreamAdaptor::rewind() {
23    off_t pos = fAsset->seek(0, SEEK_SET);
24    if (pos == (off_t)-1) {
25        SkDebugf("----- fAsset->seek(rewind) failed\n");
26        return false;
27    }
28    return true;
29}
30
31size_t AssetStreamAdaptor::read(void* buffer, size_t size) {
32    ssize_t amount;
33
34    if (NULL == buffer) {
35        if (0 == size) {  // caller is asking us for our total length
36            return fAsset->getLength();
37        }
38        // asset->seek returns new total offset
39        // we want to return amount that was skipped
40
41        off_t oldOffset = fAsset->seek(0, SEEK_CUR);
42        if (-1 == oldOffset) {
43            SkDebugf("---- fAsset->seek(oldOffset) failed\n");
44            return 0;
45        }
46        off_t newOffset = fAsset->seek(size, SEEK_CUR);
47        if (-1 == newOffset) {
48            SkDebugf("---- fAsset->seek(%d) failed\n", size);
49            return 0;
50        }
51        amount = newOffset - oldOffset;
52    } else {
53        amount = fAsset->read(buffer, size);
54        if (amount <= 0) {
55            SkDebugf("---- fAsset->read(%d) returned %d\n", size, amount);
56        }
57    }
58
59    if (amount < 0) {
60        amount = 0;
61    }
62    return amount;
63}
64
65jobject android::nullObjectReturn(const char msg[]) {
66    if (msg) {
67        SkDebugf("--- %s\n", msg);
68    }
69    return NULL;
70}
71