BufferedOutputRange is a performance enhancement over writing directly to an output stream. It holds a File open for write or an OutputRange. Ouput is accumulated in an internal buffer and written to the output stream as a block.

Writing to stdout is a key use case. BufferedOutputRange is often dramatically faster than writing to stdout directly. This is especially noticable for outputs with short lines, as it blocks many writes together in a single write.

The internal buffer is written to the output stream after flushSize has been reached. This is checked at newline boundaries, when appendln is called or when put is called with a single newline character. Other writes check maxSize, which is used to avoid runaway buffers.

struct BufferedOutputRange (
) if (
isFileHandle!(Unqual!OutputTarget) ||
isOutputRange!(Unqual!OutputTarget, char)
) {}


A destructor is present on this object, but not explicitly documented in the source.

Detailed Description

BufferedOutputRange has a put method allowing it to be used a range. It has a number of other methods providing additional control.

  • this(outputStream [, flushSize, reserveSize, maxSize]) - Constructor. Takes the output stream, e.g. stdout. Other arguments are optional, defaults normally suffice.
  • append(stuff) - Append to the internal buffer.
  • appendln(stuff) - Append to the internal buffer, followed by a newline. The buffer is flushed to the output stream if is has reached flushSize.
  • appendln() - Append a newline to the internal buffer. The buffer is flushed to the output stream if is has reached flushSize.
  • joinAppend(inputRange, delim) - An optimization of append(inputRange.joiner(delim)). For reasons that are not clear, joiner is quite slow.
  • flushIfFull() - Flush the internal buffer to the output stream if flushSize has been reached.
  • flush() - Write the internal buffer to the output stream.
  • put(stuff) - Appends to the internal buffer. Acts as appendln() if passed a single newline character, '\n' or "\n".

The internal buffer is automatically flushed when the BufferedOutputRange goes out of scope.