1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Implementation of `app::assets`
17 # This module is a client of `assets_and_resources` as the latter
18 # covers the services of Android.
21 intrude import assets_and_resources
26 jni_env
.push_local_frame
8
28 var manager
= app
.asset_manager
29 var stream
= manager
.open
(path
)
30 if stream
.is_java_null
then
31 self.error
= new Error("Failed to open asset at '{path}'")
32 jni_env
.pop_local_frame
37 var text
= stream
.read_all
39 if text
.is_java_null
then
40 self.error
= new Error("Failed to read content of asset file at '{path}'")
41 jni_env
.pop_local_frame
46 var content
= text
.to_s
47 jni_env
.pop_local_frame
53 redef class NativeInputStream
55 # Read and return all the content of this stream
56 private fun read_all
: JavaString in "Java" `{
57 // `available
` returns the n of bytes currently available, not the total.
58 // This may cause problems in the future with large asset files.
60 int size = self.available();
61 byte[] bytes = new byte[size];
62 self.read(bytes, 0, size);
64 return new String(bytes);
65 } catch (java.lang.Exception exception) {
66 exception.printStackTrace();