字符集编码中的内存不足



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

我该如何解决这个问题?

因为第一次启动应用程序时 内存错误 没有触发,并且只有第二次尝试启动它时,才会发生此错误。 我想知道是否需要从缓冲区中冲洗出来? 还是类似的东西?

如果你想分

块读取这个大文件,你可以像这样用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)
    }
}

最新更新