encode response with gzip

This commit is contained in:
hmm 2014-09-01 10:42:43 +08:00
parent c52820b1ae
commit 793d05ebb1
2 changed files with 50 additions and 0 deletions

View File

@ -0,0 +1,25 @@
package org.bench4q.recorder.httpcapture.generator;
import org.apache.log4j.Logger;
public class ContentEncoder {
protected Logger logger = Logger.getLogger(ContentEncoder.class);
protected ContentEncoder() {
}
public static ContentEncoder createEncoder(String encodeType) {
if (encodeType == null) {
return new ContentEncoder();
}
if (encodeType.equalsIgnoreCase("gzip")) {
return new GzipEncoder();
} else {
return new ContentEncoder();
}
}
public byte[] encoderContent(byte[] content){
return content;
}
}

View File

@ -0,0 +1,25 @@
package org.bench4q.recorder.httpcapture.generator;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;
public class GzipEncoder extends ContentEncoder{
public byte[] encoderContent(byte[] content){
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(outputStream);
gzipOutputStream.write(content, 0, content.length);
gzipOutputStream.finish();
gzipOutputStream.flush();
gzipOutputStream.close();
return outputStream.toByteArray();
} catch (IOException e) {
logger.error("decodeContent", e);
return null;
}
}
}