-
Notifications
You must be signed in to change notification settings - Fork 0
ByteStream
The ByteStream utility is a class designed for managing and manipulating byte streams in Salesforce Commerce Cloud (SFCC). It provides a range of methods for reading, moving, and comparing byte sequences, making it particularly useful when working with binary data, such as file uploads, downloads, and streaming operations.
Description: Creates a new ByteStream instance with the specified source of bytes.
-
Parameters:
-
source(dw.util.Bytes): The source of bytes for the stream.
-
-
Throws:
-
TypeError: If the source is not an instance ofdw.util.Bytes.
-
Usage Example:
var Bytes = require('dw/util/Bytes');
var ByteStream = require('*/cartridge/scripts/util/ByteStream');
var byteData = new Bytes('example data');
var stream = new ByteStream(byteData);Description: Compares two byte arrays for equality.
-
Parameters:
-
a(dw.util.Bytes): The first byte array. -
b(dw.util.Bytes): The second byte array.
-
-
Returns:
-
boolean: True if the byte arrays are equal, false otherwise.
-
Usage Example:
var Bytes = require('dw/util/Bytes');
var ByteStream = require('*/cartridge/scripts/util/ByteStream');
var bytes1 = new Bytes('data1');
var bytes2 = new Bytes('data1');
if (ByteStream.equal(bytes1, bytes2)) {
console.log('The byte arrays are equal.');
}Description: Checks if the end of the stream has been reached.
-
Returns:
-
boolean: True if the end of the stream has been reached, false otherwise.
-
Usage Example:
if (stream.eos()) {
console.log('End of stream reached');
}Description: Moves the position within the stream by a specified offset.
-
Parameters:
-
offset(number): The number of bytes to move the position by.
-
-
Throws:
-
TypeError: If the offset is not a number. -
RangeError: If the resulting position is out of bounds.
-
Usage Example:
stream.move(5); // Moves the position 5 bytes forwardDescription: Peeks at a byte in the stream at the current position plus an optional offset without advancing the position.
-
Parameters:
-
offset(number, optional): The offset from the current position to peek at. Defaults to 0.
-
-
Returns:
-
number: The byte at the specified position.
-
-
Throws:
-
TypeError: If the offset is out of bounds.
-
Usage Example:
var byte = stream.peek(); // Peek at the current positionDescription: Slices a portion of the stream into a new byte array.
-
Parameters:
-
start(number, optional): The starting position of the slice. Defaults to 0. -
end(number, optional): The ending position of the slice. Defaults to the end of the stream.
-
-
Returns:
-
dw.util.Bytes: The sliced bytes.
-
Usage Example:
var slicedBytes = stream.slice(0, 10); // Slices the first 10 bytesDescription: Reads a specified number of bytes from the stream.
-
Parameters:
-
n(number): The number of bytes to read.
-
-
Returns:
-
dw.util.Bytes|null: The read bytes, or null if the end of the stream has been reached.
-
-
Throws:
-
TypeError: Ifnis not a non-negative number.
-
Usage Example:
var bytes = stream.readN(5); // Reads the next 5 bytesDescription: Reads a single byte from the stream and advances the position.
-
Returns:
-
number|null: The read byte, or null if the end of the stream has been reached.
-
Usage Example:
var byte = stream.read(); // Reads the next byteDescription: Reads a line from the stream, ending at a newline character.
-
Returns:
-
dw.util.Bytes|null: The read line, or null if the end of the stream has been reached.
-
Usage Example:
var line = stream.readLine(); // Reads a line of bytes until a newline character is encounteredDescription: Reads bytes from the stream until a specified sequence is encountered.
-
Parameters:
-
sequence(dw.util.Bytes): The sequence of bytes to read until.
-
-
Returns:
-
dw.util.Bytes|null: The read bytes, or null if the end of the stream has been reached.
-
-
Throws:
-
TypeError: Ifsequenceis not an instance ofdw.util.Bytes.
-
Usage Example:
var Bytes = require('dw/util/Bytes');
var sequence = new Bytes('--Boundary');
var dataUntilBoundary = stream.readUntil(sequence); // Reads bytes until the specified sequence is encounteredDescription: Contains character codes for common control characters.
-
CR: Carriage return (
'\r', code 13) -
LF: Line feed (
'\n', code 10) -
HN: Hyphen (
'-', code 45)
Usage Example:
if (byte === ByteStream.charCode.LF) {
console.log('Line feed encountered');
}