package aalg.mailer;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.zip.DeflaterInputStream;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterInputStream;

import aalg.algebra.Utils;

public class PlainTextMessageData implements IMessageData {
	
	private static final Charset CHARSET = Charset.forName("UTF-8");
	
	private String message;

	@Override
	public void write(final OutputStream stream) throws IOException {
		final byte[] encodedMessage = message.getBytes(CHARSET);
		final DeflaterOutputStream deflater = new DeflaterOutputStream(stream);
		deflater.write(encodedMessage);
		
		deflater.finish();		
	}

	@Override
	public void read(final InputStream stream) throws IOException {
		final InflaterInputStream inflater = new InflaterInputStream(stream);
		final byte[] encodedMessage = Utils.readByteArrayToEof (inflater);
		
		message = new String(encodedMessage, CHARSET);
	}

}
