rush00121
rush00121

Reputation: 185

Adding a UTF-8 BOM to an outputStream causes the last character to not be printed in java

I have this piece of code that adds a BOM UTF-8 to an outputstream . I need to add the BOM since Excel does not identify the UTF-8 encoding implicitly and hence french characters are shown as weird characters in excel .

try {

            response.setContentType(getContentType(request, response));
            response.setContentLength(downloadData.length);
            ServletOutputStream os = response.getOutputStream();

            //Specify the BOM (Byte order Mask) for UTF-8 so that Excel can identify the encoding and open it in the correct format
            os.write(new byte[] { (byte)0xEF, (byte)0xBB, (byte)0xBF }); 
            os.write(downloadData);
            os.flush();
            response.flushBuffer();
        } catch (IOException e) {
            logger.error(e);
        }

However , when I output the outputstream to a file , the last character is not printed .

Any ideas why ?

Upvotes: 4

Views: 5090

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798764

Wouldn't the length have to be increased by 3 to account for the BOM?

Upvotes: 4

Related Questions