using System;
using System.IO;
using System.Text;

internal class MimeWriter
{
	public MimeWriter(Stream s, string boundary)
	{
		this.s = s;
		this.boundary = boundary;
	}

	public void Close()
	{
		this.s.Close();
	}

	public void WritePart(string name, object data)
	{
		this.WritePart(name, null, null, Encoding.ASCII.GetBytes(data.ToString()));
	}

	public void WritePart(string name, object data, Encoding e)
	{
		this.WritePart(name, null, null, e.GetBytes(data.ToString()));
	}

	public void WritePart(string name, string filename, string content_type, byte[] data)
	{
		byte[] bytes = this.e.GetBytes("\r\n");
		byte[] bytes2 = this.e.GetBytes("--" + this.boundary);
		this.s.Write(bytes2, 0, bytes2.Length);
		this.s.Write(bytes, 0, bytes.Length);
		bytes2 = this.e.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"", name));
		this.s.Write(bytes2, 0, bytes2.Length);
		if (filename != null)
		{
			bytes2 = this.e.GetBytes(string.Format("; filename=\"{0}\"", filename));
			this.s.Write(bytes2, 0, bytes2.Length);
		}
		this.s.Write(bytes, 0, bytes.Length);
		if (content_type != null)
		{
			bytes2 = this.e.GetBytes("Content-Type: " + content_type);
			this.s.Write(bytes2, 0, bytes2.Length);
			this.s.Write(bytes, 0, bytes.Length);
		}
		this.s.Write(bytes, 0, bytes.Length);
		this.s.Write(data, 0, data.Length);
		this.s.Write(bytes, 0, bytes.Length);
	}

	public void Finish()
	{
		byte[] bytes = this.e.GetBytes("--" + this.boundary + "--");
		this.s.Write(bytes, 0, bytes.Length);
		this.s.Close();
	}

	private Encoding e = Encoding.ASCII;

	private Stream s;

	private string boundary;
}
