I have a small snippet of code in nodejs, it is a utility class for encoding Kafka request data, and I will like to write the equivalent in Swift, what is the equivalent of the buffer class from nodejs in swift.
const INT8_SIZE = 1
const INT16_SIZE = 2
const INT32_SIZE = 4
const INT64_SIZE = 8
class Encoder {
constructor(length = 16) {
this.buffer = Buffer.alloc(length)
this.offset = 0;
}
writeInt8(value) {
this.increaseBufferSize(INT8_SIZE)
this.buffer.writeInt8(value, this.offset)
this.offset += INT8_SIZE
return this;
}
writeInt16(value) {
this.increaseBufferSize(INT16_SIZE)
this.buffer.writeInt16BE(value, this.offset)
this.offset += INT16_SIZE
return this;
}
writeInt32(value) {
this.increaseBufferSize(INT32_SIZE)
this.buffer.writeInt32BE(value, this.offset)
this.offset += INT32_SIZE
return this;
}
writeInt64(value) {
this.increaseBufferSize(INT64_SIZE)
this.buffer.writeBigInt64BE(value, this.offset)
this.offset += INT64_SIZE
return this;
}
writeString(value) {
if (value === null) {
this.writeInt16(-1)
return this
}
const byteLength = Buffer.byteLength(value)
this.increaseBufferSize(INT16_SIZE + byteLength)
this.writeInt16(byteLength)
this.buffer.write(value, this.offset, 'utf8')
this.offset += byteLength
return this
}
}