'Is there a way to use a Java Writer object to write binary data?

I want to use the writeSearchResults method of this Jira plugin interface.

The description of the interface says that it should be possible to generate PDF files but I am not sure how to do this correctly with just the Writer object that the method is passed. I would have expected to need an OutputStream object for this.

Even wrapping the Writer inside a WriterOutputStream still produces corrupted data due to extra encoding-related bytes.



Solution 1:[1]

you can do it like this:

    import java.io.*;
    
    public class WriteBinary{
        public static void main(String[] args) {
     
            if (args.length < 2) {
                System.out.println("Please provide input and output files");
                System.exit(0);
            }
     
            String inputFile = args[0];
            String outputFile = args[1];
     
            try (
                InputStream inputStream = new FileInputStream(inputFile);
                OutputStream outputStream = new FileOutputStream(outputFile);
            ) {
                int byteRead = -1;
     
                while ((byteRead = inputStream.read()) != -1) {
                    outputStream.write(byteRead);
                }
     
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

}

in this example we to binary data from a file and write it into an other.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Louis