View Javadoc

1   package com.diasparsoftware.htmlunitx;
2   
3   import java.io.*;
4   import java.net.URL;
5   
6   import com.diasparsoftware.gsbase.StreamUtil;
7   
8   public class InputStreamWebResponse extends TestableWebResponse {
9       private InputStream contentAsStream;
10  
11      public InputStreamWebResponse(URL url,
12          InputStream contentAsStream) {
13  
14          super(url);
15  
16          this.contentAsStream = contentAsStream;
17      }
18  
19      public String getContentAsString() throws IOException {
20          return StreamUtil
21              .getContentAsString(getContentAsStream());
22      }
23  
24      public InputStream getContentAsStream() {
25          getStopwatch().start();
26          getStopwatch().stop();
27          return contentAsStream;
28      }
29  
30      public long getLoadTimeInMilliSeconds() {
31          return getStopwatch().getLastTime();
32      }
33  
34      public byte[] getResponseBody() {
35          try {
36              return getContentAsBytes();
37          } catch (IOException wrapped) {
38              throw new RuntimeException(wrapped);
39          }
40      }
41  
42      protected byte[] getContentAsBytes()
43          throws UnsupportedEncodingException, IOException {
44          return getContentAsString().getBytes(
45              getContentCharSet());
46      }
47  
48      public boolean equals(Object other) {
49          if (other != null && getClass() == other.getClass()) {
50              InputStreamWebResponse that = (InputStreamWebResponse) other;
51              return this.getUrl().equals(that.getUrl()) && this
52                  .getContentAsStream().equals(
53                      that.getContentAsStream());
54          } else {
55              return false;
56          }
57      }
58  
59      public int hashCode() {
60          return 0;
61      }
62  
63      public String toString() {
64          return "an InputStreamWebResponse[contentAsStream=" + contentAsStream + "]";
65      }
66  }