简体   繁体   English

Scala中java.io.InputStream的替代品吗?

[英]Any alternatives to java.io.InputStream in Scala?

I am designing an API where one of the methods needs to provide a way to get file content. 我正在设计一种方法需要提供一种获取文件内容的方法的API。

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. 我也不想返回文件内容为Array [Byte],因为文件可能很大。

In Java I would create a method that returns an InputStream. 在Java中,我将创建一个返回InputStream的方法。

What is the recommended way of doing it in Scala? 在Scala中推荐的做法是什么?

在Scala中推荐的方法是使用InputStream而不是重新发明轮子。

Scala doesn't really have much in the way of I/O. Scala实际上并没有太多的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). 您可以返回一个Scala.io.Source ,它实际上是InputStream的包装器(几乎所有内容都必须包含,因为这是所有JVM程序均可访问的)。

There's an interesting Scala I/O library, not a standard library, based on Java 7 stuff, iirc. 基于Java 7的iirc,有一个有趣的Scala I / O库,而不是标准库。 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. 还有Scalaz Stream,这是最好的选择,尽管如果您要处理文件,它仍然在幕后使用InputStream

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM