@@ -1199,24 +1199,30 @@ public void close() throws IOException
1199
1199
{
1200
1200
super .close ();
1201
1201
1202
- /* 05-Dec-2008, tatu: To add [JACKSON-27], need to close open
1203
- * scopes.
1204
- */
1202
+ // 05-Dec-2008, tatu: To add [JACKSON-27], need to close open scopes.
1205
1203
// First: let's see that we still have buffers...
1206
- if ((_outputBuffer != null )
1207
- && isEnabled (Feature .AUTO_CLOSE_JSON_CONTENT )) {
1208
- while (true ) {
1209
- JsonStreamContext ctxt = getOutputContext ();
1210
- if (ctxt .inArray ()) {
1211
- writeEndArray ();
1212
- } else if (ctxt .inObject ()) {
1213
- writeEndObject ();
1214
- } else {
1215
- break ;
1204
+ IOException flushFail = null ;
1205
+ try {
1206
+ if ((_outputBuffer != null )
1207
+ && isEnabled (Feature .AUTO_CLOSE_JSON_CONTENT )) {
1208
+ while (true ) {
1209
+ JsonStreamContext ctxt = getOutputContext ();
1210
+ if (ctxt .inArray ()) {
1211
+ writeEndArray ();
1212
+ } else if (ctxt .inObject ()) {
1213
+ writeEndObject ();
1214
+ } else {
1215
+ break ;
1216
+ }
1216
1217
}
1217
1218
}
1219
+ _flushBuffer ();
1220
+ } catch (IOException e ) {
1221
+ // 10-Jun-2022, tatu: [core#764] Need to avoid failing here; may
1222
+ // still need to close the underlying output stream
1223
+ flushFail = e ;
1218
1224
}
1219
- _flushBuffer ();
1225
+
1220
1226
_outputTail = 0 ; // just to ensure we don't think there's anything buffered
1221
1227
1222
1228
/* 25-Nov-2008, tatus: As per [JACKSON-16] we are not to call close()
@@ -1226,15 +1232,26 @@ && isEnabled(Feature.AUTO_CLOSE_JSON_CONTENT)) {
1226
1232
* may not be properly recycled if we don't close the writer.
1227
1233
*/
1228
1234
if (_outputStream != null ) {
1229
- if (_ioContext .isResourceManaged () || isEnabled (Feature .AUTO_CLOSE_TARGET )) {
1230
- _outputStream .close ();
1231
- } else if (isEnabled (Feature .FLUSH_PASSED_TO_STREAM )) {
1232
- // If we can't close it, we should at least flush
1233
- _outputStream .flush ();
1235
+ try {
1236
+ if (_ioContext .isResourceManaged () || isEnabled (Feature .AUTO_CLOSE_TARGET )) {
1237
+ _outputStream .close ();
1238
+ } else if (isEnabled (Feature .FLUSH_PASSED_TO_STREAM )) {
1239
+ // If we can't close it, we should at least flush
1240
+ _outputStream .flush ();
1241
+ }
1242
+ } catch (IOException | RuntimeException e ) {
1243
+ if (flushFail != null ) {
1244
+ e .addSuppressed (flushFail );
1245
+ }
1246
+ throw e ;
1234
1247
}
1235
1248
}
1236
1249
// Internal buffer(s) generator has can now be released as well
1237
1250
_releaseBuffers ();
1251
+
1252
+ if (flushFail != null ) {
1253
+ throw flushFail ;
1254
+ }
1238
1255
}
1239
1256
1240
1257
@ Override
0 commit comments