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.
// create a memory buffer with 1000 bytes
const buf = new MemoryBuffer(1000);
// fill the buffer with some data
for (let i = 0; i < 1000; ++i) {
buf[i] = i;
}
// write it to a binary file in the system's temporary directory;
// (Environment.getTempPath() does not include a trailing separator)
const path = `${Environment.getTempPath()}/buffer.bin`;
const stream = File.open(path,
FileMode.Create,
FileAccess.ReadWrite,
FileShare.None);
// write the bytes to the file
const bytes_written = stream.write(buf, 0, 1000);
stream.close();
alert(`Wrote ${bytes_written} bytes to ${path}`); Undefined
Calling clear() erases all the data in the buffer, setting all bytes to zero. The buffer size is not affected
A new MemoryBuffer object containing the data requested
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.
Undefined
Calling free releases all memory associated with the memory buffer and sets the new size of the buffer to zero
Returns the present size of the memory buffer
Returns the present size of the memory buffer in bytes.
True if the buffer allocation succeeded, false upon failure
Ensures that the buffer has at least size bytes available.
A new MemoryBuffer object containing the data requested
Copies a portion of the buffer specified by the offset and length parameters into an ascii string. If the length parameter is omitted, the remainder of the buffer is copied. If no parameters are specified, the entire object is copied.