Skip to content

Commit bbfdf92

Browse files
authored
close IOContexts (#427)
1 parent ba8901f commit bbfdf92

File tree

7 files changed

+64
-27
lines changed

7 files changed

+64
-27
lines changed

csv/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvGenerator.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -504,17 +504,20 @@ public final void flush() throws IOException {
504504
@Override
505505
public void close() throws IOException
506506
{
507-
super.close();
507+
if (!isClosed()) {
508+
super.close();
508509

509-
// Let's mark row as closed, if we had any...
510-
finishRow();
511-
512-
// Write the header if necessary, occurs when no rows written
513-
if (_handleFirstLine) {
514-
_handleFirstLine();
510+
// Let's mark row as closed, if we had any...
511+
finishRow();
512+
513+
// Write the header if necessary, occurs when no rows written
514+
if (_handleFirstLine) {
515+
_handleFirstLine();
516+
}
517+
_writer.close(_ioContext.isResourceManaged() || isEnabled(JsonGenerator.Feature.AUTO_CLOSE_TARGET),
518+
isEnabled(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM));
519+
_ioContext.close();
515520
}
516-
_writer.close(_ioContext.isResourceManaged() || isEnabled(JsonGenerator.Feature.AUTO_CLOSE_TARGET),
517-
isEnabled(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM));
518521
}
519522

520523
/*

csv/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvParser.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,11 @@ private Feature(boolean defaultState) {
303303
*/
304304
protected final StreamReadConstraints _streamReadConstraints;
305305

306+
/**
307+
* @since 2.16
308+
*/
309+
protected final IOContext _ioContext;
310+
306311
protected int _formatFeatures;
307312

308313
/**
@@ -411,6 +416,7 @@ public CsvParser(IOContext ctxt, int stdFeatures, int csvFeatures,
411416
throw new IllegalArgumentException("Can not pass `null` as `java.io.Reader` to read from");
412417
}
413418
_objectCodec = codec;
419+
_ioContext = ctxt;
414420
_streamReadConstraints = ctxt.streamReadConstraints();
415421
_textBuffer = ctxt.constructReadConstrainedTextBuffer();
416422
DupDetector dups = JsonParser.Feature.STRICT_DUPLICATE_DETECTION.enabledIn(stdFeatures)
@@ -504,7 +510,12 @@ public int releaseBuffered(Writer out) throws IOException {
504510
public boolean isClosed() { return _reader.isClosed(); }
505511

506512
@Override
507-
public void close() throws IOException { _reader.close(); }
513+
public void close() throws IOException {
514+
if (!isClosed()) {
515+
_reader.close();
516+
_ioContext.close();
517+
}
518+
}
508519

509520
/*
510521
/**********************************************************

properties/src/main/java/com/fasterxml/jackson/dataformat/javaprop/JavaPropsGenerator.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,13 @@ public JsonGenerator overrideFormatFeatures(int values, int mask) { }
222222
/**********************************************************
223223
*/
224224

225-
// public void close() throws IOException
225+
@Override
226+
public void close() throws IOException {
227+
if (!isClosed()) {
228+
super.close();
229+
_ioContext.close();
230+
}
231+
}
226232

227233
// public void flush() throws IOException
228234

properties/src/main/java/com/fasterxml/jackson/dataformat/javaprop/JavaPropsParser.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ public class JavaPropsParser extends ParserMinimalBase
4646
*/
4747
protected final StreamReadConstraints _streamReadConstraints;
4848

49+
/**
50+
* @since 2.16
51+
*/
52+
protected final IOContext _ioContext;
53+
4954
/**
5055
* Although most massaging is done later, caller may be interested in the
5156
* ultimate source.
@@ -101,6 +106,7 @@ public JavaPropsParser(IOContext ctxt, int parserFeatures, Object inputSource,
101106
ObjectCodec codec, Map<?,?> sourceMap)
102107
{
103108
super(parserFeatures);
109+
_ioContext = ctxt;
104110
_streamReadConstraints = ctxt.streamReadConstraints();
105111
_objectCodec = codec;
106112
_inputSource = inputSource;
@@ -151,8 +157,11 @@ public int releaseBuffered(Writer w) throws IOException {
151157

152158
@Override
153159
public void close() throws IOException {
154-
_closed = true;
155-
_readContext = null;
160+
if (!_closed) {
161+
_ioContext.close();
162+
_closed = true;
163+
_readContext = null;
164+
}
156165
}
157166

158167
@Override

toml/src/main/java/com/fasterxml/jackson/dataformat/toml/TomlFactory.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,12 @@ public JsonParser _createParser(InputStream in, IOContext ctxt) throws IOExcepti
270270

271271
@Override
272272
public JsonParser _createParser(Reader r, IOContext ctxt) throws IOException {
273-
ObjectNode node = parse(ctxt, r);
274-
return new TreeTraversingParser(node); // don't pass our _objectCodec, this part shouldn't be customized
273+
try {
274+
ObjectNode node = parse(ctxt, r);
275+
return new TreeTraversingParser(node); // don't pass our _objectCodec, this part shouldn't be customized
276+
} finally {
277+
ctxt.close();
278+
}
275279
}
276280

277281
@Override

toml/src/main/java/com/fasterxml/jackson/dataformat/toml/TomlGenerator.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,20 +120,23 @@ public Version version() {
120120

121121
@Override
122122
public void close() throws IOException {
123-
super.close();
124-
_flushBuffer();
125-
_outputTail = 0; // just to ensure we don't think there's anything buffered
126-
127-
if (_out != null) {
128-
if (_ioContext.isResourceManaged() || isEnabled(StreamWriteFeature.AUTO_CLOSE_TARGET)) {
129-
_out.close();
130-
} else if (isEnabled(StreamWriteFeature.FLUSH_PASSED_TO_STREAM)) {
131-
// If we can't close it, we should at least flush
132-
_out.flush();
123+
if (!isClosed()) {
124+
super.close();
125+
_flushBuffer();
126+
_outputTail = 0; // just to ensure we don't think there's anything buffered
127+
128+
if (_out != null) {
129+
if (_ioContext.isResourceManaged() || isEnabled(StreamWriteFeature.AUTO_CLOSE_TARGET)) {
130+
_out.close();
131+
} else if (isEnabled(StreamWriteFeature.FLUSH_PASSED_TO_STREAM)) {
132+
// If we can't close it, we should at least flush
133+
_out.flush();
134+
}
133135
}
136+
_ioContext.close();
137+
// Internal buffer(s) generator has can now be released as well
138+
_releaseBuffers();
134139
}
135-
// Internal buffer(s) generator has can now be released as well
136-
_releaseBuffers();
137140
}
138141

139142
@Override

yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLGenerator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,7 @@ public void close() throws IOException
583583
_writer.flush();
584584
}
585585
}
586+
_ioContext.close();
586587
}
587588
}
588589

0 commit comments

Comments
 (0)