开发者

Java Project Modules - use InputStream/OutputStream or .tmpFile/byte[]

开发者 https://www.devze.com 2022-12-12 03:11 出处:网络
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 Resourcebetween the variou

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 th开发者_运维技巧e controller to deal with the outputStream directly.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消