如何获取 DataSource
消耗的物理内存量(具体来说 - ByteArrayDataSource
)?我使用 javax.mail.util.ByteArrayDataSource (byte[] bytes, String type) 构造函数,其中我得到如下所示的字节:
String str = "test";
byte[] bytes = str.getBytes();
那是str.length()
(以字节为单位)吗?还有其他想法吗?
最佳答案
str.length() 将给出字符数
bytes.length 将给出实际的字节数。
根据您的 Activity 字符集,这可能相同也可能不同。有些字符被编码为多个字节。
示例:
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
关于javax.activation.DataSource大小问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2957145/