Ошибка нехватки памяти при помещении большого JSON (InputStream) в String

Я получаю сжатый JSON от веб-службы, а затем распаковываю его (размер распакованного JSON составляет 3,2 МБ). I need to transform received InputStream to String so i can then create JSONObject and parse it. I do it with this code:

public static String InputStreamToString(InputStream in) 
    throws IOException {

    BufferedInputStream bis = new BufferedInputStream(in);
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    int result = bis.read();

    while(result != -1) {
      byte b = (byte)result;
      buf.write(b);
      result = bis.read();
    }        
    return buf.toString();
}

I receive java.lang.OutOfMemoryError on the last line: "return buf.toString();" on the emulator and device with 288MB Ram.

What shall i do?

17
задан DixieFlatline 30 April 2011 в 13:55
поделиться