Skip to content
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

Pass InputStream to Protobuf.load #693

Closed
fsantanna opened this issue Feb 6, 2020 · 6 comments
Closed

Pass InputStream to Protobuf.load #693

fsantanna opened this issue Feb 6, 2020 · 6 comments

Comments

@fsantanna
Copy link

What is your use-case and why do you need this feature?

AFAIU, currently I can only to pass a ByteArray when deserializing data.
I wonder if it would be possible to pass an InputStream instead.

I want to deserialize a variable-size data structure coming from the network, so I don't know the exact size beforehand to create a ByteArray.

(I'm sorry if I missed some important information about the design that invalidates this question.)

Describe the solution you'd like

@fsantanna
Copy link
Author

Forgot to mention that my current solution is to serialize the data structure to a ByteArray, get its resulting size, transmit the size in separate and than transmit the buffer.

@Dominaezzz
Copy link
Contributor

Not a direct answer but is relevant. #204 and #253

@sandwwraith
Copy link
Member

sandwwraith commented Feb 7, 2020

It all will be resolved after #253 – formats would operate kotlinx.io.Input and kotlinx.io.Output. Unfortunately, kotlinx.io is not mature enough (yet).

@AlexanderBykin
Copy link

@fsantanna
hi, can you please share example of how do you use protobuf with serialization/deserialization?
don't understand why it is missed at sample sources

@fsantanna
Copy link
Author

@AlexanderShniperson ,

This is how I'm doing:

import kotlinx.serialization.protobuf.ProtoBuf

@Serializable
data class MyData (...)

val myData = MyData(...)

// from data to bytearray
val bytes: ByteArray = ProtoBuf.dump(MyData.serializer(), myData)

// from bytearray back to data
val myData2 = ProtoBuf.load(MyData.serializer(), bytes)

It's actually very straightforward.

@qwwdfsad
Copy link
Contributor

Closing as duplicate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants