The BufferedOutputStream class stores written data in a buffer (a protected byte array
field named buf) until the buffer is full or the stream is flushed. Then it writes the data
onto the underlying output stream all at once. A single write of many bytes is almost
always much faster than many small writes that add up to the same thing. This is espe‐
cially true of network connections because each TCP segment or UDP packet carries a
finite amount of overhead, generally about 40 bytes’ worth. This means that sending 1
kilobyte of data 1 byte at a time actually requires sending 40 kilobytes over the wire,
whereas sending it all at once only requires sending a little more than 1K of data. Most
network cards and TCP implementations provide some level of buffering themselves,
so the real numbers aren’t quite this dramatic. Nonetheless, buffering network output
is generally a huge performance win.