Java 项目模块 - 使用 InputStream/OutputStream 或 .tmpFile/byte[]

发布于 2024-08-11 04:20:53 字数 554 浏览 3 评论 0原文

我发现自己在我的应用程序模块中传递了 InputStream/OutputStream 对象。

我想知道是否更好
- 将内容保存到磁盘并传递类似 各种方法调用之间的资源
- 使用 byte[] 数组
而不必每次都处理流。

在这些情况下您的方法是什么?
谢谢

编辑:
我有一个接收用户上传的文件的控制器。我有一个实用程序模块,它提供一些渲染文件的功能。

utilityMethod(InputStream is, OutputStream os)

InputStream中的文件是用户上传的文件。 os 是与响应关联的流。我想知道是否最好使用实用程序方法将生成的文件保存在 .tmp 文件中并返回文件路径或 byte[] 等,并让控制器直接处理outputStream。

I found myself passing InputStream/OutputStream objects around my application modules.

I'm wondering if it's better to
- save the content to disk and pass something like a Resource between the various methods calls
- use a byte[] array
instead of having to deal with streams everytime.

What's your approach in these situations?
Thanks

Edit:
I've a Controller that receives a file uploaded by the user. I've an utility module that provides some functionality to render a file.

utilityMethod(InputStream is, OutputStream os)

The file in InputStream is the one uploaded by the user. os is the stream associated with the response. I'm wondering if it's better to have the utility method to save the generated file in a .tmp file and return the file path, or a byte[], etc. and have the controller to deal with the outputStream directly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情泪▽动烟 2024-08-18 04:20:53

我尝试在 RAM 中保留尽可能多的内容(主要是因为性能原因,而且 RAM 很便宜)。因此,我使用 FileBackedBuffer 来“保存”未知大小的数据。它有一个限制。当写入的字节数少于 limit 时,它会将它们保留在内部缓冲区中。如果写入更多数据,我将创建实际文件。此类具有从中获取 InputStreamOutputStream 的方法,因此使用代码不必担心琐碎的细节。

I try to keep as much in RAM as possible (mostly because of performance reasons and RAM is cheap). So I'm using a FileBackedBuffer to "save" data of unknown size. It has a limit. When less than limit bytes are written to it, it will keep them in an internal buffer. If more data is written, I'll create the actual file. This class has methods to get an InputStream and an OutputStream from it, so the using code isn't bothered with the petty details.

謌踐踏愛綪 2024-08-18 04:20:53

答案实际上取决于问题的背景,而我们并不知道。

因此,想象一下最通用的情况,我会创建两个抽象。第一个抽象将采用InputStream/OutputStream 作为参数,而另一个抽象将采用byte[]。
接受流的人可以读取数据并将其传递给 byte[] 实现。因此,现在您的用户可以根据自己的需求/舒适度使用流抽象和 byte[] 抽象。

The answer actually depends on the context of the problem, which we dont know.

So, imagining the most generic case, I would create two abstractions. The first abstraction would take InputStream/OutputStream as parameters, whereas the other would take byte[].
The one that takes streams can read and pass the data to the byte[] implementation. So now your users can use both the stream abstraction and byte[] abstraction based on thier needs/comfort.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文