You can give the BufferedOutputStream constructor a int parameter for the buffer size. I my szenario I have one process writing to the disk and on process reading from the disk. Having a default buffer of 8192 bytes causes high fragmentation of big files.
Now I was wondering if I could reduce the fragmentation if I lift the buffer size to 1 Mb.
I think this should work. But does the BufferedOutputStream still respect my buffer if I always call
BufferedOutputStream.write(smallBuffer,0,i); // i being smaller than 8192
i is small because I am catching small paketes (8192 bytes) over the net. But I want BufferedOutputStream to really buffer a lot, and not decide to flush earlier?
Is BufferedOutputStream “stupid” enough to wait until the buffer is full or flush() is called. Or has it got some time based flushing mechanism installed?
The BufferedOutputStream does not use any time based algorithm or other statistics to invoke flush internally. It will flush as soon as the buffer is full or you flush it explitily (or before the buffered stream is closed).
In other words: a larger buffer size will reduce fragmentation for your use case.