字符集编码中的内存不足

字符集编码中的内存不足

我有一个非常大的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/

10-09 01:56