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