IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
FileInputStream.cs
Go to the documentation of this file.
1using System;
2using System.IO;
3using System.Runtime.InteropServices;
4using System.Security;
5
6using IKVM.Runtime;
9
11{
12
16 static class FileInputStream
17 {
18
19#if FIRST_PASS == false && IMPORTER == false && EXPORTER == false
20
21 static FileDescriptorAccessor fileDescriptorAccessor;
22 static FileInputStreamAccessor fileInputStreamAccessor;
23
24 static FileDescriptorAccessor FileDescriptorAccessor => JVM.Internal.BaseAccessors.Get(ref fileDescriptorAccessor);
25
26 static FileInputStreamAccessor FileInputStreamAccessor => JVM.Internal.BaseAccessors.Get(ref fileInputStreamAccessor);
27
28 static global::ikvm.@internal.CallerID __callerID;
29 delegate void __jniDelegate__open0(IntPtr jniEnv, IntPtr self, IntPtr path);
30 static __jniDelegate__open0 __jniPtr__open0;
31 delegate int __jniDelegate__read0(IntPtr jniEnv, IntPtr self);
32 static __jniDelegate__read0 __jniPtr__read0;
33 delegate int __jniDelegate__readBytes(IntPtr jniEnv, IntPtr self, IntPtr bytes, int off, int len);
34 static __jniDelegate__readBytes __jniPtr__readBytes;
35 delegate long __jniDelegate__skip(IntPtr jniEnv, IntPtr self, long toSkip);
36 static __jniDelegate__skip __jniPtr__skip0;
37 delegate int __jniDelegate__available(IntPtr jniEnv, IntPtr self);
38 static __jniDelegate__available __jniPtr__available0;
39 delegate int __jniDelegate__close0(IntPtr jniEnv, IntPtr self);
40 static __jniDelegate__close0 __jniPtr__close0;
41
42#endif
43
49 public static void open0(object self, string path)
50 {
51#if FIRST_PASS
52 throw new NotImplementedException();
53#else
54 if (JVM.Vfs.IsPath(path))
55 {
56 try
57 {
58 var fd = FileInputStreamAccessor.GetFd(self);
59 if (fd == null)
60 throw new global::java.io.IOException("The handle is invalid.");
61
62 FileDescriptorAccessor.SetPtr(fd, -1);
63 FileDescriptorAccessor.SetStream(fd, JVM.Vfs.Open(path, FileMode.Open, FileAccess.Read));
64 }
65 catch (ObjectDisposedException e)
66 {
67 throw new global::java.io.IOException(e.Message);
68 }
69 catch (ArgumentException e)
70 {
71 throw new global::java.io.FileNotFoundException(e.Message);
72 }
73 catch (SecurityException e)
74 {
75 throw new global::java.lang.SecurityException(e.Message);
76 }
77 catch (UnauthorizedAccessException)
78 {
79 throw new global::java.io.FileNotFoundException(path + " (Access is denied)");
80 }
81 catch (IOException e)
82 {
83 throw new global::java.io.FileNotFoundException(e.Message);
84 }
85 catch (NotSupportedException e)
86 {
87 throw new global::java.io.FileNotFoundException(e.Message);
88 }
89 }
90 else
91 {
92 __callerID ??= global::ikvm.@internal.CallerID.create(typeof(global::java.io.FileInputStream).TypeHandle);
93 __jniPtr__open0 ??= Marshal.GetDelegateForFunctionPointer<__jniDelegate__open0>(JNIFrame.GetFuncPtr(__callerID, "java/io/FileInputStream", nameof(open0), "(Ljava/lang/String;)V"));
94 var jniFrm = new JNIFrame();
95 var jniEnv = jniFrm.Enter(__callerID);
96 try
97 {
98 __jniPtr__open0(jniEnv, jniFrm.MakeLocalRef(self), jniFrm.MakeLocalRef(path));
99 }
100 catch (Exception ex)
101 {
102 System.Console.WriteLine("*** exception in native code ***");
103 System.Console.WriteLine(ex);
104 throw;
105 }
106 finally
107 {
108 jniFrm.Leave();
109 }
110 }
111#endif
112 }
113
119 public static int read0(object self)
120 {
121#if FIRST_PASS
122 throw new NotImplementedException();
123#else
124 var fd = FileInputStreamAccessor.GetFd(self);
125 if (fd == null)
126 throw new global::java.io.IOException("The handle is invalid.");
127
128 if (FileDescriptorAccessor.GetStream(fd) is Stream stream)
129 {
130 if (stream.CanRead == false)
131 throw new global::java.io.IOException("Read failed.");
132
133 try
134 {
135 return stream.ReadByte();
136 }
137 catch (ObjectDisposedException e)
138 {
139 throw new global::java.io.IOException(e.Message);
140 }
141 catch (NotSupportedException e)
142 {
143 throw new global::java.io.IOException(e.Message);
144 }
145 catch (IOException e)
146 {
147 throw new global::java.io.IOException(e.Message);
148 }
149 }
150 else
151 {
152 __callerID ??= global::ikvm.@internal.CallerID.create(typeof(global::java.io.FileInputStream).TypeHandle);
153 __jniPtr__read0 ??= Marshal.GetDelegateForFunctionPointer<__jniDelegate__read0>(JNIFrame.GetFuncPtr(__callerID, "java/io/FileInputStream", nameof(read0), "()I"));
154 var jniFrm = new JNIFrame();
155 var jniEnv = jniFrm.Enter(__callerID);
156 try
157 {
158 return __jniPtr__read0(jniEnv, jniFrm.MakeLocalRef(self));
159 }
160 catch (Exception ex)
161 {
162 System.Console.WriteLine("*** exception in native code ***");
163 System.Console.WriteLine(ex);
164 throw;
165 }
166 finally
167 {
168 jniFrm.Leave();
169 }
170 }
171#endif
172 }
173
182 public static int readBytes(object self, byte[] bytes, int off, int len)
183 {
184#if FIRST_PASS
185 throw new NotImplementedException();
186#else
187 var fd = FileInputStreamAccessor.GetFd(self);
188 if (fd == null)
189 throw new global::java.io.IOException("The handle is invalid.");
190
191 if (FileDescriptorAccessor.GetStream(fd) is Stream stream)
192 {
193 if (len == 0)
194 return 0;
195
196 if ((off < 0) || (off > bytes.Length) || (len < 0) || (len > (bytes.Length - off)))
197 throw new global::java.lang.IndexOutOfBoundsException();
198
199 if (stream.CanRead == false)
200 throw new global::java.io.IOException("Read failed.");
201
202 try
203 {
204 var n = stream.Read(bytes, off, len);
205 return n == 0 ? -1 : n;
206 }
207 catch (ObjectDisposedException e)
208 {
209 throw new global::java.io.IOException(e.Message);
210 }
211 catch (NotSupportedException e)
212 {
213 throw new global::java.io.IOException(e.Message);
214 }
215 catch (IOException e)
216 {
217 throw new global::java.io.IOException(e.Message);
218 }
219 }
220 else
221 {
222 __callerID ??= global::ikvm.@internal.CallerID.create(typeof(global::java.io.FileInputStream).TypeHandle);
223 __jniPtr__readBytes ??= Marshal.GetDelegateForFunctionPointer<__jniDelegate__readBytes>(JNIFrame.GetFuncPtr(__callerID, "java/io/FileInputStream", nameof(readBytes), "([BII)I"));
224 var jniFrm = new JNIFrame();
225 var jniEnv = jniFrm.Enter(__callerID);
226 try
227 {
228 return __jniPtr__readBytes(jniEnv, jniFrm.MakeLocalRef(self), jniFrm.MakeLocalRef(bytes), off, len);
229 }
230 catch (Exception ex)
231 {
232 System.Console.WriteLine("*** exception in native code ***");
233 System.Console.WriteLine(ex);
234 throw;
235 }
236 finally
237 {
238 jniFrm.Leave();
239 }
240 }
241#endif
242 }
243
250 public static long skip0(object self, long n)
251 {
252#if FIRST_PASS
253 throw new NotImplementedException();
254#else
255 var fd = FileInputStreamAccessor.GetFd(self);
256 if (fd == null)
257 throw new global::java.io.IOException("The handle is invalid.");
258
259 if (FileDescriptorAccessor.GetStream(fd) is Stream stream)
260 {
261 if (stream.CanSeek == false)
262 throw new global::java.io.IOException("The handle is invalid.");
263
264 try
265 {
266 long cur = stream.Position;
267 long end = stream.Seek(n, SeekOrigin.Current);
268 return end - cur;
269 }
270 catch (ObjectDisposedException e)
271 {
272 throw new global::java.io.IOException(e.Message);
273 }
274 catch (NotSupportedException e)
275 {
276 throw new global::java.io.IOException(e.Message);
277 }
278 catch (IOException e)
279 {
280 throw new global::java.io.IOException(e.Message);
281 }
282 }
283 else
284 {
285 __callerID ??= global::ikvm.@internal.CallerID.create(typeof(global::java.io.FileInputStream).TypeHandle);
286 __jniPtr__skip0 ??= Marshal.GetDelegateForFunctionPointer<__jniDelegate__skip>(JNIFrame.GetFuncPtr(__callerID, "java/io/FileInputStream", nameof(skip0), "(J)J"));
287 var jniFrm = new JNIFrame();
288 var jniEnv = jniFrm.Enter(__callerID);
289 try
290 {
291 return __jniPtr__skip0(jniEnv, jniFrm.MakeLocalRef(self), n);
292 }
293 catch (Exception ex)
294 {
295 System.Console.WriteLine("*** exception in native code ***");
296 System.Console.WriteLine(ex);
297 throw;
298 }
299 finally
300 {
301 jniFrm.Leave();
302 }
303 }
304#endif
305 }
306
312 public static int available0(object self)
313 {
314#if FIRST_PASS
315 throw new NotImplementedException();
316#else
317 var fd = FileInputStreamAccessor.GetFd(self);
318 if (fd == null)
319 throw new global::java.io.IOException("The handle is invalid.");
320
321 if (FileDescriptorAccessor.GetStream(fd) is Stream stream)
322 {
323 if (stream.CanSeek == false)
324 return 0;
325
326 try
327 {
328 return (int)Math.Min(int.MaxValue, Math.Max(0, stream.Length - stream.Position));
329 }
330 catch (ObjectDisposedException e)
331 {
332 throw new global::java.io.IOException(e.Message);
333 }
334 catch (NotSupportedException e)
335 {
336 throw new global::java.io.IOException(e.Message);
337 }
338 catch (IOException e)
339 {
340 throw new global::java.io.IOException(e.Message);
341 }
342 }
343 else
344 {
345 __callerID ??= global::ikvm.@internal.CallerID.create(typeof(global::java.io.FileInputStream).TypeHandle);
346 __jniPtr__available0 ??= Marshal.GetDelegateForFunctionPointer<__jniDelegate__available>(JNIFrame.GetFuncPtr(__callerID, "java/io/FileInputStream", nameof(available0), "()I"));
347 var jniFrm = new JNIFrame();
348 var jniEnv = jniFrm.Enter(__callerID);
349 try
350 {
351 return __jniPtr__available0(jniEnv, jniFrm.MakeLocalRef(self));
352 }
353 catch (Exception ex)
354 {
355 System.Console.WriteLine("*** exception in native code ***");
356 System.Console.WriteLine(ex);
357 throw;
358 }
359 finally
360 {
361 jniFrm.Leave();
362 }
363 }
364#endif
365 }
366
371 public static void close0(object self)
372 {
373#if FIRST_PASS
374 throw new NotImplementedException();
375#else
376 var fd = FileInputStreamAccessor.GetFd(self);
377 if (fd == null)
378 throw new global::java.io.IOException("The handle is invalid.");
379
380 if (FileDescriptorAccessor.GetStream(fd) is Stream stream)
381 {
382 try
383 {
384 stream.Close();
385 FileDescriptorAccessor.SetPtr(fd, -1);
386 }
387 catch
388 {
389 // ignore
390 }
391 }
392 else
393 {
394 __callerID ??= global::ikvm.@internal.CallerID.create(typeof(global::java.io.FileInputStream).TypeHandle);
395 __jniPtr__close0 ??= Marshal.GetDelegateForFunctionPointer<__jniDelegate__close0>(JNIFrame.GetFuncPtr(__callerID, "java/io/FileInputStream", nameof(close0), "()V"));
396 var jniFrm = new JNIFrame();
397 var jniEnv = jniFrm.Enter(__callerID);
398 try
399 {
400 __jniPtr__close0(jniEnv, jniFrm.MakeLocalRef(self));
401 }
402 catch (Exception ex)
403 {
404 System.Console.WriteLine("*** exception in native code ***");
405 System.Console.WriteLine(ex);
406 throw;
407 }
408 finally
409 {
410 jniFrm.Leave();
411 }
412 }
413#endif
414 }
415
416 }
417
418}
Implements the native methods for 'FileInputStream'.
static int read0(object self)
Implements the native method 'read0'.
static int readBytes(object self, byte[] bytes, int off, int len)
Implements the native method 'readBytes'.
static long skip0(object self, long n)
Implements the native method 'skip0'.
static int available0(object self)
Implements the native method 'available0'.
static void close0(object self)
Implements the native method 'close0'.
static void open0(object self, string path)
Implements the native method 'open0'.
Main state of the running JVM.
static nint GetFuncPtr(object callerID, string clazz, string name, string sig)
Invoked by managed code to acquire a function pointer to a native JNI method.
Definition JNIFrame.cs:43