|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2016, 2024 Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2016, 2025 Oracle and/or its affiliates. All rights reserved. |
3 | 3 | *
|
4 | 4 | * This program and the accompanying materials are made available under the
|
5 | 5 | * terms of the Eclipse Public License v. 2.0 which is available at
|
|
12 | 12 |
|
13 | 13 | package org.eclipse.yasson.internal;
|
14 | 14 |
|
| 15 | +import java.io.FilterReader; |
| 16 | +import java.io.FilterWriter; |
| 17 | +import java.io.IOException; |
15 | 18 | import java.io.InputStream;
|
16 | 19 | import java.io.OutputStream;
|
17 | 20 | import java.io.Reader;
|
@@ -145,15 +148,15 @@ public String toJson(Object object, Type type) throws JsonbException {
|
145 | 148 | @Override
|
146 | 149 | public void toJson(Object object, Writer writer) throws JsonbException {
|
147 | 150 | final SerializationContextImpl marshaller = new SerializationContextImpl(jsonbContext);
|
148 |
| - try (JsonGenerator generator = writerGenerator(writer)) { |
| 151 | + try (JsonGenerator generator = writerGenerator(new CloseSuppressingWriter(writer))) { |
149 | 152 | marshaller.marshallWithoutClose(object, generator);
|
150 | 153 | }
|
151 | 154 | }
|
152 | 155 |
|
153 | 156 | @Override
|
154 | 157 | public void toJson(Object object, Type type, Writer writer) throws JsonbException {
|
155 | 158 | final SerializationContextImpl marshaller = new SerializationContextImpl(jsonbContext, type);
|
156 |
| - try (JsonGenerator generator = writerGenerator(writer)) { |
| 159 | + try (JsonGenerator generator = writerGenerator(new CloseSuppressingWriter(writer))) { |
157 | 160 | marshaller.marshallWithoutClose(object, generator);
|
158 | 161 | }
|
159 | 162 | }
|
@@ -234,4 +237,17 @@ public void close() throws Exception {
|
234 | 237 | jsonbContext.getComponentInstanceCreator().close();
|
235 | 238 | }
|
236 | 239 |
|
| 240 | + private static class CloseSuppressingWriter extends FilterWriter { |
| 241 | + |
| 242 | + protected CloseSuppressingWriter(final Writer in) { |
| 243 | + super(in); |
| 244 | + } |
| 245 | + |
| 246 | + @Override |
| 247 | + public void close() { |
| 248 | + // do not close |
| 249 | + } |
| 250 | + |
| 251 | + } |
| 252 | + |
237 | 253 | }
|
0 commit comments