-1

I am trying to convert an InputStream into a byte array to write it in a file, to generate a PDF.

I have a File type with the url of a PDF, and with that, i have the inputStream of that.

File fichero_pdf = new File("C:/Users/agp2/Desktop/PDF_TRIAXE.pdf");
InputStream stream4 = new FileInputStream(fichero_pdf);

Until here everything is perfect, the problem appears when i try to transform this InputStream to a byte[] and write it in a new File. I have these two methods:

to convert the Stream to a byte[]:

private static byte[] getArrayFromInputStream(InputStream is) {

    BufferedReader br = null;
    StringBuilder sb = new StringBuilder();


    String line;
    try {

        br = new BufferedReader(new InputStreamReader(is));
        while ((line = br.readLine()) != null) {
            sb.append(line+"\n");
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return sb.toString().getBytes();

}

To write the byte[] in the new file:

...

File file=new File(dto.getTitulo());
  InputStream stream=dto.getContenido();      
               byte[] array=getStringFromInputStream(stream);
               OutputStream salida=new FileOutputStream(file);
               salida.write(array);
               salida.close();
               stream.close();
               helper.addAttachment(file.getName(), file);
           }
            mailSender.send(message);
...

The Email is sent at perfection, but when i can't open the .pdf. Also, i compare the code of the new pdf with the code of the first, and is a little bit different.

I need to create a valid pdf file from an inputStream.

Angel Gonzalez Pena
  • 145
  • 1
  • 1
  • 10

1 Answers1

1

You have 2 problems:

  1. You are trying to read bytes as strings, but you don't have to do it. In your case you should use byte streams(FileInputStream, BufferedInputStream), not char streams(InputStreamReader, BufferedReader).

  2. You loose data when you convert String to bytes here: return sb.toString().getBytes();

I would like to suggest you to use java 7 try-with-resources instead of try-catch-finally. You can read the whole file to a byte array using ByteArrayOutputStream.

Sample code does the following:

  1. getArrayFromInputStream() - reads all file bytes to byte array

  2. writeContent() - writes content to a new file, in my example pdf_sample2.pdf

Example:

public class ReadAllBytes {

// as example - write to resources folder
private static String DIR = "src\\main\\resources\\";

public static void main(String[] args) throws IOException {
    try {
        byte[] fileAsBytes = getArrayFromInputStream(new FileInputStream(new File(DIR + "pdf-sample.pdf")));
        writeContent(fileAsBytes, DIR + "pdf_sample2.pdf");
    } catch (Exception e){
        e.printStackTrace();
    }
}

private static byte[] getArrayFromInputStream(InputStream inputStream) throws IOException {
    byte[] bytes;
    byte[] buffer = new byte[1024];
    try(BufferedInputStream is = new BufferedInputStream(inputStream)){
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        int length;
        while ((length = is.read(buffer)) > -1 ) {
            bos.write(buffer, 0, length);
        }
        bos.flush();
        bytes = bos.toByteArray();
    }
    return bytes;
}

private static void writeContent(byte[] content, String fileToWriteTo) throws IOException {
    File file = new File(fileToWriteTo);
    try(BufferedOutputStream salida = new BufferedOutputStream(new FileOutputStream(file))){
        salida.write(content);
        salida.flush();
    }
}
}
Dmitrii Cheremisin
  • 1,498
  • 10
  • 11