Skip to content

Commit 617145a

Browse files
authored
close IOContext (#389)
1 parent d041c71 commit 617145a

File tree

9 files changed

+124
-123
lines changed

9 files changed

+124
-123
lines changed

avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroGenerator.java

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,6 @@ private Feature(boolean defaultState) {
8888
/**********************************************************
8989
*/
9090

91-
protected final IOContext _ioContext;
92-
9391
/**
9492
* @since 2.16
9593
*/
@@ -143,8 +141,7 @@ public AvroGenerator(IOContext ctxt, int jsonFeatures, int avroFeatures,
143141
ObjectCodec codec, OutputStream output)
144142
throws IOException
145143
{
146-
super(jsonFeatures, codec);
147-
_ioContext = ctxt;
144+
super(jsonFeatures, codec, ctxt);
148145
_streamWriteConstraints = ctxt.streamWriteConstraints();
149146
_formatFeatures = avroFeatures;
150147
_output = output;
@@ -334,46 +331,48 @@ public final void flush() throws IOException {
334331
@Override
335332
public void close() throws IOException
336333
{
337-
super.close();
338-
if (isEnabled(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT)) {
339-
AvroWriteContext ctxt;
340-
while ((ctxt = _avroContext) != null) {
341-
if (ctxt.inArray()) {
342-
writeEndArray();
343-
} else if (ctxt.inObject()) {
344-
writeEndObject();
345-
} else {
346-
break;
334+
if (!isClosed()) {
335+
if (isEnabled(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT)) {
336+
AvroWriteContext ctxt;
337+
while ((ctxt = _avroContext) != null) {
338+
if (ctxt.inArray()) {
339+
writeEndArray();
340+
} else if (ctxt.inObject()) {
341+
writeEndObject();
342+
} else {
343+
break;
344+
}
347345
}
348346
}
349-
}
350-
// May need to finalize...
351-
/* 18-Nov-2014, tatu: Since this method is (a) often called as a result of an exception,
352-
* and (b) quite likely to cause an exception of its own, need to work around
353-
* combination of problems; one part being to catch non-IOExceptions; something that
354-
* is usually NOT done. Partly this is because Avro codec is leaking low-level exceptions
355-
* such as NPE.
356-
*/
357-
if (!_complete) {
358-
try {
359-
_complete();
360-
} catch (IOException e) {
361-
throw e;
362-
} catch (Exception e) {
363-
throw new JsonGenerationException("Failed to close AvroGenerator: ("
364-
+e.getClass().getName()+"): "+e.getMessage(), e, this);
347+
// May need to finalize...
348+
/* 18-Nov-2014, tatu: Since this method is (a) often called as a result of an exception,
349+
* and (b) quite likely to cause an exception of its own, need to work around
350+
* combination of problems; one part being to catch non-IOExceptions; something that
351+
* is usually NOT done. Partly this is because Avro codec is leaking low-level exceptions
352+
* such as NPE.
353+
*/
354+
if (!_complete) {
355+
try {
356+
_complete();
357+
} catch (IOException e) {
358+
throw e;
359+
} catch (Exception e) {
360+
throw new JsonGenerationException("Failed to close AvroGenerator: ("
361+
+e.getClass().getName()+"): "+e.getMessage(), e, this);
362+
}
365363
}
366-
}
367-
if (_output != null) {
368-
if (_ioContext.isResourceManaged() || isEnabled(JsonGenerator.Feature.AUTO_CLOSE_TARGET)) {
369-
_output.close();
370-
} else if (isEnabled(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM)) {
371-
// If we can't close it, we should at least flush
372-
_output.flush();
364+
if (_output != null) {
365+
if (_ioContext.isResourceManaged() || isEnabled(JsonGenerator.Feature.AUTO_CLOSE_TARGET)) {
366+
_output.close();
367+
} else if (isEnabled(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM)) {
368+
// If we can't close it, we should at least flush
369+
_output.flush();
370+
}
373371
}
372+
// Internal buffer(s) generator has can now be released as well
373+
_releaseBuffers();
374+
super.close();
374375
}
375-
// Internal buffer(s) generator has can now be released as well
376-
_releaseBuffers();
377376
}
378377

379378
/*

cbor/src/main/java/com/fasterxml/jackson/dataformat/cbor/CBORGenerator.java

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,34 +1350,35 @@ public final void flush() throws IOException {
13501350

13511351
@Override
13521352
public void close() throws IOException {
1353-
// First: let's see that we still have buffers...
1354-
if ((_outputBuffer != null)
1355-
&& isEnabled(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT)) {
1356-
while (true) {
1357-
JsonStreamContext ctxt = getOutputContext();
1358-
if (ctxt.inArray()) {
1359-
writeEndArray();
1360-
} else if (ctxt.inObject()) {
1361-
writeEndObject();
1362-
} else {
1363-
break;
1353+
if (!isClosed()) {
1354+
// First: let's see that we still have buffers...
1355+
if ((_outputBuffer != null)
1356+
&& isEnabled(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT)) {
1357+
while (true) {
1358+
JsonStreamContext ctxt = getOutputContext();
1359+
if (ctxt.inArray()) {
1360+
writeEndArray();
1361+
} else if (ctxt.inObject()) {
1362+
writeEndObject();
1363+
} else {
1364+
break;
1365+
}
13641366
}
13651367
}
1366-
}
1367-
// boolean wasClosed = _closed;
1368-
super.close();
1369-
_flushBuffer();
1368+
_flushBuffer();
13701369

1371-
if (_ioContext.isResourceManaged()
1372-
|| isEnabled(JsonGenerator.Feature.AUTO_CLOSE_TARGET)) {
1373-
_out.close();
1374-
} else if (isEnabled(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM)) {
1375-
// 14-Jan-2019, tatu: [dataformats-binary#155]: unless prevented via feature
1376-
// If we can't close it, we should at least flush
1377-
_out.flush();
1370+
if (_ioContext.isResourceManaged()
1371+
|| isEnabled(JsonGenerator.Feature.AUTO_CLOSE_TARGET)) {
1372+
_out.close();
1373+
} else if (isEnabled(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM)) {
1374+
// 14-Jan-2019, tatu: [dataformats-binary#155]: unless prevented via feature
1375+
// If we can't close it, we should at least flush
1376+
_out.flush();
1377+
}
1378+
// Internal buffer(s) generator has can now be released as well
1379+
_releaseBuffers();
1380+
super.close();
13781381
}
1379-
// Internal buffer(s) generator has can now be released as well
1380-
_releaseBuffers();
13811382
}
13821383

13831384
/*

cbor/src/main/java/com/fasterxml/jackson/dataformat/cbor/CBORParser.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,7 @@ public void close() throws IOException {
700700
// Also, internal buffer(s) can now be released as well
701701
_releaseBuffers();
702702
}
703+
_ioContext.close();
703704
}
704705
}
705706

ion/src/main/java/com/fasterxml/jackson/dataformat/ion/IonGenerator.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,6 @@ private Feature(boolean defaultState) {
109109
/* Indicates whether the IonGenerator is responsible for closing the underlying IonWriter. */
110110
protected final boolean _ionWriterIsManaged;
111111

112-
protected final IOContext _ioContext;
113-
114112
/**
115113
* @since 2.16
116114
*/
@@ -138,13 +136,12 @@ private Feature(boolean defaultState) {
138136
public IonGenerator(int jsonFeatures, final int ionFeatures, ObjectCodec codec,
139137
IonWriter ion, boolean ionWriterIsManaged, IOContext ctxt, Closeable dst)
140138
{
141-
super(jsonFeatures, codec);
139+
super(jsonFeatures, codec, ctxt);
142140
// Overwrite the writecontext with our own implementation
143141
_writeContext = IonWriteContext.createRootContext(_writeContext.getDupDetector());
144142
_formatFeatures = ionFeatures;
145143
_writer = ion;
146144
_ionWriterIsManaged = ionWriterIsManaged;
147-
_ioContext = ctxt;
148145
_streamWriteConstraints = ctxt.streamWriteConstraints();
149146
_destination = dst;
150147
}
@@ -168,8 +165,7 @@ public StreamWriteConstraints streamWriteConstraints() {
168165
@Override
169166
public void close() throws IOException
170167
{
171-
if (!_closed) {
172-
_closed = true;
168+
if (!isClosed()) {
173169
if (_ionWriterIsManaged) {
174170
_writer.close();
175171
}
@@ -182,6 +178,7 @@ public void close() throws IOException
182178
}
183179
}
184180
}
181+
super.close();
185182
}
186183
}
187184

ion/src/main/java/com/fasterxml/jackson/dataformat/ion/IonParser.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ public void close() throws IOException {
252252
((Closeable) src).close();
253253
}
254254
}
255+
_ioContext.close();
255256
_closed = true;
256257
}
257258
}

protobuf/src/main/java/com/fasterxml/jackson/dataformat/protobuf/ProtobufGenerator.java

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ public class ProtobufGenerator extends GeneratorBase
4949
/**********************************************************
5050
*/
5151

52-
protected final IOContext _ioContext;
53-
5452
/**
5553
* @since 2.16
5654
*/
@@ -142,8 +140,7 @@ public ProtobufGenerator(IOContext ctxt, int jsonFeatures,
142140
ObjectCodec codec, OutputStream output)
143141
throws IOException
144142
{
145-
super(jsonFeatures, codec, BOGUS_WRITE_CONTEXT);
146-
_ioContext = ctxt;
143+
super(jsonFeatures, codec, ctxt, BOGUS_WRITE_CONTEXT);
147144
_streamWriteConstraints = ctxt.streamWriteConstraints();
148145
_output = output;
149146
_pbContext = _rootContext = ProtobufWriteContext.createNullContext();
@@ -370,34 +367,36 @@ public final void flush() throws IOException
370367
@Override
371368
public void close() throws IOException
372369
{
373-
super.close();
374-
if (isEnabled(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT)) {
375-
ProtobufWriteContext ctxt;
376-
while ((ctxt = _pbContext) != null) {
377-
if (ctxt.inArray()) {
378-
writeEndArray();
379-
} else if (ctxt.inObject()) {
380-
writeEndObject();
381-
} else {
382-
break;
370+
if (!isClosed()) {
371+
if (isEnabled(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT)) {
372+
ProtobufWriteContext ctxt;
373+
while ((ctxt = _pbContext) != null) {
374+
if (ctxt.inArray()) {
375+
writeEndArray();
376+
} else if (ctxt.inObject()) {
377+
writeEndObject();
378+
} else {
379+
break;
380+
}
383381
}
384382
}
385-
}
386-
// May need to finalize...
387-
if (!_complete) {
388-
_complete();
389-
}
390-
if (_output != null) {
391-
if (_ioContext.isResourceManaged() || isEnabled(JsonGenerator.Feature.AUTO_CLOSE_TARGET)) {
392-
_output.close();
393-
} else if (isEnabled(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM)) {
394-
// 14-Jan-2019, tatu: [dataformats-binary#155]: unless prevented via feature
395-
// If we can't close it, we should at least flush
396-
_output.flush();
383+
// May need to finalize...
384+
if (!_complete) {
385+
_complete();
386+
}
387+
if (_output != null) {
388+
if (_ioContext.isResourceManaged() || isEnabled(JsonGenerator.Feature.AUTO_CLOSE_TARGET)) {
389+
_output.close();
390+
} else if (isEnabled(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM)) {
391+
// 14-Jan-2019, tatu: [dataformats-binary#155]: unless prevented via feature
392+
// If we can't close it, we should at least flush
393+
_output.flush();
394+
}
397395
}
396+
// Internal buffer(s) generator has can now be released as well
397+
_releaseBuffers();
398+
super.close();
398399
}
399-
// Internal buffer(s) generator has can now be released as well
400-
_releaseBuffers();
401400
}
402401

403402
/*

protobuf/src/main/java/com/fasterxml/jackson/dataformat/protobuf/ProtobufParser.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ public void close() throws IOException
444444
// Also, internal buffer(s) can now be released as well
445445
_releaseBuffers();
446446
}
447+
_ioContext.close();
447448
}
448449
}
449450

smile/src/main/java/com/fasterxml/jackson/dataformat/smile/SmileGenerator.java

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1883,37 +1883,38 @@ public final void flush() throws IOException
18831883
@Override
18841884
public void close() throws IOException
18851885
{
1886-
// First: let's see that we still have buffers...
1887-
if (_outputBuffer != null
1888-
&& isEnabled(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT)) {
1889-
while (true) {
1890-
JsonStreamContext ctxt = getOutputContext();
1891-
if (ctxt.inArray()) {
1892-
writeEndArray();
1893-
} else if (ctxt.inObject()) {
1894-
writeEndObject();
1895-
} else {
1896-
break;
1886+
if (!isClosed()) {
1887+
// First: let's see that we still have buffers...
1888+
if (_outputBuffer != null
1889+
&& isEnabled(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT)) {
1890+
while (true) {
1891+
JsonStreamContext ctxt = getOutputContext();
1892+
if (ctxt.inArray()) {
1893+
writeEndArray();
1894+
} else if (ctxt.inObject()) {
1895+
writeEndObject();
1896+
} else {
1897+
break;
1898+
}
18971899
}
18981900
}
1899-
}
1900-
boolean wasClosed = _closed;
1901-
super.close();
19021901

1903-
if (!wasClosed && isEnabled(Feature.WRITE_END_MARKER)) {
1904-
_writeByte(BYTE_MARKER_END_OF_CONTENT);
1905-
}
1906-
_flushBuffer();
1902+
if (isEnabled(Feature.WRITE_END_MARKER)) {
1903+
_writeByte(BYTE_MARKER_END_OF_CONTENT);
1904+
}
1905+
_flushBuffer();
19071906

1908-
if (_ioContext.isResourceManaged() || isEnabled(JsonGenerator.Feature.AUTO_CLOSE_TARGET)) {
1909-
_out.close();
1910-
} else if (isEnabled(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM)) {
1911-
// If we can't close it, we should at least flush
1912-
// 14-Jan-2019, tatu: [dataformats-binary#155]: unless prevented via feature
1913-
_out.flush();
1907+
if (_ioContext.isResourceManaged() || isEnabled(JsonGenerator.Feature.AUTO_CLOSE_TARGET)) {
1908+
_out.close();
1909+
} else if (isEnabled(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM)) {
1910+
// If we can't close it, we should at least flush
1911+
// 14-Jan-2019, tatu: [dataformats-binary#155]: unless prevented via feature
1912+
_out.flush();
1913+
}
1914+
// Internal buffer(s) generator has can now be released as well
1915+
_releaseBuffers();
1916+
super.close();
19141917
}
1915-
// Internal buffer(s) generator has can now be released as well
1916-
_releaseBuffers();
19171918
}
19181919

19191920
/*

smile/src/main/java/com/fasterxml/jackson/dataformat/smile/SmileParserBase.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ public final void close() throws IOException {
421421
// Also, internal buffer(s) can now be released as well
422422
_releaseBuffers();
423423
}
424+
_ioContext.close();
424425
}
425426
}
426427

0 commit comments

Comments
 (0)