Skip to content

Commit

Permalink
try-with-resources and dedicated exception
Browse files Browse the repository at this point in the history
  • Loading branch information
yazmin48 committed Jan 3, 2025
1 parent d3fd175 commit bda078a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ public static File getElectionRolesRevisionDocument(String filePath) {
}

public static byte[] getBytesFromFile(File file) throws IOException {
FileInputStream fis = new FileInputStream(file);
try {
try (FileInputStream fis = new FileInputStream(file)) {
long length = file.length();
if (length > Integer.MAX_VALUE) {
// File is too large
Expand All @@ -68,10 +67,7 @@ public static byte[] getBytesFromFile(File file) throws IOException {
if (offset < bytes.length) {
throw new IOException("Could not completely read file " + file.getName());
}

return bytes;
} finally {
fis.close();
}
}

Expand All @@ -80,22 +76,12 @@ public static byte[] getBytesFromFile(String ruta) throws IOException {
}

public static File convertBytesArrayToFile(byte[] bytes, String ruta) {
FileOutputStream fileOuputStream = null;
try {
fileOuputStream = new FileOutputStream(ruta);
fileOuputStream.write(bytes);
fileOuputStream.flush();
try (FileOutputStream fileOutputStream = new FileOutputStream(ruta)) {
fileOutputStream.write(bytes);
fileOutputStream.flush();
return new File(ruta);
} catch (IOException e) {
appLogger.error(e);
} finally {
if (fileOuputStream != null) {
try {
fileOuputStream.close();
} catch (IOException e) {
appLogger.error(e);
}
}
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;


/**
* Util class for email handling (sending, authentication, etc)
*
*/
public class MailHelper {


private static String smtpHost;
private static String user;
private static String pass;
Expand Down Expand Up @@ -58,7 +56,6 @@ public static void setPass(String pass) {
MailHelper.pass = pass;
}


public static Session initSession(Properties props) {
props.put("mail.smtp.host", getSmtpHost());
props.put("mail.smtp.auth", "true");
Expand All @@ -70,11 +67,11 @@ protected PasswordAuthentication getPasswordAuthentication() {

}

public static boolean sendMail(Properties props, String fromString, String to, String cc, String bcc, String subject, String body) throws Exception {
public static boolean sendMail(Properties props, String fromString, String to, String cc, String bcc, String subject, String body) throws MessagingException {
return sendMail(initSession(props), fromString, to, cc, bcc, subject, body);
}

public static boolean sendMail(Session session, String fromString, String to, String cc, String bcc, String subject, String body) throws Exception {
public static boolean sendMail(Session session, String fromString, String to, String cc, String bcc, String subject, String body) throws MessagingException {
try {
if (body.contains("$user.") || body.contains("$election.") || body.contains("$auditor."))
return false;
Expand Down

0 comments on commit bda078a

Please sign in to comment.