What is the alternative to the following Java code for JavaScript?
String[] strings = {"something written here", "five", "hello world", "this is a really big string", "two words"};
ByteArrayOutputStream bos = new ByteArrayOutputStream();
for (String str : strings)
{
bos.write(str.length());
bos.write(str.getBytes());
}
ByteBuffer buffer = ByteBuffer.wrap(bos.toByteArray());
// send buffer somewhere...
I know that I can read Java's ByteBuffer sent over the network with the following JS code, but couldn't figure out a way of writing a reply using JS.
let strings = [];
let bytes = Buffer.from(data);
while (bytes.length > 0) {
let size = bytes.readInt8(0) + 1;
strings.push(bytes.toString("UTF-8", 1, size));
bytes = bytes.slice(size);
}
Let's suppose I want to reply: ["hello", "how", "are you", "doing"]