MemoryStream

A Stream that writes into a memory buffer.

This stream supports std.experimental.allocator for all of it's allocations and freeing.

This stream will automatically call dispose during it's deconstructor.

Outside of exceptions, this stream doesn't allocate GC memory unless the given allocator does.

Constructors

this
this()
Undocumented in source.
this
this(Alloc alloc)
Undocumented in source.

Destructor

~this
~this()
Undocumented in source.

Members

Functions

dispose
void dispose()
Undocumented in source. Be warned that the author may not have intended to support it.
flush
void flush()
Undocumented in source. Be warned that the author may not have intended to support it.
readToBuffer
ubyte[] readToBuffer(ubyte[] buffer)
Undocumented in source. Be warned that the author may not have intended to support it.
seek
void seek(SeekFrom from, ptrdiff_t amount)
Undocumented in source. Be warned that the author may not have intended to support it.
write
const(ubyte[]) write(ubyte[] data)
Undocumented in source. Be warned that the author may not have intended to support it.

Properties

data
inout(ubyte[]) data [@property getter]

Gets the internal buffer of the stream.

isDisposed
bool isDisposed [@property getter]
Undocumented in source. Be warned that the author may not have intended to support it.
length
size_t length [@property setter]
Undocumented in source. Be warned that the author may not have intended to support it.
length
size_t length [@property getter]
Undocumented in source. Be warned that the author may not have intended to support it.
position
size_t position [@property setter]
Undocumented in source. Be warned that the author may not have intended to support it.
position
size_t position [@property getter]
Undocumented in source. Be warned that the author may not have intended to support it.
readTimeout
Duration readTimeout [@property setter]
Undocumented in source. Be warned that the author may not have intended to support it.
readTimeout
Duration readTimeout [@property getter]
Undocumented in source. Be warned that the author may not have intended to support it.
writeTimeout
Duration writeTimeout [@property setter]
Undocumented in source. Be warned that the author may not have intended to support it.
writeTimeout
Duration writeTimeout [@property getter]
Undocumented in source. Be warned that the author may not have intended to support it.

Inherited Members

From Stream

Can
enum Can

A flag enum specifying what the stream is capable of doing.

SeekFrom
enum SeekFrom

Used with seek.

write
const(ubyte[]) write(ubyte[] data)

Writes bytes into the stream.

readToBuffer
ubyte[] readToBuffer(ubyte[] buffer)

Reads bytes from the stream into a buffer.

dispose
void dispose()

Disposes of the stream's resources.

flush
void flush()

Flushes the stream's data.

seek
void seek(SeekFrom from, ptrdiff_t amount)

Seeks somewhere into the stream.

isDisposed
bool isDisposed [@property getter]
writeTimeout
Duration writeTimeout [@property setter]

Sets the write timeout for the stream.

writeTimeout
Duration writeTimeout [@property getter]

Gets the write timeout for the stream.

readTimeout
Duration readTimeout [@property setter]

Sets the read timeout for the stream.

readTimeout
Duration readTimeout [@property getter]

Gets the read timeout for the stream.

length
size_t length [@property setter]

Sets the length of the stream's data.

length
size_t length [@property getter]

Gets the length of the stream's data.

position
size_t position [@property setter]

Sets the position of the stream's 'cursor'.

position
size_t position [@property getter]

Gets the position of the stream's 'cursor'.

read
ubyte[] read(size_t amount)

Creates a GC-allocated buffer with a specified size, and attempts to read in a certain amount of bytes.

copyTo
void copyTo(Stream to, ubyte[] buffer)

Copies data from the current position in this stream to the end of this stream, into the given stream. The given stream is written to using it's current position as well.

seekStart
void seekStart(ptrdiff_t amount)

Shortcut for seek(SeekFrom.Start)

seekCurr
void seekCurr(ptrdiff_t amount)

Shortcut for seek(SeekFrom.Current)

seekEnd
void seekEnd(ptrdiff_t amount)

Shortcut for seek(SeekFrom.End)

copyToStack
void copyToStack(Stream to)

A helper function for copyTo, that uses a buffer placed on the stack.

copyToAlloc
void copyToAlloc(Stream to, size_t bufferSize)
void copyToAlloc(Stream to, size_t bufferSize, Alloc alloc)

A helper function for copyTo, that uses a buffer created from an std.experimental.allocator Allocator. The buffer is of course disposed of afterwards.

copyToGC
void copyToGC(Stream to, size_t bufferSize)

A helper function for copyTo, that uses a buffer allocated by the GC.

can
Can can [@property getter]
canWrite
bool canWrite [@property getter]
canRead
bool canRead [@property getter]
canSeek
bool canSeek [@property getter]
canTimeout
bool canTimeout [@property getter]

Examples

import std.experimental.allocator.mallocator      : Mallocator;
import std.experimental.allocator.building_blocks : StatsCollector, Options;

alias Alloc   = StatsCollector!(Mallocator, Options.all, Options.all);
alias MStream = MemoryStream!(Alloc*);
auto alloc    = new Alloc();

auto stream = new MStream(alloc);
assert(stream.position == 0);
assert(stream.length   == 0);
assert(stream.canWrite);
assert(stream.canRead);
assert(stream.canSeek);
assert(!stream.canTimeout);
assert(!stream.isDisposed);

stream.write([0, 0, 0, 0]); // Testing to see if it cleans memory after use.
stream.dispose();
assert(stream.isDisposed);

stream = new MStream(alloc);
stream.write([0, 1, 2, 3, 4]);
assert(stream.position == 5);
assert(stream.length == 5);
assert(stream.data == [0, 1, 2, 3, 4]);

stream.length = 20;
assert(stream.length == 20);

stream.length = 5;
assert(stream.length == 5);

stream.position = stream.position - 2;
assert(stream.read(2) == [3, 4]);

assert(alloc.bytesUsed > 0);
destroy(stream); // To simulate the GC collecting it
// import std.stdio;
// alloc.reportStatistics(stdout);

assert(alloc.bytesUsed == 0);

Meta