/**
   * gzip-compressed HTTP response.
   *
   * @throws Exception
   */
  public void testCompressedHttpRecord() throws Exception {
    String payload = "hogehogehogehogehoge";
    String ctype = "text/plain";
    WARCRecordInfo recinfo =
        new TestWARCRecordInfo(
            TestWARCRecordInfo.buildCompressedHttpResponseBlock(ctype, payload.getBytes()));
    recinfo.setMimetype(ctype);
    TestARCReader ar = new TestARCReader(recinfo);
    ARCRecord rec = ar.get(0);
    ArcResource res = new ArcResource(rec, ar);
    res.parseHeaders();

    assertEquals("statusCode", 200, res.getStatusCode());
    assertEquals("content-type", ctype, res.getHeader("Content-Type"));

    Resource zres = TextReplayRenderer.decodeResource(res);
    assertTrue("wrapped with GzipDecodingResource", (zres instanceof GzipDecodingResource));

    byte[] buf = new byte[payload.getBytes().length + 1];
    int n = zres.read(buf);
    assertEquals("content length", buf.length - 1, n);

    res.close();
  }
 /**
  * Read first {@code sniffLength} bytes of {@code resource}'s payload, decoding {@code
  * Content-Encoding} if any. Reset {@code resource}'s read position back to zero.
  *
  * @param resource Resource to load bytes from
  * @return bytes, zero-padded if payload is shorter.
  * @throws IOException
  */
 protected byte[] peekContent(Resource resource) throws IOException {
   byte[] bbuffer = new byte[Math.max(sniffLength, MINIMUM_SNIFF_BUFFER_SIZE)];
   String encoding = resource.getHeader("content-encoding");
   if ("gzip".equalsIgnoreCase(encoding) || "x-gzip".equalsIgnoreCase(encoding)) {
     // use larger readlimit, because gzip-ed data can be larger than the original
     // at low compression level.
     resource.mark(sniffLength + 100);
     @SuppressWarnings("resource")
     Resource z = new GzipDecodingResource(resource);
     z.read(bbuffer, 0, sniffLength);
     resource.reset();
   } else {
     resource.mark(sniffLength);
     resource.read(bbuffer, 0, sniffLength);
     resource.reset();
   }
   return bbuffer;
 }
Example #3
0
  /**
   * @param charSet
   * @throws IOException
   */
  public void readFully(String charSet) throws IOException {
    this.charSet = charSet;
    int recordLength = (int) resource.getRecordLength();

    // convert bytes to characters for charset:
    InputStreamReader isr = new InputStreamReader(resource, charSet);

    char[] cbuffer = new char[C_BUFFER_SIZE];

    // slurp the whole thing into RAM:
    sb = new StringBuilder(recordLength);

    // Skip the UTF-8 BOM 0xFEFF
    int firstChar = isr.read();
    if ((firstChar != '\uFEFF') && (firstChar != -1)) {
      sb.append((char) firstChar);
    }

    for (int r = -1; (r = isr.read(cbuffer, 0, C_BUFFER_SIZE)) != -1; ) {
      sb.append(cbuffer, 0, r);
    }
  }