IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
FileOutputStream.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
10
12{
13
14 static class FileOutputStream
15 {
16
17#if FIRST_PASS == false
18
19 static FileDescriptorAccessor fileDescriptorAccessor;
20 static FileOutputStreamAccessor fileOutputStreamAccessor;
21
22 static FileDescriptorAccessor FileDescriptorAccessor => JVM.Internal.BaseAccessors.Get(ref fileDescriptorAccessor);
23
24 static FileOutputStreamAccessor FileOutputStreamAccessor => JVM.Internal.BaseAccessors.Get(ref fileOutputStreamAccessor);
25
26 static global::ikvm.@internal.CallerID __callerID;
27 delegate void __jniDelegate__open0(IntPtr jniEnv, IntPtr this_, IntPtr path, sbyte append);
28 static __jniDelegate__open0 __jniPtr__open0;
29 delegate void __jniDelegate__write(IntPtr jniEnv, IntPtr this_, int byte_, sbyte append);
30 static __jniDelegate__write __jniPtr__write;
31 delegate void __jniDelegate__writeBytes(IntPtr jniEnv, IntPtr this_, IntPtr bytes, int off, int len, sbyte append);
32 static __jniDelegate__writeBytes __jniPtr__writeBytes;
33 delegate int __jniDelegate__close0(IntPtr jniEnv, IntPtr this_);
34 static __jniDelegate__close0 __jniPtr__close0;
35
36#endif
37
44 public static void open0(object this_, string path, bool append)
45 {
46#if FIRST_PASS
47 throw new NotImplementedException();
48#else
49 if (JVM.Vfs.IsPath(path))
50 {
51 try
52 {
53 var fd = FileOutputStreamAccessor.GetFd(this_);
54 if (fd == null)
55 throw new global::java.io.IOException("The handle is invalid.");
56
57 FileDescriptorAccessor.SetPtr(fd, -1);
58 FileDescriptorAccessor.SetStream(fd, JVM.Vfs.Open(path, append ? FileMode.Append : FileMode.Create, FileAccess.Write));
59 }
60 catch (ObjectDisposedException e)
61 {
62 throw new global::java.io.IOException(e.Message);
63 }
64 catch (ArgumentException e)
65 {
66 throw new global::java.io.FileNotFoundException(e.Message);
67 }
68 catch (SecurityException e)
69 {
70 throw new global::java.lang.SecurityException(e.Message);
71 }
72 catch (UnauthorizedAccessException)
73 {
74 throw new global::java.io.FileNotFoundException(path + " (Access is denied)");
75 }
76 catch (IOException e)
77 {
78 throw new global::java.io.FileNotFoundException(e.Message);
79 }
80 catch (NotSupportedException e)
81 {
82 throw new global::java.io.FileNotFoundException(e.Message);
83 }
84 }
85 else
86 {
87 __callerID ??= global::ikvm.@internal.CallerID.create(typeof(global::java.io.FileOutputStream).TypeHandle);
88 __jniPtr__open0 ??= Marshal.GetDelegateForFunctionPointer<__jniDelegate__open0>(JNIFrame.GetFuncPtr(__callerID, "java/io/FileOutputStream", nameof(open0), "(Ljava/lang/String;Z)V"));
89 var jniFrm = new JNIFrame();
90 var jniEnv = jniFrm.Enter(__callerID);
91 try
92 {
93 var selfRef = jniFrm.MakeLocalRef(this_);
94 var pathRef = jniFrm.MakeLocalRef(path);
95 __jniPtr__open0(jniEnv, selfRef, pathRef, append ? JNIEnv.JNI_TRUE : JNIEnv.JNI_FALSE);
96 }
97 catch (Exception ex)
98 {
99 System.Console.WriteLine("*** exception in native code ***");
100 System.Console.WriteLine(ex);
101 throw;
102 }
103 finally
104 {
105 jniFrm.Leave();
106 }
107 }
108#endif
109 }
110
117 public static void write(object self, int byte_, bool append)
118 {
119#if FIRST_PASS
120 throw new NotImplementedException();
121#else
122 var fd = FileOutputStreamAccessor.GetFd(self);
123 if (fd == null)
124 throw new global::java.io.IOException("The handle is invalid.");
125
126 if (FileDescriptorAccessor.GetStream(fd) is Stream stream)
127 {
128 if (stream.CanWrite == false)
129 throw new global::java.io.IOException("The handle is invalid.");
130
131 try
132 {
133 stream.WriteByte((byte)byte_);
134 stream.Flush();
135 }
136 catch (ObjectDisposedException e)
137 {
138 throw new global::java.io.IOException(e.Message);
139 }
140 catch (NotSupportedException e)
141 {
142 throw new global::java.io.IOException(e.Message);
143 }
144 catch (IOException e)
145 {
146 throw new global::java.io.IOException(e.Message);
147 }
148 }
149 else
150 {
151 __callerID ??= global::ikvm.@internal.CallerID.create(typeof(global::java.io.FileOutputStream).TypeHandle);
152 __jniPtr__write ??= Marshal.GetDelegateForFunctionPointer<__jniDelegate__write>(JNIFrame.GetFuncPtr(__callerID, "java/io/FileOutputStream", nameof(write), "(IZ)V"));
153 var jniFrm = new JNIFrame();
154 var jniEnv = jniFrm.Enter(__callerID);
155 try
156 {
157 var selfRef = jniFrm.MakeLocalRef(self);
158 __jniPtr__write(jniEnv, selfRef, byte_, append ? JNIEnv.JNI_TRUE : JNIEnv.JNI_FALSE);
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 void writeBytes(object self, byte[] bytes, int off, int len, bool append)
183 {
184#if FIRST_PASS
185 throw new NotImplementedException();
186#else
187 var fd = FileOutputStreamAccessor.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 ((off < 0) || (off > bytes.Length) || (len < 0) || (len > (bytes.Length - off)))
194 throw new global::java.lang.IndexOutOfBoundsException();
195
196 if (stream.CanWrite == false)
197 throw new global::java.io.IOException("The handle is invalid.");
198
199 try
200 {
201 stream.Write(bytes, off, len);
202 stream.Flush();
203 }
204 catch (ObjectDisposedException e)
205 {
206 throw new global::java.io.IOException(e.Message);
207 }
208 catch (NotSupportedException e)
209 {
210 throw new global::java.io.IOException(e.Message);
211 }
212 catch (IOException e)
213 {
214 throw new global::java.io.IOException(e.Message);
215 }
216 }
217 else
218 {
219 __callerID ??= global::ikvm.@internal.CallerID.create(typeof(global::java.io.FileOutputStream).TypeHandle);
220 __jniPtr__writeBytes ??= Marshal.GetDelegateForFunctionPointer<__jniDelegate__writeBytes>(JNIFrame.GetFuncPtr(__callerID, "java/io/FileOutputStream", nameof(writeBytes), "([BIIZ)V"));
221 var jniFrm = new JNIFrame();
222 var jniEnv = jniFrm.Enter(__callerID);
223 try
224 {
225 __jniPtr__writeBytes(jniEnv, jniFrm.MakeLocalRef(self), jniFrm.MakeLocalRef(bytes), off, len, append ? JNIEnv.JNI_TRUE : JNIEnv.JNI_FALSE);
226 }
227 catch (Exception ex)
228 {
229 System.Console.WriteLine("*** exception in native code ***");
230 System.Console.WriteLine(ex);
231 throw;
232 }
233 finally
234 {
235 jniFrm.Leave();
236 }
237 }
238#endif
239 }
240
245 public static void close0(object this_)
246 {
247#if FIRST_PASS
248 throw new NotImplementedException();
249#else
250 var fd = FileOutputStreamAccessor.GetFd(this_);
251 if (fd == null)
252 throw new global::java.io.IOException("The handle is invalid.");
253
254 if (FileDescriptorAccessor.GetStream(fd) is Stream stream)
255 {
256 try
257 {
258 stream.Close();
259 FileDescriptorAccessor.SetPtr(fd, -1);
260 }
261 catch
262 {
263 // ignore
264 }
265 }
266 else
267 {
268 __callerID ??= global::ikvm.@internal.CallerID.create(typeof(global::java.io.FileOutputStream).TypeHandle);
269 __jniPtr__close0 ??= Marshal.GetDelegateForFunctionPointer<__jniDelegate__close0>(JNIFrame.GetFuncPtr(__callerID, "java/io/FileOutputStream", nameof(close0), "()V"));
270 var jniFrm = new JNIFrame();
271 var jniEnv = jniFrm.Enter(__callerID);
272 try
273 {
274 __jniPtr__close0(jniEnv, jniFrm.MakeLocalRef(this_));
275 }
276 catch (Exception ex)
277 {
278 System.Console.WriteLine("*** exception in native code ***");
279 System.Console.WriteLine(ex);
280 throw;
281 }
282 finally
283 {
284 jniFrm.Leave();
285 }
286 }
287#endif
288 }
289
290 }
291
292}
static void write(object self, int byte_, bool append)
Implements the native method 'write'.
static void writeBytes(object self, byte[] bytes, int off, int len, bool append)
Implements the native method 'writeBytes'.
static void close0(object this_)
Implements the native method 'close0'.
static void open0(object this_, string path, bool append)
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