• 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 #include <inttypes.h>
22 #include <log/log.h>
23 
24 using namespace android;
25 
AssetStreamAdaptor(Asset * asset)26 AssetStreamAdaptor::AssetStreamAdaptor(Asset* asset)
27     : fAsset(asset)
28 {
29 }
30 
rewind()31 bool AssetStreamAdaptor::rewind() {
32     off64_t pos = fAsset->seek(0, SEEK_SET);
33     if (pos == (off64_t)-1) {
34         ALOGD("----- fAsset->seek(rewind) failed\n");
35         return false;
36     }
37     return true;
38 }
39 
getLength() const40 size_t AssetStreamAdaptor::getLength() const {
41     return fAsset->getLength();
42 }
43 
isAtEnd() const44 bool AssetStreamAdaptor::isAtEnd() const {
45     return fAsset->getRemainingLength() == 0;
46 }
47 
onDuplicate() const48 SkStreamRewindable* AssetStreamAdaptor::onDuplicate() const {
49     // Cannot create a duplicate, since each AssetStreamAdaptor
50     // would be modifying the Asset.
51     //return new AssetStreamAdaptor(fAsset);
52     return NULL;
53 }
54 
hasPosition() const55 bool AssetStreamAdaptor::hasPosition() const {
56     return fAsset->seek(0, SEEK_CUR) != -1;
57 }
58 
getPosition() const59 size_t AssetStreamAdaptor::getPosition() const {
60     const off64_t offset = fAsset->seek(0, SEEK_CUR);
61     if (offset == -1) {
62         ALOGD("---- fAsset->seek(0, SEEK_CUR) failed\n");
63         return 0;
64     }
65 
66     return offset;
67 }
68 
seek(size_t position)69 bool AssetStreamAdaptor::seek(size_t position) {
70     if (fAsset->seek(position, SEEK_SET) == -1) {
71         ALOGD("---- fAsset->seek(0, SEEK_SET) failed\n");
72         return false;
73     }
74 
75     return true;
76 }
77 
move(long offset)78 bool AssetStreamAdaptor::move(long offset) {
79     if (fAsset->seek(offset, SEEK_CUR) == -1) {
80         ALOGD("---- fAsset->seek(%li, SEEK_CUR) failed\n", offset);
81         return false;
82     }
83 
84     return true;
85 }
86 
read(void * buffer,size_t size)87 size_t AssetStreamAdaptor::read(void* buffer, size_t size) {
88     ssize_t amount;
89 
90     if (NULL == buffer) {
91         if (0 == size) {
92             return 0;
93         }
94         // asset->seek returns new total offset
95         // we want to return amount that was skipped
96 
97         off64_t oldOffset = fAsset->seek(0, SEEK_CUR);
98         if (-1 == oldOffset) {
99             ALOGD("---- fAsset->seek(oldOffset) failed\n");
100             return 0;
101         }
102         off64_t newOffset = fAsset->seek(size, SEEK_CUR);
103         if (-1 == newOffset) {
104             ALOGD("---- fAsset->seek(%zu) failed\n", size);
105             return 0;
106         }
107         amount = newOffset - oldOffset;
108     } else {
109         amount = fAsset->read(buffer, size);
110     }
111 
112     if (amount < 0) {
113         amount = 0;
114     }
115     return amount;
116 }
117 
CopyAssetToData(Asset * asset)118 sk_sp<SkData> android::CopyAssetToData(Asset* asset) {
119     if (NULL == asset) {
120         return NULL;
121     }
122 
123     const off64_t seekReturnVal = asset->seek(0, SEEK_SET);
124     if ((off64_t)-1 == seekReturnVal) {
125         ALOGD("---- copyAsset: asset rewind failed\n");
126         return NULL;
127     }
128 
129     const off64_t size = asset->getLength();
130     if (size <= 0) {
131         ALOGD("---- copyAsset: asset->getLength() returned %" PRId64 "\n", size);
132         return NULL;
133     }
134 
135     sk_sp<SkData> data(SkData::MakeUninitialized(size));
136     const off64_t len = asset->read(data->writable_data(), size);
137     if (len != size) {
138         ALOGD("---- copyAsset: asset->read(%" PRId64 ") returned %" PRId64 "\n", size, len);
139         return NULL;
140     }
141 
142     return data;
143 }
144 
nullObjectReturn(const char msg[])145 jobject android::nullObjectReturn(const char msg[]) {
146     if (msg) {
147         ALOGD("--- %s\n", msg);
148     }
149     return NULL;
150 }
151 
isSeekable(int descriptor)152 bool android::isSeekable(int descriptor) {
153     return ::lseek64(descriptor, 0, SEEK_CUR) != -1;
154 }
155 
get_env_or_die(JavaVM * jvm)156 JNIEnv* android::get_env_or_die(JavaVM* jvm) {
157     JNIEnv* env;
158     if (jvm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
159         LOG_ALWAYS_FATAL("Failed to get JNIEnv for JavaVM: %p", jvm);
160     }
161     return env;
162 }
163 
requireEnv(JavaVM * jvm)164 JNIEnv* android::requireEnv(JavaVM* jvm) {
165     JNIEnv* env;
166     if (jvm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
167         if (jvm->AttachCurrentThreadAsDaemon(&env, nullptr) != JNI_OK) {
168             LOG_ALWAYS_FATAL("Failed to AttachCurrentThread!");
169         }
170     }
171     return env;
172 }
173