ext-encoding
ext-encoding copied to clipboard
ByteBuffer::__construct() behaviour is always surprising to one use case or the other
If the caller wants to read a ByteBuffer for reading, the offset should be 0, so that the bytes can be read from the beginning.
If the caller wants to write a ByteBuffer, the offset should be placed at the end of the input data, as if writeByteArray() was used.
Possible solutions:
- Make the offset parameter mandatory
- Make a parameter that indicates whether the offset should be at the beginning or the end (slightly easier to use than a mandatory offset parameter)
- Make the constructor private and have
::reader()and::writer()static factory functions - Split
ByteBufferinto aByteBufferReaderandByteBufferWriter - Separate read offset and write offset (effectively what
BinaryStreamwas implicitly doing)