blob: b7d1f3ab220fd240c90ef91c35795bd15a87e1fc [file] [log] [blame]
Wei-Ta Chen6b849e22010-09-07 17:32:18 +08001/*
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() {
Kenny Rootddb76c42010-11-24 12:56:06 -080023 off64_t pos = fAsset->seek(0, SEEK_SET);
24 if (pos == (off64_t)-1) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080025 SkDebugf("----- fAsset->seek(rewind) failed\n");
26 return false;
27 }
28 return true;
29}
30
Leon Scroggins IIIca320212013-08-20 17:59:39 -040031size_t AssetStreamAdaptor::getLength() const {
32 return fAsset->getLength();
33}
34
35bool AssetStreamAdaptor::isAtEnd() const {
36 return fAsset->getRemainingLength() == 0;
37}
38
39SkStreamRewindable* AssetStreamAdaptor::duplicate() const {
40 SkASSERT(false);
41 // Cannot create a duplicate, since each AssetStreamAdaptor
42 // would be modifying the Asset.
43 //return new AssetStreamAdaptor(fAsset);
44 return NULL;
45}
46
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080047size_t AssetStreamAdaptor::read(void* buffer, size_t size) {
48 ssize_t amount;
49
50 if (NULL == buffer) {
Leon Scroggins IIIca320212013-08-20 17:59:39 -040051 if (0 == size) {
52 return 0;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080053 }
54 // asset->seek returns new total offset
55 // we want to return amount that was skipped
56
Kenny Rootddb76c42010-11-24 12:56:06 -080057 off64_t oldOffset = fAsset->seek(0, SEEK_CUR);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080058 if (-1 == oldOffset) {
59 SkDebugf("---- fAsset->seek(oldOffset) failed\n");
60 return 0;
61 }
Kenny Rootddb76c42010-11-24 12:56:06 -080062 off64_t newOffset = fAsset->seek(size, SEEK_CUR);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080063 if (-1 == newOffset) {
64 SkDebugf("---- fAsset->seek(%d) failed\n", size);
65 return 0;
66 }
67 amount = newOffset - oldOffset;
68 } else {
69 amount = fAsset->read(buffer, size);
70 if (amount <= 0) {
71 SkDebugf("---- fAsset->read(%d) returned %d\n", size, amount);
72 }
73 }
74
75 if (amount < 0) {
76 amount = 0;
77 }
78 return amount;
79}
80
Leon Scroggins IIIca320212013-08-20 17:59:39 -040081SkMemoryStream* android::CopyAssetToStream(Asset* asset) {
82 if (NULL == asset) {
83 return NULL;
84 }
85
86 off64_t size = asset->seek(0, SEEK_SET);
87 if ((off64_t)-1 == size) {
88 SkDebugf("---- copyAsset: asset rewind failed\n");
89 return NULL;
90 }
91
92 size = asset->getLength();
93 if (size <= 0) {
94 SkDebugf("---- copyAsset: asset->getLength() returned %d\n", size);
95 return NULL;
96 }
97
98 SkMemoryStream* stream = new SkMemoryStream(size);
99 void* data = const_cast<void*>(stream->getMemoryBase());
100 off64_t len = asset->read(data, size);
101 if (len != size) {
102 SkDebugf("---- copyAsset: asset->read(%d) returned %d\n", size, len);
103 delete stream;
104 stream = NULL;
105 }
106 return stream;
107}
108
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800109jobject android::nullObjectReturn(const char msg[]) {
110 if (msg) {
111 SkDebugf("--- %s\n", msg);
112 }
113 return NULL;
114}