A BufWriter
has a private field buf
which is inaccessible. How can I know the size of the data currently contained in it?
In the main () function, we opened the "Demo.txt" file and then created a buffer-writer using bufio.NewWriter () function. Then we got the available buffer size using the Available () function and print the result on the console screen.
So the buffer length property should be considered read only, and another way should be used to set the size of a buffer. One way to go about changing the buffer length would be to just use a method like buffer slice to create a new buffer of the desired size and then set that to the variable that is being used.
Buffers have a length property that contains the number of bytes in the buffer. For buffers containing UTF8 encoded strings, the buffer's length is equivalent to the length of the string. For example, if you read a text file from the file system using fs, the resulting buffer's length is the same as the number of characters in the text file.
The reason why is because of the nature of Unicode. However in nodejs when working with buffers the buffer length property of a buffer can be used to get the amount of memory that the buffer is taking up at least. In addition if buffers are used the right way buffer length can be used as a way to get the actual data size of a string.
You cannot (check the source to be sure). The inner buf
is not exposed in any fashion, presumably to allow for the implementation to change without breaking compatibility.
Well, you can, but it's terrible and hacky. You can print the BufWriter
with the debugging formatter:
use std::io::prelude::*;
use std::io::{self, BufWriter};
fn main() {
let mut b = BufWriter::new(io::sink());
b.write_all(b"hello, world").expect("Unable to write");
println!("{:?}", b)
}
BufWriter { writer: Sink { .. }, buffer: 12/8192 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With