Skip to content

Commit 9e9f43f

Browse files
greek1979Verdent
authored andcommitted
Do not close passed-in Writer (#604)
1 parent 14e68b6 commit 9e9f43f

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

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

+17-3
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,7 @@
1212

1313
package org.eclipse.yasson.internal;
1414

15+
import java.io.FilterWriter;
1516
import java.io.InputStream;
1617
import java.io.OutputStream;
1718
import java.io.Reader;
@@ -145,15 +146,15 @@ public String toJson(Object object, Type type) throws JsonbException {
145146
@Override
146147
public void toJson(Object object, Writer writer) throws JsonbException {
147148
final SerializationContextImpl marshaller = new SerializationContextImpl(jsonbContext);
148-
try (JsonGenerator generator = writerGenerator(writer)) {
149+
try (JsonGenerator generator = writerGenerator(new CloseSuppressingWriter(writer))) {
149150
marshaller.marshallWithoutClose(object, generator);
150151
}
151152
}
152153

153154
@Override
154155
public void toJson(Object object, Type type, Writer writer) throws JsonbException {
155156
final SerializationContextImpl marshaller = new SerializationContextImpl(jsonbContext, type);
156-
try (JsonGenerator generator = writerGenerator(writer)) {
157+
try (JsonGenerator generator = writerGenerator(new CloseSuppressingWriter(writer))) {
157158
marshaller.marshallWithoutClose(object, generator);
158159
}
159160
}
@@ -234,4 +235,17 @@ public void close() throws Exception {
234235
jsonbContext.getComponentInstanceCreator().close();
235236
}
236237

238+
private static class CloseSuppressingWriter extends FilterWriter {
239+
240+
protected CloseSuppressingWriter(final Writer in) {
241+
super(in);
242+
}
243+
244+
@Override
245+
public void close() {
246+
// do not close
247+
}
248+
249+
}
250+
237251
}

0 commit comments

Comments
 (0)