1

I am designing an API where one of the methods needs to provide a way to get file content.

I do not want the method to return a file location as potentially files will be stored in the database. I also do not want to return file content as Array[Byte] as the files can be quite big.

In Java I would create a method that returns an InputStream.

What is the recommended way of doing it in Scala?

2
  • And why wouldn't a plain ole InputStream fit the bill? It's a pretty reasonable abstraction Commented May 25, 2015 at 8:54
  • InputStream would fit the bill. I am still learning Scala and very often find that there are many ways to achieve the same thing. For example akka-http doesn't use InputStreams. So I asked this question to find out what is the most Scala-way to do what I want without sacrificing performance. Commented May 25, 2015 at 9:05

2 Answers 2

2

Scala doesn't really have much in the way of I/O. You could return a Scala.io.Source, which is really a wrapper around InputStream (as pretty much anything will have to be, since that's all any JVM program has access to).

There's an interesting Scala I/O library, not a standard library, based on Java 7 stuff, iirc. And there's Scalaz Stream, which is the best thing out there, though it still uses an InputStream under the covers if you are handling files.

Sign up to request clarification or add additional context in comments.

1 Comment

io.Source looks more like java.io.Reader and works with character streams. I guess I will just use InputStream then. Thank you.
1

The recommended way of doing it in Scala is by using InputStream instead of reinventing the wheel.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.