diff --git a/pom.xml b/pom.xml index e96e88b6..a9765d9c 100644 --- a/pom.xml +++ b/pom.xml @@ -69,8 +69,9 @@ maven-compiler-plugin 3.1 - 1.5 - 1.5 + 1.8 + 1.8 + true @@ -80,7 +81,7 @@ org.codehaus.mojo.signature - java15 + java18 1.0 @@ -168,6 +169,21 @@ maven-deploy-plugin 2.8.1 + + + org.apache.maven.plugins + maven-jdeps-plugin + 3.0.0 + + + + jdkinternals + test-jdkinternals + + + + + @@ -186,7 +202,7 @@ com.lowagie itext - 2.0.8 + 2.1.7 bouncycastle diff --git a/src/main/java/org/jrimum/bopepo/pdf/Files.java b/src/main/java/org/jrimum/bopepo/pdf/Files.java index 2f6b105b..121bd71e 100644 --- a/src/main/java/org/jrimum/bopepo/pdf/Files.java +++ b/src/main/java/org/jrimum/bopepo/pdf/Files.java @@ -142,11 +142,12 @@ public static File bytesToFile(File file, byte[] bytes) + Integer.MAX_VALUE); } - OutputStream out = new FileOutputStream(file); - - out.write(bytes); - out.flush(); - out.close(); + try (OutputStream out = new FileOutputStream(file)) { + out.write(bytes); + out.flush(); + } catch (IOException ex) { + throw new IOException(ex); + } return file; } @@ -187,30 +188,32 @@ public static ByteArrayOutputStream bytesToStream(byte[] bytes) * @throws IllegalArgumentException * Caso o {@code file} seja {@code null}. */ - public static byte[] fileToBytes(File file) throws IOException { - - Objects.checkNotNull(file); + public static byte[] fileToBytes(File file) throws IOException { - InputStream is = new FileInputStream(file); + Objects.checkNotNull(file); - byte[] bytes = new byte[(int) file.length()]; + byte[] bytes; + int offset; + try (InputStream is = new FileInputStream(file)) { + bytes = new byte[(int) file.length()]; + offset = 0; + int numRead = 0; + while ((offset < bytes.length) + && ((numRead = is.read(bytes, offset, bytes.length - offset)) >= 0)) { + offset += numRead; + } + } catch (IOException e) { - int offset = 0; - int numRead = 0; - - while ((offset < bytes.length) - && ((numRead = is.read(bytes, offset, bytes.length - offset)) >= 0)) { - offset += numRead; - } + throw new IllegalStateException(e); - is.close(); + } - Objects.checkArgument(offset == bytes.length, - "Não foi possível completar a leitura do arquivo: " - + file.getName()); + Objects.checkArgument(offset == bytes.length, + "Não foi possível completar a leitura do arquivo: " + + file.getName()); - return bytes; - } + return bytes; + } /** * Retorna o conteúdo do {@code InputStream} em um array de bytes. @@ -225,25 +228,29 @@ public static byte[] fileToBytes(File file) throws IOException { * @throws IllegalArgumentException * Caso o {@code input} seja {@code null}. */ - public static byte[] toByteArray(InputStream input) throws IOException { + public static byte[] toByteArray(InputStream input) throws IOException { - Objects.checkNotNull(input); + Objects.checkNotNull(input); - ByteArrayOutputStream output = new ByteArrayOutputStream(); + + try (ByteArrayOutputStream output = new ByteArrayOutputStream()) { - byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; + byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; - int n = 0; + int n = 0; - while (-1 != (n = input.read(buffer))) { + while (-1 != (n = input.read(buffer))) { - output.write(buffer, 0, n); - } - - input.close(); + output.write(buffer, 0, n); + } + input.close(); + return output.toByteArray(); - return output.toByteArray(); - } + } catch (IOException ex) { + throw new IOException(ex); + } + + } public static File zip(File f){ @@ -264,40 +271,28 @@ public static File zip(String zipedName, File f){ public static byte[] zip(byte[] fileToZip, String fileZipedName){ - ByteArrayOutputStream obout = new ByteArrayOutputStream(); - - ZipOutputStream out = null; + byte[] bytes; - try { + try (ByteArrayOutputStream obout = new ByteArrayOutputStream(); + ZipOutputStream out= new ZipOutputStream(obout);) { - out = new ZipOutputStream(obout); out.setMethod(ZipOutputStream.DEFLATED); out.putNextEntry(new ZipEntry(fileZipedName)); out.write(fileToZip); + + bytes = obout.toByteArray(); } catch (IOException e) { throw new IllegalStateException(e); - }finally{ - - if(out != null){ - - try { - - // Close the input stream and return bytes - out.close(); - - } catch (Exception e) { - - return Exceptions.throwIllegalStateException(e); - } - } + } + - return obout.toByteArray(); + return bytes; } public static File zip(Collection files){ @@ -341,69 +336,64 @@ public static byte[] zipBytes(Map files) { // Create a buffer for reading the files byte[] buf = new byte[DEFAULT_BUFFER_SIZE]; - ByteArrayOutputStream outs = new ByteArrayOutputStream(); + byte[] bytes; + - try { - // Create the ZIP file - ZipOutputStream out = new ZipOutputStream(outs); + try ( // Create the ZIP file + ByteArrayOutputStream outs = new ByteArrayOutputStream(); + ZipOutputStream out = new ZipOutputStream(outs)) { // Compress the files for (Entry entry : files.entrySet()) { if (entry.getValue() != null) { - ByteArrayInputStream in = new ByteArrayInputStream(entry - .getValue()); - - // Add ZIP entry to output stream. - out.putNextEntry(new ZipEntry(normalizeName(entry.getKey()))); - - // Transfer bytes from the file to the ZIP file - int len; - - while ((len = in.read(buf)) > 0) { - out.write(buf, 0, len); - } - - // Complete the entry - out.closeEntry(); - in.close(); + // Add ZIP entry to output stream. + try (ByteArrayInputStream in = new ByteArrayInputStream(entry + .getValue())) { + // Add ZIP entry to output stream. + out.putNextEntry(new ZipEntry(normalizeName(entry.getKey()))); + + // Transfer bytes from the file to the ZIP file + int len; + + while ((len = in.read(buf)) > 0) { + out.write(buf, 0, len); + } + + // Complete the entry + out.closeEntry(); + } } } - - // Complete the ZIP file - out.close(); - - return outs.toByteArray(); - + // Complete the ZIP file + + bytes = outs.toByteArray(); } catch (IOException e) { throw new IllegalStateException(e); } + return bytes; } public static byte[] toByteArray(File file){ try{ - InputStream is = new FileInputStream(file); - - long length = file.length(); - - if (length > Integer.MAX_VALUE) { - Exceptions.throwIllegalArgumentException(String.format("File is too large! Max file length capacity is %s bytes.",length)); - } - - byte[] bytes = new byte[(int)length]; - - int offset = 0; - int numRead = 0; - while ((offset < bytes.length) - && ((numRead=is.read(bytes, offset, bytes.length-offset)) >= 0)) { - offset += numRead; - } - - is.close(); + byte[] bytes; + int offset; + try (InputStream is = new FileInputStream(file)) { + long length = file.length(); + if (length > Integer.MAX_VALUE) { + Exceptions.throwIllegalArgumentException(String.format("File is too large! Max file length capacity is %s bytes.",length)); + } bytes = new byte[(int)length]; + offset = 0; + int numRead = 0; + while ((offset < bytes.length) + && ((numRead=is.read(bytes, offset, bytes.length-offset)) >= 0)) { + offset += numRead; + } + } if (offset < bytes.length) { throw new IOException("Could not completely read file "+file.getName()); diff --git a/src/main/java/org/jrimum/bopepo/pdf/PDFs.java b/src/main/java/org/jrimum/bopepo/pdf/PDFs.java index fb14d171..4c580ca8 100644 --- a/src/main/java/org/jrimum/bopepo/pdf/PDFs.java +++ b/src/main/java/org/jrimum/bopepo/pdf/PDFs.java @@ -141,9 +141,10 @@ public static byte[] mergeFiles(Collection pdfFiles) { */ public static byte[] mergeFiles(Collection pdfFiles, PdfDocInfo info) { - try{ + byte[] bytes; + try(ByteArrayOutputStream byteOS = new ByteArrayOutputStream()){ + - ByteArrayOutputStream byteOS = new ByteArrayOutputStream(); Document document = new Document(); @@ -176,9 +177,11 @@ public static byte[] mergeFiles(Collection pdfFiles, PdfDocInfo info) { copy.close(); document.close(); - byteOS.close(); + + + bytes = byteOS.toByteArray(); - return byteOS.toByteArray(); + return bytes; }catch (Exception e) { return Exceptions.throwIllegalStateException(e); diff --git a/src/main/java/org/jrimum/bopepo/view/ResourceBundle.java b/src/main/java/org/jrimum/bopepo/view/ResourceBundle.java index e4e25881..bde3fb37 100644 --- a/src/main/java/org/jrimum/bopepo/view/ResourceBundle.java +++ b/src/main/java/org/jrimum/bopepo/view/ResourceBundle.java @@ -156,13 +156,13 @@ private BufferedImage loadLogotipoDoBanco(String codigo){ private byte[] loadPdf(String fileName){ byte[] pdf = null; - InputStream is = null; - try { - - is = ClassLoaders.getResource( + + try (InputStream is = ClassLoaders.getResource( "/pdf/"+fileName, - this.getClass()).openStream(); + this.getClass()).openStream()){ + + pdf = Files.toByteArray(is); @@ -170,14 +170,6 @@ private byte[] loadPdf(String fileName){ Exceptions.throwIllegalStateException(e); - }finally{ - if(is != null){ - try { - is.close(); - } catch (Exception e) { - Exceptions.throwIllegalStateException(e); - } - } } return pdf; diff --git a/src/test/java/org/jrimum/bopepo/campolivre/TestCLBancoDeBrasilia.java b/src/test/java/org/jrimum/bopepo/campolivre/TestCLBancoDeBrasilia.java index 4f107646..e43676b1 100644 --- a/src/test/java/org/jrimum/bopepo/campolivre/TestCLBancoDeBrasilia.java +++ b/src/test/java/org/jrimum/bopepo/campolivre/TestCLBancoDeBrasilia.java @@ -70,8 +70,8 @@ public void setUp() { createCampoLivreToTest(); setCampoLivreEsperadoComoString("0000586002006100000107045"); - assertEquals(4, titulo.getParametrosBancarios().getValor(CHAVE_ASBACE_DIGITO1)); - assertEquals(5, titulo.getParametrosBancarios().getValor(CHAVE_ASBACE_DIGITO2)); + assertEquals((Object)4, titulo.getParametrosBancarios().getValor(CHAVE_ASBACE_DIGITO1)); + assertEquals((Object)5, titulo.getParametrosBancarios().getValor(CHAVE_ASBACE_DIGITO2)); } @Test @@ -84,8 +84,8 @@ public void seGeraCorretoParaCarteiraComRegistro(){ setCampoLivreEsperadoComoString("0000586002006200000107031"); seCampoLivreEscritoEstaCorreto(); - assertEquals(3, titulo.getParametrosBancarios().getValor(CHAVE_ASBACE_DIGITO1)); - assertEquals(1, titulo.getParametrosBancarios().getValor(CHAVE_ASBACE_DIGITO2)); + assertEquals((Object)3, titulo.getParametrosBancarios().getValor(CHAVE_ASBACE_DIGITO1)); + assertEquals((Object)1, titulo.getParametrosBancarios().getValor(CHAVE_ASBACE_DIGITO2)); } @Test(expected = CampoLivreException.class)