So I've got a function that reads all the available bytes stored in the buffer and returns it as a byte array:
Aside from the issue that PeterH pointed out, there is a flaw in your logic.
When the while loop ends, you have read all the serial data in the buffer. If you are using 0023 or earlier, where flush() throws away random amounts of unread data, there is no sense calling it when you have just finished making sure that the buffer contains no unread data.
If you are using 1.0 or later, where flush() blocks until the outgoing serial buffer is empty, I'm sure you can see how that is a useless activity to perform.
In either case, the flush() call, as in so much code where it is used, is useless. Get rid of it.