package org.bench4q.recorder.httpcapture.generator;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import org.apache.log4j.Logger;

public class ContentDecoder {
	protected Logger logger = Logger.getLogger(ContentDecoder.class);

	protected ContentDecoder() {
	}

	public static ContentDecoder createDecoder(String encodeType) {
		if (encodeType == null) {
			return new ContentDecoder();
		}

		if (encodeType.equalsIgnoreCase("gzip")) {
			return new GzipDecoder();
		} else {
			return new ContentDecoder();
		}
	}

	public byte[] decodeContent(InputStream inputStream) {
		try {
			ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
			byte[] buf = new byte[1024];
			int len;
			while ((len = inputStream.read(buf)) > 0) {
				outputStream.write(buf, 0, len);
			}
//			System.out.println(outputStream.toString());
//			System.out.println("ouputStream's size is" + outputStream.size());
			return outputStream.toByteArray();
		} catch (Exception e) {
			logger.error(e, e);
			return null;
		}

	}

}
