Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

close IOContexts #427

Merged
merged 2 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -504,17 +504,20 @@ public final void flush() throws IOException {
@Override
public void close() throws IOException
{
super.close();
if (!isClosed()) {
super.close();

// Let's mark row as closed, if we had any...
finishRow();

// Write the header if necessary, occurs when no rows written
if (_handleFirstLine) {
_handleFirstLine();
// Let's mark row as closed, if we had any...
finishRow();

// Write the header if necessary, occurs when no rows written
if (_handleFirstLine) {
_handleFirstLine();
}
_writer.close(_ioContext.isResourceManaged() || isEnabled(JsonGenerator.Feature.AUTO_CLOSE_TARGET),
isEnabled(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM));
_ioContext.close();
}
_writer.close(_ioContext.isResourceManaged() || isEnabled(JsonGenerator.Feature.AUTO_CLOSE_TARGET),
isEnabled(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM));
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,11 @@ private Feature(boolean defaultState) {
*/
protected final StreamReadConstraints _streamReadConstraints;

/**
* @since 2.16
*/
protected final IOContext _ioContext;

protected int _formatFeatures;

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

@Override
public void close() throws IOException { _reader.close(); }
public void close() throws IOException {
if (!isClosed()) {
_reader.close();
_ioContext.close();
}
}

/*
/**********************************************************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,13 @@ public JsonGenerator overrideFormatFeatures(int values, int mask) { }
/**********************************************************
*/

// public void close() throws IOException
@Override
public void close() throws IOException {
if (!isClosed()) {
super.close();
_ioContext.close();
}
}

// public void flush() throws IOException

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public class JavaPropsParser extends ParserMinimalBase
*/
protected final StreamReadConstraints _streamReadConstraints;

/**
* @since 2.16
*/
protected final IOContext _ioContext;

/**
* Although most massaging is done later, caller may be interested in the
* ultimate source.
Expand Down Expand Up @@ -101,6 +106,7 @@ public JavaPropsParser(IOContext ctxt, int parserFeatures, Object inputSource,
ObjectCodec codec, Map<?,?> sourceMap)
{
super(parserFeatures);
_ioContext = ctxt;
_streamReadConstraints = ctxt.streamReadConstraints();
_objectCodec = codec;
_inputSource = inputSource;
Expand Down Expand Up @@ -151,8 +157,11 @@ public int releaseBuffered(Writer w) throws IOException {

@Override
public void close() throws IOException {
_closed = true;
_readContext = null;
if (!_closed) {
_ioContext.close();
_closed = true;
_readContext = null;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,12 @@ public JsonParser _createParser(InputStream in, IOContext ctxt) throws IOExcepti

@Override
public JsonParser _createParser(Reader r, IOContext ctxt) throws IOException {
ObjectNode node = parse(ctxt, r);
return new TreeTraversingParser(node); // don't pass our _objectCodec, this part shouldn't be customized
try {
ObjectNode node = parse(ctxt, r);
return new TreeTraversingParser(node); // don't pass our _objectCodec, this part shouldn't be customized
} finally {
ctxt.close();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,20 +120,23 @@ public Version version() {

@Override
public void close() throws IOException {
super.close();
_flushBuffer();
_outputTail = 0; // just to ensure we don't think there's anything buffered

if (_out != null) {
if (_ioContext.isResourceManaged() || isEnabled(StreamWriteFeature.AUTO_CLOSE_TARGET)) {
_out.close();
} else if (isEnabled(StreamWriteFeature.FLUSH_PASSED_TO_STREAM)) {
// If we can't close it, we should at least flush
_out.flush();
if (!isClosed()) {
super.close();
_flushBuffer();
_outputTail = 0; // just to ensure we don't think there's anything buffered

if (_out != null) {
if (_ioContext.isResourceManaged() || isEnabled(StreamWriteFeature.AUTO_CLOSE_TARGET)) {
_out.close();
} else if (isEnabled(StreamWriteFeature.FLUSH_PASSED_TO_STREAM)) {
// If we can't close it, we should at least flush
_out.flush();
}
}
_ioContext.close();
// Internal buffer(s) generator has can now be released as well
_releaseBuffers();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be moved before IOContext.close() (not a huge deal, but it will likely return buffers into recycler, and that should happen before releasing recycler into pool).
I can move after merge.

}
// Internal buffer(s) generator has can now be released as well
_releaseBuffers();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,7 @@ public void close() throws IOException
_writer.flush();
}
}
_ioContext.close();
}
}

Expand Down