/**
  * 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;
 }
Ejemplo n.º 2
0
  /**
   * 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();
  }