2012-06-17 10 views
6

मैं स्ट्रिंग के लिए अपनी bytearray परिवर्तित करने के लिए निम्न कोड का उपयोग करें:बाइटएरे से स्ट्रिंग में कनवर्ट करने में आउटऑफमेमरी अपवाद?

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

लेकिन जब bytearray काफी बड़ी है मैं निम्न अपवाद मिलता है।

क्या बाइटएरे से स्ट्रिंग को आउट ऑफ़ मेमोरी अपवाद के रूप में परिवर्तित करने का कोई अन्य तरीका है?

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

क्या आकार स्ट्रिंग होगा (बेस 64 dcoding, साथ ही gzip विकोडक वहाँ srteaming जाता है) को यह पुनर्लेखन के लिए? यदि यह बहुत बड़ा है, तो क्या आप वाकई इसे एक स्ट्रिंग के रूप में रखना चाहते हैं? –

+0

क्या आप बाइटएरे के आकार को जानते हैं जो अपवाद को फेंकने का कारण बनता है? रूपांतरण से पहले बाइटएरे के आकार की जांच करना संभव होगा और यदि इस महत्वपूर्ण आकार पर, छोटे चरणों में परिवर्तित हो सकता है? – Kerry

+0

बाइटएरे का आकार 3663125 बाइट्स – breceivemail

उत्तर

0

टुकड़ा में मदद मिलेगी you.Try मात्रा में पढ़ने के लिए

   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

है कि यह 3 एमबी स्ट्रिंग को संभालने के लिए बुद्धिमान नहीं है। –

+0

** ** में ** क्या है? – breceivemail

+0

मेरी स्ट्रिंग यूनिकोड है। क्या यह सच है ti उपयोग 'sb.append ((char) byteRead); '? – breceivemail

0

मैं 1000 एक तरह चार तार में यह टूट जाएगा के बाद एक वक़्त। 3663125 बाइट्स विशेष रूप से एंड्रॉइड के लिए बहुत मेमोरी है।

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 को बदल सकते हैं जो आप चाहते हैं कि यह बहुत छोटा हो।

+0

मेरी स्ट्रिंग यूनिकोड है। क्या यह सही टीआई 'स्ट्रिंग = स्ट्रिंग + ((char) बाइटएरे [x]) का उपयोग करता है; '? – breceivemail

+0

@breceivemail मेरा संपादन देखें, मैंने इसे बदल दिया ताकि बाइट्स यूनिकोड का उपयोग करके परिवर्तित हो जाएंगे। – John

+0

यूनिकोड स्ट्रिंग में हमारे पास कुछ खराब डेटा रूपांतरण होता है जब बाइटएरे को विभाजित किया जाता है। – breceivemail

0

आप स्मृति को आवंटित कर रहे हैं - पहले आप बेस 64 को डिक्रॉप करते हैं और इसके लिए बफर आवंटित करते हैं, फिर आप इसे डीकंप्रेस करते हैं और बीओओएस (जो स्मृति के हिस्सों को आवंटित और पुन: आवंटित कर रहे हैं) में लिखते हैं और आप इसे फिर से स्ट्रिंग में कॉपी करते हैं - कोई आश्चर्य नहीं कि यह स्मृति से बाहर चला जाता है।

कोशिश प्रक्रिया स्ट्रीमिंग

संबंधित मुद्दे