Open fzhinkin opened 1 year ago
It's also worth revisiting names for JVM-specific Buffer
extensions as some of them are confusing (compared to Source
/Sink
, where operations are more obvious, thanks to type names), like:
Buffer.readTo(out: OutputStream, byteCount: Long = size)
Buffer.write(input: InputStream, byteCount: Long): Buffer
We may need to add some currently missing extensions and change the receiver of others to one of Buffer
's interfaces - Sink
or Source
.
Some observations from looking on API doc:
Buffer
(or interfaces) also accepts byteCount
(readTo
/readAtMostTo
/write
)ByteArray
also accepts startIndex
and endIndex
(readTo
/readAtMostTo
/write
)copyTo
which accepts ByteArray
or ByteBuffer
copyTo
/readTo
/transferTo
which accepts ByteBuffer
, only readAtMostTo
/write
/transferFrom
ByteBuffer
doesn't have any other arguments - may be provide byteCount=byteBuffer.remaining
or even start/end indices - it depends on how ByteBuffer
is used (relative vs absolute) accessOverall, there are following core functions to work with bulks of memory: readTo
/readAtMostTo
/write
/copyTo
/transferTo
/transferFrom
- ideally I think we should provide all of them for each primitive (where applicable), be it Buffer
(and interfaces), ByteArray
, ByteBuffer
, InputStream
/OutputStream
and may be ByteChannel
(ByteWriteChannel
/ByteReadChannel
). But then it can be too much :)
Buffer.smth
and Source.smth
but do a check is Buffer
and use fast path if so because I believe most of the APIs will use Source/Sink
while under the hood they could have Buffer
. This could also reduce public API a little. Also this will reduce confusion when there is declaration like Buffer.**(Buffer/ByteBuffer)
as they will become Source.**
and Sink.**
which is additionally directly shows in which direction operation goes, and so only Buffer.copyTo
will be additional functionality that is specific to Buffer
Currently (https://github.com/Kotlin/kotlinx-io/pull/136), kotlinx-io will only provide a bare minimum of extensions required for interop with Java-specific APIs:
j.i.InputStream
andj.n.ReadableByteChannel
intoSource
.j.o.OutputStream
andj.n.WritableByteChannel
intoSink
.j.n.ByteBuffer
andjava.io
streams.At the moment, it seems like interaction with most other core Java APIs could be performed via
java.io
streams orjava.nio
channels and buffers, so it should be sufficient to cover most cases.This issue opened to initiate a discussion on which extensions should be added additionally, not only for JVM but for other targets too.