How to use resources(file and memory) in try

I wrote a small piece of code to do some pdf encryption with openpdf, and intellij`s sonarlint was complaining about “Resources should be closed” more details here non compliant public class PasswordProtectedPDF { private static final Logger logger = Logger.getLogger(PasswordProtectedPDF.class.getName()); static final String USER_PASSWORD = "111"; static final String OWNER_PASSWORD = "111"; public static void main(String[] args) { try { File f = new File("1_protected.pdf"); FileOutputStream out = new FileOutputStream(f); File pdfFile = new File("1.pdf"); PdfReader reader = new PdfReader(pdfFile.getPath()); PdfStamper stamper = new PdfStamper(reader, out); HashMap<String, String> info = new HashMap<>(); info.put("Producer", ""); reader.getInfo().forEach((key, value) -> { logger.info("Key: " + key + ", Value: " + value); }); stamper.setInfoDictionary(info); stamper.setEncryption(USER_PASSWORD.getBytes(), OWNER_PASSWORD.getBytes(), PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128); stamper.close(); logger.info("Password protected PDF created successfully."); } catch (IOException e) { logger.severe("Error creating password protected PDF: " + e.getMessage()); } } } Compliant public class PasswordProtectedPDF { private static final Logger logger = Logger.getLogger(PasswordProtectedPDF.class.getName()); static final String USER_PASSWORD = "111"; static final String OWNER_PASSWORD = "111"; public static void main(String[] args) { try ( FileOutputStream out = new FileOutputStream(new File("1_protected.pdf")); PdfReader reader = new PdfReader(new File("1.pdf").getPath()) ) { PdfStamper stamper = new PdfStamper(reader, out); HashMap<String, String> info = new HashMap<>(); info.put("Producer", ""); reader.getInfo().forEach((key, value) -> { logger.info("Key: " + key + ", Value: " + value); }); stamper.setInfoDictionary(info); stamper.setEncryption(USER_PASSWORD.getBytes(), OWNER_PASSWORD.getBytes(), PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128); stamper.close(); logger.info("Password protected PDF created successfully."); } catch (IOException e) { logger.severe("Error creating password protected PDF: " + e.getMessage()); } } } just a reminder we can define multiple resources in try block ...

April 18, 2024 · 2 min · Özkan Pakdil

How to deploy to maven central repo

Yesterday I found dockFX library, when I first try it did not work. Today I manage to upgrade the code to java17 and working with latest javafx21, then I wonder how to deploy this to maven central repository. Took around 3 hours, better to write this down for others. Go login to https://central.sonatype.com/account I used my gmail login and I was in. Go to https://central.sonatype.com/publishing/namespaces to create namespace, you need to create a verification repository on github if you want to use io.github.yourusername as explained here ...

March 10, 2024 · 6 min · Özkan Pakdil

How to assign shortcut to a button in fxml

I wanted to assign a button in fxml a keyboard shortcut. below is the example how to do that. <Button mnemonicParsing="true" text="_Click Me"> Now that button will have underscore on that “C” and user can call it automatically with key combination of ALT+C, The UI form/window that button should have the focus.

January 2, 2024 · 1 min · Özkan Pakdil

Character isEmoji and some others added to java 21

In java 21 we get new API for emojis like below isEmoji(int codePoint) isEmojiComponent(int codePoint) isEmojiModifier(int codePoint) isEmojiModifierBase(int codePoint) isEmojiPresentation(int codePoint) isExtendedPictographic(int codePoint) here is a working example public static void main(String[] args) { StringBuilder sb = new StringBuilder(); sb.appendCodePoint(0x1F600); // Grinning face sb.appendCodePoint(0x1F601); // Grinning face with big eyes sb.appendCodePoint(0x1F602); // Grinning face with tears sb.appendCodePoint(0x1F923); // Rolling on the floor laughing sb.appendCodePoint(0x1F970); // Smiling face with hearts sb.appendCodePoint(0x1F60D); // Smiling face with heart-eyes sb.appendCodePoint(0x1F929); // Star-struck sb.appendCodePoint(0x1F618); // Face blowing a kiss sb.appendCodePoint(0x1F617); // Kissing face sb.appendCodePoint(0x263A); // Smiling face System.out.println(sb); var codePoint = Character.codePointAt("😃", 0); var isEmoji = Character.isEmoji(codePoint); System.out.println("😃 is an emoji: " + isEmoji); int[] surrogates = { 0xD83D, 0xDC7D }; String alienEmojiString = new String(surrogates, 0, surrogates.length); System.out.println(alienEmojiString); } seeing these emojis in vscode ...

November 20, 2023 · 1 min · Özkan Pakdil

Using sqlite with h2 console

Sometimes we(developers/programmers) may need to see database tables in tree and run some select, insert quickly on the db, H2 console is the best tool in spring boot to reach database on dev environment. And I saw a question about “Using Flyway with SQLite database in spring boot”, I have been using liquibase around 5 years but I have not tried flyway before, learning opportunity. So I preapred an example with gradle spring boot, sqlite and h2 console and answered the question. ...

October 10, 2023 · 1 min · Özkan Pakdil