3

Given a byte array in UTF-8 encoding (as result of base64 decoding of a String) - what is please a correct way to write it to a file in UTF-8 encoding?

Is the following source code (writing the array byte by byte) correct?

OutputStreamWriter osw = new OutputStreamWriter(
    new FileOutputStream(tmpFile), Charset.forName("UTF-8"));
for (byte b: buffer)
    osw.write(b);
osw.close();
Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
  • It's duplicated (http://stackoverflow.com/questions/1001540/how-to-write-a-utf-8-file-with-java) – Estimate May 20 '15 at 09:37
  • 3
    If you already have a buffer of bytes you don't need a writer. A writer is for writing characters or Strings. – Robert May 20 '15 at 09:38
  • 2
    Based on your comment on the answer below, are you sure your array is in UTF-8 encoding? It will only be so if the original data that was encoded in base64 was in UTF-8 itself. What was the input of your base64 encode? – RealSkeptic May 20 '15 at 09:41
  • Some objects encoded with [Base64.encode](https://github.com/Himmele/Mindroid.java/blob/master/src/mindroid/util/Base64.java)... (I know it's a stupid answer, but the source code is huge with 2500 Java files) – Alexander Farber May 20 '15 at 09:45
  • 2
    That is the code of `Base64`, but what was encoded with it? Are you sure they were strings? Were they Java strings? Were they byte arrays that were converted from strings? If you don't know what the actual objects were, it's going to be hard to print them as you don't even know they are strings. You can encode *anything* in base64, including images and sounds. – RealSkeptic May 20 '15 at 09:53
  • @RealSkeptic is correct; you have to know what was passed into Base64.encode to know whether this will work. – Louis Wasserman May 20 '15 at 15:31

1 Answers1

5

Don't use a Writer. Just use the OutputStream. A complete solution using try-with-resource looks as follows:

try (FileOutputStream fos = new FileOutputStream(tmpFile)) {
    fos.write(buffer);
}

Or even better, as Jon points out below:

Files.write(Paths.get(tmpFile), buffer);
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • 2
    Or `Files.write` to make it even simpler. – Jon Skeet May 20 '15 at 09:34
  • When I use just OutputStream I get the error (later, when [trying to use Efficient XML](http://stackoverflow.com/questions/30335999/content-is-not-allowed-in-prolog-with-efficient-xml)): **Invalid byte 1 of 1-byte UTF-8 sequence**. So I am trying to find a proper way to write the byte array to file in UTF-8 encoding – Alexander Farber May 20 '15 at 09:35
  • 7
    But you said the array *is* in UTF-8 encoding, no? I think you should check your assumptions. Make sure the `buffer` is indeed proper UTF-8 and that it represents something Efficient XML can handle. – aioobe May 20 '15 at 09:36
  • I think the problem is while reading in that case, create a buffer read and write simultaneously – Nitin Dandriyal May 20 '15 at 09:40