Instead of elementLength, use byteStream.size(). No need to track this yourself, as the byte stream does it for you.