[英]What is the best way to store a byte array in memory
Somewhere in my server transaction I receive from some backend job a byte array that represents a pdf document. 在服务器事务中的某处,我从某个后端作业接收一个表示pdf文档的字节数组。
Later on in the transaction that pdf needs to be written to a client. 稍后在交易中,需要将pdf写入客户端。 But at the moment I have that byte array and I want to store it in memory until the pdf file needs to be written to the client.
但是目前,我有该字节数组,我想将其存储在内存中,直到需要将pdf文件写入客户端为止。
What is the best way to deal with a byte array? 处理字节数组的最佳方法是什么?
Create a modelobject and keep the byte array there as a plain byte array? 创建一个ModelObject并将字节数组保留为纯字节数组?
So for example like this: 因此,例如:
public class PdfDocument {
private byte[] bytearray;
private String pdftitle;
}
Or is there a better Java Object than just keeping it in memory as a plain byte array? 还是有比将其作为纯字节数组保留在内存中更好的Java对象? Like ByteBuffer or something else.
像ByteBuffer或其他东西。
Thanks 谢谢
It depends on how you receive the data and whether it's changed in some way: 这取决于您如何接收数据以及数据是否以某种方式更改:
PdfDocument
object itself). PdfDocument
对象本身仅需几个字节)。 You should take however into consideration whether the backend guarantees that it won't change the array later. 但是,您应该考虑后端是否保证它以后不会更改阵列。 If that's not the case, or if you want to be safe, you could create a copy with
Arrays.copyOf
: 如果不是这种情况,或者为了安全起见,可以使用
Arrays.copyOf
创建一个副本:
byte[] bytearray = Arrays.copyOf(bytearray, bytearray.length);
It seems like you're searching for something more fancy, but I guess your class is perfectly fine here. 似乎您正在寻找更花哨的东西,但我想您的课程在这里很好。
The variable "bytearray" is a reference to an object of class Array. 变量“ bytearray”是对Array类的对象的引用。 Reference takes up very little memory space.
引用占用很少的内存空间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.