Let CRAMContainerStreamWriter callers keep ownership of their stream - #1789
Conversation
…1092) CRAMContainerStreamWriter takes an OutputStream created by the caller but unconditionally closed it in finish(), so code that manages the stream's lifecycle itself had no way to finish writing without also having its stream closed underneath it -- and a subsequent close() by the owner would then fail. Add finish(writeEOFContainer, closeStream) so the caller can opt out of the close. The existing finish(writeEOFContainer) overload delegates with closeStream=true, so behaviour is unchanged for all current callers, including CRAMFileWriter, which relies on it to close the file stream it owns. The stream is flushed either way, so opting out never loses data.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthrough
ChangesCRAM stream closure
Estimated code review effort: 2 (Simple) | ~10 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In
`@src/test/java/htsjdk/samtools/cram/CRAMContainerStreamWriterClosureTest.java`:
- Around line 57-68: Update CloseRecordingStream and
finish_does_not_close_stream_when_close_stream_is_false so the test records
whether flush() was invoked and asserts that flag after writer.finish(true,
false). Remove the size-based assertion, since ByteArrayOutputStream visibility
does not verify flushing, while preserving the assertion that the
caller-provided stream remains open.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: d442414a-82ea-4241-9f64-55b0fd2d1584
📒 Files selected for processing (2)
src/main/java/htsjdk/samtools/CRAMContainerStreamWriter.javasrc/test/java/htsjdk/samtools/cram/CRAMContainerStreamWriterClosureTest.java
Per review on #1789: ByteArrayOutputStream makes writes visible immediately and inherits a no-op flush(), so asserting size() > 0 proved only that bytes had been written -- it would have passed just as happily if finish(..., false) had stopped flushing altogether. Track flush() on the recording stream and assert that instead, so the test actually pins the behaviour that matters: declining to close the stream must not decline to flush it. Verified by removing the outputStream.flush() call from finish(): the new assertion fails, where the old one passed.
|
Good catch, thanks — fixed in the follow-up commit. You were right that the assertion proved nothing about flushing:
I confirmed the new assertion actually bites by temporarily removing the The old |
Three test classes added in #1789, #1791 and #1792 used snake_case method names without the conventional test prefix, which is neither idiomatic Java nor what this repo does: of 2417 test methods, 2020 start with "test" and only 199 contain an underscore at all. Rename the nine offending methods in these three classes to testXxx camelCase. Test bodies are untouched; this is naming only.
Three test classes added in #1789, #1791 and #1792 used snake_case method names without the conventional test prefix, which is neither idiomatic Java nor what this repo does: of 2417 test methods, 2020 start with "test" and only 199 contain an underscore at all. Rename the nine offending methods in these three classes to testXxx camelCase. Test bodies are untouched; this is naming only.
Closes #1092.
CRAMContainerStreamWritertakes anOutputStreamcreated by the caller but unconditionally closed it infinish(), so code that manages the stream lifecycle itself had no way to finish writing without having its stream closed underneath it — and a subsequentclose()by the owner would then fail.Adds
finish(writeEOFContainer, closeStream)so callers can opt out of the close. The existingfinish(writeEOFContainer)overload delegates withcloseStream=true, so behaviour is unchanged for every current caller, includingCRAMFileWriter, which relies on it to close the file stream it owns.The stream is flushed either way, so opting out never loses data.
Tests cover both overloads, that the single-argument form still closes (backwards compatibility), and that a caller can keep using its own stream afterwards.
Summary by CodeRabbit
New Features
Bug Fixes