# RESOLVED: Converting a file (or InputStream in general) to Sequence (of bytes or chars)

**URL:** <https://discuss.kotlinlang.org/t/resolved-converting-a-file-or-inputstream-in-general-to-sequence-of-bytes-or-chars/1359>\
**Category:** Libraries\
**Created:** [December 14, 2015, 4:16pm UTC](https://discuss.kotlinlang.org/t/resolved-converting-a-file-or-inputstream-in-general-to-sequence-of-bytes-or-chars/1359 "2015-12-14T16:16:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![m.h.shams](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/m.h.shams/32/1482_2.png) [@m.h.shams](https://discuss.kotlinlang.org/u/m.h.shams)\
**Post date:** [December 14, 2015, 4:16pm UTC](https://discuss.kotlinlang.org/t/resolved-converting-a-file-or-inputstream-in-general-to-sequence-of-bytes-or-chars/1359/1 "2015-12-14T16:16:23Z")

</div>

Hi,

If I’ve understood it correctly, one should be able to convert a file (or input steam) to a sequence by following function:

`fun <T : Any> sequence(nextFunction: () -> T?): Sequence<T> ()` ([kotlin doc](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/sequence.html#sequence))

This will require to pass a function to read bytes from the stream or file , one byte at a time.

The question is, does it make sense to have an extension function (or maybe member function) in File and InputStream to private such Sequence instance ? (I mean in Kotlin standard library)

Cheers,

---

<div class="post-metadata">

**Author:** ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)\
**Post date:** [December 14, 2015, 5:36pm UTC](https://discuss.kotlinlang.org/t/resolved-converting-a-file-or-inputstream-in-general-to-sequence-of-bytes-or-chars/1359/2 "2015-12-14T17:36:40Z")

</div>

There is the `iterator` extension function for `BufferedInputStream` which allows to iterate through such streams. You can then convert such iterator to a one-time consumable sequence with the `asSequence` extension:

`stream.iterator().asSequence()`

---

<div class="post-metadata">

**Author:** ![m.h.shams](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/m.h.shams/32/1482_2.png) [@m.h.shams](https://discuss.kotlinlang.org/u/m.h.shams)\
**Post date:** [December 14, 2015, 9:36pm UTC](https://discuss.kotlinlang.org/t/resolved-converting-a-file-or-inputstream-in-general-to-sequence-of-bytes-or-chars/1359/3 "2015-12-14T21:36:36Z")

</div>

Thanks, That was exactly what I needed.

---

<div class="post-metadata">

**Author:** ![sergey.mashkov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/sergey.mashkov/32/1678_2.png) [@sergey.mashkov](https://discuss.kotlinlang.org/u/sergey.mashkov)\
**Post date:** [December 21, 2015, 6:19pm UTC](https://discuss.kotlinlang.org/t/resolved-converting-a-file-or-inputstream-in-general-to-sequence-of-bytes-or-chars/1359/4 "2015-12-21T18:19:19Z")

</div>

Be ready to poor performance so in some cases (huge files or heavy load) it may work dramatically slow. In spite of the fact that you are iterating over the buffered stream it is still slow because:

- boxing/unboxing for each byte (works faster than for `Int` but takes time anyway)
- per-byte handling in most cases slower than batch handling

So I believe there are only two reasons to do so:

- the files volume is low (you handle only few small files, perhaps only once)
- the underlying logic is per-byte anyway and it’s relatively slow (much more expensive than boxing, `Sequence.next`, `Iterator.next` calls and so on)

---

<div class="post-metadata">

**Author:** ![m.h.shams](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/m.h.shams/32/1482_2.png) [@m.h.shams](https://discuss.kotlinlang.org/u/m.h.shams)\
**Post date:** [December 21, 2015, 9:50pm UTC](https://discuss.kotlinlang.org/t/resolved-converting-a-file-or-inputstream-in-general-to-sequence-of-bytes-or-chars/1359/5 "2015-12-21T21:50:52Z")

</div>

Thanks for the hint.

I need to read the data in small chunks (4 bytes) and depending on some conditions the code should decide to continue or stop.

given that the sequence is taken from buffered stream, it shouldn’t impact the performance that much.
