Aayush Neupane
Aayush Neupane

Reputation: 1246

Using Buffer.from() with the contents of fs.readFileSync() makes a buffer with different data

let fb = fs.readFileSync(filePath);
let fileData = fb.toString("utf8");
b = Buffer.from(fileData, "utf8");
console.log(fb);
console.log(b);
console.log(b == fb);
console.log(b.toString("utf8") === fb.toString("utf8"));

I converted buffer to UTF8 string and again tried to get the buffer back using Buffer.from() but I can see that two buffer are different. However, if I again compare toString() result of these two buffers they are same.

Output of the above program

enter image description here

Upvotes: 3

Views: 2579

Answers (1)

jfriend00
jfriend00

Reputation: 707328

If fileData is not entirely legal UTF8, then per the buffer.toString() doc:

If encoding is 'utf8' and a byte sequence in the input is not valid UTF-8, then each invalid byte is replaced with the replacement character U+FFFD.

So, if you convert to a string and then back to a buffer and the encoding was not perfect utf8 to start with, then the process is not reversible because some illegal utf8 bytes will have been replaced.

Comparing two .toString('utf8') results will then each contain the same "fixed" version (with the replaced bytes).

The key here is don't convert a Buffer to a utf8 string unless it's really legal utf8 data.

Upvotes: 4

Related Questions