blob: 4f9ce8bfcef742612d068461574e78737726d99c [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"
Leon Scroggins III34497892015-01-20 15:52:43 -050019#include "SkData.h"
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080020
21using namespace android;
22
Ben Wagnerc2d39572015-01-12 17:06:21 -050023AssetStreamAdaptor::AssetStreamAdaptor(Asset* asset)
Leon Scroggins IIIb9c58ab2013-12-03 15:10:04 -050024 : fAsset(asset)
Leon Scroggins IIIb9c58ab2013-12-03 15:10:04 -050025{
26}
27
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080028bool AssetStreamAdaptor::rewind() {
Kenny Rootddb76c42010-11-24 12:56:06 -080029 off64_t pos = fAsset->seek(0, SEEK_SET);
30 if (pos == (off64_t)-1) {
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080031 SkDebugf("----- fAsset->seek(rewind) failed\n");
32 return false;
33 }
34 return true;
35}
36
Leon Scroggins IIIca320212013-08-20 17:59:39 -040037size_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 {
Leon Scroggins IIIca320212013-08-20 17:59:39 -040046 // Cannot create a duplicate, since each AssetStreamAdaptor
47 // would be modifying the Asset.
48 //return new AssetStreamAdaptor(fAsset);
49 return NULL;
50}
51
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080052size_t AssetStreamAdaptor::read(void* buffer, size_t size) {
53 ssize_t amount;
54
55 if (NULL == buffer) {
Leon Scroggins IIIca320212013-08-20 17:59:39 -040056 if (0 == size) {
57 return 0;
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080058 }
59 // asset->seek returns new total offset
60 // we want to return amount that was skipped
61
Kenny Rootddb76c42010-11-24 12:56:06 -080062 off64_t oldOffset = fAsset->seek(0, SEEK_CUR);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080063 if (-1 == oldOffset) {
64 SkDebugf("---- fAsset->seek(oldOffset) failed\n");
65 return 0;
66 }
Kenny Rootddb76c42010-11-24 12:56:06 -080067 off64_t newOffset = fAsset->seek(size, SEEK_CUR);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080068 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
Leon Scroggins IIIca320212013-08-20 17:59:39 -040086SkMemoryStream* android::CopyAssetToStream(Asset* asset) {
87 if (NULL == asset) {
88 return NULL;
89 }
90
Leon Scroggins III34497892015-01-20 15:52:43 -050091 const off64_t seekReturnVal = asset->seek(0, SEEK_SET);
92 if ((off64_t)-1 == seekReturnVal) {
Leon Scroggins IIIca320212013-08-20 17:59:39 -040093 SkDebugf("---- copyAsset: asset rewind failed\n");
94 return NULL;
95 }
96
Leon Scroggins III34497892015-01-20 15:52:43 -050097 const off64_t size = asset->getLength();
Leon Scroggins IIIca320212013-08-20 17:59:39 -040098 if (size <= 0) {
99 SkDebugf("---- copyAsset: asset->getLength() returned %d\n", size);
100 return NULL;
101 }
102
Leon Scroggins III34497892015-01-20 15:52:43 -0500103 SkAutoTUnref<SkData> data(SkData::NewUninitialized(size));
104 const off64_t len = asset->read(data->writable_data(), size);
Leon Scroggins IIIca320212013-08-20 17:59:39 -0400105 if (len != size) {
106 SkDebugf("---- copyAsset: asset->read(%d) returned %d\n", size, len);
Leon Scroggins III34497892015-01-20 15:52:43 -0500107 return NULL;
Leon Scroggins IIIca320212013-08-20 17:59:39 -0400108 }
Leon Scroggins III34497892015-01-20 15:52:43 -0500109
110 return new SkMemoryStream(data);
Leon Scroggins IIIca320212013-08-20 17:59:39 -0400111}
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}