TRUNK-6593: try-with-resources for the other OpenmrsUtil methods#6102
TRUNK-6593: try-with-resources for the other OpenmrsUtil methods#6102liujs123456 wants to merge 1 commit into
Conversation
📝 WalkthroughWalkthroughThis PR replaces manual finally-block resource closes with try-with-resources in OpenmrsUtil: getFileAsString, saveDocument, storeProperties, and loadProperties; behavior and exception logging are preserved, and saveDocument adds an IOException catch for stream-close warnings. ChangesStream handling modernization
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 PMD (7.24.0)api/src/main/java/org/openmrs/util/OpenmrsUtil.java[ERROR] Cannot load ruleset rulesets/java/android.xml/DoNotHardCodeSDCard: Cannot resolve rule/ruleset reference 'rulesets/java/android.xml/DoNotHardCodeSDCard'. Make sure the resource is a valid file or URL and is on the CLASSPATH. Use --debug (or a fine log level) to see the current classpath. 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 |
171896a to
d9d7617
Compare
PR openmrs#5979 already refactored getFileAsBytes. There are 4 other methods in OpenmrsUtil with the same manual try/finally/close pattern, so refactored them to use try-with-resources too: - getFileAsString (also fixes a missing finally block) - saveDocument - storeProperties(Properties, File, String) - loadProperties
d9d7617 to
dfb7f67
Compare
|
There was a problem hiding this comment.
🧹 Nitpick comments (1)
api/src/main/java/org/openmrs/util/OpenmrsUtil.java (1)
228-228: ⚡ Quick winRemove unnecessary buffer reallocation inside the loop.
Great work adopting try-with-resources! 🎉 I noticed one small inefficiency: reallocating
bufon every iteration is unnecessary sinceString.valueOf(buf, 0, numRead)copies the characters into theStringBuilder. The buffer can be reused safely, which will reduce allocations and GC pressure.♻️ Suggested improvement
try (BufferedReader reader = new BufferedReader( new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8))) { char[] buf = new char[1024]; int numRead; while ((numRead = reader.read(buf)) != -1) { String readData = String.valueOf(buf, 0, numRead); fileData.append(readData); - buf = new char[1024]; } }🤖 Prompt for 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. In `@api/src/main/java/org/openmrs/util/OpenmrsUtil.java` at line 228, The loop currently reallocates the char[] named buf on every iteration (see usage with String.valueOf(buf, 0, numRead) and the StringBuilder accumulator), which is unnecessary; move the allocation of buf (char[] buf = new char[1024]) to before the loop so the same buffer is reused across iterations in OpenmrsUtil, and remove the in-loop reallocation to reduce allocations and GC pressure.
🤖 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.
Nitpick comments:
In `@api/src/main/java/org/openmrs/util/OpenmrsUtil.java`:
- Line 228: The loop currently reallocates the char[] named buf on every
iteration (see usage with String.valueOf(buf, 0, numRead) and the StringBuilder
accumulator), which is unnecessary; move the allocation of buf (char[] buf = new
char[1024]) to before the loop so the same buffer is reused across iterations in
OpenmrsUtil, and remove the in-loop reallocation to reduce allocations and GC
pressure.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 11974fe4-b7d1-43f7-8d96-ed88e7887248
📒 Files selected for processing (1)
api/src/main/java/org/openmrs/util/OpenmrsUtil.java



PR #5979 already refactored getFileAsBytes. There are 4 other methods in OpenmrsUtil that use the same manual try/finally/close pattern, so refactored them to use try-with-resources too:
getFileAsString also didn't have a finally block at all, so the reader would have been leaked if read() threw an exception.
JIRA: https://issues.openmrs.org/browse/TRUNK-6593
Summary by CodeRabbit