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

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

Ktor build with graalvm

I wanted to test ktor.io with graalvm build, main problem is all code I found using gradle and my project is using mvn here is the code. like other modules I needed to create reflect config json, otherwise ktor serialize is not working properly with graal and when I send a request to “/hello” endpoint I got empty response. I tried to write myself but did not work and at the end I used command below ...

November 13, 2022 · 1 min · Özkan Pakdil

Disable Spring Boot Hikari Datasource with aspectj

There is database which will shutdown in uncertain future and application wants to be restarted and still working after that. In spring boot every datasource bean depends to some other bean, if we use @ConditionalOnProperty which actually not creates the bean after that other beans fails to initialize and app stop booting. If need to disable a datasource without deleting all the spring beans or without using @ConditionalOnProperty When @ConditionalOnProperty comes to scene it is like deleting or changing all other spring beans which is not really required if you just want to disable a datasource. ...

August 4, 2022 · 1 min · Özkan Pakdil

Getting com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class

I am trying to write small microservice benchmark for java, lately java microservice framework are popping up from every corner 😊 actually it is becoming little bit annoying, java world becoming js world. Anyway while writing some test I start getting the error below Internal Server Error Error handling d1e23f6f-3947-497a-be41-27ba9f7f4791-1, org.jboss.resteasy.spi.UnhandledException: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class com.mascix.ApplicationInfo and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) The stacktrace below has been reversed to show the root cause first. Click Here to see the original stacktrace com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class com.mascix.ApplicationInfo and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77) at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1277) at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:400) This was coming from quarkus example. I got same error in spring boot and micronaut, my first approach was configure the bean or singleton and give ...

August 19, 2020 · 1 min · Özkan Pakdil