将ByteArray转换为String时出现OutOfMemory异常?

7

我使用以下代码将我的ByteArray转换为String:

String sReturn = new String(byteArray, "UTF-8");

当ByteArray足够大时,我会遇到以下异常。有没有其他方法可以将ByteArray转换为String而不会出现内存溢出异常?
06-17 12:27:37.594: E/dalvikvm(1617): Out of memory: Heap Size=30663KB, Allocated=22087KB, Bitmap Size=936KB, Limit=32768KB
06-17 12:27:37.594: E/dalvikvm(1617): Extra info: Footprint=30663KB, Allowed Footprint=30663KB, Trimmed=616KB
06-17 12:27:37.594: W/dalvikvm(1617): threadid=9: thread exiting with uncaught exception (group=0x4001d648)
06-17 12:27:37.594: E/AndroidRuntime(1617): FATAL EXCEPTION: Thread-19
06-17 12:27:37.594: E/AndroidRuntime(1617): java.lang.OutOfMemoryError: (Heap Size=30663KB, Allocated=22087KB, Bitmap Size=936KB)
06-17 12:27:37.594: E/AndroidRuntime(1617):     at java.lang.String.<init>(String.java:422)
06-17 12:27:37.594: E/AndroidRuntime(1617):     at java.lang.String.<init>(String.java:276)
06-17 12:27:37.594: E/AndroidRuntime(1617):     at org.mabna.order.utils.Utilities.decompress(Utilities.java:389)
06-17 12:27:37.594: E/AndroidRuntime(1617):     at org.mabna.order.utils.WebserviceResponse.getClearedResponse(WebserviceResponse.java:18)
06-17 12:27:37.594: E/AndroidRuntime(1617):     at org.mabna.order.businessLayer.BoWebService.getDataForUpdate(BoWebService.java:216)
06-17 12:27:37.594: E/AndroidRuntime(1617):     at org.mabna.order.ui.ActToolDataExchange.threadGetDataForFullUpdate(ActToolDataExchange.java:389)
06-17 12:27:37.594: E/AndroidRuntime(1617):     at org.mabna.order.ui.ActToolDataExchange.access$9(ActToolDataExchange.java:380)
06-17 12:27:37.594: E/AndroidRuntime(1617):     at org.mabna.order.ui.ActToolDataExchange$35.run(ActToolDataExchange.java:639)
06-17 12:27:37.594: E/AndroidRuntime(1617):     at org.mabna.order.utils.Utilities$4.run(Utilities.java:924)

更新

public static String decompress(String zipText) throws IOException {
    byte[] compressed = Base64.decode(zipText);
    if (compressed.length > 4) {
        GZIPInputStream gzipInputStream = new GZIPInputStream(
                new ByteArrayInputStream(compressed, 4,
                        compressed.length - 4));

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        for (int value = 0; value != -1;) {
            value = gzipInputStream.read();
            if (value != -1) {
                baos.write(value);
            }
        }
        gzipInputStream.close();
        baos.close();

        byte[] byteArray = baos.toByteArray();

        Log.i("toByteArray", String.valueOf(byteArray.length));

        String sReturn = new String(byteArray, "UTF-8");

        return sReturn;
    } else {
        return "";
    }
}



public static String decrypt(String encrypted, String password)
        throws Exception {

    byte[] encrypteddata = Base64.decode(encrypted);

    byte[] bytes = decrypt(encrypteddata, password);

    String result = new String(bytes, "UTF-8");

    return result;
}

