Skip to content

[ SI-7514 ] Introduce Source.fromResource(...) method #4561

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

Merged
merged 1 commit into from
Jul 7, 2015
Merged

[ SI-7514 ] Introduce Source.fromResource(...) method #4561

merged 1 commit into from
Jul 7, 2015

Conversation

jdevelop
Copy link
Contributor

Creates Source from named classpath resource.

Simplifies

val src = io.Source.fromInputStream(classOf[ClassInst].getResourceAsStream("/name"))

to

val src = io.Source.fromClassPath("/name")

@som-snytt
Copy link
Contributor

This test is flaky:

Test suite finished with 1 case failing:
fail - scalap/t8679.scala  [output differs]% scalac t8679.scala

@jdevelop
Copy link
Contributor Author

@som-snytt I assume that this failing test has nothing related to regression caused by my PR?

@SethTisue
Copy link
Member

/rebuild

@jdevelop
Copy link
Contributor Author

Seems it's in a good shape now.

@adriaanm adriaanm mentioned this pull request Jun 24, 2015
@adriaanm
Copy link
Contributor

For the record, the build issues turned out to be a hotspot bug (see #4578 (comment)).

@adriaanm adriaanm self-assigned this Jun 29, 2015

def fromClassPath(resource: String, classLdr: ClassLoader = ClassLoader.getSystemClassLoader)(implicit codec: Codec): BufferedSource = {
val is = classLdr.getResourceAsStream(resource)
createBufferedSource(is, reset = () => fromInputStream(is)(codec), close = () => is.close())(codec)
Copy link
Contributor

Choose a reason for hiding this comment

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

Couldn't this be implemented more simply as fromInputStream(classLdr.getResourceAsStream(resource))?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ping @jdevelop. I'd like to avoid code duplication here, unless there's some compelling performance reason.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@adriaanm Updated, pls take a look. Thanks!

@adriaanm
Copy link
Contributor

adriaanm commented Jul 2, 2015

(almost -- see comments below) LGTM -- thanks for your contribution and the informative commit message!


def fromClassPath(resource: String, classLdr: ClassLoader = ClassLoader.getSystemClassLoader)(implicit codec: Codec): BufferedSource = {
fromInputStream(classLdr.getResourceAsStream(resource))(codec)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Extraneous brace alert! See previous line. I'm surprised it passes muster with Jenkins. He is very particular.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if it's in conflict with the current code style - I'll remove that. But as recent discoveries prove, a missing brace could cause some .. undesired behavior ;)

Copy link
Contributor

Choose a reason for hiding this comment

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

I wouldn't put that in braces, but I didn't want to cause friction here, since we hadn't been as prompt with our review as I'd have liked.

Copy link
Contributor

Choose a reason for hiding this comment

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

And by "we" I mean "me".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@adriaanm @som-snytt No problem with that, I removed the braces. :)

@adriaanm adriaanm removed the reviewed label Jul 6, 2015
@adriaanm
Copy link
Contributor

adriaanm commented Jul 6, 2015

Gentle reminder that we're cutting M2 this week, hopefully on Wednesday (sorry for the rush -- we're already running a bit behind schedule). If you get a chance, please update this PR so we can still consider it for inclusion. Otherwise, we'll have to move it to M3.

@jdevelop
Copy link
Contributor Author

jdevelop commented Jul 6, 2015

@adriaanm @SethTisue @som-snytt @dragos pls take a look

Thanks!

@adriaanm
Copy link
Contributor

adriaanm commented Jul 6, 2015

LGTM. Thanks for the quick turnaround!

@dragos
Copy link
Contributor

dragos commented Jul 7, 2015

LGTM

@@ -28,6 +28,10 @@ class SourceTest {
@Test def canIterateLines() = {
assertEquals(sampler.lines.size, (Source fromString sampler).getLines.size)
}
@Test def loadFromClassPath() = {
Copy link
Member

Choose a reason for hiding this comment

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

test name should be updated since the method name changed

@SethTisue
Copy link
Member

I'm going to ask for one more change on this: the new method should have Scaladoc. Other than that (and the test name, see comment above) I'm ready to hit "merge".

@SethTisue SethTisue changed the title [ SI-7514 ] Introduce Source.fromClassPath(resource) method [ SI-7514 ] Introduce Source.fromResource(...) method Jul 7, 2015
Creates Source from named classpath resource.

Simplifies

 val src = io.Source.fromInputStream(classOf[ClassInst].getResourceAsStream("/name"))

to

 val src = io.Source.fromClassPath("/name")
@jdevelop
Copy link
Contributor Author

jdevelop commented Jul 7, 2015

@SethTisue updated the test name, added some JavaDocs. Pease take a look.

@som-snytt
Copy link
Contributor

By the end of this PR, jdevelop will have lost about 10 kg. I think I saw SethTisue in a movie about boot camp. You will take this tooth brush and make this PR shine so I can see my reflection, is that clear, soldier? Yessir, yessir!

@jdevelop
Copy link
Contributor Author

jdevelop commented Jul 7, 2015

@som-snytt 👍

@adriaanm
Copy link
Contributor

adriaanm commented Jul 7, 2015

@jdevelop, you've certainly earned a medal for shiniest, most polished PR ✨

@adriaanm adriaanm added the release-notes worth highlighting in next release notes label Jul 7, 2015
adriaanm added a commit that referenced this pull request Jul 7, 2015
SI-7514 Introduce Source.fromResource(...) method
@adriaanm adriaanm merged commit b92c3af into scala:2.12.x Jul 7, 2015
@adriaanm
Copy link
Contributor

adriaanm commented Jul 7, 2015

cue 🎆!

@adriaanm adriaanm added the welcome hello new contributor! label Jul 7, 2015
@SethTisue
Copy link
Member

thank you @jdevelop!!

@adriaanm
Copy link
Contributor

adriaanm commented Jul 7, 2015

And of course there's a twist (or a tweest?) http://stackoverflow.com/questions/3121449/getclass-getclassloader-getresourceasstream-is-caching-the-resource (suggested on Twitter by @carlfish)

@carlfish
Copy link

carlfish commented Jul 7, 2015

Yeah, it's one of those nasty classloader wrinkles that nobody realises until you discover 50-100MB of your application footprint is made up of plugin files you loaded once, but are being held in memory forever. :)

@adriaanm
Copy link
Contributor

adriaanm commented Jul 7, 2015

It does look like BufferedSource's close method will call close on the underlying InputStream. Is that not enough?

@carlfish
Copy link

carlfish commented Jul 8, 2015

On further investigation, it seems this caching only happens in certain non-core classloaders, like the one in Tomcat web servers. Since this makes it somebody else's bad implementation decision, it might be worth ignoring after all.

@adriaanm
Copy link
Contributor

adriaanm commented Jul 8, 2015

Ok, thanks for looking into it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-notes worth highlighting in next release notes welcome hello new contributor!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants