• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "SkData.h"
20 
21 using namespace android;
22 
AssetStreamAdaptor(Asset * asset)23 AssetStreamAdaptor::AssetStreamAdaptor(Asset* asset)
24     : fAsset(asset)
25 {
26 }
27 
rewind()28 bool AssetStreamAdaptor::rewind() {
29     off64_t pos = fAsset->seek(0, SEEK_SET);
30     if (pos == (off64_t)-1) {
31         SkDebugf("----- fAsset->seek(rewind) failed\n");
32         return false;
33     }
34     return true;
35 }
36 
getLength() const37 size_t AssetStreamAdaptor::getLength() const {
38     return fAsset->getLength();
39 }
40 
isAtEnd() const41 bool AssetStreamAdaptor::isAtEnd() const {
42     return fAsset->getRemainingLength() == 0;
43 }
44 
duplicate() const45 SkStreamRewindable* AssetStreamAdaptor::duplicate() const {
46     // Cannot create a duplicate, since each AssetStreamAdaptor
47     // would be modifying the Asset.
48     //return new AssetStreamAdaptor(fAsset);
49     return NULL;
50 }
51 
read(void * buffer,size_t size)52 size_t AssetStreamAdaptor::read(void* buffer, size_t size) {
53     ssize_t amount;
54 
55     if (NULL == buffer) {
56         if (0 == size) {
57             return 0;
58         }
59         // asset->seek returns new total offset
60         // we want to return amount that was skipped
61 
62         off64_t oldOffset = fAsset->seek(0, SEEK_CUR);
63         if (-1 == oldOffset) {
64             SkDebugf("---- fAsset->seek(oldOffset) failed\n");
65             return 0;
66         }
67         off64_t newOffset = fAsset->seek(size, SEEK_CUR);
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     }
76 
77     if (amount < 0) {
78         amount = 0;
79     }
80     return amount;
81 }
82 
CopyAssetToStream(Asset * asset)83 SkMemoryStream* android::CopyAssetToStream(Asset* asset) {
84     if (NULL == asset) {
85         return NULL;
86     }
87 
88     const off64_t seekReturnVal = asset->seek(0, SEEK_SET);
89     if ((off64_t)-1 == seekReturnVal) {
90         SkDebugf("---- copyAsset: asset rewind failed\n");
91         return NULL;
92     }
93 
94     const off64_t size = asset->getLength();
95     if (size <= 0) {
96         SkDebugf("---- copyAsset: asset->getLength() returned %d\n", size);
97         return NULL;
98     }
99 
100     sk_sp<SkData> data(SkData::MakeUninitialized(size));
101     const off64_t len = asset->read(data->writable_data(), size);
102     if (len != size) {
103         SkDebugf("---- copyAsset: asset->read(%d) returned %d\n", size, len);
104         return NULL;
105     }
106 
107     return new SkMemoryStream(std::move(data));
108 }
109 
nullObjectReturn(const char msg[])110 jobject android::nullObjectReturn(const char msg[]) {
111     if (msg) {
112         SkDebugf("--- %s\n", msg);
113     }
114     return NULL;
115 }
116 
isSeekable(int descriptor)117 bool android::isSeekable(int descriptor) {
118     return ::lseek64(descriptor, 0, SEEK_CUR) != -1;
119 }
120