javax.activation.DataSource大小问题
如何获取 DataSource
(具体来说 - ByteArrayDataSource
)?我使用 javax.mail.util.ByteArrayDataSource (byte[] bytes, String type) 构造函数,其中我得到如下所示的字节:
String str = "test";
byte[] bytes = str.getBytes();
那是 str.length () 以字节为单位?还有其他想法吗?
How do I get the amount of physical memory, consumed by the DataSource
(specifically — ByteArrayDataSource
)? I use javax.mail.util.ByteArrayDataSource (byte[] bytes, String type)
constructor, where I get bytes
like this:
String str = "test";
byte[] bytes = str.getBytes();
Would that be str.length()
in bytes? Any other ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
str.length() 将给出字节数
。length 将给出实际字节数。
根据您的活动字符集,这可能相同也可能不同。有些字符被编码为多个字节。
示例:
返回
4
4
1
4
str.length() will give the number of characters
bytes.length will give the actual number of bytes.
Depending on your active character set this may or may not be the same. Some characters are encoded as multiple bytes.
example :
returns
4
4
1
4
目前尚不清楚你在问什么。
byte[]
消耗的字节数就是它的长度:bytes.length
Java
String
在内存中消耗的字节数是每个字符 2 个字节,因为在内存中它被编码为 UCS-2:2*str.length()
当序列化为
byte[]
时,String
消耗的字节数取决于您选择的字符编码。您必须将其序列化并检查。但你真正想做什么?
It's not clear what you're asking.
The number of bytes consumed by a
byte[]
is just its length:bytes.length
The number of bytes consumed, in memory, by a Java
String
, is 2 bytes per character, since in memory it is encoded as UCS-2:2*str.length()
The number of bytes consumed by a
String
, when serialized to abyte[]
, depends on the character encoding you select. You would have to serialize it and check.But what are you really trying to do?