IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
RandomFileAccess.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
20 static unsafe class RandomAccessFile
21 {
22
23#if FIRST_PASS == false
24
25 static FileDescriptorAccessor fileDescriptorAccessor;
26 static RandomAccessFileAccessor randomAccessFileAccessor;
27
28 static FileDescriptorAccessor FileDescriptorAccessor => JVM.Internal.BaseAccessors.Get(ref fileDescriptorAccessor);
29
30 static RandomAccessFileAccessor RandomAccessFileAccessor => JVM.Internal.BaseAccessors.Get(ref randomAccessFileAccessor);
31
32 static global::ikvm.@internal.CallerID __callerID;
33 delegate void __jniDelegate__open0(IntPtr jniEnv, IntPtr self, IntPtr name, int mode);
34 static __jniDelegate__open0 __jniPtr__open0;
35 delegate int __jniDelegate__read0(IntPtr jniEnv, IntPtr self);
36 static __jniDelegate__read0 __jniPtr__read0;
37 delegate int __jniDelegate__readBytes(IntPtr jniEnv, IntPtr self, IntPtr bytes, int off, int len);
38 static __jniDelegate__readBytes __jniPtr__readBytes;
39 delegate void __jniDelegate__write0(IntPtr jniEnv, IntPtr self, int @byte);
40 static __jniDelegate__write0 __jniPtr__write0;
41 delegate void __jniDelegate__writeBytes(IntPtr jniEnv, IntPtr self, IntPtr bytes, int off, int len);
42 static __jniDelegate__writeBytes __jniPtr__writeBytes;
43 delegate long __jniDelegate__getFilePointer(IntPtr jniEnv, IntPtr self);
44 static __jniDelegate__getFilePointer __jniPtr__getFilePointer;
45 delegate void __jniDelegate__seek0(IntPtr jniEnv, IntPtr self, long pos);
46 static __jniDelegate__seek0 __jniPtr__seek0;
47 delegate long __jniDelegate__length(IntPtr jniEnv, IntPtr self);
48 static __jniDelegate__length __jniPtr__length;
49 delegate void __jniDelegate__setLength(IntPtr jniEnv, IntPtr self, long newLength);
50 static __jniDelegate__setLength __jniPtr__setLength;
51 delegate void __jniDelegate__close0(IntPtr jniEnv, IntPtr self);
52 static __jniDelegate__close0 __jniPtr__close0;
53
54#endif
55
56 const int O_RDONLY = 1;
57 const int O_RDWR = 2;
58 const int O_SYNC = 4;
59 const int O_DSYNC = 8;
60
67 public static void open0(object self, string name, int mode)
68 {
69#if FIRST_PASS
70 throw new NotImplementedException();
71#else
72 if (JVM.Vfs.IsPath(name))
73 {
74 var fd = RandomAccessFileAccessor.GetFd(self);
75 if (fd == null)
76 throw new global::java.io.IOException("Invalid file handle.");
77
78 try
79 {
80 var fileMode = (FileMode)0;
81 if ((mode & O_RDONLY) == O_RDONLY)
82 fileMode |= FileMode.Open;
83 if ((mode & O_RDWR) == O_RDWR)
84 fileMode |= FileMode.OpenOrCreate;
85
86 var fileAccess = (FileAccess)0;
87 if ((mode & O_RDONLY) == O_RDONLY)
88 fileAccess |= FileAccess.Read;
89 if ((mode & O_RDWR) == O_RDWR)
90 fileAccess |= FileAccess.ReadWrite;
91
92 FileDescriptorAccessor.SetPtr(fd, -1);
93 FileDescriptorAccessor.SetStream(fd, JVM.Vfs.Open(name, fileMode, fileAccess));
94 return;
95 }
96 catch (ObjectDisposedException e)
97 {
98 throw new global::java.io.IOException(e.Message);
99 }
100 catch (ArgumentException e)
101 {
102 throw new global::java.io.FileNotFoundException(e.Message);
103 }
104 catch (SecurityException e)
105 {
106 throw new global::java.lang.SecurityException(e.Message);
107 }
108 catch (UnauthorizedAccessException)
109 {
110 throw new global::java.io.FileNotFoundException(name + " (Access is denied)");
111 }
112 catch (IOException e)
113 {
114 throw new global::java.io.FileNotFoundException(e.Message);
115 }
116 catch (NotSupportedException e)
117 {
118 throw new global::java.io.FileNotFoundException(e.Message);
119 }
120 }
121 else
122 {
123 __callerID ??= global::ikvm.@internal.CallerID.create(typeof(global::java.io.RandomAccessFile).TypeHandle);
124 __jniPtr__open0 ??= Marshal.GetDelegateForFunctionPointer<__jniDelegate__open0>(JNIFrame.GetFuncPtr(__callerID, "java/io/RandomAccessFile", nameof(open0), "(Ljava/lang/String;I)V"));
125 var jniFrm = new JNIFrame();
126 var jniEnv = jniFrm.Enter(__callerID);
127 try
128 {
129 __jniPtr__open0(jniEnv, jniFrm.MakeLocalRef(self), jniFrm.MakeLocalRef(name), mode);
130 }
131 catch (Exception ex)
132 {
133 System.Console.WriteLine("*** exception in native code ***");
134 System.Console.WriteLine(ex);
135 throw;
136 }
137 finally
138 {
139 jniFrm.Leave();
140 }
141 }
142#endif
143 }
144
150 public static int read0(object self)
151 {
152#if FIRST_PASS
153 throw new NotImplementedException();
154#else
155 var fd = RandomAccessFileAccessor.GetFd(self);
156 if (fd == null)
157 throw new global::java.io.IOException("Invalid file handle.");
158
159 if (FileDescriptorAccessor.GetStream(fd) is Stream stream)
160 {
161 if (stream.CanRead == false)
162 throw new global::java.io.IOException("Read failed.");
163
164 try
165 {
166 stream.Flush();
167 return stream.ReadByte();
168 }
169 catch (ObjectDisposedException e)
170 {
171 throw new global::java.io.IOException(e.Message);
172 }
173 catch (NotSupportedException e)
174 {
175 throw new global::java.io.IOException(e.Message);
176 }
177 catch (IOException e)
178 {
179 throw new global::java.io.IOException(e.Message);
180 }
181 }
182 else
183 {
184 __callerID ??= global::ikvm.@internal.CallerID.create(typeof(global::java.io.RandomAccessFile).TypeHandle);
185 __jniPtr__read0 ??= Marshal.GetDelegateForFunctionPointer<__jniDelegate__read0>(JNIFrame.GetFuncPtr(__callerID, "java/io/RandomAccessFile", nameof(read0), "()I"));
186 var jniFrm = new JNIFrame();
187 var jniEnv = jniFrm.Enter(__callerID);
188 try
189 {
190 return __jniPtr__read0(jniEnv, jniFrm.MakeLocalRef(self));
191 }
192 catch (Exception ex)
193 {
194 System.Console.WriteLine("*** exception in native code ***");
195 System.Console.WriteLine(ex);
196 throw;
197 }
198 finally
199 {
200 jniFrm.Leave();
201 }
202 }
203#endif
204 }
205
214 public static int readBytes(object self, byte[] bytes, int off, int len)
215 {
216#if FIRST_PASS
217 throw new NotImplementedException();
218#else
219 var fd = RandomAccessFileAccessor.GetFd(self);
220 if (fd == null)
221 throw new global::java.io.IOException("Invalid file handle.");
222
223 if (FileDescriptorAccessor.GetStream(fd) is Stream stream)
224 {
225 if (bytes == null)
226 throw new global::java.lang.NullPointerException();
227
228 if ((off < 0) || (off > bytes.Length) || (len < 0) || (len > (bytes.Length - off)))
229 throw new global::java.lang.IndexOutOfBoundsException();
230
231 if (len == 0)
232 return 0;
233
234 if (stream.CanRead == false)
235 throw new global::java.io.IOException("Read failed.");
236
237 try
238 {
239 var n = stream.Read(bytes, off, len);
240 return n == 0 ? -1 : n;
241 }
242 catch (ObjectDisposedException e)
243 {
244 throw new global::java.io.IOException(e.Message);
245 }
246 catch (NotSupportedException e)
247 {
248 throw new global::java.io.IOException(e.Message);
249 }
250 catch (IOException e)
251 {
252 throw new global::java.io.IOException(e.Message);
253 }
254 }
255 else
256 {
257 __callerID ??= global::ikvm.@internal.CallerID.create(typeof(global::java.io.RandomAccessFile).TypeHandle);
258 __jniPtr__readBytes ??= Marshal.GetDelegateForFunctionPointer<__jniDelegate__readBytes>(JNIFrame.GetFuncPtr(__callerID, "java/io/RandomAccessFile", nameof(readBytes), "([BII)I"));
259 var jniFrm = new JNIFrame();
260 var jniEnv = jniFrm.Enter(__callerID);
261 try
262 {
263 return __jniPtr__readBytes(jniEnv, jniFrm.MakeLocalRef(self), jniFrm.MakeLocalRef(bytes), off, len);
264 }
265 catch (Exception ex)
266 {
267 System.Console.WriteLine("*** exception in native code ***");
268 System.Console.WriteLine(ex);
269 throw;
270 }
271 finally
272 {
273 jniFrm.Leave();
274 }
275 }
276#endif
277 }
278
284 public static void write0(object self, int @byte)
285 {
286#if FIRST_PASS
287 throw new NotImplementedException();
288#else
289 var fd = RandomAccessFileAccessor.GetFd(self);
290 if (fd == null)
291 throw new global::java.io.IOException("Invalid file handle.");
292
293 if (FileDescriptorAccessor.GetStream(fd) is Stream stream)
294 {
295 if (stream.CanWrite == false)
296 throw new global::java.io.IOException("Write failed.");
297
298 try
299 {
300 stream.WriteByte((byte)@byte);
301 }
302 catch (ObjectDisposedException e)
303 {
304 throw new global::java.io.IOException(e.Message);
305 }
306 catch (NotSupportedException e)
307 {
308 throw new global::java.io.IOException(e.Message);
309 }
310 catch (IOException e)
311 {
312 throw new global::java.io.IOException(e.Message);
313 }
314 }
315 else
316 {
317 __callerID ??= global::ikvm.@internal.CallerID.create(typeof(global::java.io.RandomAccessFile).TypeHandle);
318 __jniPtr__write0 ??= Marshal.GetDelegateForFunctionPointer<__jniDelegate__write0>(JNIFrame.GetFuncPtr(__callerID, "java/io/RandomAccessFile", nameof(write0), "(I)V"));
319 var jniFrm = new JNIFrame();
320 var jniEnv = jniFrm.Enter(__callerID);
321 try
322 {
323 __jniPtr__write0(jniEnv, jniFrm.MakeLocalRef(self), @byte);
324 }
325 catch (Exception ex)
326 {
327 System.Console.WriteLine("*** exception in native code ***");
328 System.Console.WriteLine(ex);
329 throw;
330 }
331 finally
332 {
333 jniFrm.Leave();
334 }
335 }
336#endif
337 }
338
346 public static void writeBytes(object self, byte[] bytes, int off, int len)
347 {
348#if FIRST_PASS
349 throw new NotImplementedException();
350#else
351 var fd = RandomAccessFileAccessor.GetFd(self);
352 if (fd == null)
353 throw new global::java.io.IOException("Invalid file handle.");
354
355 if (FileDescriptorAccessor.GetStream(fd) is Stream stream)
356 {
357 if ((off < 0) || (off > bytes.Length) || (len < 0) || (len > (bytes.Length - off)))
358 throw new global::java.lang.IndexOutOfBoundsException();
359 if (stream.CanWrite == false)
360 throw new global::java.io.IOException("Write failed.");
361
362 try
363 {
364 stream.Write(bytes, off, len);
365 }
366 catch (ObjectDisposedException e)
367 {
368 throw new global::java.io.IOException(e.Message);
369 }
370 catch (NotSupportedException e)
371 {
372 throw new global::java.io.IOException(e.Message);
373 }
374 catch (IOException e)
375 {
376 throw new global::java.io.IOException(e.Message);
377 }
378 }
379 else
380 {
381 __callerID ??= global::ikvm.@internal.CallerID.create(typeof(global::java.io.RandomAccessFile).TypeHandle);
382 __jniPtr__writeBytes ??= Marshal.GetDelegateForFunctionPointer<__jniDelegate__writeBytes>(JNIFrame.GetFuncPtr(__callerID, "java/io/RandomAccessFile", nameof(writeBytes), "([BII)V"));
383 var jniFrm = new JNIFrame();
384 var jniEnv = jniFrm.Enter(__callerID);
385 try
386 {
387 __jniPtr__writeBytes(jniEnv, jniFrm.MakeLocalRef(self), jniFrm.MakeLocalRef(bytes), off, len);
388 }
389 catch (Exception ex)
390 {
391 System.Console.WriteLine("*** exception in native code ***");
392 System.Console.WriteLine(ex);
393 throw;
394 }
395 finally
396 {
397 jniFrm.Leave();
398 }
399 }
400#endif
401 }
402
408 public static long getFilePointer(object self)
409 {
410#if FIRST_PASS
411 throw new NotImplementedException();
412#else
413 var fd = RandomAccessFileAccessor.GetFd(self);
414 if (fd == null)
415 throw new global::java.io.IOException("Invalid file handle.");
416
417 if (FileDescriptorAccessor.GetStream(fd) is Stream stream)
418 {
419 try
420 {
421 return stream.Position;
422 }
423 catch (ObjectDisposedException e)
424 {
425 throw new global::java.io.IOException(e.Message);
426 }
427 catch (NotSupportedException e)
428 {
429 throw new global::java.io.IOException(e.Message);
430 }
431 catch (IOException e)
432 {
433 throw new global::java.io.IOException(e.Message);
434 }
435 }
436 else
437 {
438 __callerID ??= global::ikvm.@internal.CallerID.create(typeof(global::java.io.RandomAccessFile).TypeHandle);
439 __jniPtr__getFilePointer ??= Marshal.GetDelegateForFunctionPointer<__jniDelegate__getFilePointer>(JNIFrame.GetFuncPtr(__callerID, "java/io/RandomAccessFile", nameof(getFilePointer), "()J"));
440 var jniFrm = new JNIFrame();
441 var jniEnv = jniFrm.Enter(__callerID);
442 try
443 {
444 return __jniPtr__getFilePointer(jniEnv, jniFrm.MakeLocalRef(self));
445 }
446 catch (Exception ex)
447 {
448 System.Console.WriteLine("*** exception in native code ***");
449 System.Console.WriteLine(ex);
450 throw;
451 }
452 finally
453 {
454 jniFrm.Leave();
455 }
456 }
457#endif
458 }
459
465 public static void seek0(object self, long pos)
466 {
467#if FIRST_PASS
468 throw new NotImplementedException();
469#else
470 var fd = RandomAccessFileAccessor.GetFd(self);
471 if (fd == null)
472 throw new global::java.io.IOException("Invalid file handle.");
473
474 if (FileDescriptorAccessor.GetStream(fd) is Stream stream)
475 {
476 try
477 {
478 stream.Position = pos;
479 }
480 catch (ObjectDisposedException e)
481 {
482 throw new global::java.io.IOException(e.Message);
483 }
484 catch (NotSupportedException e)
485 {
486 throw new global::java.io.IOException(e.Message);
487 }
488 catch (ArgumentOutOfRangeException)
489 {
490 throw new global::java.io.IOException("Negative seek offset.");
491 }
492 catch (IOException e)
493 {
494 throw new global::java.io.IOException(e.Message);
495 }
496 }
497 else
498 {
499 __callerID ??= global::ikvm.@internal.CallerID.create(typeof(global::java.io.RandomAccessFile).TypeHandle);
500 __jniPtr__seek0 ??= Marshal.GetDelegateForFunctionPointer<__jniDelegate__seek0>(JNIFrame.GetFuncPtr(__callerID, "java/io/RandomAccessFile", nameof(seek0), "(J)V"));
501 var jniFrm = new JNIFrame();
502 var jniEnv = jniFrm.Enter(__callerID);
503 try
504 {
505 __jniPtr__seek0(jniEnv, jniFrm.MakeLocalRef(self), pos);
506 }
507 catch (Exception ex)
508 {
509 System.Console.WriteLine("*** exception in native code ***");
510 System.Console.WriteLine(ex);
511 throw;
512 }
513 finally
514 {
515 jniFrm.Leave();
516 }
517 }
518#endif
519 }
520
526 public static long length(object self)
527 {
528#if FIRST_PASS
529 throw new NotImplementedException();
530#else
531 var fd = RandomAccessFileAccessor.GetFd(self);
532 if (fd == null)
533 throw new global::java.io.IOException("Invalid file handle.");
534
535 if (FileDescriptorAccessor.GetStream(fd) is Stream stream)
536 {
537 try
538 {
539 return stream.Length;
540 }
541 catch (ObjectDisposedException e)
542 {
543 throw new global::java.io.IOException(e.Message);
544 }
545 catch (NotSupportedException e)
546 {
547 throw new global::java.io.IOException(e.Message);
548 }
549 catch (IOException e)
550 {
551 throw new global::java.io.IOException(e.Message);
552 }
553 }
554 else
555 {
556 __callerID ??= global::ikvm.@internal.CallerID.create(typeof(global::java.io.RandomAccessFile).TypeHandle);
557 __jniPtr__length ??= Marshal.GetDelegateForFunctionPointer<__jniDelegate__length>(JNIFrame.GetFuncPtr(__callerID, "java/io/RandomAccessFile", nameof(length), "()J"));
558 var jniFrm = new JNIFrame();
559 var jniEnv = jniFrm.Enter(__callerID);
560 try
561 {
562 return __jniPtr__length(jniEnv, jniFrm.MakeLocalRef(self));
563 }
564 catch (Exception ex)
565 {
566 System.Console.WriteLine("*** exception in native code ***");
567 System.Console.WriteLine(ex);
568 throw;
569 }
570 finally
571 {
572 jniFrm.Leave();
573 }
574 }
575#endif
576 }
577
583 public static void setLength(object self, long newLength)
584 {
585#if FIRST_PASS
586 throw new NotImplementedException();
587#else
588 var fd = RandomAccessFileAccessor.GetFd(self);
589 if (fd == null)
590 throw new global::java.io.IOException("Invalid file handle.");
591
592 if (FileDescriptorAccessor.GetStream(fd) is Stream stream)
593 {
594 var p = stream.Position;
595
596 try
597 {
598 stream.SetLength(newLength);
599 stream.Position = Math.Min(p, stream.Length);
600 }
601 catch (ObjectDisposedException e)
602 {
603 stream.Position = p;
604 throw new global::java.io.IOException(e.Message);
605 }
606 catch (NotSupportedException e)
607 {
608 stream.Position = p;
609 throw new global::java.io.IOException(e.Message);
610 }
611 catch (UnauthorizedAccessException e)
612 {
613 stream.Position = p;
614 throw new global::java.io.IOException(e.Message);
615 }
616 catch (IOException e)
617 {
618 stream.Position = p;
619 throw new global::java.io.IOException(e.Message);
620 }
621 }
622 else
623 {
624 __callerID ??= global::ikvm.@internal.CallerID.create(typeof(global::java.io.RandomAccessFile).TypeHandle);
625 __jniPtr__setLength ??= Marshal.GetDelegateForFunctionPointer<__jniDelegate__setLength>(JNIFrame.GetFuncPtr(__callerID, "java/io/RandomAccessFile", nameof(setLength), "(J)V"));
626 var jniFrm = new JNIFrame();
627 var jniEnv = jniFrm.Enter(__callerID);
628 try
629 {
630 __jniPtr__setLength(jniEnv, jniFrm.MakeLocalRef(self), newLength);
631 }
632 catch (Exception ex)
633 {
634 System.Console.WriteLine("*** exception in native code ***");
635 System.Console.WriteLine(ex);
636 throw;
637 }
638 finally
639 {
640 jniFrm.Leave();
641 }
642 }
643#endif
644 }
645
650 public static void close0(object self)
651 {
652#if FIRST_PASS
653 throw new NotImplementedException();
654#else
655 var fd = RandomAccessFileAccessor.GetFd(self);
656 if (fd == null)
657 return;
658
659 if (FileDescriptorAccessor.GetStream(fd) is Stream stream)
660 {
661 try
662 {
663 stream.Close();
664 FileDescriptorAccessor.SetPtr(fd, -1);
665 }
666 catch
667 {
668 // ignore
669 }
670 }
671 else
672 {
673 __callerID ??= global::ikvm.@internal.CallerID.create(typeof(global::java.io.RandomAccessFile).TypeHandle);
674 __jniPtr__close0 ??= Marshal.GetDelegateForFunctionPointer<__jniDelegate__close0>(JNIFrame.GetFuncPtr(__callerID, "java/io/RandomAccessFile", nameof(close0), "()V"));
675 var jniFrm = new JNIFrame();
676 var jniEnv = jniFrm.Enter(__callerID);
677 try
678 {
679 __jniPtr__close0(jniEnv, jniFrm.MakeLocalRef(self));
680 }
681 catch (Exception ex)
682 {
683 System.Console.WriteLine("*** exception in native code ***");
684 System.Console.WriteLine(ex);
685 throw;
686 }
687 finally
688 {
689 jniFrm.Leave();
690 }
691 }
692#endif
693 }
694
695 }
696
697}
Implements the native methods for 'RandomAccessFile'.
static void close0(object self)
Implements the native method 'close0'.
static long getFilePointer(object self)
Implements the native method 'getFilePointer'.
static void writeBytes(object self, byte[] bytes, int off, int len)
Implements the native method 'writeBytes'.
static int readBytes(object self, byte[] bytes, int off, int len)
Implements the native method 'readBytes'.
static long length(object self)
Implements the native method 'length'.
static void seek0(object self, long pos)
Implements the native method 'seek0'.
static int read0(object self)
Implements the native method 'read0'.
static void write0(object self, int @byte)
Implements the native method 'write'.
static void setLength(object self, long newLength)
Implements the native method 'setLength'.
static void open0(object self, string name, int mode)
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