javax.activation.DataSource大小问题

发布于 2024-09-04 09:39:24 字数 580 浏览 8 评论 0原文

如何获取 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 技术交流群。

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

发布评论

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

评论(2

¢好甜 2024-09-11 09:39:24

str.length() 将给出字节数

。length 将给出实际字节数。

根据您的活动字符集,这可能相同也可能不同。有些字符被编码为多个字节。

示例:

public static void main(String[] args) {
    String s1 = "test";
    byte[] b1 = s1.getBytes();
    System.out.println(s1.length());
    System.out.println(b1.length);

    String s2 = "\u0177";
    byte[] b2 = s1.getBytes();
    System.out.println(s2.length());
    System.out.println(b2.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 :

public static void main(String[] args) {
    String s1 = "test";
    byte[] b1 = s1.getBytes();
    System.out.println(s1.length());
    System.out.println(b1.length);

    String s2 = "\u0177";
    byte[] b2 = s1.getBytes();
    System.out.println(s2.length());
    System.out.println(b2.length);

}

returns

4
4
1
4

涫野音 2024-09-11 09:39:24

目前尚不清楚你在问什么。

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 a byte[], depends on the character encoding you select. You would have to serialize it and check.

But what are you really trying to do?

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