Developer Resources

MemoryBuffer

Overview

The MemoryBuffer class allows an arbitrary amount of data to be stored in a memory buffer. MemoryBuffer automatically manages the deallocation of the memory as soon as the object is destroyed.

Constructor

MemoryBuffer(size : Integer)

Arguments

size
The new size of the buffer.

Methods

MemoryBuffer.clear
Clears the memory buffer
MemoryBuffer.copy
Copies either all or a portion of the buffer to a new MemoryBuffer object.
MemoryBuffer.free
Frees the memory buffer
MemoryBuffer.getSize
Retrieves the size of the buffer.
MemoryBuffer.setSize
Sets the size of the buffer.

Example

// create a memory buffer with 1000 bytes
var buf = new MemoryBuffer(1000);

// fill the buffer with some data
var i;
for (i = 0; i < 1000; ++i)
{
    buf[i] = i;
}

// create a binary file
var stream = File.open("text.txt",
                       FileMode.Create,
                       FileAccess.ReadWrite,
                       FileShare.None);

// write the bytes to the file     
var bytes_written = stream.write(buf, 0, 1000);

MemoryBuffer.clear

function MemoryBuffer.clear() : Undefined

Returns

Undefined

Description

Calling clear() erases all the data in the buffer, setting all bytes to zero. The buffer size is not affected

MemoryBuffer.copy

function MemoryBuffer.copy() : MemoryBuffer
function MemoryBuffer.copy(offset : Integer, length : Integer) : MemoryBuffer

Arguments

offset
Offset at which the copy should start.
length
Number of bytes to copy.

Returns

A new MemoryBuffer object containing the data requested

Description

Copies a portion of the buffer specified by the offset and length parameters. If the length parameter is omitted, the remainder of the buffer is copied. If no parameters are specified, the entire object is copied.

MemoryBuffer.free

function MemoryBuffer.free() : Undefined

Returns

Undefined

Description

Calling free releases all memory associated with the memory buffer and sets the new size of the buffer to zero

MemoryBuffer.getSize

function MemoryBuffer.getSize(size : Integer) : Integer

Returns

Returns the present size of the memory buffer

Description

Returns the present size of the memory buffer in bytes.

MemoryBuffer.setSize

function MemoryBuffer.setSize(size : Integer) : Boolean

Arguments

size
The new size of the buffer.

Returns

True if the buffer allocation succeeded, false upon failure

Description

Ensures that the buffer has at least size bytes available.