gpt4 book ai didi

java - 从 byte[] 创建 StringBuilder

转载 作者:IT王子 更新时间:2023-10-28 23:38:47 26 4
gpt4 key购买 nike

有没有办法从 byte[] 创建一个 StringBuilder

我想使用 StringBuilder 来提高内存使用率,但我首先是一个 byte[],所以我必须创建一个 Stringbyte[] 然后从 String 创建 StringBuilder 我不认为这个解决方案是最佳的。

谢谢

最佳答案

基本上,您最好的选择似乎是使用 CharsetDecoder直接。

方法如下:

byte[] srcBytes = getYourSrcBytes();

//Whatever charset your bytes are endoded in
Charset charset = Charset.forName("UTF-8");
CharsetDecoder decoder = charset.newDecoder();

//ByteBuffer.wrap simply wraps the byte array, it does not allocate new memory for it
ByteBuffer srcBuffer = ByteBuffer.wrap(srcBytes);
//Now, we decode our srcBuffer into a new CharBuffer (yes, new memory allocated here, no can do)
CharBuffer resBuffer = decoder.decode(srcBuffer);

//CharBuffer implements CharSequence interface, which StringBuilder fully support in it's methods
StringBuilder yourStringBuilder = new StringBuilder(resBuffer);

添加:

经过一些测试,简单的 new String(bytes) 似乎要快得多,而且似乎没有比这更快的简单方法了。这是我运行的测试:

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.text.ParseException;

public class ConsoleMain {
public static void main(String[] args) throws IOException, ParseException {
StringBuilder sb1 = new StringBuilder("abcdefghijklmnopqrstuvwxyz");
for (int i=0;i<19;i++) {
sb1.append(sb1);
}
System.out.println("Size of buffer: "+sb1.length());
byte[] src = sb1.toString().getBytes("UTF-8");
StringBuilder res;

long startTime = System.currentTimeMillis();
res = testStringConvert(src);
System.out.println("Conversion using String time (msec): "+(System.currentTimeMillis()-startTime));
if (!res.toString().equals(sb1.toString())) {
System.err.println("Conversion error");
}

startTime = System.currentTimeMillis();
res = testCBConvert(src);
System.out.println("Conversion using CharBuffer time (msec): "+(System.currentTimeMillis()-startTime));
if (!res.toString().equals(sb1.toString())) {
System.err.println("Conversion error");
}
}

private static StringBuilder testStringConvert(byte[] src) throws UnsupportedEncodingException {
String s = new String(src, "UTF-8");
StringBuilder b = new StringBuilder(s);
return b;
}

private static StringBuilder testCBConvert(byte[] src) throws CharacterCodingException {
Charset charset = Charset.forName("UTF-8");
CharsetDecoder decoder = charset.newDecoder();
ByteBuffer srcBuffer = ByteBuffer.wrap(src);
CharBuffer resBuffer = decoder.decode(srcBuffer);
StringBuilder b = new StringBuilder(resBuffer);
return b;
}
}

结果:

Size of buffer: 13631488
Conversion using String time (msec): 91
Conversion using CharBuffer time (msec): 252

以及 IDEONE 上的修改(内存消耗更少)版本:Here .

关于java - 从 byte[] 创建 StringBuilder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11115082/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com