Skip to content

Commit

Permalink
Improve end-of-stream handling. (#255)
Browse files Browse the repository at this point in the history
* Improve end-of-stream handling.

As discussed in issue #247.

* Update docs/essentials/index.md.

Improve wording.

Co-authored-by: Maxim Schuwalow <16665913+mschuwalow@users.noreply.github.com>

* Update for latest master.

Co-authored-by: Maxim Schuwalow <16665913+mschuwalow@users.noreply.github.com>
Co-authored-by: Jakub Czuchnowski <jakub.czuchnowski@gmail.com>
  • Loading branch information
3 people authored and svroonland committed Aug 8, 2021
1 parent e237fd6 commit 5b42a99
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions docs/essentials/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,26 @@ val read100: ZIO[Blocking, Option[IOException], Chunk[Byte]] =

End-of-stream will be signalled with `None`. Any errors will be wrapped in `Some`.

### End-Of-Stream Handling

When reading from channels, the end of the stream may be reached at any time. This is indicated by the read effect failing with an `java.io.EOFException`. If you would prefer to explicitly represent the end-of-stream condition in the error channel, use the `eofCheck` extension method:

```scala mdoc:silent
import zio._
import zio.blocking.Blocking
import zio.nio._
import zio.nio.channels._
import zio.nio.file.Path
import java.io.IOException

val read100: ZIO[Blocking, Option[IOException], Chunk[Byte]] =
FileChannel.open(Path("foo.txt"))
.asSomeError
.use(_.readChunk(100).eofCheck)
```

End-of-stream will be signalled with `None`. Any errors will be wrapped in `Some`.

## References

- [ZIO github page](http://github.com/zio/zio)
Expand Down

0 comments on commit 5b42a99

Please sign in to comment.