public static byte[] decrypt(byte[] encrypted, String password)
        throws Exception {

    byte[] passwordKey = encodeDigest(password);
    try {
        aesCipher = Cipher.getInstance(CIPHER_TRANSFORMATION);
    } catch (NoSuchAlgorithmException e) {
        throw new Exception(
                "Decryption Exception: No such algorithm\r\n" + e
                        .toString());
    } catch (NoSuchPaddingException e) {
        throw new Exception(
                "Decryption Exception: No such padding PKCS5\r\n" + e
                        .toString());
    }
    secretKey = new SecretKeySpec(passwordKey, CIPHER_ALGORITHM);

    try {
        aesCipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
    } catch (InvalidKeyException e) {
        throw new Exception(
                "Decryption Exception: Invalid key\r\n" + e.toString());
    } catch (InvalidAlgorithmParameterException e) {
        throw new Exception(
                "Decryption Exception: Invalid algorithm\r\n" + e
                        .toString());
    }

    byte[] encryptedData;
    try {
        encryptedData = aesCipher.doFinal(encrypted);
    } catch (IllegalBlockSizeException e) {
        throw new Exception(
                "Decryption Exception: Illegal block size\r\n" + e
                        .toString());
    } catch (BadPaddingException e) {
        throw new Exception(
                "Decryption Exception: Bad padding\r\n" + e
                        .toString());
    }
    return encryptedData;
}

1
字符串的大小会是多少?如果非常大,您确定要将其作为一个字符串吗? - Denys Séguret
3
你打算以后用那个字符串做什么?也许你可以返回一个流。 - Vic
2
如果是gzip解压缩,您需要使用GZIPInputStream,而且确实需要使用输入流和读取器。在org.mabna.order.utils.Utilities.decompress方法中实现。 - Dmitry Polishuk
能否合并解密和解压缩方法? - Bobs
我不会同时拥有3.5M字节数组和3.5M字符串,更不用说像你一样尝试同时拥有它们了。我会找到一种方法来处理输入。延迟更少,内存更少。 - user207421
显示剩余7条评论
3个回答

1
以下代码片段将对您有所帮助。尝试分块阅读。
             StringBuilder sb=new StringBuilder();  

             Log.v("abc","length : " + byteArray.length);  

             while (totalBytesRead < formDataLength) {    
                 byteRead = in.read(dataBytes, totalBytesRead, formDataLength);    
                 // byteRead = in.read(dataBytes);    
                 //totalBytesRead += byteRead;    
                 sb.append((char)byteRead);  
             }    

              String s=sb.toString(); 

1
除非你不明智,否则不要处理3MB的字符串。 - Denys Séguret
我的字符串是Unicode编码的。使用sb.append((char)byteRead)是正确的吗? - Bobs

0

你在过度分配内存——首先你解压base64并为此分配缓冲区,然后将其解压并分块写入BAOS(它正在分配和重新分配内存块),然后你再次将其复制到字符串中——难怪会耗尽内存。

试着重写为流处理过程(有流式base64解码器,以及gzip解码器)


0

我会将其分成每次1000个字符的字符串。 3663125字节是很多内存,特别是对于Android。

ArrayList<String> strings = new ArrayList<String>();
byte[] set = new byte[1000];
for(int x = 0, index = 0; x < byteArray.length;x++, index++)
{
    set[index] = byteArray[x];
    if(index == 999)
    {
        strings.add(new String(set, "Unicode"));
        index = 0;
        if(byteArray.length - x < 1000) // shorten the set when there are less than 1000 bytes left
            set = new byte[byteArray.length - x];
   }
}

strings.add(new String(set, "Unicode"));

String stringArray[] = (String[])strings.toArray();

这将为您分解它,如果1000太小,您可以更改为任何您想要的大小。


我的字符串是Unicode。使用string = string + ((char)byteArray[x]);是正确的吗? - Bobs
@breceivemail,请查看我的编辑,我已更改它,以便使用 Unicode 转换字节。 - John
在 Unicode 字符串中,当字节数组被分割时,可能会出现一些不良的数据转换。 - Bobs
在 Unicode 中:[1,2,3,4,5] -> 原始字符串,[1,2] -> 字符串1,[3,4,5] -> 字符串2,原始字符串 <> 字符串1 + 字符串2 - Bobs
在将其转换为ByteArray之前,ByteArray来自哪里?您可以在将其转换为ByteArray之前将其拆分。 - John
这是一个压缩的字符串。它无法被分割。 - Bobs

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接