-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inconsistent and confusing Sink/Source read/write-methods naming #137
Comments
Nice improvements! I’m a bit skeptical of
It doesn’t do this obviously, but I could be forgiven for expecting it. |
I also like |
@swankjesse Another option is to adopt |
Hey! Finally kotlinx-io is resurrecting! But, I believe that may be some more radical changes are needed. I could be wrong in some parts, as I was less involved in streaming IO comparing to working with fixed-size buffers. So here is my vision of core methods naming: For writing:
For reading:
Contract:
Some notes:
Questions (maybe better to move those to separate issues):
|
Right, probably unnecessary in Kotlin where a scoping function can trivially make your |
As for naming convention for read/write methods, we're gravitating towards the following:
This set of rules leads to following renaming:
|
Strong thumbs up on these naming improvements. |
Sink and Source provide methods to read and write data; for some of them, the behavior could be unclear from the name itself. The naming could be improved to make what to expect from different methods more obvious.
Here is a table describing methods and their behavior:
RawSource::read(sink: Buffer, bytesCount: Long): Long
byteCount
bytes intosink
and returns the number of bytes read, or-1
if the source is exhaustedSource::read(sink: ByteArray, offset: Int, byteCount: Int): Int
byteCount
bytes and writes it intosink
starting atoffset
, returns the number of bytes read, or-1
if the source is exhaustedSource::readFully(sink: RawSink, byteCount: Long): Unit
byteCount
bytes from the source and writes it intosink
, throws exception if there are not enough data in the sourceSource::readAll(sink: RawSink): Long
sink
, return the number of bytes readSource::readFully(sink: ByteArray)
sink.length
bytes from from the sourceSource::readByteArray()
Source::readByteArray(byteCount: Long): ByteArray
byteCount
bytes from the source into newly allocated arrayRawSink::write(source: Buffer, byteCount: Long)
byteCount
bytes fromsource
Sink::write(source: ByteArray, offset: Int, byteCount: Int): Sink
byteCount
bytes fromsource
starting fromoffset
Sink::writeAll(source: RawSource): Long
source
until its exhaustion and writes it to this sink, return the number of bytes writtenSink::write(source: RawSource, byteCount: Long): Sink
byteCount
bytes fromsource
and writes into this sink, throws whatever exceptionsource
will throw during the readThere are some inconsistencies in the behavior of read/write method pairs, as well as in their naming:
Source::read(sink: ByteArray, offset: Int, byteCount: Int): Int
is a unix-style read method that does not guarantee that the whole array's slice will be filled but instead returns how many bytes were actually read. Corresponding write-method,Sink::write(source: ByteArray, offset: Int, byteCount: Int): Sink
, unlike it's unix ancestor, writes the whole slice.Source::readFully(sink: RawSink, byteCount: Long): Unit
containsFully
in the name, it's write counterpart,Sink::write(source: RawSource, byteCount: Long): Sink
, doesn't.readAll
writewriteAll
methods, which do conceptually the same thing asreadFully/write
, but use a different naming convention.The behavior of
Source::read(sink: ByteArray, offset: Int, byteCount: Int): Int
andSink::write(source: ByteArray, offset: Int, byteCount: Int): Sink
mimicsjava.io.InputStream/OutputStream
, so leaving names and behavior without any changes might be OK. Also, there are extension functions to fill the whole array with data.However, other functions may have names allowing to distinguish them from each other and to provide a more obvious hint regarding expected behavior.
I looked at some other IO libraries, and here are the naming conventions adopted there:
I'm suggesting the following rename:
Source::read(sink: ByteArray, offset: Int, byteCount: Int): Int
Source::readFully(sink: RawSink, byteCount: Long): Unit
Source::readExactly(sink: RawSink, byteCount: Long): Unit
Source::readFully(sink: ByteArray)
Source::readExactly(sink: ByteArray)
Source::readAll(sink: RawSink): Long
Source::readAllTo(sink: RawSink): Long
Sink::write(source: ByteArray, offset: Int, byteCount: Int): Sink
Sink::writeAll(source: RawSource): Long
Sink::writeAllFrom(source: RawSource): Long
Sink::write(source: RawSource, byteCount: Long): Sink
Write methods, except
writeAll
, have the same behavior so they could have the same name;readExactly
better describes the operation compared toreadFully
;readAllTo
/writeAllFrom
additionally stress out the direction of an operation.Your opinion will be welcomed if you have better ideas regarding the naming.
The text was updated successfully, but these errors were encountered: