ktorio / ktor-io Goto Github PK
View Code? Open in Web Editor NEWCollection of IO primitives to work with network and files written in Kotlin using kotlinx.coroutines library
License: Apache License 2.0
Collection of IO primitives to work with network and files written in Kotlin using kotlinx.coroutines library
License: Apache License 2.0
In most of my use-cases, I need a read-only view of buffers (both Buffer and CompositeBuffer) with:
It seems from e5l/view-and-refcount and whyoleg/resources that we are taking the route of having reference-counted underlying storages. So the following design proposal is based on this idea.
As a first step, I would propose that we extract a ReadBuffer
interface from Buffer
containing:
var readIndex: Int
relative read index,val readLimit: Int
exclusive read limit index (always equal to writeIndex
in Buffer
's default implementation),fun read*()
relative read methods,fun get*At(...)
absolute read methods,fun copyTo*(...)
relative and absolute bulk read methods,fun duplicate(): ReadBuffer
method that returns a duplicate read buffer (with shared underlying storage but independent readIndex
),fun slice(...): ReadBuffer
methods that returns sliced duplicates.Additionally, we would add the following to the Buffer
interface:
val writeLimit: Int
for "symmetry" (always equal to capacity
)override fun duplicate(): Buffer
method that returns a duplicate buffer (with shared underlying storage but independent readIndex
and writeIndex
).As stated in design/BUFFER.md
The main purpose of IO is read and write arrays of bytes. There is a ByteArray - common Kotlin array of bytes abstraction, but it usually is not the efficient way to represent array of bytes on the platform(ByteBuffer on JVM, NSData on iOS, CPointer on Native, ArrayBuffer on Js and so on).
In multiplatform code, it will be ok to use just Buffer
, but in platform specific code, near interop with already written Java or Native code, we need to have access to underlying primitive, like ByteBuffer
on JVM and CPointer
or ByteArray
(which can be converted to CPointer
) on Native.
My use case is using JDK and OpenSSL for cryptography API, where JDK API need ByteArray or ByteBuffer and OpenSSL native API has CPointer. Of course, we may read ByteArray
from Buffer
and use it, and also we can create Buffer
from ByteArray
- but this will be not efficient as copies will be created.
I would think, that such a functionality should be hidden under some UnsafeBufferApi
(similar to DelicateCoroutinesApi
) opt-in annotation, as such code will be to easy to misuse.
Possible designs:
Buffer
implementation public with public property/function to access underlying primitiveByteBuffer
or ByteArray
or other primitive can be accessed both as ByteArray
and ByteBuffer
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.