@@ -105,7 +105,10 @@ impl DataOutputStream {
105105 async fn write ( jvm : & Jvm , _: & mut RuntimeContext , this : ClassInstanceRef < Self > , b : i32 ) -> Result < ( ) > {
106106 tracing:: debug!( "java.io.DataOutputStream::write({this:?}, {b:?})" ) ;
107107
108- let out = jvm. get_field ( & this, "java/io/DataOutputStream" , "out" , "Ljava/io/OutputStream;" ) . await ?;
108+ let out: ClassInstanceRef < OutputStream > = jvm. get_field ( & this, "java/io/DataOutputStream" , "out" , "Ljava/io/OutputStream;" ) . await ?;
109+ if out. is_null ( ) {
110+ return Err ( jvm. exception ( "java/lang/NullPointerException" , "underlying stream is null" ) . await ) ;
111+ }
109112 let _: ( ) = jvm. invoke_virtual ( & out, "java/io/OutputStream" , "write" , "(I)V" , ( b, ) ) . await ?;
110113
111114 Ok ( ( ) )
@@ -114,7 +117,10 @@ impl DataOutputStream {
114117 async fn write_byte ( jvm : & Jvm , _: & mut RuntimeContext , this : ClassInstanceRef < Self > , v : i32 ) -> Result < ( ) > {
115118 tracing:: debug!( "java.io.DataOutputStream::writeByte({this:?}, {v:?})" ) ;
116119
117- let out = jvm. get_field ( & this, "java/io/DataOutputStream" , "out" , "Ljava/io/OutputStream;" ) . await ?;
120+ let out: ClassInstanceRef < OutputStream > = jvm. get_field ( & this, "java/io/DataOutputStream" , "out" , "Ljava/io/OutputStream;" ) . await ?;
121+ if out. is_null ( ) {
122+ return Err ( jvm. exception ( "java/lang/NullPointerException" , "underlying stream is null" ) . await ) ;
123+ }
118124 let _: ( ) = jvm. invoke_virtual ( & out, "java/io/OutputStream" , "write" , "(I)V" , ( v, ) ) . await ?;
119125
120126 Ok ( ( ) )
@@ -137,7 +143,10 @@ impl DataOutputStream {
137143 let mut byte_array = jvm. instantiate_array ( "B" , bytes. len ( ) as _ ) . await ?;
138144 jvm. store_array ( & mut byte_array, 0 , cast_vec :: < u8 , i8 > ( bytes. to_vec ( ) ) ) . await ?;
139145
140- let out = jvm. get_field ( & this, "java/io/DataOutputStream" , "out" , "Ljava/io/OutputStream;" ) . await ?;
146+ let out: ClassInstanceRef < OutputStream > = jvm. get_field ( & this, "java/io/DataOutputStream" , "out" , "Ljava/io/OutputStream;" ) . await ?;
147+ if out. is_null ( ) {
148+ return Err ( jvm. exception ( "java/lang/NullPointerException" , "underlying stream is null" ) . await ) ;
149+ }
141150 let _: ( ) = jvm. invoke_virtual ( & out, "java/io/OutputStream" , "write" , "([B)V" , ( byte_array, ) ) . await ?;
142151
143152 Ok ( ( ) )
@@ -156,7 +165,10 @@ impl DataOutputStream {
156165 let mut byte_array = jvm. instantiate_array ( "B" , bytes. len ( ) as _ ) . await ?;
157166 jvm. store_array ( & mut byte_array, 0 , cast_vec :: < u8 , i8 > ( bytes. to_vec ( ) ) ) . await ?;
158167
159- let out = jvm. get_field ( & this, "java/io/DataOutputStream" , "out" , "Ljava/io/OutputStream;" ) . await ?;
168+ let out: ClassInstanceRef < OutputStream > = jvm. get_field ( & this, "java/io/DataOutputStream" , "out" , "Ljava/io/OutputStream;" ) . await ?;
169+ if out. is_null ( ) {
170+ return Err ( jvm. exception ( "java/lang/NullPointerException" , "underlying stream is null" ) . await ) ;
171+ }
160172 let _: ( ) = jvm. invoke_virtual ( & out, "java/io/OutputStream" , "write" , "([B)V" , ( byte_array, ) ) . await ?;
161173
162174 Ok ( ( ) )
@@ -169,7 +181,10 @@ impl DataOutputStream {
169181 let mut byte_array = jvm. instantiate_array ( "B" , bytes. len ( ) as _ ) . await ?;
170182 jvm. store_array ( & mut byte_array, 0 , cast_vec :: < u8 , i8 > ( bytes. to_vec ( ) ) ) . await ?;
171183
172- let out = jvm. get_field ( & this, "java/io/DataOutputStream" , "out" , "Ljava/io/OutputStream;" ) . await ?;
184+ let out: ClassInstanceRef < OutputStream > = jvm. get_field ( & this, "java/io/DataOutputStream" , "out" , "Ljava/io/OutputStream;" ) . await ?;
185+ if out. is_null ( ) {
186+ return Err ( jvm. exception ( "java/lang/NullPointerException" , "underlying stream is null" ) . await ) ;
187+ }
173188 let _: ( ) = jvm. invoke_virtual ( & out, "java/io/OutputStream" , "write" , "([B)V" , ( byte_array, ) ) . await ?;
174189
175190 Ok ( ( ) )
@@ -189,23 +204,38 @@ impl DataOutputStream {
189204
190205 async fn write_bytes ( jvm : & Jvm , _: & mut RuntimeContext , this : ClassInstanceRef < Self > , s : ClassInstanceRef < String > ) -> Result < ( ) > {
191206 tracing:: debug!( "java.io.DataOutputStream::writeBytes({this:?}, {s:?})" ) ;
207+ if s. is_null ( ) {
208+ return Err ( jvm. exception ( "java/lang/NullPointerException" , "argument is null" ) . await ) ;
209+ }
192210
193211 let chars: ClassInstanceRef < Array < JavaChar > > = jvm. invoke_virtual ( & s, "java/lang/String" , "toCharArray" , "()[C" , ( ) ) . await ?;
194212 let length = jvm. array_length ( & chars) . await ?;
195213 let chars: Vec < JavaChar > = jvm. load_array ( & chars, 0 , length) . await ?;
214+ if chars. is_empty ( ) {
215+ return Ok ( ( ) ) ;
216+ }
196217 let mut bytes = jvm. instantiate_array ( "B" , chars. len ( ) ) . await ?;
197218 jvm. store_array ( & mut bytes, 0 , chars. into_iter ( ) . map ( |value| value as i8 ) ) . await ?;
198219
199- let out = jvm. get_field ( & this, "java/io/DataOutputStream" , "out" , "Ljava/io/OutputStream;" ) . await ?;
220+ let out: ClassInstanceRef < OutputStream > = jvm. get_field ( & this, "java/io/DataOutputStream" , "out" , "Ljava/io/OutputStream;" ) . await ?;
221+ if out. is_null ( ) {
222+ return Err ( jvm. exception ( "java/lang/NullPointerException" , "underlying stream is null" ) . await ) ;
223+ }
200224 jvm. invoke_virtual ( & out, "java/io/OutputStream" , "write" , "([B)V" , ( bytes, ) ) . await
201225 }
202226
203227 async fn write_chars ( jvm : & Jvm , _: & mut RuntimeContext , this : ClassInstanceRef < Self > , s : ClassInstanceRef < String > ) -> Result < ( ) > {
204228 tracing:: debug!( "java.io.DataOutputStream::writeChars({this:?}, {s:?})" ) ;
229+ if s. is_null ( ) {
230+ return Err ( jvm. exception ( "java/lang/NullPointerException" , "argument is null" ) . await ) ;
231+ }
205232
206233 let chars: ClassInstanceRef < Array < JavaChar > > = jvm. invoke_virtual ( & s, "java/lang/String" , "toCharArray" , "()[C" , ( ) ) . await ?;
207234 let length = jvm. array_length ( & chars) . await ?;
208235 let chars: Vec < JavaChar > = jvm. load_array ( & chars, 0 , length) . await ?;
236+ if chars. is_empty ( ) {
237+ return Ok ( ( ) ) ;
238+ }
209239 let mut data = Vec :: with_capacity ( chars. len ( ) * 2 ) ;
210240 for value in chars {
211241 data. push ( ( value >> 8 ) as i8 ) ;
@@ -215,12 +245,18 @@ impl DataOutputStream {
215245 let mut bytes = jvm. instantiate_array ( "B" , data. len ( ) ) . await ?;
216246 jvm. store_array ( & mut bytes, 0 , data) . await ?;
217247
218- let out = jvm. get_field ( & this, "java/io/DataOutputStream" , "out" , "Ljava/io/OutputStream;" ) . await ?;
248+ let out: ClassInstanceRef < OutputStream > = jvm. get_field ( & this, "java/io/DataOutputStream" , "out" , "Ljava/io/OutputStream;" ) . await ?;
249+ if out. is_null ( ) {
250+ return Err ( jvm. exception ( "java/lang/NullPointerException" , "underlying stream is null" ) . await ) ;
251+ }
219252 jvm. invoke_virtual ( & out, "java/io/OutputStream" , "write" , "([B)V" , ( bytes, ) ) . await
220253 }
221254
222255 async fn write_utf ( jvm : & Jvm , _: & mut RuntimeContext , this : ClassInstanceRef < Self > , s : ClassInstanceRef < String > ) -> Result < ( ) > {
223256 tracing:: debug!( "java.io.DataOutputStream::writeUTF({this:?}, {s:?})" ) ;
257+ if s. is_null ( ) {
258+ return Err ( jvm. exception ( "java/lang/NullPointerException" , "argument is null" ) . await ) ;
259+ }
224260
225261 let chars: ClassInstanceRef < Array < JavaChar > > = jvm. invoke_virtual ( & s, "java/lang/String" , "toCharArray" , "()[C" , ( ) ) . await ?;
226262 let length = jvm. array_length ( & chars) . await ?;
@@ -238,14 +274,20 @@ impl DataOutputStream {
238274 let mut bytes = jvm. instantiate_array ( "B" , data. len ( ) ) . await ?;
239275 jvm. store_array ( & mut bytes, 0 , data) . await ?;
240276
241- let out = jvm. get_field ( & this, "java/io/DataOutputStream" , "out" , "Ljava/io/OutputStream;" ) . await ?;
277+ let out: ClassInstanceRef < OutputStream > = jvm. get_field ( & this, "java/io/DataOutputStream" , "out" , "Ljava/io/OutputStream;" ) . await ?;
278+ if out. is_null ( ) {
279+ return Err ( jvm. exception ( "java/lang/NullPointerException" , "underlying stream is null" ) . await ) ;
280+ }
242281 jvm. invoke_virtual ( & out, "java/io/OutputStream" , "write" , "([B)V" , ( bytes, ) ) . await
243282 }
244283
245284 async fn close ( jvm : & Jvm , _: & mut RuntimeContext , this : ClassInstanceRef < Self > ) -> Result < ( ) > {
246285 tracing:: debug!( "java.io.DataOutputStream::close({this:?})" ) ;
247286
248- let out = jvm. get_field ( & this, "java/io/DataOutputStream" , "out" , "Ljava/io/OutputStream;" ) . await ?;
287+ let out: ClassInstanceRef < OutputStream > = jvm. get_field ( & this, "java/io/DataOutputStream" , "out" , "Ljava/io/OutputStream;" ) . await ?;
288+ if out. is_null ( ) {
289+ return Err ( jvm. exception ( "java/lang/NullPointerException" , "underlying stream is null" ) . await ) ;
290+ }
249291 let _: ( ) = jvm. invoke_virtual ( & out, "java/io/OutputStream" , "close" , "()V" , [ ] ) . await ?;
250292
251293 Ok ( ( ) )
@@ -254,7 +296,10 @@ impl DataOutputStream {
254296 async fn flush ( jvm : & Jvm , _: & mut RuntimeContext , this : ClassInstanceRef < Self > ) -> Result < ( ) > {
255297 tracing:: debug!( "java.io.DataOutputStream::flush({this:?})" ) ;
256298
257- let out = jvm. get_field ( & this, "java/io/DataOutputStream" , "out" , "Ljava/io/OutputStream;" ) . await ?;
299+ let out: ClassInstanceRef < OutputStream > = jvm. get_field ( & this, "java/io/DataOutputStream" , "out" , "Ljava/io/OutputStream;" ) . await ?;
300+ if out. is_null ( ) {
301+ return Err ( jvm. exception ( "java/lang/NullPointerException" , "underlying stream is null" ) . await ) ;
302+ }
258303 let _: ( ) = jvm. invoke_virtual ( & out, "java/io/OutputStream" , "flush" , "()V" , [ ] ) . await ?;
259304
260305 Ok ( ( ) )
0 commit comments