Merge branch 'master' into polymorphic_extern_classes
[nit.git] / lib / java / io.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2014 Romain Chanoir <romain.chanoir@viacesi.fr>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Services from the `java.io` package
18 #
19 # This module is used by `android::assets_and_resources` and `android::audio`.
20 module io
21
22 import java
23
24 in "Java" `{
25 import java.io.File;
26 import java.io.FileInputStream;
27 import java.io.FileOutputStream;
28 import java.io.FileDescriptor;
29 import java.io.IOException;
30 import java.io.SyncFailedException;
31 `}
32
33 extern class NativeFile in "Java" `{ java.io.File `}
34 super JavaObject
35 redef type SELF: NativeFile
36
37 fun can_execute: Bool in "Java" `{ return recv.canExecute(); `}
38 fun can_read: Bool in "Java" `{ return recv.canRead(); `}
39 fun can_write: Bool in "Java" `{ return recv.canWrite(); `}
40 fun create_file: Bool in "Java" `{
41 try {
42 return recv.createNewFile();
43 }catch(IOException e){
44 e.printStackTrace();
45 return false;
46 }
47 `}
48 fun delete: Bool in "Java" `{ return recv.delete(); `}
49 fun delete_on_exit in "Java" `{ recv.deleteOnExit(); `}
50 fun exists: Bool in "Java" `{ return recv.exists(); `}
51 fun absolute_file: NativeFile in "Java" `{ return recv.getAbsoluteFile(); `}
52 fun absolute_path: JavaString in "Java" `{ return recv.getAbsolutePath(); `}
53 fun canonical_file: NativeFile in "Java" `{
54 try {
55 return recv.getCanonicalFile();
56 }catch(IOException e){
57 e.printStackTrace();
58 return null;
59 }
60 `}
61 fun free_space: Int in "Java" `{ return (int)recv.getFreeSpace(); `}
62 fun name: JavaString in "Java" `{ return recv.getName(); `}
63 fun parent: JavaString in "Java" `{ return recv.getParent(); `}
64 fun parent_file: NativeFile in "Java" `{ return recv.getParentFile(); `}
65 fun path: JavaString in "Java" `{ return recv.getPath(); `}
66 fun total_space: Int in "Java" `{ return (int)recv.getTotalSpace(); `}
67 fun usable_space: Int in "Java" `{ return (int)recv.getUsableSpace(); `}
68 fun absolute: Bool in "Java" `{ return recv.isAbsolute(); `}
69 fun is_directory: Bool in "Java" `{ return recv.isDirectory(); `}
70 fun is_file: Bool in "Java" `{ return recv.isFile(); `}
71 fun is_hidden: Bool in "Java" `{ return recv.isHidden(); `}
72 fun last_modified: Int in "Java" `{ return (int)recv.lastModified(); `}
73 fun length: Int in "Java" `{ return (int)recv.length(); `}
74 fun set_readable(r: Bool, owner_only: Bool): Bool in "Java" `{ return recv.setReadable(r, owner_only); `}
75 fun set_writable(w: Bool, owner_only: Bool): Bool in "Java" `{ return recv.setWritable(w, owner_only); `}
76 end
77
78 extern class NativeFileInputStream in "Java" `{ java.io.FileInputStream `}
79 super JavaObject
80 redef type SELF: NativeFileInputStream
81
82 fun available: Int in "Java" `{
83 try {
84 return recv.available();
85 }catch(IOException e){
86 e.printStackTrace();
87 return -1;
88 }
89 `}
90 fun close in "Java" `{
91 try {
92 recv.close();
93 }catch(IOException e){
94 e.printStackTrace();
95 }
96 `}
97 fun read: Int in "Java" `{
98 try {
99 return recv.read();
100 }catch(IOException e){
101 e.printStackTrace();
102 return -1;
103 }
104 `}
105 fun skip(byte_count: Int): Int in "Java" `{
106 try {
107 return (int)recv.skip(byte_count);
108 }catch(IOException e){
109 e.printStackTrace();
110 return -1;
111 }
112 `}
113 end
114
115 extern class NativeFileOutputStream in "Java" `{ java.io.FileOutputStream `}
116 super JavaObject
117 redef type SELF: NativeFileOutputStream
118
119 fun close in "Java" `{
120 try {
121 recv.close();
122 }catch(IOException e){
123 e.printStackTrace();
124 }
125 `}
126 fun write(one_byte: Int) in "Java" `{
127 try {
128 recv.write((byte)one_byte);
129 }catch(IOException e){
130 e.printStackTrace();
131 }
132 `}
133 fun flush in "Java" `{
134 try {
135 recv.flush();
136 }catch(IOException e){
137 e.printStackTrace();
138 }
139 `}
140 end
141
142 extern class NativeFileDescriptor in "Java" `{ java.io.FileDescriptor `}
143 super JavaObject
144 redef type SELF: NativeFileDescriptor
145 fun sync in "Java" `{
146 try{
147 recv.sync();
148 }catch(SyncFailedException e){
149 e.printStackTrace();
150 }
151 `}
152 fun valid: Bool in "Java" `{ return recv.valid(); `}
153 end
154
155 extern class NativeInputStream in "Java" `{ java.io.InputStream `}
156 super JavaObject
157 redef type SELF: NativeInputStream
158
159 fun available: Int in "Java" `{
160 try {
161 return recv.available();
162 }catch(IOException e){
163 e.printStackTrace();
164 return -1;
165 }
166 `}
167
168 fun close in "Java" `{
169 try {
170 recv.close();
171 }catch(IOException e){
172 e.printStackTrace();
173 }
174 `}
175 end