blob: eb416cbe7ad2c4b114829976dd01b1e69cd215ab [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
Leon Scroggins IIIb9c58ab2013-12-03 15:10:04 -050022AssetStreamAdaptor::AssetStreamAdaptor(Asset* asset, OwnAsset ownAsset,
23 HasMemoryBase hasMemoryBase)
24 : fAsset(asset)
25 , fMemoryBase(kYes_HasMemoryBase == hasMemoryBase ?
26 asset->getBuffer(false) : NULL)
27 , fOwnAsset(ownAsset)
28{
29}
30
31AssetStreamAdaptor::~AssetStreamAdaptor() {
32 if (kYes_OwnAsset == fOwnAsset) {
33 delete fAsset;
34 }
35}
36
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080037bool AssetStreamAdaptor::rewind() {
Kenny Rootddb76c42010-11-24 12:56:06 -080038 off64_t pos = fAsset->seek(0, SEEK_SET);
39 if (pos == (off64_t)-1) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080040 SkDebugf("----- fAsset->seek(rewind) failed\n");
41 return false;
42 }
43 return true;
44}
45
Leon Scroggins IIIca320212013-08-20 17:59:39 -040046size_t AssetStreamAdaptor::getLength() const {
47 return fAsset->getLength();
48}
49
50bool AssetStreamAdaptor::isAtEnd() const {
51 return fAsset->getRemainingLength() == 0;
52}
53
54SkStreamRewindable* AssetStreamAdaptor::duplicate() const {
55 SkASSERT(false);
56 // Cannot create a duplicate, since each AssetStreamAdaptor
57 // would be modifying the Asset.
58 //return new AssetStreamAdaptor(fAsset);
59 return NULL;
60}
61
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080062size_t AssetStreamAdaptor::read(void* buffer, size_t size) {
63 ssize_t amount;
64
65 if (NULL == buffer) {
Leon Scroggins IIIca320212013-08-20 17:59:39 -040066 if (0 == size) {
67 return 0;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080068 }
69 // asset->seek returns new total offset
70 // we want to return amount that was skipped
71
Kenny Rootddb76c42010-11-24 12:56:06 -080072 off64_t oldOffset = fAsset->seek(0, SEEK_CUR);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080073 if (-1 == oldOffset) {
74 SkDebugf("---- fAsset->seek(oldOffset) failed\n");
75 return 0;
76 }
Kenny Rootddb76c42010-11-24 12:56:06 -080077 off64_t newOffset = fAsset->seek(size, SEEK_CUR);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080078 if (-1 == newOffset) {
79 SkDebugf("---- fAsset->seek(%d) failed\n", size);
80 return 0;
81 }
82 amount = newOffset - oldOffset;
83 } else {
84 amount = fAsset->read(buffer, size);
85 if (amount <= 0) {
86 SkDebugf("---- fAsset->read(%d) returned %d\n", size, amount);
87 }
88 }
89
90 if (amount < 0) {
91 amount = 0;
92 }
93 return amount;
94}
95
Leon Scroggins IIIca320212013-08-20 17:59:39 -040096SkMemoryStream* android::CopyAssetToStream(Asset* asset) {
97 if (NULL == asset) {
98 return NULL;
99 }
100
101 off64_t size = asset->seek(0, SEEK_SET);
102 if ((off64_t)-1 == size) {
103 SkDebugf("---- copyAsset: asset rewind failed\n");
104 return NULL;
105 }
106
107 size = asset->getLength();
108 if (size <= 0) {
109 SkDebugf("---- copyAsset: asset->getLength() returned %d\n", size);
110 return NULL;
111 }
112
113 SkMemoryStream* stream = new SkMemoryStream(size);
114 void* data = const_cast<void*>(stream->getMemoryBase());
115 off64_t len = asset->read(data, size);
116 if (len != size) {
117 SkDebugf("---- copyAsset: asset->read(%d) returned %d\n", size, len);
118 delete stream;
119 stream = NULL;
120 }
121 return stream;
122}
123
Wei-Ta Chen6b849e22010-09-07 17:32:18 +0800124jobject android::nullObjectReturn(const char msg[]) {
125 if (msg) {
126 SkDebugf("--- %s\n", msg);
127 }
128 return NULL;
129}