Search code examples
javastreamvaadin

Strings in downloadfile weird symbols


I've got a String array that contains the content for a downloadable file. I am converting it to a Stream for the download but there are some random values in the downloadfile. I don't know if it is due to the encoding and if yes, how can I change it?

var downloadButton = new DownloadLink(btn, "test.csv", () -> {
try {
   ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
   ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
   for (int i = 0; i < downloadContent.size(); i++) {
    objectOutputStream.writeUTF(downloadContent.get(i));
   }
objectOutputStream.flush();
objectOutputStream.close();
byte[] byteArray = byteArrayOutputStream.toByteArray();

ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);

objectInputStream.close();

return new ByteArrayInputStream(byteArray);

This is the DownloadLink class.

public class DownloadLink extends Anchor {
    public DownloadLink(Button button, String fileName, InputStreamFactory fileProvider) {
        super(new StreamResource(fileName, fileProvider), "");
        getElement().setAttribute("download", fileName);
        add(button);
        getStyle().set("display", "contents");
    }
}

this is the output file


Solution

  • ObjectOutputStream is part of the Java serialization system. In addition to the data itself, it also includes metadata about the original Java types and such. It's only intended for writing data that will later be read back using ObjectInputStream.

    To create a file for others to download, you could instead use a PrintWriter that wraps the original output stream. On the other hand, you're using the output stream to create a byte[] which means that a more straightforward, but slightly less efficient, way would be to create a concatenated string from all the array elements and then use getBytes(StandardCharsets.UTF_8) on it to directly get a byte array.