0

While reading the SequenceInputStream the second file(sample2.pdf) is not showing up in final pdf . Other formats like txt,xml,csv works fine . I don't want to use PDFReader or any other libraries for reading.

public class ReadStream{

       private InputStream readInputStream()
        {

         //I have two input streams like below 
         List<InputStream> inputStream1= new ArrayList<>();
         inputStream1.add(new FileInputStream("sample1.pdf"));
         inputStream1.add(new FileInputStream("sample2.pdf"));
         //And I use combine them like below
         return new SequenceInputStream(Collections.enumeration(inputStream1));
        }

}

Here is my test class

public class TestDemo{
 @Test
public void testStream(){
     try (InputStream stream = new ReadStream().readInputStream()) {
                    FileUtils.copyInputStreamToFile(stream,
                        new File("destFile.pdf"));
                    final byte[] decryptedBytes =Files.readAllBytes(new File("destFile.pdf");
                    Files.write(Paths.get("final.pdf"), decryptedBytes));
                }
    
    }
}

I could also see the size of SequenceInputStream as 2. Anyone who can shed some light would be appreciated. Also how to read the SequenceInputStream as List of InputStream separately?

Pradeep
  • 1,947
  • 3
  • 22
  • 45
  • 1
    *"Other formats like txt,xml,csv works fine"* - that's unlikely. Plain text maybe yes, but for XML input you get an output file with two root elements, so the output is no valid XML anymore. For csv you may suddenly have headers in the middle of the file. – mkl Jul 15 '22 at 05:08
  • Yes that’s right the one who consumes the library should ensure to skip the headers and focus only on data . – Pradeep Jul 15 '22 at 06:08
  • This is pretty much same issues as https://coderanch.com/t/581071/java/SequenceInputStream-read-streams https://stackoverflow.com/questions/46288556/second-byte-overwrites-first-byte-while-merging-the-pdf – Pradeep Jul 15 '22 at 18:53

0 Answers0