blob: 3c471bd3ca282615580ed972a3153c461af144ab [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
Ben Wagnerc2d39572015-01-12 17:06:21 -050022AssetStreamAdaptor::AssetStreamAdaptor(Asset* asset)
Leon Scroggins IIIb9c58ab2013-12-03 15:10:04 -050023 : fAsset(asset)
Leon Scroggins IIIb9c58ab2013-12-03 15:10:04 -050024{
25}
26
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080027bool AssetStreamAdaptor::rewind() {
Kenny Rootddb76c42010-11-24 12:56:06 -080028 off64_t pos = fAsset->seek(0, SEEK_SET);
29 if (pos == (off64_t)-1) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080030 SkDebugf("----- fAsset->seek(rewind) failed\n");
31 return false;
32 }
33 return true;
34}
35
Leon Scroggins IIIca320212013-08-20 17:59:39 -040036size_t AssetStreamAdaptor::getLength() const {
37 return fAsset->getLength();
38}
39
40bool AssetStreamAdaptor::isAtEnd() const {
41 return fAsset->getRemainingLength() == 0;
42}
43
44SkStreamRewindable* AssetStreamAdaptor::duplicate() const {
Leon Scroggins IIIca320212013-08-20 17:59:39 -040045 // Cannot create a duplicate, since each AssetStreamAdaptor
46 // would be modifying the Asset.
47 //return new AssetStreamAdaptor(fAsset);
48 return NULL;
49}
50
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080051size_t AssetStreamAdaptor::read(void* buffer, size_t size) {
52 ssize_t amount;
53
54 if (NULL == buffer) {
Leon Scroggins IIIca320212013-08-20 17:59:39 -040055 if (0 == size) {
56 return 0;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080057 }
58 // asset->seek returns new total offset
59 // we want to return amount that was skipped
60
Kenny Rootddb76c42010-11-24 12:56:06 -080061 off64_t oldOffset = fAsset->seek(0, SEEK_CUR);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080062 if (-1 == oldOffset) {
63 SkDebugf("---- fAsset->seek(oldOffset) failed\n");
64 return 0;
65 }
Kenny Rootddb76c42010-11-24 12:56:06 -080066 off64_t newOffset = fAsset->seek(size, SEEK_CUR);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080067 if (-1 == newOffset) {
68 SkDebugf("---- fAsset->seek(%d) failed\n", size);
69 return 0;
70 }
71 amount = newOffset - oldOffset;
72 } else {
73 amount = fAsset->read(buffer, size);
74 if (amount <= 0) {
75 SkDebugf("---- fAsset->read(%d) returned %d\n", size, amount);
76 }
77 }
78
79 if (amount < 0) {
80 amount = 0;
81 }
82 return amount;
83}
84
Leon Scroggins IIIca320212013-08-20 17:59:39 -040085SkMemoryStream* android::CopyAssetToStream(Asset* asset) {
86 if (NULL == asset) {
87 return NULL;
88 }
89
90 off64_t size = asset->seek(0, SEEK_SET);
91 if ((off64_t)-1 == size) {
92 SkDebugf("---- copyAsset: asset rewind failed\n");
93 return NULL;
94 }
95
96 size = asset->getLength();
97 if (size <= 0) {
98 SkDebugf("---- copyAsset: asset->getLength() returned %d\n", size);
99 return NULL;
100 }
101
102 SkMemoryStream* stream = new SkMemoryStream(size);
103 void* data = const_cast<void*>(stream->getMemoryBase());
104 off64_t len = asset->read(data, size);
105 if (len != size) {
106 SkDebugf("---- copyAsset: asset->read(%d) returned %d\n", size, len);
107 delete stream;
108 stream = NULL;
109 }
110 return stream;
111}
112
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800113jobject android::nullObjectReturn(const char msg[]) {
114 if (msg) {
115 SkDebugf("--- %s\n", msg);
116 }
117 return NULL;
118}