gpt4 book ai didi

java - 字符集编码内存不足

转载 作者:行者123 更新时间:2023-12-02 07:26:07 30 4
gpt4 key购买 nike

我有一个非常大的ByteBuffer,大小约为几MB。当我运行 ByteBuffer

FileChannel fc = new FileInputStream(new File(decodedUri)).getChannel();
ByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
CharBuffer cb;
if (encoding == null)
encoding = "UTF-8";
cb = Charset.forName(encoding).decode(bb);

每隔一段时间,我都会收到一个OutOfMemoryError,堆栈跟踪可以追溯到Charset.decode。正是这一行触发了错误。

cb = Charset.forName(encoding).decode(bb);

如何解决这个问题?

因为第一次启动应用程序时不会触发OutOfMemoryError,只有第二次尝试启动它时才会出现此错误。我想知道是否需要对缓冲区进行某种刷新?或者类似的东西?

最佳答案

如果你想分块读取这个大文件,你可以使用 AsyncTask 来完成:

static class StreamTask extends AsyncTask<String, Void, Integer> {
private static final int BUFFER_LENGTH = 1024 * 8; // Adjust to taste

// Param #0 = file name
// Param #1 = charset name
@Override
protected Integer doInBackground(String... params) {

if (params.length != 2) {
throw new IllegalArgumentException();
}

int chars = 0;
CharsetDecoder cd = Charset.forName(params[1]).newDecoder();
try {
FileInputStream fin = new FileInputStream(params[0]);
try {
FileChannel fc = fin.getChannel();
ByteBuffer bb = ByteBuffer.allocateDirect(BUFFER_LENGTH);
CharBuffer cb = CharBuffer.allocate(BUFFER_LENGTH);

while (fc.read(bb) != -1) {
// Flip the buffer, decode the contents
bb.flip();
cd.decode(bb, cb, false); // You should probably look at CoderResult also.
// Flip & extract the decoded characters.
cb.flip();
chars += cb.remaining();
onCharacters(cb.array(), cb.position(), cb.remaining());
cb.clear();
// Prepare the buffer for reuse.
bb.compact();
}

// fc.read(..) returned -1 -> EOF, but bb may still contain
// stuff to decode.
bb.flip();
cd.decode(bb, cb, true);
cd.flush(cb);
cb.flip();
if (cb.remaining() > 0) {
chars += cb.remaining();
onCharacters(cb.array(), cb.position(), cb.remaining());
}
} finally {
fin.close();
}

} catch (IOException e) {
chars = -1;
}
return chars;
}


protected void onCharacters(char[] ch, int offset, int length) {
// Do something with the characters (still running in the AsyncTask thread)
}
}

关于java - 字符集编码内存不足,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13559889/

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