Skip to content

Commit 8093f43

Browse files
authored
support IOContext closing in GeneratorBase (#1094)
1 parent 40cd6b7 commit 8093f43

File tree

3 files changed

+39
-27
lines changed

3 files changed

+39
-27
lines changed

src/main/java/com/fasterxml/jackson/core/base/GeneratorBase.java

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.fasterxml.jackson.core.base;
22

33
import com.fasterxml.jackson.core.*;
4+
import com.fasterxml.jackson.core.io.IOContext;
45
import com.fasterxml.jackson.core.json.DupDetector;
56
import com.fasterxml.jackson.core.json.JsonWriteContext;
67
import com.fasterxml.jackson.core.json.PackageVersion;
@@ -69,6 +70,9 @@ public abstract class GeneratorBase extends JsonGenerator
6970
*/
7071
protected int _features;
7172

73+
// since 2.16
74+
protected final IOContext _ioContext;
75+
7276
/**
7377
* Flag set to indicate that implicit conversion from number
7478
* to JSON String is needed (as per
@@ -101,24 +105,38 @@ public abstract class GeneratorBase extends JsonGenerator
101105
/**********************************************************
102106
*/
103107

104-
@SuppressWarnings("deprecation")
108+
@Deprecated // since 2.16
105109
protected GeneratorBase(int features, ObjectCodec codec) {
110+
this(features, codec, (IOContext) null);
111+
}
112+
113+
// @since 2.16
114+
@SuppressWarnings("deprecation")
115+
protected GeneratorBase(int features, ObjectCodec codec, IOContext ioContext) {
106116
super();
107117
_features = features;
108118
_objectCodec = codec;
119+
_ioContext = ioContext;
109120
DupDetector dups = Feature.STRICT_DUPLICATE_DETECTION.enabledIn(features)
110121
? DupDetector.rootDetector(this) : null;
111122
_writeContext = JsonWriteContext.createRootContext(dups);
112123
_cfgNumbersAsStrings = Feature.WRITE_NUMBERS_AS_STRINGS.enabledIn(features);
113124
}
114125

115126
// @since 2.5
116-
@SuppressWarnings("deprecation")
127+
@Deprecated // since 2.16
117128
protected GeneratorBase(int features, ObjectCodec codec, JsonWriteContext ctxt) {
129+
this(features, codec, null, ctxt);
130+
}
131+
132+
// @since 2.16
133+
@SuppressWarnings("deprecation")
134+
protected GeneratorBase(int features, ObjectCodec codec, IOContext ioContext, JsonWriteContext jsonWriteContext) {
118135
super();
119136
_features = features;
120137
_objectCodec = codec;
121-
_writeContext = ctxt;
138+
_ioContext = ioContext;
139+
_writeContext = jsonWriteContext;
122140
_cfgNumbersAsStrings = Feature.WRITE_NUMBERS_AS_STRINGS.enabledIn(features);
123141
}
124142

@@ -413,7 +431,14 @@ public void writeTree(TreeNode rootNode) throws IOException {
413431
*/
414432

415433
@Override public abstract void flush() throws IOException;
416-
@Override public void close() throws IOException { _closed = true; }
434+
@Override public void close() throws IOException {
435+
if (!_closed) {
436+
if (_ioContext != null) {
437+
_ioContext.close();
438+
}
439+
_closed = true;
440+
}
441+
}
417442
@Override public boolean isClosed() { return _closed; }
418443

419444
/*

src/main/java/com/fasterxml/jackson/core/json/JsonGeneratorImpl.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ public abstract class JsonGeneratorImpl extends GeneratorBase
4545
/**********************************************************
4646
*/
4747

48-
protected final IOContext _ioContext;
49-
5048
/**
5149
* @since 2.16
5250
*/
@@ -120,19 +118,10 @@ public abstract class JsonGeneratorImpl extends GeneratorBase
120118
/**********************************************************
121119
*/
122120

123-
@Override
124-
public void close() throws IOException {
125-
if (!isClosed()) {
126-
super.close();
127-
_ioContext.close();
128-
}
129-
}
130-
131121
@SuppressWarnings("deprecation")
132122
public JsonGeneratorImpl(IOContext ctxt, int features, ObjectCodec codec)
133123
{
134-
super(features, codec);
135-
_ioContext = ctxt;
124+
super(features, codec, ctxt);
136125
_streamWriteConstraints = ctxt.streamWriteConstraints();
137126
if (Feature.ESCAPE_NON_ASCII.enabledIn(features)) {
138127
// inlined `setHighestNonEscapedChar()`

src/test/java/com/fasterxml/jackson/core/io/BufferRecyclerPoolTest.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,28 @@
1212

1313
public class BufferRecyclerPoolTest extends BaseTest
1414
{
15-
public void testNoOp() {
15+
public void testNoOp() throws Exception {
1616
// no-op pool doesn't actually pool anything, so avoid checking it
1717
checkBufferRecyclerPoolImpl(BufferRecyclerPool.NonRecyclingPool.shared(), false);
1818
}
1919

20-
public void testThreadLocal() {
20+
public void testThreadLocal() throws Exception {
2121
checkBufferRecyclerPoolImpl(BufferRecyclerPool.ThreadLocalPool.shared(), true);
2222
}
2323

24-
public void testLockFree() {
24+
public void testLockFree() throws Exception {
2525
checkBufferRecyclerPoolImpl(BufferRecyclerPool.LockFreePool.nonShared(), true);
2626
}
2727

28-
public void testConcurrentDequeue() {
28+
public void testConcurrentDequeue() throws Exception {
2929
checkBufferRecyclerPoolImpl(BufferRecyclerPool.ConcurrentDequePool.nonShared(), true);
3030
}
3131

32-
public void testBounded() {
32+
public void testBounded() throws Exception {
3333
checkBufferRecyclerPoolImpl(BufferRecyclerPool.BoundedPool.nonShared(1), true);
3434
}
3535

36-
private void checkBufferRecyclerPoolImpl(BufferRecyclerPool pool, boolean checkPooledResource) {
36+
private void checkBufferRecyclerPoolImpl(BufferRecyclerPool pool, boolean checkPooledResource) throws Exception {
3737
JsonFactory jsonFactory = JsonFactory.builder()
3838
.bufferRecyclerPool(pool)
3939
.build();
@@ -50,23 +50,21 @@ private void checkBufferRecyclerPoolImpl(BufferRecyclerPool pool, boolean checkP
5050
}
5151
}
5252

53-
protected final BufferRecycler write(Object value, JsonFactory jsonFactory, int expectedSize) {
53+
private BufferRecycler write(Object value, JsonFactory jsonFactory, int expectedSize) throws Exception {
5454
BufferRecycler bufferRecycler;
5555
NopOutputStream out = new NopOutputStream();
5656
try (JsonGenerator gen = jsonFactory.createGenerator(out)) {
5757
bufferRecycler = ((JsonGeneratorImpl) gen).ioContext()._bufferRecycler;
5858
gen.writeObject(value);
59-
} catch (IOException e) {
60-
throw new RuntimeException(e);
6159
}
6260
assertEquals(expectedSize, out.size);
6361
return bufferRecycler;
6462
}
6563

66-
public class NopOutputStream extends OutputStream {
64+
private static class NopOutputStream extends OutputStream {
6765
protected int size = 0;
6866

69-
public NopOutputStream() { }
67+
NopOutputStream() { }
7068

7169
@Override
7270
public void write(int b) throws IOException { ++size; }

0 commit comments

Comments
 (0)