title: How do I read HttpEntity without consuming it?
body: <p>I've got <code>org.apache.http.HttpResponse</code> object, which I'm using at different places in the code. One of those places is for logging.</p>
  
  <p>The problem is that when I run following log code:</p>
  
  <pre><code>HttpEntity entity = response.getEntity();
  try {
      String content = Base64.encodeToString(
              EntityUtils.toByteArray(entity), Base64.DEFAULT);
      sb.append(content + "\r\n");
  } catch (Exception e) {
      sb.append("\r\n\r\n====EXCEPTION=====\r\n" + e.toString()
              + "\r\n");
  }
  </code></pre>
  
  <p>and than I try to read entry content in the actual processing code, that causes the code to throw following exception: </p>
  
  <pre><code>java.lang.IllegalStateException: Content has been consumed
  </code></pre>
  
  <p>My question is: how do I read the entity without consuming it in the log code?</p>
  
  <p><strong>UPDATE</strong>
  here's the full code of the function I use to transform httpresponse to string:</p>
  
  <pre><code>static String toString(org.apache.http.HttpResponse response) {
      try {
          if (response == null) {
              return "null";
          }
  
          StringBuilder sb = new StringBuilder();
          sb.append("==============BEGIN HttpResponse================\r\n");
          StatusLine sl = response.getStatusLine();
          if (sl == null) {
              sb.append("status line is null\r\n");
          } else {
              sb.append(String.format("%s %s\r\n", sl.getStatusCode(),
                      sl.getReasonPhrase()));
          }
  
          for (Header h : response.getAllHeaders()) {
              if (h == null) {
                  sb.append("header is null\r\n");
                  continue;
              }
              sb.append(String.format("%s: %s\r\n", h.getName(), h.getValue()));
          }
  
          sb.append("\r\r\r\n");
  
          HttpEntity entity = response.getEntity();
          if (entity == null) {
              sb.append("content is null");
          } else {
              try {
                  String content = Base64.encodeToString(
                          EntityUtils.toByteArray(entity), Base64.DEFAULT);
  
                  sb.append(content + "\r\n");
              } catch (Exception e) {
                  sb.append("\r\n\r\n====EXCEPTION=====\r\n" + e.toString()
                          + "\r\n");
              }
          }
  
          sb.append("\r\n==============END HttpResponse================\r\n");
  
          return sb.toString();
      } catch (Exception e) {
          return e.toString();
      }
  }
  </code></pre>