Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pequenas alteracoes try with resources, java 8 ,versao itext #25

Open
wants to merge 5 commits into
base: 0.2-Helio
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
<source>1.8</source>
<target>1.8</target>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
Expand All @@ -80,7 +81,7 @@
<configuration>
<signature>
<groupId>org.codehaus.mojo.signature</groupId>
<artifactId>java15</artifactId>
<artifactId>java18</artifactId>
<version>1.0</version>
</signature>
</configuration>
Expand Down Expand Up @@ -168,6 +169,21 @@
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.1</version>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jdeps-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<goals>
<goal>jdkinternals</goal>
<goal>test-jdkinternals</goal>
</goals>
</execution>
</executions>
</plugin>

</plugins>

</build>
Expand All @@ -186,7 +202,7 @@
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.0.8</version>
<version>2.1.7</version>
<exclusions>
<exclusion>
<groupId>bouncycastle</groupId>
Expand Down
188 changes: 89 additions & 99 deletions src/main/java/org/jrimum/bopepo/pdf/Files.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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.
Expand All @@ -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){

Expand All @@ -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<File> files){
Expand Down Expand Up @@ -341,69 +336,64 @@ public static byte[] zipBytes(Map<String, byte[]> 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<String, byte[]> 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());
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/org/jrimum/bopepo/pdf/PDFs.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,10 @@ public static byte[] mergeFiles(Collection<byte[]> pdfFiles) {
*/
public static byte[] mergeFiles(Collection<byte[]> pdfFiles, PdfDocInfo info) {

try{
byte[] bytes;
try(ByteArrayOutputStream byteOS = new ByteArrayOutputStream()){


ByteArrayOutputStream byteOS = new ByteArrayOutputStream();

Document document = new Document();

Expand Down Expand Up @@ -176,9 +177,11 @@ public static byte[] mergeFiles(Collection<byte[]> pdfFiles, PdfDocInfo info) {

copy.close();
document.close();
byteOS.close();


bytes = byteOS.toByteArray();

return byteOS.toByteArray();
return bytes;

}catch (Exception e) {
return Exceptions.throwIllegalStateException(e);
Expand Down
18 changes: 5 additions & 13 deletions src/main/java/org/jrimum/bopepo/view/ResourceBundle.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,28 +156,20 @@ 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);

} catch (Exception e) {

Exceptions.throwIllegalStateException(e);

}finally{
if(is != null){
try {
is.close();
} catch (Exception e) {
Exceptions.throwIllegalStateException(e);
}
}
}

return pdf;
Expand Down
Loading