Question: I currently have the following query set and I wonder if there is a way to bring that part `question.answers.all()` into the `questions=` query set.

```
def get_questions_and_answers(self):
    questions = (
        self.request.event.surveys.get(template=settings.SURVEY_POST_EVENT)
        .questions.exclude(focus=QuestionFocus.EMAIL)
        .all()
    )

    questions_and_answers = {}
    for question in questions:
        questions_and_answers[question] = question.answers.all()

    return questions_and_answers

```

**models.py**

```
class Question(TimeStampedModel[...]):
    survey = models.ForeignKey(related_name='questions')
    type = models.CharField([...])
    focus = models.CharField([...])

class Answer(TimeStampedModel):
    question = models.ForeignKey(related_name='answers')
    response = models.ForeignKey(related_name='answers')
    answer = models.TextField([...])

class Survey(TimeStampedModel):
    event = models.ForeignKey(related_name='surveys')
    template = models.CharField([...])

```

Answer: 
The closest equivalent to a `ByteArrayOutputStream` would probably be a [`Writable` stream](https://nodejs.org/api/stream.html#stream_class_stream_writable) that is backed by a growing array. I guess you'll have to [implement this yourself](https://nodejs.org/api/stream.html#stream_implementing_a_writable_stream) though, see e.g. in [Convert stream into buffer?](https://stackoverflow.com/q/19906488/1048572).

It's probably simpler to directly append buffers to an array as they come in, then [concat them all](https://stackoverflow.com/a/45899032/1048572), to implement your growing buffer.

```
const strings = ["something written here", "five", "hello world", "this is a really big string", "two words"];

const output = [];

for (const str of strings) {
    const bytes = Buffer.from(str, "utf-8");
    output.push(Buffer.from([bytes.length+1]));
    output.push(bytes);
}

const buffer = Buffer.concat(output);

```

Btw I'm pretty sure you want to use `bytes.length`, not `str.length`, for your [length-prefixed strings](https://en.wikipedia.org/wiki/String_(computer_science)#Length-prefixed)? Same in your Java code, you need to use `str.getBytes().length` not `str.length()`.
Highest Score Bergi solution is absolutely nice, but I don't get it why he has the code `output.push(Buffer.from([bytes.length+1]));`, and if I add it, it will go wrong. It will work when I remove it.