Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 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" |
Leon Scroggins III | 3449789 | 2015-01-20 15:52:43 -0500 | [diff] [blame^] | 19 | #include "SkData.h" |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 20 | |
| 21 | using namespace android; |
| 22 | |
Ben Wagner | c2d3957 | 2015-01-12 17:06:21 -0500 | [diff] [blame] | 23 | AssetStreamAdaptor::AssetStreamAdaptor(Asset* asset) |
Leon Scroggins III | b9c58ab | 2013-12-03 15:10:04 -0500 | [diff] [blame] | 24 | : fAsset(asset) |
Leon Scroggins III | b9c58ab | 2013-12-03 15:10:04 -0500 | [diff] [blame] | 25 | { |
| 26 | } |
| 27 | |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 28 | bool AssetStreamAdaptor::rewind() { |
Kenny Root | ddb76c4 | 2010-11-24 12:56:06 -0800 | [diff] [blame] | 29 | off64_t pos = fAsset->seek(0, SEEK_SET); |
| 30 | if (pos == (off64_t)-1) { |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 31 | SkDebugf("----- fAsset->seek(rewind) failed\n"); |
| 32 | return false; |
| 33 | } |
| 34 | return true; |
| 35 | } |
| 36 | |
Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 37 | size_t AssetStreamAdaptor::getLength() const { |
| 38 | return fAsset->getLength(); |
| 39 | } |
| 40 | |
| 41 | bool AssetStreamAdaptor::isAtEnd() const { |
| 42 | return fAsset->getRemainingLength() == 0; |
| 43 | } |
| 44 | |
| 45 | SkStreamRewindable* AssetStreamAdaptor::duplicate() const { |
Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 46 | // 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 Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 52 | size_t AssetStreamAdaptor::read(void* buffer, size_t size) { |
| 53 | ssize_t amount; |
| 54 | |
| 55 | if (NULL == buffer) { |
Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 56 | if (0 == size) { |
| 57 | return 0; |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 58 | } |
| 59 | // asset->seek returns new total offset |
| 60 | // we want to return amount that was skipped |
| 61 | |
Kenny Root | ddb76c4 | 2010-11-24 12:56:06 -0800 | [diff] [blame] | 62 | off64_t oldOffset = fAsset->seek(0, SEEK_CUR); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 63 | if (-1 == oldOffset) { |
| 64 | SkDebugf("---- fAsset->seek(oldOffset) failed\n"); |
| 65 | return 0; |
| 66 | } |
Kenny Root | ddb76c4 | 2010-11-24 12:56:06 -0800 | [diff] [blame] | 67 | off64_t newOffset = fAsset->seek(size, SEEK_CUR); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 68 | 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 III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 86 | SkMemoryStream* android::CopyAssetToStream(Asset* asset) { |
| 87 | if (NULL == asset) { |
| 88 | return NULL; |
| 89 | } |
| 90 | |
Leon Scroggins III | 3449789 | 2015-01-20 15:52:43 -0500 | [diff] [blame^] | 91 | const off64_t seekReturnVal = asset->seek(0, SEEK_SET); |
| 92 | if ((off64_t)-1 == seekReturnVal) { |
Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 93 | SkDebugf("---- copyAsset: asset rewind failed\n"); |
| 94 | return NULL; |
| 95 | } |
| 96 | |
Leon Scroggins III | 3449789 | 2015-01-20 15:52:43 -0500 | [diff] [blame^] | 97 | const off64_t size = asset->getLength(); |
Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 98 | if (size <= 0) { |
| 99 | SkDebugf("---- copyAsset: asset->getLength() returned %d\n", size); |
| 100 | return NULL; |
| 101 | } |
| 102 | |
Leon Scroggins III | 3449789 | 2015-01-20 15:52:43 -0500 | [diff] [blame^] | 103 | SkAutoTUnref<SkData> data(SkData::NewUninitialized(size)); |
| 104 | const off64_t len = asset->read(data->writable_data(), size); |
Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 105 | if (len != size) { |
| 106 | SkDebugf("---- copyAsset: asset->read(%d) returned %d\n", size, len); |
Leon Scroggins III | 3449789 | 2015-01-20 15:52:43 -0500 | [diff] [blame^] | 107 | return NULL; |
Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 108 | } |
Leon Scroggins III | 3449789 | 2015-01-20 15:52:43 -0500 | [diff] [blame^] | 109 | |
| 110 | return new SkMemoryStream(data); |
Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 111 | } |
| 112 | |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 113 | jobject android::nullObjectReturn(const char msg[]) { |
| 114 | if (msg) { |
| 115 | SkDebugf("--- %s\n", msg); |
| 116 | } |
| 117 | return NULL; |
| 118 | } |