Skip to content

Commit 5688945

Browse files
committed
Do not close passed-in Writer (#604)
1 parent 14e68b6 commit 5688945

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/main/java/org/eclipse/yasson/internal/JsonBinding.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
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.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0 which is available at
@@ -12,6 +12,9 @@
1212

1313
package org.eclipse.yasson.internal;
1414

15+
import java.io.FilterReader;
16+
import java.io.FilterWriter;
17+
import java.io.IOException;
1518
import java.io.InputStream;
1619
import java.io.OutputStream;
1720
import java.io.Reader;
@@ -145,15 +148,15 @@ public String toJson(Object object, Type type) throws JsonbException {
145148
@Override
146149
public void toJson(Object object, Writer writer) throws JsonbException {
147150
final SerializationContextImpl marshaller = new SerializationContextImpl(jsonbContext);
148-
try (JsonGenerator generator = writerGenerator(writer)) {
151+
try (JsonGenerator generator = writerGenerator(new CloseSuppressingWriter(writer))) {
149152
marshaller.marshallWithoutClose(object, generator);
150153
}
151154
}
152155

153156
@Override
154157
public void toJson(Object object, Type type, Writer writer) throws JsonbException {
155158
final SerializationContextImpl marshaller = new SerializationContextImpl(jsonbContext, type);
156-
try (JsonGenerator generator = writerGenerator(writer)) {
159+
try (JsonGenerator generator = writerGenerator(new CloseSuppressingWriter(writer))) {
157160
marshaller.marshallWithoutClose(object, generator);
158161
}
159162
}
@@ -234,4 +237,17 @@ public void close() throws Exception {
234237
jsonbContext.getComponentInstanceCreator().close();
235238
}
236239

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+
237253
}

0 commit comments

Comments
 (0)