MemoryStream

A stream over a memory buffer.

MemoryStreams allow to write to a memory backed buffer, said buffer is owned by the memory stream and will grow to accomodate writes.

Ownership can be taken from a memory stream using take.

Threadsafety: Memory streams are not thread safe objects; to share them between threads you must first finish your stream operations, then take ownership of the memory; you can then copy the result out of thread-local memory.

More...

Constructors

this
this(size_t reserved)

Creates a new memory stream with reserved bytes of memory.

this
this(ubyte[] buffer)

Takes ownership of an existing buffer.

Destructor

~this
~this()

Destructor.

Members

Functions

close
void close()

Closes the stream.

flush
bool flush()

Clears all buffers of the stream and causes data to be written to the underlying device.

read
ptrdiff_t read(ubyte[] buffer)

Reads bytes from the specified stream in to the specified buffer

seek
long seek(ptrdiff_t offset, SeekOrigin origin)

Sets the reading position within the stream

take
ubyte[] take()

Takes ownership of the memory in the memory stream, causing the memory stream to reset to its initial state.

write
ptrdiff_t write(ubyte[] buffer)

Writes bytes from the specified buffer in to the stream

Properties

canFlush
bool canFlush [@property getter]

Whether the stream can be flushed to disk.

canRead
bool canRead [@property getter]

Whether the stream can be read from.

canSeek
bool canSeek [@property getter]

Whether the stream can be seeked.

canTimeout
bool canTimeout [@property getter]

Whether the stream can timeout during operations.

canWrite
bool canWrite [@property getter]

Whether the stream can be written to.

length
ptrdiff_t length [@property getter]

Length of the stream.

readTimeout
int readTimeout [@property getter]

Timeout in milliseconds before a read operation will fail.

tell
ptrdiff_t tell [@property getter]

Position in stream

writeTimeout
int writeTimeout [@property getter]

Timeout in milliseconds before a write operation will fail.

Inherited Members

From Stream

canRead
bool canRead [@property getter]

Whether the stream can be read from.

canWrite
bool canWrite [@property getter]

Whether the stream can be written to.

canSeek
bool canSeek [@property getter]

Whether the stream can be seeked.

canTimeout
bool canTimeout [@property getter]

Whether the stream can timeout during operations.

canFlush
bool canFlush [@property getter]

Whether the stream can be flushed to disk.

length
ptrdiff_t length [@property getter]

Length of the stream.

tell
ptrdiff_t tell [@property getter]

Position in stream

readTimeout
int readTimeout [@property getter]

Timeout in milliseconds before a read operation will fail.

writeTimeout
int writeTimeout [@property getter]

Timeout in milliseconds before a write operation will fail.

flush
bool flush()

Clears all buffers of the stream and causes data to be written to the underlying device.

seek
long seek(ptrdiff_t offset, SeekOrigin origin)

Sets the reading position within the stream

close
void close()

Closes the stream.

read
ptrdiff_t read(ubyte[] buffer)

Reads bytes from the specified stream in to the specified buffer

write
ptrdiff_t write(ubyte[] buffer)

Writes bytes from the specified buffer in to the stream

Detailed Description

Memory safety

Generally, memory streams are safe to use as long as you do not mess with their ownership semantics; when passing an existing buffer to the memory stream, make sure that no other variable is referencing the backing buffer. The buffer's memory address may change during resize operations, which can lead to stale pointers outside of the memory stream.

See Also

nulib.io.stream.rw.StreamReader, nulib.io.stream.rw.StreamWriter

Meta