# Class Stdio.Buffer

Inheritance graph

Description

  A buffer to use as input or buffering when doing I/O. It is similar to String.Buffer, but can only contain 8bit data and is designed for protocol parsing. It is optimized for reading from the beginning and adding to the end, and will try to minimize the amount of data copying that is done.

  The class maintains two separate offsets, one for reading and one for writing. The functions that add data all do so at the write offset (the end of the buffer), and reading is done from the read offset (the start of the buffer).

  The class can also be used to directly read from and write to filedescriptors if so desired. This eliminates at least one memory copy.

Note

  The "avoid copy" part means that a Buffer will never shrink unless you call the trim function.

Method create

  `Stdio.Buffer Stdio.Buffer(`int`|`void` `len`)`
  `Stdio.Buffer Stdio.Buffer(`string(8bit)` `contents`)`
  `Stdio.Buffer Stdio.Buffer(`System.Memory`|`String.Buffer` `contents`)`

Description

  If passed an integer or no argument, create a buffer of that size, or if no argument is given, 226 bytes.

  If contents are specified a new buffer with the contents of the given string/System.Memory or String.Buffer will be created.

Note

  In the String.Buffer case the data has to be copied unless there is only one reference to the String.Buffer object, since modifications of the String.Buffer would cause the Buffer to point into invalid memory.

  In all other cases this will not copy the string data, instead data will be read from the source until it needs to be modified, so the buffer creation is fast regardless of the length of the string.

  However, as an example, if the buffer is created with a 100Gb System.Memory mmap:ed file as the contents and you later on try to modify the buffer using one of the add functions (or sprintf and similar) the old contents will be copied.

  You can use read_only() to avoid accidents.