gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:17:00 25 4
gpt4 key购买 nike

我使用以下代码将我的 ByteArray 转换为字符串:

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;
}

最佳答案

下面的 fragment 会帮助你。试着分块阅读

             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();

关于java - 将 ByteArray 转换为 String 时出现 OutOfMemory 异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11069802/

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