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

Fix ZStream.fromInputStream #3678

Merged
merged 2 commits into from May 21, 2020
Merged

Fix ZStream.fromInputStream #3678

merged 2 commits into from May 21, 2020

Conversation

iravid
Copy link
Member

@iravid iravid commented May 21, 2020

@@ -767,6 +767,8 @@ object Chunk {

/**
* Returns a chunk backed by an array.
*
* WARNING: The array must not be mutated after creating the chunk.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it would make sense to have a naming convention to serve the same purpose as this warning. Chunk.fromArray could copy by default and then the alternate "unsafe" one would be the fast version. Depends if there is already a precedent for that in the ZIO codebase.

Also when seeing this, I also thought about if there might be a use for a Chunk.fromArray[A](array: Array[A], start: Int, len: Int) version.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@reibitto I like the slicing version! Could be a nice PR if you have the cycles.

Re the naming: I think a warning is sufficient because we're not going to add the other variant. The caller can always defensively copy if required.

@simpadjo
Copy link
Contributor

simpadjo commented May 21, 2020

Maybe I'm too late for the party, but here is my take:

ZStream.managed(ZManaged.effectTotal(is)).flatMap(capturedIs => {
      ZStream.fromEffect(
        blocking.effectBlocking({
          val buf = Array.ofDim[Byte](chunkSize)
          val res = capturedIs.read(buf)
          if(res < 0) {
            Chunk.empty
          } else{
            val arr = if(res < chunkSize){
              buf.take(res)
            } else buf
            Chunk.fromArray(arr)
          }
        }).refineToOrDie[IOException]).takeWhile(_.nonEmpty).flattenChunks
    })

@iravid iravid merged commit 0a169d2 into zio:master May 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants