Pike v8.0 release 1738

Method Stdio.Buffer()->create()


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.