Buffer

Buffer objects are used for working with binary data. They can be used much like arrays, but are much more efficient since they only store bytes.

new Buffer()

Constructor method.

Create a new empty buffer.

Returns:

Buffer.

EXAMPLE

var buffer = new mupdf.Buffer();
new Buffer(original)

Constructor method.

Create a new buffer with a copy of the data from the original buffer.

Arguments:
  • originalBuffer.

Returns:

Buffer.

EXAMPLE

var buffer = new mupdf.Buffer(buffer);
readFile(fileName)

mutool only

Constructor method.

Create a new buffer with the contents of a file.

Arguments:
  • fileName – The path to the file to read.

Returns:

Buffer.

EXAMPLE

var buffer = mupdf.readFile("my_file.pdf");

Instance properties

length

mutool only

The number of bytes in the buffer. Read-only.

[n]

mutool only

Read/write the byte at index ‘n’. Will throw exceptions on out of bounds accesses.

EXAMPLE

var byte = buffer[0];

Instance methods

getLength()

wasm only

Returns the number of bytes in the buffer. Read-only.

Returns:

Integer.

EXAMPLE

var length = buffer.getLength();
writeByte(b)

Append a single byte to the end of the buffer.

Arguments:
  • b – The byte value. Only the least significant 8 bits of the value are appended to the buffer.

EXAMPLE

buffer.writeByte(0x2a);
readByte(at)

wasm only

Read the byte at the supplied index.

Arguments:
  • atInteger.

EXAMPLE

buffer.readByte(0);
writeRune(c)

mutool only

Encode a unicode character as UTF-8 and append to the end of the buffer.

Arguments:
  • c – The character unicode codepoint.

EXAMPLE

buffer.writeRune(0x4f60); // To append U+4f60
buffer.writeRune(0x597d); // To append U+597d
buffer.writeRune(0xff01); // To append U+ff01
writeLine(...)

Append arguments to the end of the buffer, separated by spaces, ending with a newline.

Arguments:
  • ... – List of arguments.

EXAMPLE

buffer.writeLine("a line");
write(...)

Append arguments to the end of the buffer, separated by spaces.

Arguments:
  • ... – List of arguments.

EXAMPLE

buffer.write("hello", "world");
writeBuffer(data)

Append the contents of the data buffer to the end of the buffer.

Arguments:
  • data – Data buffer.

EXAMPLE

buffer.writeBuffer(anotherBuffer);
slice(start end)

Create a new buffer containing a (subset of) the data in this buffer. Start and end are offsets from the beginning of this buffer, and if negative from the end of this buffer.

Arguments:
  • start – Start index.

  • end – End index.

Returns:

Buffer.

EXAMPLE

var buffer = new Buffer();
buffer.write("hello", "world"); // buffer contains "hello world"
var newBuffer = buffer.slice(1, -1); // newBuffer contains "ello worl"
save(fileName)

mutool only

Write the contents of the buffer to a file.

Arguments:
  • fileName – Filename to save to.

EXAMPLE

buffer.save("my_buffer_filename");
asUint8Array()

wasm only

Returns the buffer as a Uint8Array.

Returns:

Uint8Array.

EXAMPLE

var arr = buffer.asUint8Array();
asString()

wasm only

Returns the buffer as a String.

Returns:

String.

EXAMPLE

var str = buffer.asString();