Use try-with-resources in AbstractTransporter#1947
Conversation
Refactor stream handling in utilGet and utilPut while preserving the existing close and flush contracts. Add tests for the close=false and close=true paths. Closes apache#1521 Generated-by: Codex
gnodet
left a comment
There was a problem hiding this comment.
Review Summary
Nice cleanup, @Aayush10016! The try-with-resources conversion in AbstractTransporter is correct and actually improves exception handling over the original code.
What's good
In the original code, is.close() (in utilGet) and os.close() (in utilPut) were called in plain finally blocks, meaning their exceptions could replace the primary exception from the try block. The try-with-resources approach properly adds close exceptions as suppressed exceptions instead — this is the correct Java behavior.
The tests are well-focused and verify the close/flush contract for both close=true and close=false branches.
Minor observations (non-blocking)
-
Code duplication: The
transportStarted+copylogic is duplicated in both branches of theif/elsefor each method. An alternative is to extract the common body into a private helper method, though the current form with 2-line duplication is clear and defensible. -
Exception-path tests: The tests cover the happy path well, but don't test the exception path — e.g., verifying streams are still properly closed when
copy()throws. Since improved exception handling is a key benefit of this refactor, an exception-path test would strengthen confidence. This is a nice-to-have, not a blocker. -
Pre-existing: In the
utilPutclose=falsebranch,os.flush()remains in a plainfinallyblock, meaning ifcopy()throws and thenflush()also throws, the flush exception replaces the original. This is unchanged by this PR and could be addressed in a follow-up.
Overall this is a solid improvement. The contributor checklist items noted in the maintainer comment should still be addressed.
This review was generated by an AI agent and may contain inaccuracies. Please verify all suggestions before applying.
Claude Code on behalf of Guillaume Nodet
Summary
AbstractTransporterstream handling to use try-with-resources for the close-on-completion paths.close=falsebehavior by leaving download input streams open and flushing upload output streams.utilGetandutilPutclose/flush contracts.Closes #1521.
Verification
mvn -pl maven-resolver-spi -am testwith Temurin JDK 21.0.11