12
12
13
13
package org .eclipse .yasson .internal ;
14
14
15
+ import java .io .IOException ;
15
16
import java .io .InputStream ;
16
17
import java .io .OutputStream ;
17
18
import java .io .Reader ;
18
19
import java .io .StringReader ;
19
20
import java .io .StringWriter ;
20
21
import java .io .Writer ;
21
22
import java .lang .reflect .Type ;
23
+ import java .nio .CharBuffer ;
22
24
import java .nio .charset .Charset ;
23
25
import java .util .Map ;
24
26
import java .util .Set ;
@@ -73,15 +75,15 @@ public <T> T fromJson(String str, Type type) throws JsonbException {
73
75
74
76
@ Override
75
77
public <T > T fromJson (Reader reader , Class <T > type ) throws JsonbException {
76
- try (JsonParser parser = jsonbContext .getJsonProvider ().createParser (reader )) {
78
+ try (JsonParser parser = jsonbContext .getJsonProvider ().createParser (new CloseSuppressingReader ( reader ) )) {
77
79
DeserializationContextImpl unmarshaller = new DeserializationContextImpl (jsonbContext );
78
80
return deserialize (type , parser , unmarshaller );
79
81
}
80
82
}
81
83
82
84
@ Override
83
85
public <T > T fromJson (Reader reader , Type type ) throws JsonbException {
84
- try (JsonParser parser = jsonbContext .getJsonProvider ().createParser (reader )) {
86
+ try (JsonParser parser = jsonbContext .getJsonProvider ().createParser (new CloseSuppressingReader ( reader ) )) {
85
87
DeserializationContextImpl unmarshaller = new DeserializationContextImpl (jsonbContext );
86
88
return deserialize (type , parser , unmarshaller );
87
89
}
@@ -119,7 +121,7 @@ public <T> T fromJsonStructure(JsonStructure jsonStructure, Type runtimeType) th
119
121
120
122
private JsonParser inputStreamParser (InputStream stream ) {
121
123
return jsonbContext .getJsonParserFactory ()
122
- .createParser (stream ,
124
+ .createParser (new CloseSuppressingInputStream ( stream ) ,
123
125
Charset .forName ((String ) jsonbContext .getConfig ()
124
126
.getProperty (JsonbConfig .ENCODING ).orElse ("UTF-8" )));
125
127
}
@@ -145,15 +147,15 @@ public String toJson(Object object, Type type) throws JsonbException {
145
147
@ Override
146
148
public void toJson (Object object , Writer writer ) throws JsonbException {
147
149
final SerializationContextImpl marshaller = new SerializationContextImpl (jsonbContext );
148
- try (JsonGenerator generator = writerGenerator (writer )) {
150
+ try (JsonGenerator generator = writerGenerator (new CloseSuppressingWriter ( writer ) )) {
149
151
marshaller .marshallWithoutClose (object , generator );
150
152
}
151
153
}
152
154
153
155
@ Override
154
156
public void toJson (Object object , Type type , Writer writer ) throws JsonbException {
155
157
final SerializationContextImpl marshaller = new SerializationContextImpl (jsonbContext , type );
156
- try (JsonGenerator generator = writerGenerator (writer )) {
158
+ try (JsonGenerator generator = writerGenerator (new CloseSuppressingWriter ( writer ) )) {
157
159
marshaller .marshallWithoutClose (object , generator );
158
160
}
159
161
}
@@ -169,15 +171,15 @@ private JsonGenerator writerGenerator(Writer writer) {
169
171
@ Override
170
172
public void toJson (Object object , OutputStream stream ) throws JsonbException {
171
173
final SerializationContextImpl marshaller = new SerializationContextImpl (jsonbContext );
172
- try (JsonGenerator generator = streamGenerator (stream )) {
174
+ try (JsonGenerator generator = streamGenerator (new CloseSuppressingOutputStream ( stream ) )) {
173
175
marshaller .marshall (object , generator );
174
176
}
175
177
}
176
178
177
179
@ Override
178
180
public void toJson (Object object , Type type , OutputStream stream ) throws JsonbException {
179
181
final SerializationContextImpl marshaller = new SerializationContextImpl (jsonbContext , type );
180
- try (JsonGenerator generator = streamGenerator (stream )) {
182
+ try (JsonGenerator generator = streamGenerator (new CloseSuppressingOutputStream ( stream ) )) {
181
183
marshaller .marshall (object , generator );
182
184
}
183
185
}
@@ -234,4 +236,246 @@ public void close() throws Exception {
234
236
jsonbContext .getComponentInstanceCreator ().close ();
235
237
}
236
238
239
+ /**
240
+ * {@link OutputStream} that suppresses {@link OutputStream#close()}.
241
+ */
242
+ static final class CloseSuppressingOutputStream extends OutputStream {
243
+
244
+ private final OutputStream delegate ;
245
+
246
+ CloseSuppressingOutputStream (OutputStream delegate ) {
247
+ this .delegate = delegate ;
248
+ }
249
+
250
+ @ Override
251
+ public void close () {
252
+ // suppress
253
+ }
254
+
255
+ @ Override
256
+ public void write (int b ) throws IOException {
257
+ delegate .write (b );
258
+ }
259
+
260
+ @ Override
261
+ public void write (byte [] b ) throws IOException {
262
+ delegate .write (b );
263
+ }
264
+
265
+ @ Override
266
+ public void write (byte [] b , int off , int len ) throws IOException {
267
+ delegate .write (b , off , len );
268
+ }
269
+
270
+ }
271
+
272
+ /**
273
+ * {@link InputStream} that suppresses {@link InputStream#close()}.
274
+ */
275
+ static final class CloseSuppressingInputStream extends InputStream {
276
+
277
+ private final InputStream delegate ;
278
+
279
+ CloseSuppressingInputStream (InputStream delegate ) {
280
+ this .delegate = delegate ;
281
+ }
282
+
283
+ @ Override
284
+ public void close () {
285
+ // suppress
286
+ }
287
+
288
+ @ Override
289
+ public int read () throws IOException {
290
+ return delegate .read ();
291
+ }
292
+
293
+ @ Override
294
+ public int read (byte [] b ) throws IOException {
295
+ return delegate .read (b );
296
+ }
297
+
298
+ @ Override
299
+ public int read (byte [] b , int off , int len ) throws IOException {
300
+ return delegate .read (b , off , len );
301
+ }
302
+
303
+ @ Override
304
+ public byte [] readAllBytes () throws IOException {
305
+ return delegate .readAllBytes ();
306
+ }
307
+
308
+ @ Override
309
+ public byte [] readNBytes (int len ) throws IOException {
310
+ return delegate .readNBytes (len );
311
+ }
312
+
313
+ @ Override
314
+ public int readNBytes (byte [] b , int off , int len ) throws IOException {
315
+ return delegate .readNBytes (b , off , len );
316
+ }
317
+
318
+ @ Override
319
+ public long skip (long n ) throws IOException {
320
+ return delegate .skip (n );
321
+ }
322
+
323
+ @ Override
324
+ public int available () throws IOException {
325
+ return delegate .available ();
326
+ }
327
+
328
+ @ Override
329
+ public void mark (int readlimit ) {
330
+ delegate .mark (readlimit );
331
+ }
332
+
333
+ @ Override
334
+ public void reset () throws IOException {
335
+ delegate .reset ();
336
+ }
337
+
338
+ @ Override
339
+ public boolean markSupported () {
340
+ return delegate .markSupported ();
341
+ }
342
+
343
+ @ Override
344
+ public long transferTo (OutputStream out ) throws IOException {
345
+ return delegate .transferTo (out );
346
+ }
347
+
348
+ }
349
+
350
+ /**
351
+ * {@link Reader} that suppresses {@link Reader#close()}.
352
+ */
353
+ static final class CloseSuppressingReader extends Reader {
354
+
355
+ private final Reader delegate ;
356
+
357
+ CloseSuppressingReader (Reader delegate ) {
358
+ this .delegate = delegate ;
359
+ }
360
+
361
+ @ Override
362
+ public int read (CharBuffer target ) throws IOException {
363
+ return delegate .read (target );
364
+ }
365
+
366
+ @ Override
367
+ public int read () throws IOException {
368
+ return delegate .read ();
369
+ }
370
+
371
+ @ Override
372
+ public int read (char [] cbuf ) throws IOException {
373
+ return delegate .read (cbuf );
374
+ }
375
+
376
+ @ Override
377
+ public int read (char [] cbuf , int off , int len ) throws IOException {
378
+ return delegate .read (cbuf , off , len );
379
+ }
380
+
381
+ @ Override
382
+ public long skip (long n ) throws IOException {
383
+ return delegate .skip (n );
384
+ }
385
+
386
+ @ Override
387
+ public boolean ready () throws IOException {
388
+ return delegate .ready ();
389
+ }
390
+
391
+ @ Override
392
+ public boolean markSupported () {
393
+ return delegate .markSupported ();
394
+ }
395
+
396
+ @ Override
397
+ public void mark (int readAheadLimit ) throws IOException {
398
+ delegate .mark (readAheadLimit );
399
+ }
400
+
401
+ @ Override
402
+ public void reset () throws IOException {
403
+ delegate .reset ();
404
+ }
405
+
406
+ @ Override
407
+ public void close () {
408
+ // suppress
409
+ }
410
+
411
+ @ Override
412
+ public long transferTo (Writer out ) throws IOException {
413
+ return delegate .transferTo (out );
414
+ }
415
+
416
+ }
417
+
418
+ /**
419
+ * {@link Writer} that suppresses {@link Writer#close()}.
420
+ */
421
+ static final class CloseSuppressingWriter extends Writer {
422
+
423
+ private final Writer delegate ;
424
+
425
+ CloseSuppressingWriter (Writer delegate ) {
426
+ this .delegate = delegate ;
427
+ }
428
+
429
+ @ Override
430
+ public void write (int c ) throws IOException {
431
+ delegate .write (c );
432
+ }
433
+
434
+ @ Override
435
+ public void write (char [] cbuf ) throws IOException {
436
+ delegate .write (cbuf );
437
+ }
438
+
439
+ @ Override
440
+ public void write (char [] cbuf , int off , int len ) throws IOException {
441
+ delegate .write (cbuf , off , len );
442
+ }
443
+
444
+ @ Override
445
+ public void write (String str ) throws IOException {
446
+ delegate .write (str );
447
+ }
448
+
449
+ @ Override
450
+ public void write (String str , int off , int len ) throws IOException {
451
+ delegate .write (str , off , len );
452
+ }
453
+
454
+ @ Override
455
+ public Writer append (CharSequence csq ) throws IOException {
456
+ return delegate .append (csq );
457
+ }
458
+
459
+ @ Override
460
+ public Writer append (CharSequence csq , int start , int end ) throws IOException {
461
+ return delegate .append (csq , start , end );
462
+ }
463
+
464
+ @ Override
465
+ public Writer append (char c ) throws IOException {
466
+ return delegate .append (c );
467
+ }
468
+
469
+ @ Override
470
+ public void flush () throws IOException {
471
+ delegate .flush ();
472
+ }
473
+
474
+ @ Override
475
+ public void close () {
476
+ // suppress
477
+ }
478
+
479
+ }
480
+
237
481
}
0 commit comments