ext-encoding icon indicating copy to clipboard operation
ext-encoding copied to clipboard

ByteBuffer::__construct() behaviour is always surprising to one use case or the other

Open dktapps opened this issue 1 year ago • 0 comments

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 ByteBuffer into a ByteBufferReader and ByteBufferWriter
  • Separate read offset and write offset (effectively what BinaryStream was implicitly doing)

dktapps avatar Jan 29 '24 19:01 dktapps