OutputStream issue with Documents4j

437 Views Asked by At

I'm using Documents4j to convert documents to PDF/a. I want to build a function that return a String representation of my file with this code:

    String input=...;
    DocumentType[] docType= {DocumentType.CSV,DocumentType.DOC,DocumentType.MHTML,DocumentType.MS_EXCEL,DocumentType.MS_WORD,DocumentType.ODS,DocumentType.PDF,DocumentType.RTF,DocumentType.TEXT,DocumentType.XML};

    IConverter converter = LocalConverter.make();

    ByteArrayInputStream in= new ByteArrayInputStream(input.getBytes());       
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    Future<Boolean> conversion = converter.convert(in)
    .as(docType[n-1])
    .to(out) 
    .as(DocumentType.PDFA)
    .prioritizeWith(1000) // optional
    .schedule();

    String output=out.toString();

    in.close();
    out.flush();
    out.close();
    System.out.println(output);
    return(output);

But my output is blank. I think I misused the .to() method by inputing an inappropriate argument. Which OutputStream am I supposed to use if it's not a ByteArrayOutputStream? If there isn't any viable OutputStream other than FileOutputStream, have you any idea of how to return a String in output without creating a file at any point?

Thanks in advance for your attention and answers.

1

There are 1 best solutions below

0
On BEST ANSWER

When calling schedule instead of exectue, the job runs in the background. You are facing a racing condition here.

Also, there is no good reason to call toString on an byte array output stream; ratheer use getBytes().