1212
1313package org .eclipse .yasson .internal ;
1414
15+ import java .io .IOException ;
1516import java .io .InputStream ;
1617import java .io .OutputStream ;
1718import java .io .Reader ;
1819import java .io .StringReader ;
1920import java .io .StringWriter ;
2021import java .io .Writer ;
2122import java .lang .reflect .Type ;
23+ import java .nio .CharBuffer ;
2224import java .nio .charset .Charset ;
2325import java .util .Map ;
2426import java .util .Set ;
@@ -73,15 +75,15 @@ public <T> T fromJson(String str, Type type) throws JsonbException {
7375
7476 @ Override
7577 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 ) )) {
7779 DeserializationContextImpl unmarshaller = new DeserializationContextImpl (jsonbContext );
7880 return deserialize (type , parser , unmarshaller );
7981 }
8082 }
8183
8284 @ Override
8385 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 ) )) {
8587 DeserializationContextImpl unmarshaller = new DeserializationContextImpl (jsonbContext );
8688 return deserialize (type , parser , unmarshaller );
8789 }
@@ -119,7 +121,7 @@ public <T> T fromJsonStructure(JsonStructure jsonStructure, Type runtimeType) th
119121
120122 private JsonParser inputStreamParser (InputStream stream ) {
121123 return jsonbContext .getJsonParserFactory ()
122- .createParser (stream ,
124+ .createParser (new CloseSuppressingInputStream ( stream ) ,
123125 Charset .forName ((String ) jsonbContext .getConfig ()
124126 .getProperty (JsonbConfig .ENCODING ).orElse ("UTF-8" )));
125127 }
@@ -145,15 +147,15 @@ public String toJson(Object object, Type type) throws JsonbException {
145147 @ Override
146148 public void toJson (Object object , Writer writer ) throws JsonbException {
147149 final SerializationContextImpl marshaller = new SerializationContextImpl (jsonbContext );
148- try (JsonGenerator generator = writerGenerator (writer )) {
150+ try (JsonGenerator generator = writerGenerator (new CloseSuppressingWriter ( writer ) )) {
149151 marshaller .marshallWithoutClose (object , generator );
150152 }
151153 }
152154
153155 @ Override
154156 public void toJson (Object object , Type type , Writer writer ) throws JsonbException {
155157 final SerializationContextImpl marshaller = new SerializationContextImpl (jsonbContext , type );
156- try (JsonGenerator generator = writerGenerator (writer )) {
158+ try (JsonGenerator generator = writerGenerator (new CloseSuppressingWriter ( writer ) )) {
157159 marshaller .marshallWithoutClose (object , generator );
158160 }
159161 }
@@ -226,12 +228,254 @@ private JsonGenerator streamGenerator(OutputStream stream) {
226228 Map <String , ?> factoryProperties = jsonbContext .createJsonpProperties (jsonbContext .getConfig ());
227229 final String encoding = (String ) jsonbContext .getConfig ().getProperty (JsonbConfig .ENCODING ).orElse ("UTF-8" );
228230 return jsonbContext .getJsonProvider ().createGeneratorFactory (factoryProperties )
229- .createGenerator (stream , Charset .forName (encoding ));
231+ .createGenerator (new CloseSuppressingOutputStream ( stream ) , Charset .forName (encoding ));
230232 }
231233
232234 @ Override
233235 public void close () throws Exception {
234236 jsonbContext .getComponentInstanceCreator ().close ();
235237 }
236238
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+
237481}
0 commit comments