コード例 #1
0
 byte[] getResponseData(HttpEntity entity) throws IOException {
   byte[] responseBody = null;
   if (entity != null) {
     InputStream instream = entity.getContent();
     if (instream != null) {
       long contentLength = entity.getContentLength();
       if (contentLength > Integer.MAX_VALUE) {
         throw new IllegalArgumentException("HTTP entity too large to be buffered in memory");
       }
       if (contentLength < 0) {
         contentLength = BUFFER_SIZE;
       }
       try {
         ByteArrayBuffer buffer = new ByteArrayBuffer((int) contentLength);
         try {
           byte[] tmp = new byte[BUFFER_SIZE];
           int l, count = 0;
           // do not send messages if request has been cancelled
           while ((l = instream.read(tmp)) != -1 && !Thread.currentThread().isInterrupted()) {
             count += l;
             buffer.append(tmp, 0, l);
             sendProgressMessage(count, (int) contentLength);
           }
         } finally {
           instream.close();
         }
         responseBody = buffer.toByteArray();
       } catch (OutOfMemoryError e) {
         System.gc();
         throw new IOException("File too large to fit into available memory");
       }
     }
   }
   return responseBody;
 }