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


#1

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)

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,


#2

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()


#3

Thanks, That was exactly what I needed.


#4

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)

#5

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.