/**
 * 
 */
package at.gv.egiz.pdfas.io;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import at.gv.egiz.pdfas.api.io.DataSource;

/**
 * A byte array backed DataSource.
 * 
 * @author wprinz
 */
public class ByteArrayDataSource implements DataSource
{

  /**
   * The byte array.
   */
  protected byte[] data = null;

  /**
   * The mime type.
   */
  protected String mimeType = null;

  /**
   * The character encoding.
   */
  protected String characterEncoding = null;

  /**
   * @param data
   * @param mimeType
   */
  public ByteArrayDataSource(byte[] data, String mimeType)
  {
    this(data, mimeType, null);
  }

  /**
   * @param data
   * @param mimeType
   * @param characterEncoding
   */
  public ByteArrayDataSource(byte[] data, String mimeType, String characterEncoding)
  {
    this.data = data;
    this.mimeType = mimeType;
    this.characterEncoding = characterEncoding;
  }

  /**
   * @see at.gv.egiz.pdfas.api.io.DataSource#createInputStream()
   */
  public InputStream createInputStream()
  {
    return new ByteArrayInputStream(this.data);
  }

  /**
   * @see at.gv.egiz.pdfas.api.io.DataSource#getAsByteArray()
   */
  public byte[] getAsByteArray()
  {
    return this.data;
  }

  /**
   * @see at.gv.egiz.pdfas.api.io.DataSource#getCharacterEncoding()
   */
  public String getCharacterEncoding()
  {
    return this.characterEncoding;
  }

  /**
   * @see at.gv.egiz.pdfas.api.io.DataSource#getMimeType()
   */
  public String getMimeType()
  {
    return this.mimeType;
  }

  /**
   * @see at.gv.egiz.pdfas.api.io.DataSource#getLength()
   */
  public int getLength()
  {
    return this.data.length;
  }

}