Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Implement io.readClassResource and io.readClassLoaderResource #2733

Merged
merged 5 commits into from
Dec 2, 2021

Conversation

armanbilge
Copy link
Member

Addresses #2503.

chunkSize: Int,
classLoader: ClassLoader = getClass().getClassLoader()
)(implicit F: Sync[F]): Stream[F, Byte] =
Stream.eval(F.delay(Option(classLoader.getResourceAsStream(name)))).flatMap {
Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe this should be blocking 🤔

@mpilquist
Copy link
Member

It might be nice to do this in a way which can use Files if the resource is a jar:file URL. We don't currently allow it because we have no way to construct a java.nio.file.Path via a URI.

BTW, we should also note that the resource path is absolute, not relative to a class. E.g., classOf[Foo].getResource("foo.properties") vs classOf[Foo].getClassLoader.getResource("a/b/c/foo.properties")

@armanbilge
Copy link
Member Author

It might be nice to do this in a way which can use Files if the resource is a jar:file URL.

How would that work exactly? Do you mean use Files to implement this method? Or do you mean add a method to the Files API which works with a URL instead of Path?

For the former, I'm not sure if that's possible, since the file is embedded in the jar right? Seems like JavaURLConnection would be how to interact with those.

@mpilquist
Copy link
Member

Yeah okay, I forgot that there's no default jar: file system handler. I wanted a way to do this without going through InputStream but I guess that's not possible.

@armanbilge
Copy link
Member Author

Ohh, no I think I got you. Maybe this is possible, using FileSystems.newFileSystem? Would this actually make a difference?

@mpilquist
Copy link
Member

Right, we'd have to call newFileSystem with a path to the JAR and then create a java.nio.file.Path from that file system for the resource in the JAR. And we'd likely still need a fallback to current implementation. Probably best to stick with what you've got -- the original fs2 InputStream integration seemed to have high overhead but I'm probably worried about an implementation that no longer exists.

def readClassResource[F[_], C](
name: String,
chunkSize: Int
)(implicit F: Sync[F], ct: ClassTag[C]): Stream[F, Byte] =
Copy link
Member Author

Choose a reason for hiding this comment

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

H/t @Daenyth for the classtag trick.

@mpilquist mpilquist merged commit 594f8fe into typelevel:main Dec 2, 2021
@mpilquist mpilquist changed the title Implement io.readResource Implement io.readClassResource and io.readClassLoaderResource Dec 2, 2021
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants