In the famous words :-) of Patrick Chan:
You might consider a more efficient implementation where
the client specifies a block size and the implementation creates
blocks as needed. It should never do copies.
Then have toByteArray create a single large array and copy in the blocks.
I'd also recommend another method which returns an enumeration of
the blocks to avoid the "new" and "copies" of toByteArray().
Want me to do it for you?
Also shouldnt write(int b) -> write(byte b)?