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" 19 20 using namespace android; 21 22 bool AssetStreamAdaptor::rewind() { 23 off64_t pos = fAsset->seek(0, SEEK_SET); 24 if (pos == (off64_t)-1) { 25 SkDebugf("----- fAsset->seek(rewind) failed\n"); 26 return false; 27 } 28 return true; 29 } 30 31 size_t AssetStreamAdaptor::getLength() const { 32 return fAsset->getLength(); 33 } 34 35 bool AssetStreamAdaptor::isAtEnd() const { 36 return fAsset->getRemainingLength() == 0; 37 } 38 39 SkStreamRewindable* 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 47 size_t AssetStreamAdaptor::read(void* buffer, size_t size) { 48 ssize_t amount; 49 50 if (NULL == buffer) { 51 if (0 == size) { 52 return 0; 53 } 54 // asset->seek returns new total offset 55 // we want to return amount that was skipped 56 57 off64_t oldOffset = fAsset->seek(0, SEEK_CUR); 58 if (-1 == oldOffset) { 59 SkDebugf("---- fAsset->seek(oldOffset) failed\n"); 60 return 0; 61 } 62 off64_t newOffset = fAsset->seek(size, SEEK_CUR); 63 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 81 SkMemoryStream* 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 109 jobject android::nullObjectReturn(const char msg[]) { 110 if (msg) { 111 SkDebugf("--- %s\n", msg); 112 } 113 return NULL; 114 } 115