OutBuffer provides a way to build up an array of bytes out of raw data. It is useful for things like preparing an array of bytes to write out to a file. OutBuffer's byte order is the format native to the computer. To control the byte order (endianness), use a class derived from OutBuffer.
OutBuffer's internal buffer is allocated with the GC. Pointers stored into the buffer are scanned by the GC, but you have to ensure proper alignment, e.g. by usingalignSize((void*).sizeof).
pure nothrow @safe inout(ubyte)[]
toBytes() inout scope;
Convert to array of bytes.
pure nothrow @trusted void
reserve(size_t
nbytes);
Preallocate nbytes more to the size of the internal buffer.
This is a speed optimization, a good guess at the maximum size of the resulting buffer will improve performance by eliminating reallocations and copying.
put enables OutBuffer to be used as an OutputRange.
pure nothrow @safe void
write(scope const(ubyte)[]
bytes);
pure nothrow @safe void
write(byte
b);
pure nothrow @safe void
write(char
c);
pure nothrow @safe void
write(dchar
c);
pure nothrow @safe void
write(short
s);
pure nothrow @safe void
write(int
i);
pure nothrow @safe void
write(long
l);
Append data to the internal buffer.
pure nothrow @safe void
fill(size_t
nbytes, ubyte
val = 0);
Append nbytes of val to the internal buffer.
Parameters:size_tnbytes | Number of bytes to fill. |
ubyteval | Value to fill, defaults to 0. |
pure nothrow @safe void
fill0(size_t
nbytes);
Append nbytes of 0 to the internal buffer.
Paramnbytes - number of bytes to fill.
pure nothrow @safe void
alignSize(size_t
alignsize, ubyte
val = 0);
Append bytes until the buffer aligns on a power of 2 boundary.
By default fills with 0 bytes.
Parameters:size_talignsize | Alignment value. Must be power of 2. |
ubyteval | Value to fill, defaults to 0. |
Examples:OutBuffer buf =new OutBuffer();buf.write(cast(ubyte) 1);buf.align2();writeln(buf.toBytes());// "\x01\x00"buf.write(cast(ubyte) 2);buf.align4();writeln(buf.toBytes());// "\x01\x00\x02\x00"buf.write(cast(ubyte) 3);buf.alignSize(8);writeln(buf.toBytes());// "\x01\x00\x02\x00\x03\x00\x00\x00"
Examples:ditto
OutBuffer buf =new OutBuffer();buf.write(cast(ubyte) 1);buf.align2(0x55);writeln(buf.toBytes());// "\x01\x55"buf.write(cast(ubyte) 2);buf.align4(0x55);writeln(buf.toBytes());// "\x01\x55\x02\x55"buf.write(cast(ubyte) 3);buf.alignSize(8, 0x55);writeln(buf.toBytes());// "\x01\x55\x02\x55\x03\x55\x55\x55"
pure nothrow @safe void
clear();
Clear the data in the buffer
pure nothrow @safe void
align2(ubyte
val = 0);
Optimize common special case alignSize(2)
Parameters:ubyteval | Value to fill, defaults to 0. |
pure nothrow @safe void
align4(ubyte
val = 0);
Optimize common special case alignSize(4)
Parameters:ubyteval | Value to fill, defaults to 0. |
pure nothrow @safe string
toString() const;
Convert internal buffer to array of chars.
nothrow @system void
vprintf(scope string
format, va_list
args);
Append output of C's vprintf() to internal buffer.
@system void
printf(scope string
format, ...);
Append output of C's printf() to internal buffer.
void
writef(Char, A...)(scope const(Char)[]
fmt, A
args);
void
writef(alias fmt, A...)(A
args)
if (isSomeString!(typeof(fmt)));
Formats and writes its arguments in text format to the OutBuffer.
Examples:OutBuffer b =new OutBuffer();b.writef("a%sb", 16);writeln(b.toString());// "a16b" Examples:OutBuffer b =new OutBuffer();b.writef!"a%sb"(16);writeln(b.toString());// "a16b"
void
writefln(Char, A...)(scope const(Char)[]
fmt, A
args);
void
writefln(alias fmt, A...)(A
args)
if (isSomeString!(typeof(fmt)));
Formats and writes its arguments in text format to the OutBuffer, followed by a newline.
Examples:OutBuffer b =new OutBuffer();b.writefln("a%sb", 16);writeln(b.toString());// "a16b\n" Examples:OutBuffer b =new OutBuffer();b.writefln!"a%sb"(16);writeln(b.toString());// "a16b\n"
pure nothrow @safe void
spread(size_t
index, size_t
nbytes);
At offset index into buffer, create nbytes of space by shifting upwards all data past index.