Stirling-PDF/app
Balázs Szücs 3fdfb9632b
refactor(core): simplify resource management with try-with-resources (#4873)
# Description of Changes
TLDR:
- Replaced manual resource handling with try-with-resources in multiple
controllers
- Eliminated redundant `finally` blocks for improved readability
- Added missing resource closures to ensure proper cleanup
- Updated `ScalePagesController` with static utility improvements and
logging annotation

This pull request focuses on improving resource management and code
clarity across several PDF processing controllers. The main changes
involve refactoring to use try-with-resources for automatic resource
cleanup, removing manual close calls and finally blocks, and
restructuring helper methods for better organization. These updates
enhance code safety, maintainability, and performance.

**Resource Management Improvements:**
- Refactored multiple controllers (`EditTableOfContentsController`,
`PdfOverlayController`, `ScalePagesController`, `SplitPDFController`,
and `ScannerEffectController`) to use try-with-resources for managing
`PDDocument`, `ByteArrayOutputStream`, and other closable resources,
eliminating the need for manual close calls and finally blocks. This
reduces the risk of resource leaks and simplifies the code.

**Code Organization and Clarity:**
- Moved and consolidated helper methods (`getTargetSize`, `getSizeMap`)
in `ScalePagesController` for better encapsulation and static usage, and
added the `@Slf4j` annotation for logging
- Removed redundant imports and unused variables to clean up the
codebase.

**Performance Enhancements:**
- Optimized object instantiation by creating reusable objects (e.g.,
`LayerUtility` in `ScalePagesController`) outside of loops to improve
performance.


<!--
Please provide a summary of the changes, including:

- What was changed
- Why the change was made
- Any challenges encountered

Closes #(issue_number)
-->

---

## Checklist

### General

- [X] I have read the [Contribution
Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md)
- [X] I have read the [Stirling-PDF Developer
Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/DeveloperGuide.md)
(if applicable)
- [ ] I have read the [How to add new languages to
Stirling-PDF](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md)
(if applicable)
- [X] I have performed a self-review of my own code
- [X] My changes generate no new warnings

### Documentation

- [ ] I have updated relevant docs on [Stirling-PDF's doc
repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/)
(if functionality has heavily changed)
- [ ] I have read the section [Add New Translation
Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md#add-new-translation-tags)
(for new translation tags only)

### Translations (if applicable)

- [ ] I ran
[`scripts/counter_translation.py`](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/docs/counter_translation.md)

### UI Changes (if applicable)

- [ ] Screenshots or videos demonstrating the UI changes are attached
(e.g., as comments or direct attachments in the PR)

### Testing (if applicable)

- [X] I have tested my changes locally. Refer to the [Testing
Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/DeveloperGuide.md#6-testing)
for more details.

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>
2025-11-17 10:47:54 +00:00
..
common feat(conversion): add eBook to PDF via Calibre (EPUB/MOBI/AZW3/FB2/TXT/DOCX) (#4644) 2025-11-11 21:44:18 +00:00
core refactor(core): simplify resource management with try-with-resources (#4873) 2025-11-17 10:47:54 +00:00
proprietary refactor(common, core, proprietary): migrate boxed Booleans to primitive booleans and adopt is* accessors to reduce null checks/NPE risk (#4153) 2025-11-11 17:16:48 +00:00
allowed-licenses.json