使用Java创建字节数组,其大小由重新长psented $ P $ [英] Java creating byte array whose size is represented by a long

查看:170
本文介绍了使用Java创建字节数组,其大小由重新长psented $ P $的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个字节数组,其大小为键入的。例如,认为它是:

I'm trying to create a byte array whose size is of type long. For example, think of it as:

long x = _________;
byte[] b = new byte[x]; 

显然,你只能指定一个 INT 的字节数组的大小。

在有人问我为什么会需要一个字节数组如此之大,我会说我需要封装,我不写的消息格式的数据,而这些消息类型之一有一个unsigned int型的长度(在Java中)。

Before anyone asks why I would need a byte array so large, I'll say I need to encapsulate data of message formats that I am not writing, and one of these message types has a length of an unsigned int (long in Java).

有没有一种方法来创建此字节数组?

Is there a way to create this byte array?

我在想,如果有没有办法解决它,我可以创建一个字节数组输出流,并保持喂养它字节,但我不知道是否有一个大小的字节数组的任何限制......

I am thinking if there's no way around it, I can create a byte array output stream and keep feeding it bytes, but I don't know if there's any restriction on a size of a byte array...

推荐答案

(这可能是一个有点晚了OP,但它可能仍然是对别人有用)

不幸的是Java不支持数组超过2 31 -1元素。最大的消费空间2吉布为字节[] 阵列,或者空间16吉布为长[] 阵列。

Unfortunately Java does not support arrays with more than 231−1 elements. The maximum consumption is 2 GiB of space for a byte[] array, or 16 GiB of space for a long[] array.

虽然它可能是不适用在这种情况下,如果阵列将是稀疏 ,你也许可以用一个关联的数据结构像的 地图 以相互匹配使用的胶印为适当的值。此外,特罗韦提供存储比标准的Java集合的原始值的内存更有效的实现。

While it is probably not applicable in this case, if the array is going to be sparse, you might be able to get away with using an associative data structure like a Map to match each used offset to the appropriate value. In addition, Trove provides an more memory-efficient implementation for storing primitive values than standard Java collections.

如果数组不疏,你真的,真的需要在内存中的整个BLOB,你可能会使用一个二维结构,例如以地图匹配偏移模1024至适当的1024字节数组。这种方法可能会更高效的内存即使是稀疏数组,因为相邻的单元格填充可以共享相同的地图项。

If the array is not sparse and you really, really do need the whole blob in memory, you will probably have to use a two-dimensional structure, e.g. with a Map matching offsets modulo 1024 to the proper 1024-byte array. This approach might be be more memory efficient even for sparse arrays, since adjacent filled cells can share the same Map entry.

这篇关于使用Java创建字节数组,其大小由重新长psented $ P $的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